summaryrefslogtreecommitdiff
path: root/internal/draft/parser_test.go
blob: c6d454d117c2f2f3ed6dd6ac701737f125ff2512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package draft

import (
	"os"
	"strings"
	"testing"
)

func TestParseDraftSevsk(t *testing.T) {
	raw, err := os.ReadFile("../../.draft/sevsk.md.draft")
	if err != nil {
		t.Fatal(err)
	}

	doc, err := ParseDraft("../../.draft/sevsk.md.draft", "sevsk", raw)
	if err != nil {
		t.Fatal(err)
	}
	if doc.Metadata == nil || doc.Metadata.Title == "" {
		t.Fatal("expected frontmatter metadata")
	}

	var galleries int
	for _, section := range doc.Sections {
		for _, block := range section.Blocks {
			if block.Kind != GalleryBlock {
				continue
			}
			galleries++
			for _, p := range block.Gallery.Photos {
				if !strings.HasPrefix(p.Key, "Sevsk/20241008-") {
					t.Errorf("unexpected photo key %q", p.Key)
				}
				if strings.Contains(p.Caption, "2x") {
					t.Errorf("layout token leaked into caption %q", p.Caption)
				}
			}
		}
	}
	if galleries == 0 {
		t.Fatal("expected at least one gallery")
	}

	// First gallery: 4 photos, captions on photos 1 and 3, none on 2; photo 4 had "2x |" => empty caption.
	first := firstGallery(doc)
	if first == nil || len(first.Photos) != 4 {
		t.Fatalf("first gallery: want 4 photos, got %v", first)
	}
	if first.Timezone != "Europe/Moscow" {
		t.Errorf("first gallery tz = %q", first.Timezone)
	}
	if first.Photos[0].Caption != "Никольская церковь. Действующая" {
		t.Errorf("photo[0] caption = %q", first.Photos[0].Caption)
	}
	if first.Photos[1].Caption != "" {
		t.Errorf("photo[1] caption = %q, want empty", first.Photos[1].Caption)
	}
	if first.Photos[3].Caption != "" {
		t.Errorf("photo[3] caption = %q, want empty (only 2x token)", first.Photos[3].Caption)
	}
	if first.Photos[3].Key != "Sevsk/20241008-094036.jpg" {
		t.Errorf("photo[3] key = %q", first.Photos[3].Key)
	}
}

func firstGallery(doc *Document) *Gallery {
	for _, section := range doc.Sections {
		for _, block := range section.Blocks {
			if block.Kind == GalleryBlock {
				return block.Gallery
			}
		}
	}
	return nil
}

func TestSplitSectionsSeparator(t *testing.T) {
	body := "intro\n...\nmiddle\n...\nend"
	sections := splitSections(body)
	if len(sections) != 3 {
		t.Fatalf("want 3 sections, got %d", len(sections))
	}
}