~bigbes/sr-ht-spec

ref: c87a11bdd81b96964284187ef8b46e8f9c8bcdbc sr-ht-spec/doc/scan_test.go -rw-r--r-- 9.9 KiB
c87a11bd — Eugene Blikh chimw: the request line, the HEAD twins and the routing refusals 9 days ago
                                                                                
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package doc

import (
	"context"
	"strings"
	"testing"

	"sourcecraft.dev/bigbes/sr-ht-spec/core"
	"sourcecraft.dev/bigbes/sr-ht-spec/gitx"
)

func spec(id, title, body string) string {
	return "---\nid: " + id + "\ntitle: " + title + "\nstatus: draft\n---\n\n" + body + "\n"
}

// The archive keys on the frontmatter id when there is one. Paths move; ids do
// not, so this is what makes [[SPEC-0007]] survive a rename.
func TestScanKeysOnDocumentID(t *testing.T) {
	arc := archiveOf(t, map[string]string{
		"specs/0007-storage.md": spec("SPEC-0007", "Storage model", "See [[SPEC-0003]]."),
		"specs/0003-old.md":     spec("SPEC-0003", "Old model", "superseded"),
	})

	p := mustPage(t, arc, "specs/0007-storage.md")
	if p.ID != "SPEC-0007" || p.DocID != "SPEC-0007" {
		t.Fatalf("ID/DocID = %q/%q, want SPEC-0007", p.ID, p.DocID)
	}
	if p.Title != "Storage model" || p.Status != core.StatusDraft {
		t.Errorf("title/status = %q/%q", p.Title, p.Status)
	}
	if p.Blob == "" {
		t.Errorf("Blob (the render cache key) was not carried over from git")
	}
	if got, ok := arc.Page("SPEC-0007"); !ok || got != p {
		t.Errorf("Page(SPEC-0007) did not return the document")
	}

	got := arc.Resolve("specs", "SPEC-0003")
	if got.Missing || got.Href != "/~bigbes/rfcs/specs/0003-old" {
		t.Errorf("Resolve(SPEC-0003) = %+v", got)
	}
}

// A document with no usable id is still a document: it keys on its path, stays
// readable, and keeps its path occupied. Refusing to serve it would turn one
// cosmetic typo into an outage for the whole space.
func TestScanToleratesDocumentsWithoutAnID(t *testing.T) {
	arc := archiveOf(t, map[string]string{
		"notes/free-form.md": "# Free form\n\nNo frontmatter at all.\n",
		"notes/broken.md":    "---\ntitle: [unclosed\n---\n\n# Recovered Title\n",
		"notes/lower.md":     "---\nid: spec-0007\ntitle: Lowercase id\nstatus: draft\n---\n\nbody\n",
	})

	for path, want := range map[string]string{
		"notes/free-form.md": "Free form",
		"notes/broken.md":    "Recovered Title",
		"notes/lower.md":     "Lowercase id",
	} {
		p := mustPage(t, arc, path)
		if p.Title != want {
			t.Errorf("%s title = %q, want %q", path, p.Title, want)
		}
		if p.DocID != "" {
			t.Errorf("%s claimed DocID %q; a malformed id must not enter id resolution", path, p.DocID)
		}
		if p.ID != strings.TrimSuffix(path, core.DocExt) {
			t.Errorf("%s ID = %q, want the path without %q", path, p.ID, core.DocExt)
		}
	}
}

// The design tolerates a duplicated id on the approved branch and refuses to
// resolve it, rather than letting one of the two documents win silently.
func TestScanRefusesToResolveADuplicatedID(t *testing.T) {
	arc := archiveOf(t, map[string]string{
		"specs/a.md": spec("SPEC-0007", "First claimant", "a"),
		"specs/b.md": spec("SPEC-0007", "Second claimant", "b"),
	})

	for _, path := range []string{"specs/a.md", "specs/b.md"} {
		p := mustPage(t, arc, path)
		if p.DocID != "SPEC-0007" {
			t.Errorf("%s: DocID = %q, want the id as authored", path, p.DocID)
		}
		if p.ID != strings.TrimSuffix(path, core.DocExt) {
			t.Errorf("%s: ID = %q, want the path", path, p.ID)
		}
	}
	if _, ok := arc.Page("SPEC-0007"); ok {
		t.Errorf("a duplicated id must resolve to neither document")
	}
	if got := arc.Resolve("specs", "SPEC-0007"); !got.Missing {
		t.Errorf("Resolve(SPEC-0007) = %+v, want Missing", got)
	}
}

