package web import ( "fmt" "strings" "testing" "sourcecraft.dev/bigbes/sr-ht-spec/core" "sourcecraft.dev/bigbes/sr-ht-spec/service" ) // These tests are about the markup: the classes and attributes the stylesheet // and the selection script are pinned to, and the escaping discipline. What a // row *says* — which number lands in which track — is diffrows_test.go's, on // the model rather than through a layer of angle brackets. // render is renderDocDiff for a document nobody has commented on, which is what // the presentation tests are about. func render(oldSrc, newSrc []byte) diffView { return renderDocDiff(docDiff{DocID: "SPEC-0007", Path: "specs/0007-storage.md", Base: oldSrc, Proposed: newSrc}) } // blockIDs pulls the rendered block ids out of a diff, in document order, so a // test can compare two renders without depending on the digest itself. func blockIDs(html string) []string { var ids []string for _, rest := range strings.Split(html, ` id="b-`)[1:] { ids = append(ids, "b-"+rest[:strings.IndexByte(rest, '"')]) } return ids } // TestRenderDocDiffUnchanged proves a proposal that does not change a document // is reported as unchanged rather than as an empty diff. func TestRenderDocDiffUnchanged(t *testing.T) { src := []byte("# Title\n\nOne paragraph.\n") v := render(src, src) if !v.Unchanged { t.Fatalf("Unchanged = false, want true for identical input") } if v.HTML != "" { t.Errorf("HTML = %q, want empty for an unchanged document", v.HTML) } } // TestRenderDocDiffInlineWordChange proves a small edit renders inline with // / marks and not as two columns. func TestRenderDocDiffInlineWordChange(t *testing.T) { old := []byte("# Title\n\nThe quick brown fox jumps over the lazy dog.\n") nw := []byte("# Title\n\nThe quick red fox jumps over the lazy dog.\n") v := render(old, nw) if v.Unchanged { t.Fatalf("Unchanged = true, want a change") } html := string(v.HTML) if !strings.Contains(html, "brown") { t.Errorf("missing inline deletion of 'brown'; got:\n%s", html) } if !strings.Contains(html, "red") { t.Errorf("missing inline insertion of 'red'; got:\n%s", html) } if strings.Contains(html, "ph-region") { t.Errorf("a one-word edit fell back to a line range; got:\n%s", html) } } // TestRenderDocDiffRegionFallbackBelowThreshold proves a block rewritten enough // to fall below the similarity threshold renders as the paired old/new region // rows — the Phase 0 verdict's hard requirement, and the one place the gutter // states a range instead of a line number. func TestRenderDocDiffRegionFallbackBelowThreshold(t *testing.T) { // A block rewritten to ~0.58 similarity: paired as a modify (above the 0.40 // pairing floor) but shredded enough to fall below the 0.75 inline switch. old := []byte("# Title\n\nThe committee approved the annual budget after a long and " + "contentious debate that lasted well into the evening.\n") nw := []byte("# Title\n\nThe committee rejected the annual budget after a brief and " + "quiet discussion that ended early in the afternoon.\n") v := render(old, nw) html := string(v.HTML) if !strings.Contains(html, `ph-r-del ph-blk-start ph-region"`) || !strings.Contains(html, `ph-r-ins ph-region"`) { t.Fatalf("a wholesale rewrite did not render as a region pair; got:\n%s", html) } // The old side keeps its deletions and the new side its insertions, so each // row is a paragraph a reviewer can read straight through. if !strings.Contains(html, "approved") || !strings.Contains(html, "rejected") { t.Errorf("the region rows lost their word marks; got:\n%s", html) } } // TestRenderDocDiffEscapesContent proves document content is HTML-escaped: a // document that contains markup cannot inject it into the review page. func TestRenderDocDiffEscapesContent(t *testing.T) { old := []byte("# Title\n\nplain text here.\n") nw := []byte("# Title\n\nplain text here.\n") v := render(old, nw) html := string(v.HTML) if strings.Contains(html, "