package api
import (
"context"
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/artifact"
"craftdesign.group/quarry/internal/index"
)
// #265: landing.get carries the branch's design note to a caller who
// may read the wiki (j), and to every caller on a forge with no
// capability plane; not to one without j, and not when the page is
// missing or its newest edit is fossil's deletion.
func TestBranchNoteFollowsTheWikiGate(t *testing.T) {
ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ix.Close() })
wiki := func(hash, date, body string) index.Parsed {
return index.Parsed{Hash: strings.Repeat(hash, 20), Art: &artifact.Artifact{Type: artifact.Wiki,
WikiTitle: "branch/work", Date: date, User: "eve", Mimetype: "text/x-markdown", Body: []byte(body)}}
}
if err := ix.FoldNow([]index.Parsed{wiki("d4", "2026-08-20T12:00:00", "The plan.\n")}); err != nil {
t.Fatal(err)
}
repo := &Repo{Index: ix}
if n := branchNote(as("reader", "oj"), repo, "alpha", "work"); !n.Present || !strings.Contains(n.HTML, "The plan") || n.User != "eve" {
t.Errorf("FAIL: a wiki reader got %+v", n)
}
if n := branchNote(context.Background(), repo, "alpha", "work"); !n.Present {
t.Errorf("FAIL: a forge with no capability plane hid the note")
}
if n := branchNote(as("stranger", "o"), repo, "alpha", "work"); n.Present || n.HTML != "" {
t.Errorf("FAIL: a caller without j got the note: %+v", n)
}
if n := branchNote(as("reader", "oj"), repo, "alpha", "other"); n.Present || n.Name != "branch/other" {
t.Errorf("FAIL: a branch with no page = %+v", n)
}
if err := ix.FoldNow([]index.Parsed{wiki("e5", "2026-08-21T12:00:00", "")}); err != nil {
t.Fatal(err)
}
if n := branchNote(as("reader", "oj"), repo, "alpha", "work"); n.Present {
t.Errorf("FAIL: a deleted note still shows: %+v", n)
}
}