// The approved head, a pinned sha and a proposal branch are the same code path
// with a different revision. That is the whole reason the checkout was dropped.
func TestArchiveIsBuiltTheSameWayAtEveryRevision(t *testing.T) {
	ctx := context.Background()
	repo := space(t)

	first := commit(t, repo, 1, repo.ApprovedBranch(), map[string]string{
		"specs/0007-storage.md": spec("SPEC-0007", "Storage model", "v1"),
	})
	pinned, err := repo.ResolveRev(ctx, first)
	if err != nil {
		t.Fatalf("ResolveRev(%q): %v", first, err)
	}
	second := commit(t, repo, 2, first, map[string]string{
		"specs/0007-storage.md": spec("SPEC-0007", "Storage model, revised", "v2"),
		"notes/aside.md":        "# Aside\n",
	})

	at := func(rev string) *Archive { return archiveAt(t, repo, rev) }

	head := at(pinned.String())
	if len(head.Pages) != 1 {
		t.Fatalf("pinned rev has %d documents, want 1", len(head.Pages))
	}
	if got := mustPage(t, head, "specs/0007-storage.md").Title; got != "Storage model" {
		t.Errorf("pinned rev title = %q, want the revision as it was", got)
	}

	draft := at(second)
	if len(draft.Pages) != 2 {
		t.Fatalf("proposal branch has %d documents, want 2", len(draft.Pages))
	}
	if got := mustPage(t, draft, "specs/0007-storage.md").Title; got != "Storage model, revised" {
		t.Errorf("proposal branch title = %q", got)
	}
	if draft.Rev != second {
		t.Errorf("Rev = %q, want the revision the caller named", draft.Rev)
	}
}

// `parent:` is a wikilink, so it must resolve to the same document the same
// link written in the body would. The two are separate code paths that happen
// to call the same lookup, and they agree only as long as the hierarchy pass
// hands it the directory shape Resolve expects — "" at the space root, which is
// what DirOf produces and what path.Dir spells "." instead.
//
// The hierarchy pass used to spell it path.Dir. That was unobservable: for a
// root-level document the section-proximity step is subsumed by the
// same-directory step above it, so "." only ever skipped a lookup that had
// already answered. It stayed unobservable by coincidence of two ranking rules,
// which is the kind of thing that stops being true quietly. The colliding stem
// below is what makes the two directories name different documents, so this
// test fails if a future change gives that skipped step something to say.
func TestParentResolvesLikeAWikilinkFromTheSameDocument(t *testing.T) {
	arc := archiveOf(t, map[string]string{
		"storage.md":       spec("SPEC-0100", "Root storage", "the root one"),
		"specs/storage.md": spec("SPEC-0200", "Section storage", "the specs one"),
		"overview.md":      "---\nid: SPEC-0300\ntitle: Overview\nstatus: draft\nparent: \"[[storage]]\"\n---\n\nbody\n",
		"specs/detail.md":  "---\nid: SPEC-0400\ntitle: Detail\nstatus: draft\nparent: \"[[storage]]\"\n---\n\nbody\n",
	})

	for path, want := range map[string]string{
		"overview.md":     "SPEC-0100", // at the root: the root storage
		"specs/detail.md": "SPEC-0200", // under specs/: the one beside it
	} {
		p := mustPage(t, arc, path)
		if p.ParentID != want {
			t.Errorf("%s parent = %q, want %q", path, p.ParentID, want)
		}
		if len(p.Crumbs) != 1 || p.Crumbs[0] != want {
			t.Errorf("%s crumbs = %v, want [%s]", path, p.Crumbs, want)
		}
		// The invariant behind the fix, stated directly: whatever the body's
		// own [[storage]] resolves to is what `parent:` must have resolved to.
		if got := arc.Resolve(DirOf(p.Path), "storage"); got.PageID != p.ParentID {
			t.Errorf("%s: parent resolved to %q but the same wikilink in its body resolves to %q",
				path, p.ParentID, got.PageID)
		}
	}
}

