Skip to content
quarry Artifact [dd86fee808]
Login

Artifact [dd86fee808]

Artifact dd86fee80871ff6d47bd5c9ef0bffbf49ad9674798de60739b4378b188c227ff:


package index

import (
	"testing"

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

func foldWiki(t *testing.T, ix *Index, hash, title, date, body string) {
	t.Helper()
	if err := ix.FoldNow([]Parsed{{Hash: hash, Art: &artifact.Artifact{
		Type: artifact.Wiki, WikiTitle: title, Date: date, User: "dan",
		Body: []byte(body),
	}}}); err != nil {
		t.Fatal(err)
	}
}

// Fossil deletes a wiki page by writing an edit with an EMPTY body.
// A page whose newest edit is empty must vanish from the list and the
// count — while its history stays fully readable, because the edits are
// real artifacts and a deleted page is still part of the record. And a
// page deleted then re-created is simply live again: the newest edit
// decides, not any edit.
func TestDeletedWikiPagesLeaveTheListButKeepTheirHistory(t *testing.T) {
	ix := openIndex(t)
	foldWiki(t, ix, "w1", "Guide", "2026-01-01T10:00:00", "how to")
	foldWiki(t, ix, "w2", "Scratch", "2026-01-02T10:00:00", "notes")
	foldWiki(t, ix, "w3", "Scratch", "2026-01-03T10:00:00", "") // deleted
	foldWiki(t, ix, "w4", "Phoenix", "2026-01-04T10:00:00", "v1")
	foldWiki(t, ix, "w5", "Phoenix", "2026-01-05T10:00:00", "")   // deleted...
	foldWiki(t, ix, "w6", "Phoenix", "2026-01-06T10:00:00", "v2") // ...and back

	titles, err := ix.WikiTitles()
	if err != nil {
		t.Fatal(err)
	}
	var names []string
	for _, w := range titles {
		names = append(names, w.Title)
	}
	if len(names) != 2 || names[0] != "Guide" || names[1] != "Phoenix" {
		t.Fatalf("live pages = %v, want [Guide Phoenix]: newest edit decides", names)
	}

	// The count sits above the list and must agree with it — a count
	// that includes what the list hides is a confirmation leak in
	// miniature and simply wrong here.
	c, err := ix.Counts()
	if err != nil {
		t.Fatal(err)
	}
	if c.WikiPages != 2 {
		t.Fatalf("count = %d, want 2 (the list's own rule)", c.WikiPages)
	}

	// History intact: the deleted page's revisions still resolve.
	if edits, err := ix.WikiHistory("Scratch"); err != nil || len(edits) != 2 {
		t.Fatalf("deleted page's history: %d edits, err=%v; the record must survive deletion", len(edits), err)
	}
	if rev, ok, err := ix.WikiRevision("Scratch", ""); err != nil || !ok || len(rev.Body) != 0 {
		t.Fatalf("deleted page's latest revision unreadable (ok=%v err=%v)", ok, err)
	}
	old, ok, err := ix.WikiRevision("Scratch", "w2")
	if err != nil || !ok || string(old.Body) != "notes" {
		t.Fatalf("deleted page's old content unreadable (ok=%v err=%v body=%q)", ok, err, old.Body)
	}
}