package index
import (
"reflect"
"strings"
"testing"
"time"
)
const h10 = "8a0ba3d480"
func TestParseMarker(t *testing.T) {
for _, tc := range []struct {
name, prose string
want Marker
}{
{"plain remark", "looks fine", Marker{}},
{"empty", "", Marker{}},
{"blocking", "blocking: handle the nil case", Marker{Kind: MarkBlocking}},
{"blocking, any case", "BlOcKiNg: x", Marker{Kind: MarkBlocking}},
{"blocking after blank space", "\n \tblocking: x", Marker{Kind: MarkBlocking}},
{"blocking with nothing said", "blocking:", Marker{Kind: MarkBlocking}},
{"blocking is not a prefix match on a word", "blockingly: x", Marker{}},
{"blocking later in the text", "this is blocking: yes", Marker{}},
{"resolved", "resolved: " + h10, Marker{Kind: MarkResolved, Hash: h10}},
{"resolved with a body", "resolved: " + h10 + "\n\nfixed in the next push", Marker{Kind: MarkResolved, Hash: h10}},
{"resolved, upper-case hash", "Resolved: 8A0BA3D480", Marker{Kind: MarkResolved, Hash: h10}},
{"resolved with crlf", "resolved: " + h10 + "\r\nthanks", Marker{Kind: MarkResolved, Hash: h10}},
{"reopened", "reopened: " + h10 + "ab", Marker{Kind: MarkReopened, Hash: h10 + "ab"}},
{"full-length hash", "resolved: " + strings.Repeat("a", 64), Marker{Kind: MarkResolved, Hash: strings.Repeat("a", 64)}},
{"hash too short", "resolved: 8a0ba3d48", Marker{}},
{"hash too long", "resolved: " + strings.Repeat("a", 65), Marker{}},
{"hash not hex", "resolved: 8a0ba3d48g", Marker{}},
{"no hash", "resolved:", Marker{}},
{"words after the hash", "resolved: " + h10 + " done", Marker{}},
{"marker not on the first line", "thanks\nresolved: " + h10, Marker{}},
{"resolved text under a blocking marker is blocking", "blocking: resolved: " + h10, Marker{Kind: MarkBlocking}},
} {
if got := ParseMarker(tc.prose); got != tc.want {
t.Errorf("FAIL: %s: ParseMarker(%q) = %+v, want %+v", tc.name, tc.prose, got, tc.want)
}
}
}
// What the writer produces, the reader reads, with and without a line
// note's header above it.
func TestWrittenMarkersReadBack(t *testing.T) {
if got := ParseMarker(AsBlocking("check the error")); got.Kind != MarkBlocking {
t.Errorf("FAIL: AsBlocking wrote %q, read as %+v", AsBlocking("check the error"), got)
}
if got := AsBlocking("Blocking: already"); got != "Blocking: already" {
t.Errorf("FAIL: AsBlocking doubled a marker: %q", got)
}
for _, kind := range []MarkerKind{MarkResolved, MarkReopened} {
for _, text := range []string{"", " ", "done"} {
body := FormatMarker(kind, h10, text)
if got := ParseMarker(body); got != (Marker{Kind: kind, Hash: h10}) {
t.Errorf("FAIL: %q read as %+v", body, got)
}
}
}
note := LineNote{File: "a.go", Side: "new", Line: 3}
_, prose, ok := ParseLineNote(FormatLineNote(note, AsBlocking("guard this")))
if !ok || ParseMarker(prose).Kind != MarkBlocking {
t.Errorf("FAIL: a blocking line note's prose %q does not read as blocking", prose)
}
if got := BlockingText(" Blocking: guard this"); got != "guard this" {
t.Errorf("FAIL: BlockingText = %q", got)
}
}
func post(hash, date, user, body string) ForumPost {
return ForumPost{Hash: hash, Parent: "root", Date: date, User: user, Body: body}
}
// edit is a later version of a post: it carries its own date and the
// user the editing clone claimed.
func edit(prior ForumPost, hash, date, user, body string) ForumPost {
return ForumPost{Hash: hash, Parent: prior.Parent, Date: date, User: user, Body: body, Prior: prior.Hash}
}
// one is a thread whose every post is a single version.
func one(posts ...ForumPost) [][]ForumPost {
var chains [][]ForumPost
for _, p := range posts {
chains = append(chains, []ForumPost{p})
}
return chains
}
var received = time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
// writers is a Provenance from a table: who wrote each hash, received
// well after every date in these tests unless a test says otherwise.
type writers map[string]string
func (w writers) prov(hash string) (string, time.Time, bool) {
who, ok := w[hash]
return who, received, ok
}
// claimed is the forge having recorded exactly what each post claims.
func claimed(chains [][]ForumPost) Provenance {
w := writers{}
for _, c := range chains {
for _, v := range c {
w[v.Hash] = v.User
}
}
return w.prov
}
// anyone and nobody are the two extremes of who may settle.
func anyone(string, string) bool { return true }
func nobody(string, string) bool { return false }
func hashes(ns []BlockingNote) []string {
var h []string
for _, n := range ns {
h = append(h, n.Hash)
}
return h
}
const (
b1 = "b1b1b1b1b1b1b1b1"
b2 = "b2b2b2b2b2b2b2b2"
)
// authors lets a comment's author and the landing's author (dan) settle.
func authors(actor, blockAuthor string) bool { return actor == blockAuthor || actor == "dan" }
func TestOpenBlockingFoldsResolvesAndReopensInPostOrder(t *testing.T) {
root := post("root", "2026-01-01T00:00:00", "dan", "zit-landing: 1\nbranch: x\ntarget: trunk\n\nland x")
root.Parent = ""
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
resolve := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "dan", FormatMarker(MarkResolved, b1[:10], "fixed"))
reopen := post("c2c2c2c2c2c2", "2026-01-01T03:00:00", "eve", FormatMarker(MarkReopened, b1, ""))
again := post("c3c3c3c3c3c3", "2026-01-01T04:00:00", "dan", FormatMarker(MarkResolved, b1, ""))
for _, tc := range []struct {
name string
posts []ForumPost
want []string
}{
{"nothing blocking", []ForumPost{root, post("a", "2026-01-01T01:00:00", "eve", "fine")}, nil},
{"open", []ForumPost{root, blocker}, []string{b1}},
{"resolved", []ForumPost{root, blocker, resolve}, nil},
{"resolved then reopened", []ForumPost{root, blocker, resolve, reopen}, []string{b1}},
{"resolved, reopened, resolved", []ForumPost{root, blocker, resolve, reopen, again}, nil},
{"input order does not matter", []ForumPost{again, reopen, root, resolve, blocker}, nil},
{"a reopen of an open blocker changes nothing", []ForumPost{root, blocker, reopen}, []string{b1}},
} {
chains := one(tc.posts...)
if got := hashes(OpenBlocking(chains, claimed(chains), authors)); !reflect.DeepEqual(got, tc.want) {
t.Errorf("FAIL: %s: open = %v, want %v", tc.name, got, tc.want)
}
}
// Equal times fall to hash order: the later hash is the newer.
sameTime := func(resHash, reoHash string) [][]ForumPost {
return one(root, blocker,
post(resHash, "2026-01-01T05:00:00", "dan", FormatMarker(MarkResolved, b1, "")),
post(reoHash, "2026-01-01T05:00:00", "dan", FormatMarker(MarkReopened, b1, "")))
}
if c := sameTime("aaaa", "bbbb"); len(OpenBlocking(c, claimed(c), authors)) != 1 {
t.Errorf("FAIL: a reopen with the later hash at the same time should win")
}
if c := sameTime("bbbb", "aaaa"); len(OpenBlocking(c, claimed(c), authors)) != 0 {
t.Errorf("FAIL: a resolve with the later hash at the same time should win")
}
}
func TestOpenBlockingCountsOnlyAuthorizedSettlers(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
stray := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "mallory", FormatMarker(MarkResolved, b1, ""))
own := post("c2c2c2c2c2c2", "2026-01-01T03:00:00", "eve", FormatMarker(MarkResolved, b1, ""))
reopen := post("c3c3c3c3c3c3", "2026-01-01T04:00:00", "mallory", FormatMarker(MarkReopened, b1, ""))
open := func(posts ...ForumPost) int {
c := one(posts...)
return len(OpenBlocking(c, claimed(c), authors))
}
if open(blocker, stray) != 1 {
t.Errorf("FAIL: a stranger's resolve settled a blocker")
}
if open(blocker, stray, own) != 0 {
t.Errorf("FAIL: the author's resolve was not counted")
}
// A reopen takes nobody's authority: anyone who can comment could start
// a new blocker, so anyone's reopen counts.
if open(blocker, own, reopen) != 1 {
t.Errorf("FAIL: a stranger's reopen did not count")
}
c := one(blocker, own)
if got := OpenBlocking(c, claimed(c), nobody); len(got) != 1 {
t.Errorf("FAIL: with no one allowed to settle, the blocker closed: %v", hashes(got))
}
}
// The user a post claims is whatever its clone wrote. A marker is judged
// by the account the forge recorded as carrying it, and one with no
// record is judged by nobody.
func TestAMarkersWriterIsWhoCarriedItNotWhoItClaims(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
forged := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "dan", FormatMarker(MarkResolved, b1, "")) // claims dan
c := one(blocker, forged)
w := writers{b1: "eve", forged.Hash: "mallory"}
if got := OpenBlocking(c, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a resolve claiming the landing author but carried by mallory settled: %v", hashes(got))
}
w = writers{b1: "eve", forged.Hash: "dan"}
if got := OpenBlocking(c, w.prov, authors); len(got) != 0 {
t.Errorf("FAIL: a resolve carried by the landing author did not settle: %v", hashes(got))
}
w = writers{b1: "eve"}
if got := OpenBlocking(c, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a resolve with no recorded writer settled: %v", hashes(got))
}
}
// A client picks its posts' dates. Order is by when the forge had them.
func TestAFutureDatedResolveDoesNotBeatALaterReopen(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
resolve := post("c1c1c1c1c1c1", "2099-01-01T00:00:00", "dan", FormatMarker(MarkResolved, b1, ""))
reopen := post("c2c2c2c2c2c2", "2026-01-01T03:00:00", "eve", FormatMarker(MarkReopened, b1, ""))
c := one(blocker, resolve, reopen)
at := map[string]time.Time{
b1: time.Date(2026, 1, 1, 1, 0, 0, 0, time.UTC),
resolve.Hash: time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC),
reopen.Hash: time.Date(2026, 1, 1, 3, 0, 0, 0, time.UTC),
}
prov := func(h string) (string, time.Time, bool) {
for _, p := range []ForumPost{blocker, resolve, reopen} {
if p.Hash == h {
return p.User, at[h], true
}
}
return "", time.Time{}, false
}
if got := OpenBlocking(c, prov, authors); len(got) != 1 {
t.Errorf("FAIL: a resolve dated 2099 beat a later reopen: open = %v", hashes(got))
}
}
func TestAnEditedBlockingPostKeepsItsResolveAndItsBlock(t *testing.T) {
v1 := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
v2 := edit(v1, "d2d2d2d2d2d2d2", "2026-01-01T02:30:00", "eve", "blocking: nil case (typo fixed)")
resolve := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "dan", FormatMarker(MarkResolved, b1[:10], ""))
chains := [][]ForumPost{{v1, v2}, {resolve}}
if got := OpenBlocking(chains, claimed(chains), authors); len(got) != 0 {
t.Errorf("FAIL: an edit reopened a comment resolved against its first version: %v", hashes(got))
}
// A resolve by the newest version's hash settles it too, and the
// comment is named by its newest version.
resolve2 := post("c2c2c2c2c2c2", "2026-01-01T03:00:00", "dan", FormatMarker(MarkResolved, v2.Hash, ""))
chains = [][]ForumPost{{v1, v2}, {resolve2}}
if got := OpenBlocking(chains, claimed(chains), authors); len(got) != 0 {
t.Errorf("FAIL: a resolve of the newest version did not settle: %v", hashes(got))
}
chains = [][]ForumPost{{v1, v2}}
if got := OpenBlocking(chains, claimed(chains), authors); len(got) != 1 || got[0].Hash != v2.Hash || got[0].User != "eve" || got[0].Text != "nil case (typo fixed)" {
t.Errorf("FAIL: the open comment is %+v, want the newest version, by its author", got)
}
}
func TestEditingAMarkerIsJudgedByTheEditorNotTheOriginalAuthor(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
remark := post("a1a1a1a1a1a1", "2026-01-01T02:00:00", "dan", "looks fine to me")
// A moderator edits the landing author's remark into a resolve. The
// version carries dan as its author; the editor was mod.
forged := edit(remark, "a2a2a2a2a2a2", "2026-01-01T03:00:00", "dan", FormatMarker(MarkResolved, b1, ""))
chains := [][]ForumPost{{blocker}, {remark, forged}}
w := writers{b1: "eve", remark.Hash: "dan", forged.Hash: "mod"}
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a moderator's edit settled a comment through the landing author's post: %v", hashes(got))
}
// The same edit by dan himself counts.
w[forged.Hash] = "dan"
if got := OpenBlocking(chains, w.prov, authors); len(got) != 0 {
t.Errorf("FAIL: the landing author's own edit into a resolve did not count: %v", hashes(got))
}
// A resolve edited away stops counting.
gone := edit(forged, "a3a3a3a3a3a3", "2026-01-01T04:00:00", "dan", "never mind")
chains = [][]ForumPost{{blocker}, {remark, forged, gone}}
w[gone.Hash] = "dan"
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a resolve edited away still counted: %v", hashes(got))
}
}
func TestADroppedMarkerDoesNotRemoveABlockerUnlessASettlerDroppedIt(t *testing.T) {
v1 := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
deprefixed := edit(v1, "d2d2d2d2d2d2d2", "2026-01-01T02:00:00", "eve", "nil case")
for _, tc := range []struct {
name, editor string
open int
}{
{"a stranger de-prefixes it", "mallory", 1},
{"a moderator with no say in it", "mod", 1},
{"the author withdraws it", "eve", 0},
{"the landing author removes it", "dan", 0},
{"an edit with no recorded writer", "", 1},
} {
chains := [][]ForumPost{{v1, deprefixed}}
w := writers{b1: "eve"}
if tc.editor != "" {
w[deprefixed.Hash] = tc.editor
}
if got := OpenBlocking(chains, w.prov, authors); len(got) != tc.open {
t.Errorf("FAIL: %s: open = %v, want %d", tc.name, hashes(got), tc.open)
}
}
// A deletion is an edit to nothing.
deleted := edit(v1, "d3d3d3d3d3d3d3", "2026-01-01T02:00:00", "mallory", "")
chains := [][]ForumPost{{v1, deleted}}
w := writers{b1: "eve", deleted.Hash: "mallory"}
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a stranger's deletion removed a blocker: %v", hashes(got))
}
}
func TestOpenBlockingIgnoresWhatIsNotAReviewRemark(t *testing.T) {
// An accept whose message happens to open with the marker is an
// event, not a review comment.
accepted := post("d1d1d1d1d1d1", "2026-01-01T03:00:00", "dan", "zit-landing-state: accepted\n\nblocking: x")
c := one(accepted)
if got := OpenBlocking(c, claimed(c), anyone); len(got) != 0 {
t.Errorf("FAIL: a state reply read as a blocker: %v", hashes(got))
}
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
// A prefix that names two blockers names neither.
other := post("b1b1b1b1b1ff", "2026-01-01T01:30:00", "eve", "blocking: other")
amb := post("c2c2c2c2c2c2", "2026-01-01T02:00:00", "eve", FormatMarker(MarkResolved, "b1b1b1b1b1", ""))
c = one(blocker, other, amb)
if got := OpenBlocking(c, claimed(c), anyone); len(got) != 2 {
t.Errorf("FAIL: an ambiguous prefix settled something: %v", hashes(got))
}
// A resolve with other text on the marker line is not one.
loose := post("c3c3c3c3c3c3", "2026-01-01T03:00:00", "eve", "resolved: "+b1+" yes")
c = one(blocker, loose)
if got := OpenBlocking(c, claimed(c), anyone); len(got) != 1 {
t.Errorf("FAIL: a malformed resolve settled: %v", hashes(got))
}
}
func TestOpenBlockingNamesWhereAndWhat(t *testing.T) {
note := LineNote{File: "internal/a.go", Side: "new", Line: 42}
line := post(b1, "2026-01-01T01:00:00", "eve", FormatLineNote(note, AsBlocking("guard the nil\n\nsecond paragraph")))
whole := post(b2, "2026-01-01T02:00:00", "eve", "blocking:\n\nthis whole approach needs a rethink")
c := one(line, whole)
got := OpenBlocking(c, claimed(c), anyone)
for i := range got {
got[i].Chain = nil
}
want := []BlockingNote{
{Hash: b1, User: "eve", Date: "2026-01-01T01:00:00", File: "internal/a.go", Line: 42, Text: "guard the nil"},
{Hash: b2, User: "eve", Date: "2026-01-01T02:00:00", Text: "this whole approach needs a rethink"},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("FAIL: got %+v, want %+v", got, want)
}
}
// Reopening grants no power anyone lacks, so nothing done to a reopen can
// take it away: not an edit by a moderator, not a lost carrier record.
func TestNothingDoneToAReopenTakesItAway(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
resolve := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "dan", FormatMarker(MarkResolved, b1, ""))
reopen := post("c2c2c2c2c2c2", "2026-01-01T03:00:00", "eve", FormatMarker(MarkReopened, b1, ""))
touched := edit(reopen, "c3c3c3c3c3c3", "2026-01-01T04:00:00", "eve", "an innocuous edit by a moderator")
gutted := edit(touched, "c4c4c4c4c4c4", "2026-01-01T05:00:00", "eve", "")
w := writers{b1: "eve", resolve.Hash: "dan", reopen.Hash: "eve", touched.Hash: "mod", gutted.Hash: "mallory"}
chains := [][]ForumPost{{blocker}, {resolve}, {reopen, touched, gutted}}
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: edits to the reopen cancelled it: open = %v", hashes(got))
}
// The reopener's carrier records are gone with their account.
delete(w, reopen.Hash)
delete(w, touched.Hash)
delete(w, gutted.Hash)
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a reopen with no recorded writer was lost: open = %v", hashes(got))
}
// Unrecorded, it sorts as the newest act, so a resolve after it
// cannot beat it; the way through is an override.
later := post("c5c5c5c5c5c5", "2026-01-01T06:00:00", "dan", FormatMarker(MarkResolved, b1, ""))
w[later.Hash] = "dan"
chains = append(chains, []ForumPost{later})
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: a resolve beat a reopen the forge has no receipt for: open = %v", hashes(got))
}
}
// A resolve is judged by the newest version of its post written by
// someone who may settle; later versions by anyone else are ignored.
func TestALaterVersionByAnotherDoesNotCancelAResolve(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case")
resolve := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "dan", FormatMarker(MarkResolved, b1, ""))
touched := edit(resolve, "c2c2c2c2c2c2", "2026-01-01T03:00:00", "dan", "tidied by a moderator")
chains := [][]ForumPost{{blocker}, {resolve, touched}}
w := writers{b1: "eve", resolve.Hash: "dan", touched.Hash: "mod"}
if got := OpenBlocking(chains, w.prov, authors); len(got) != 0 {
t.Errorf("FAIL: a moderator's edit of dan's resolve reopened the comment: %v", hashes(got))
}
// dan's own later edit says something else, and takes it back.
w[touched.Hash] = "dan"
if got := OpenBlocking(chains, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: dan editing his resolve away did not take it back: %v", hashes(got))
}
}
// The "own blocker" rule goes by who the forge recorded as writing the
// first version, and by the claimed name only when it recorded none.
func TestABlockersAuthorIsItsRecordedWriter(t *testing.T) {
blocker := post(b1, "2026-01-01T01:00:00", "eve", "blocking: nil case") // claims eve
byMallory := post("c1c1c1c1c1c1", "2026-01-01T02:00:00", "mallory", FormatMarker(MarkResolved, b1, ""))
byEve := post("c2c2c2c2c2c2", "2026-01-01T02:00:00", "eve", FormatMarker(MarkResolved, b1, ""))
w := writers{b1: "mallory", byMallory.Hash: "mallory", byEve.Hash: "eve"}
if got := OpenBlocking([][]ForumPost{{blocker}, {byMallory}}, w.prov, authors); len(got) != 0 {
t.Errorf("FAIL: the account that wrote the blocker could not resolve it: %v", hashes(got))
}
if got := OpenBlocking([][]ForumPost{{blocker}, {byEve}}, w.prov, authors); len(got) != 1 {
t.Errorf("FAIL: the name the blocker claims resolved it: %v", hashes(got))
}
if got := OpenBlocking([][]ForumPost{{blocker}, {byEve}}, writers{byEve.Hash: "eve"}.prov, authors); len(got) != 0 {
t.Errorf("FAIL: with no recorded writer the claimed name was not used: %v", hashes(got))
}
}