// FromDocuments sorts by path, so an archive is identical whatever order the
// tree walk yields — which is what makes the index reproducible.
func TestFromDocumentsIsPathOrdered(t *testing.T) {
	docs := []gitx.Document{
		{Path: "specs/z.md", Data: []byte(spec("SPEC-0002", "Z", "z"))},
		{Path: "notes/a.md", Data: []byte(spec("SPEC-0001", "A", "a"))},
	}
	arc := FromDocuments(fxSpace, "main", docs)
	if len(arc.Pages) != 2 || arc.Pages[0].Path != "notes/a.md" {
		t.Fatalf("pages = %v", arc.Pages)
	}
}

// gitx enumerates documents only, so a git-fed archive has no attachment index
// and says so by marking the link missing instead of inventing an href. An
// attachment index supplied through FromPages resolves as it always did.
func TestAttachmentsComeFromTheCallerNotFromTheTreeWalk(t *testing.T) {
	arc := archiveOf(t, map[string]string{"notes/a.md": "# A\n"})
	if len(arc.Assets()) != 0 {
		t.Fatalf("Scan invented an attachment index: %v", arc.Assets())
	}
	if got := arc.Resolve("notes", "diagram.png"); !got.Missing {
		t.Errorf("Resolve(diagram.png) = %+v, want Missing", got)
	}

	withAssets := FromPages(fxSpace, "main", arc.Pages, nil, map[string]string{
		"diagram.png":        "assets/diagram.png",
		"assets/diagram.png": "assets/diagram.png",
	})
	got := withAssets.Resolve("notes", "diagram.png")
	if got.Missing || got.Href != "/~bigbes/rfcs/assets/diagram.png" {
		t.Errorf("Resolve(diagram.png) = %+v", got)
	}
}

// The two halves this package merged — the archive and the renderer — meet
// here: a body rendered with the archive as its Resolver must link to the
// space's own hrefs, feed the link graph, and mark what did not resolve.
func TestRenderThroughTheArchive(t *testing.T) {
	arc := archiveOf(t, map[string]string{
		"specs/0007-storage.md": spec("SPEC-0007", "Storage model",
			"Supersedes [[SPEC-0003]] and [[SPEC-0099]].\n\n## Trade-offs\n\nSee [[0003-old|the old one]]."),
		"specs/0003-old.md": spec("SPEC-0003", "Old model", "superseded"),
	})

	p := mustPage(t, arc, "specs/0007-storage.md")
	_, body := ParseFront([]byte(spec("SPEC-0007", "Storage model",
		"Supersedes [[SPEC-0003]] and [[SPEC-0099]].\n\n## Trade-offs\n\nSee [[0003-old|the old one]].")))

	res := NewRenderer().Render(body, "specs", arc)
	p.Links = res.LinkedIDs
	p.WordCount = res.WordCount

	if !strings.Contains(res.HTML, `href="/~bigbes/rfcs/specs/0003-old"`) {
		t.Errorf("resolved wikilink did not become a space href:\n%s", res.HTML)
	}
	if !strings.Contains(res.HTML, `class="wikilink-missing"`) {
		t.Errorf("unresolved wikilink was not marked broken:\n%s", res.HTML)
	}
	if got := strings.Join(res.MissingWikilinks, ","); got != "SPEC-0099" {
		t.Errorf("MissingWikilinks = %v", res.MissingWikilinks)
	}
	if got := strings.Join(res.LinkedIDs, ","); got != "SPEC-0003" {
		t.Errorf("LinkedIDs = %v, want the id once, deduped across both spellings", res.LinkedIDs)
	}
	if len(res.Headings) != 1 || res.Headings[0].ID != "trade-offs" {
		t.Errorf("headings = %+v", res.Headings)
	}

	back := arc.Backlinks("SPEC-0003")
	if len(back) != 1 || back[0].ID != "SPEC-0007" {
		t.Fatalf("Backlinks(SPEC-0003) = %v", back)
	}
}