Skip to content
quarry Artifact [4c1a187f39]
Login

Artifact [4c1a187f39]

Artifact 4c1a187f399b17ae74b53d6030739bcd350994f325ea6e504c5be45795cdc5a1:


package index

import (
	"testing"

	"craftdesign.group/quarry/internal/artifact"
)

func wikiEdit(hash, title, date string) Parsed {
	return Parsed{Hash: hash, Art: &artifact.Artifact{
		Type: artifact.Wiki, Date: date, User: "dan",
		WikiTitle: title, Body: []byte("body"),
	}}
}

// Counts is memoized because it is read on every page render, which
// makes staleness the thing to prove. A nav that says "wiki 3" after a
// fourth page is saved is a small lie, and small lies about counts are
// how a reader learns not to trust the rest of the page.
func TestCountsRefreshAfterAFold(t *testing.T) {
	ix := openIndex(t)

	got, err := ix.Counts(false)
	if err != nil {
		t.Fatal(err)
	}
	if got.WikiPages != 0 {
		t.Fatalf("empty index reports %d wiki pages", got.WikiPages)
	}

	if err := ix.FoldNow([]Parsed{
		wikiEdit("w1", "Home", "2026-01-01T00:00:00"),
		wikiEdit("w2", "Install", "2026-01-02T00:00:00"),
	}); err != nil {
		t.Fatal(err)
	}
	got, err = ix.Counts(false)
	if err != nil {
		t.Fatal(err)
	}
	if got.WikiPages != 2 {
		t.Fatalf("after fold: %d wiki pages, want 2 — the memo went stale", got.WikiPages)
	}

	// A second edit to an EXISTING page must not raise the page count:
	// the nav links to a list of pages, so it has to count pages.
	if err := ix.FoldNow([]Parsed{
		wikiEdit("w3", "Home", "2026-01-03T00:00:00"),
	}); err != nil {
		t.Fatal(err)
	}
	got, _ = ix.Counts(false)
	if got.WikiPages != 2 {
		t.Fatalf("re-editing a page changed the count to %d, want 2", got.WikiPages)
	}

	// Reset is the reindex path; the memo must not survive it.
	if err := ix.Reset(); err != nil {
		t.Fatal(err)
	}
	got, err = ix.Counts(false)
	if err != nil {
		t.Fatal(err)
	}
	if got.WikiPages != 0 {
		t.Fatalf("after Reset: %d wiki pages, want 0 — the memo survived a reindex", got.WikiPages)
	}
}

// The forum count is THREADS, not posts, because that is what the forum
// page lists. Counting posts would send a reader to a page showing
// fewer rows than the nav promised.
func TestForumCountIsThreadsNotPosts(t *testing.T) {
	ix := openIndex(t)
	root := "f1"
	if err := ix.FoldNow([]Parsed{
		{Hash: root, Art: &artifact.Artifact{
			Type: artifact.Forum, Date: "2026-01-01T00:00:00", User: "dan",
			ThreadTitle: "A thread", Body: []byte("hi"),
		}},
		{Hash: "f2", Art: &artifact.Artifact{
			Type: artifact.Forum, Date: "2026-01-02T00:00:00", User: "dan",
			InReplyTo: root, ThreadRoot: root, Body: []byte("reply"),
		}},
		{Hash: "f3", Art: &artifact.Artifact{
			Type: artifact.Forum, Date: "2026-01-03T00:00:00", User: "dan",
			InReplyTo: root, ThreadRoot: root, Body: []byte("reply"),
		}},
	}); err != nil {
		t.Fatal(err)
	}
	got, err := ix.Counts(false)
	if err != nil {
		t.Fatal(err)
	}
	if got.ForumThreads != 1 {
		t.Fatalf("ForumThreads = %d for one thread with two replies, want 1", got.ForumThreads)
	}
}