package web
import (
"net/http/httptest"
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/artifact"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/ops"
)
// An edited forum post reads as ONE post on the thread page — the
// newest text, marked edited, with a link to every version — and the
// history page has them all.
func TestForumThreadFoldsEditsWithAHistoryLink(t *testing.T) {
ix := landingIndex(t, false)
root := strings.Repeat("a1", 20)
v1, v2 := strings.Repeat("b2", 20), strings.Repeat("c3", 20)
if err := ix.FoldNow([]index.Parsed{
{Hash: root, Art: &artifact.Artifact{Type: artifact.Forum, ThreadTitle: "Editing",
Date: "2026-01-01T10:00:00", User: "dan", Mimetype: "text/x-markdown", Body: []byte("opener")}},
{Hash: v1, Art: &artifact.Artifact{Type: artifact.Forum, ThreadRoot: root, InReplyTo: root,
Date: "2026-01-02T10:00:00", User: "eve", Mimetype: "text/x-markdown", Body: []byte("teh first draft")}},
{Hash: v2, Art: &artifact.Artifact{Type: artifact.Forum, ThreadRoot: root, InReplyTo: root, Parents: []string{v1},
Date: "2026-01-03T10:00:00", User: "eve", Mimetype: "text/x-markdown", Body: []byte("the corrected draft")}},
}); err != nil {
t.Fatal(err)
}
r := ops.NewRegistry()
if err := api.RegisterAll(r, api.Deps{Version: "v",
Repos: api.NewRepos(map[string]*api.Repo{"alpha": {Index: ix}})}); err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(HandlerWith(r, nil, nil, nil))
t.Cleanup(srv.Close)
page := landingGet(t, srv, "/r/alpha/forum/"+root[:12])
if strings.Contains(page, "teh first draft") {
t.Errorf("the thread still shows the replaced version")
}
if !strings.Contains(page, "the corrected draft") {
t.Errorf("the thread lacks the newest version")
}
hist := "/r/alpha/forum/" + root[:12] + "/history/" + v2[:12]
if !strings.Contains(page, `href="`+hist+`"`) || !strings.Contains(page, ">edited<") {
t.Errorf("the edited post has no history link")
}
if strings.Count(page, `class="f-post"`) != 2 {
t.Errorf("thread shows %d posts, want 2", strings.Count(page, `class="f-post"`))
}
h := landingGet(t, srv, hist)
for _, want := range []string{"teh first draft", "the corrected draft", v1[:12], v2[:12]} {
if !strings.Contains(h, want) {
t.Errorf("history lacks %q", want)
}
}
// Newest first.
if strings.Index(h, "the corrected draft") > strings.Index(h, "teh first draft") {
t.Errorf("history is not newest first")
}
}