package api
import (
"encoding/json"
"errors"
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// blockEnv is two landings by dan in a real store, and a forge that
// knows who holds what and who carried which post. Each test gets its own
// letters, so one test changing what someone holds is nobody else's.
type blockEnv struct {
d Deps
dir string
ids [2]string
ix *index.Index
db *oltp.DB
letters map[string]string
}
// dan opened both landings, eve reviews, mallory is a stranger with the
// forum letter only, mod is a forum moderator, ann may accept, root is an
// admin.
func blockingLetters() map[string]string {
return map[string]string{"dan": "3", "eve": "3", "mallory": "3", "mod": "35", "ann": "3i", "root": "3ia"}
}
func newBlockEnv(t *testing.T, checks ChecksStore) *blockEnv {
t.Helper()
e := &blockEnv{letters: blockingLetters()}
var pipeSync func() (int, error)
e.dir, e.ids, e.ix, pipeSync = premergeStore(t)
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
e.db = db
e.d = Deps{Version: "v", ZitVersion: zitcli.MinVersion, Checks: checks, Notify: &fakeNotify{}, Carriers: db,
CapsFor: func(account, _ string) CapSet { return oltp.CapSet(e.letters[account]) },
Repos: NewRepos(map[string]*Repo{
"test": {Chat: "test", Index: e.ix, Zit: zitcli.New(e.dir), Sync: pipeSync}})}
return e
}
func blockingDeps(t *testing.T) *blockEnv { return newBlockEnv(t, noChecks{}) }
func (e *blockEnv) say(user string, in landingSayIn, verb string) (string, error) {
in.Repo = "test"
out, err := e.d.landingSay(as(user, e.letters[user]), in, verb)
return out.Hash, err
}
// synced writes a post the way a stock Fossil clone's push would land it:
// straight into the store under whatever user it claims, carried by the
// account that authenticated to push it.
func (e *blockEnv) synced(t *testing.T, carrier, claims, landing, body, parent string) string {
t.Helper()
h, err := zitcli.New(e.dir).LandingSay(testToken, "comment", landing, claims, body, authoredMimetype, parent)
if err != nil {
t.Fatal(err)
}
if carrier != "" {
if err := e.db.RecordCarriers("test", carrier, []string{h}); err != nil {
t.Fatal(err)
}
}
if _, err := testRepo(t, e.d).Sync(); err != nil {
t.Fatal(err)
}
return h
}
func (e *blockEnv) open(t *testing.T, id string) []string {
t.Helper()
return openHashes(t, e.d, e.ix, id)
}
func threadPost(t *testing.T, ix *index.Index, landing, hash string) index.ForumPost {
t.Helper()
_, posts, _, err := ix.ForumThreadPosts(landing)
if err != nil {
t.Fatal(err)
}
for _, p := range posts {
if p.Hash == hash {
return p
}
}
t.Fatalf("no post %s in %s", hash, landing)
return index.ForumPost{}
}
func openHashes(t *testing.T, d Deps, ix *index.Index, id string) []string {
t.Helper()
l, ok, err := ix.LandingByPrefix(id)
if err != nil || !ok {
t.Fatalf("no landing %s: %v", id, err)
}
_, open, _, err := d.blockingFor("test", testRepo(t, d), l)
if err != nil {
t.Fatal(err)
}
var out []string
for _, n := range open {
out = append(out, n.Hash)
}
return out
}
func testRepo(t *testing.T, d Deps) *Repo {
t.Helper()
repo, ok := d.Repos.Get("test")
if !ok {
t.Fatal("no repo")
}
return repo
}
func refusalCode(err error) string {
var ref *ops.Refusal
if errors.As(err, &ref) {
return ref.Code
}
return ""
}
func TestTheBlockingFlagWritesTheMarkerInThePost(t *testing.T) {
e := blockingDeps(t)
ids, ix := e.ids, e.ix
h, err := e.say("eve", landingSayIn{ID: ids[0], Message: "handle the nil case", Blocking: true}, "comment")
if err != nil {
t.Fatal(err)
}
if body := threadPost(t, ix, ids[0], h).Body; index.ParseMarker(body).Kind != index.MarkBlocking || !strings.HasPrefix(body, "blocking: handle") {
t.Errorf("FAIL: the post body is %q, want it to open with the marker", body)
}
// Already marked is not marked twice.
h2, err := e.say("eve", landingSayIn{ID: ids[0], Message: "Blocking: and this", Blocking: true}, "comment")
if err != nil {
t.Fatal(err)
}
if body := threadPost(t, ix, ids[0], h2).Body; strings.TrimSpace(body) != "Blocking: and this" {
t.Errorf("FAIL: the marker was doubled: %q", body)
}
// On a line, the marker sits under the header and the note still reads as one.
h3, err := e.say("eve", landingSayIn{ID: ids[0], Message: "guard it", Blocking: true,
File: "one.txt", Side: "new", Line: 1}, "comment")
if err != nil {
t.Fatal(err)
}
note, prose, ok := index.ParseLineNote(threadPost(t, ix, ids[0], h3).Body)
if !ok || note.File != "one.txt" || index.ParseMarker(prose).Kind != index.MarkBlocking {
t.Errorf("FAIL: a blocking line note reads as note=%+v prose=%q ok=%v", note, prose, ok)
}
if got := e.open(t, ids[0]); len(got) != 3 {
t.Errorf("FAIL: open = %v, want the three blocking comments", got)
}
// What cannot be written.
for name, in := range map[string]landingSayIn{
"nothing said": {Blocking: true},
"blocking + resolve": {Blocking: true, Message: "x", Resolves: h},
"resolve + reopen": {Resolves: h, Reopens: h},
} {
in.ID = ids[0]
if _, err := e.say("eve", in, "comment"); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: %s answered %v, want an invalid request", name, err)
}
}
if _, err := e.say("ann", landingSayIn{ID: ids[0], Message: "x", Blocking: true}, "reject"); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: a reject carrying the blocking flag answered %v", err)
}
}
func TestWhoMayResolveABlockingComment(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
block := func() string {
h, err := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if err != nil {
t.Fatal(err)
}
return h
}
for _, tc := range []struct {
who string
want bool
}{
{"eve", true}, // the comment's author
{"dan", true}, // the landing's author, with no accept letter
{"ann", true}, // an accepter
{"mallory", false}, // neither
{"mod", false}, // a forum moderator is not an accepter
} {
h := block()
_, err := e.say(tc.who, landingSayIn{ID: ids[0], Resolves: h[:12], Message: "done"}, "comment")
switch {
case tc.want && err != nil:
t.Errorf("FAIL: %s was refused a resolve: %v", tc.who, err)
case !tc.want && !errors.Is(err, ops.ErrForbidden):
t.Errorf("FAIL: %s resolved a comment they may not: %v", tc.who, err)
}
still := false
for _, o := range e.open(t, ids[0]) {
still = still || o == h
}
if still == tc.want {
t.Errorf("FAIL: after %s's resolve the comment open=%v, want %v", tc.who, still, !tc.want)
}
}
// Anyone who may comment may reopen: it lets nothing through, and
// anyone could start a new blocking comment as easily.
h := block()
if _, err := e.say("eve", landingSayIn{ID: ids[0], Resolves: h}, "comment"); err != nil {
t.Fatal(err)
}
if _, err := e.say("mallory", landingSayIn{ID: ids[0], Reopens: h}, "comment"); err != nil {
t.Errorf("FAIL: a commenter was refused a reopen: %v", err)
}
reopened := false
for _, o := range e.open(t, ids[0]) {
reopened = reopened || o == h
}
if !reopened {
t.Errorf("FAIL: the reopen did not take: open = %v", e.open(t, ids[0]))
}
}
func TestAResolveIsAThreadedReplyWithTheMarkerFirst(t *testing.T) {
e := blockingDeps(t)
ids, ix := e.ids, e.ix
h, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
r, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: h[:10]}, "comment")
if err != nil {
t.Fatal(err)
}
p := threadPost(t, ix, ids[0], r)
if strings.TrimSpace(p.Body) != "resolved: "+h || p.Parent != h {
t.Errorf("FAIL: the resolve is %q under %s, want the marker with the full hash, under the comment", p.Body, p.Parent)
}
o, err := e.say("eve", landingSayIn{ID: ids[0], Reopens: strings.ToUpper(h[:14]), Message: "not fixed"}, "comment")
if err != nil {
t.Fatal(err)
}
if body := threadPost(t, ix, ids[0], o).Body; strings.TrimSpace(body) != "reopened: "+h+"\n\nnot fixed" {
t.Errorf("FAIL: the reopen is %q", body)
}
if got := e.open(t, ids[0]); len(got) != 1 || got[0] != h {
t.Errorf("FAIL: resolve then reopen leaves %v open, want the comment", got)
}
}
func TestAResolveOfTheWrongThingIsRefusedWithACode(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
plain, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "looks fine"}, "comment")
blocker, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
elsewhere, _ := e.say("eve", landingSayIn{ID: ids[1], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("eve", landingSayIn{ID: ids[0], Resolves: plain}, "comment"); refusalCode(err) != RefusalNotBlocking || !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: resolving a plain remark answered %v", err)
}
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: ids[0]}, "comment"); refusalCode(err) != RefusalNotBlocking {
t.Errorf("FAIL: resolving the opener answered %v", err)
}
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: elsewhere}, "comment"); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: resolving another landing's comment answered %v", err)
}
if _, err := e.say("eve", landingSayIn{ID: ids[0], Resolves: blocker[:9]}, "comment"); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: a nine-character prefix answered %v", err)
}
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: "0000000000"}, "comment"); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: an unknown hash answered %v", err)
}
if _, err := e.say("eve", landingSayIn{ID: ids[0], Reopens: blocker}, "comment"); refusalCode(err) != RefusalBlockingUnchanged {
t.Errorf("FAIL: reopening an open comment answered %v", err)
}
if _, err := e.say("eve", landingSayIn{ID: ids[0], Resolves: blocker}, "comment"); err != nil {
t.Fatal(err)
}
_, err := e.say("eve", landingSayIn{ID: ids[0], Resolves: blocker}, "comment")
var ref *ops.Refusal
if !errors.As(err, &ref) || ref.Code != RefusalBlockingUnchanged || ref.Fields["state"] != "resolved" {
t.Errorf("FAIL: resolving twice answered %v", err)
}
}
// Who may settle is decided before anything about the thread is said, so
// a stranger cannot use the refusals to learn what is in it.
func TestAStrangerIsRefusedBeforeTheStateIsChecked(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
plain, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "looks fine"}, "comment")
blocker, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("eve", landingSayIn{ID: ids[0], Resolves: blocker}, "comment"); err != nil {
t.Fatal(err)
}
for name, in := range map[string]landingSayIn{
"a plain remark": {Resolves: plain},
"a resolved comment": {Resolves: blocker},
"a hash that is not there": {Resolves: "0000000000"},
} {
in.ID = ids[0]
if _, err := e.say("mallory", in, "comment"); !errors.Is(err, ops.ErrForbidden) || refusalCode(err) != "" {
t.Errorf("FAIL: a stranger resolving %s answered %v, want a plain forbidden", name, err)
}
}
}
// What a stock Fossil clone can write: any user string, no capability
// check. The reply is in the thread, and settles nothing.
func TestAStrayResolveSyncedInFromAnotherCloneSettlesNothing(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
blocker, err := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if err != nil {
t.Fatal(err)
}
marker := index.FormatMarker(index.MarkResolved, blocker, "trust me")
// Claiming to be the landing's author does not make mallory him.
e.synced(t, "mallory", "dan", ids[0], marker, blocker)
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: a stranger's synced resolve, claiming dan, settled the comment: open = %v", got)
}
// Nor does a post nobody recorded a carrier for.
e.synced(t, "", "dan", ids[0], marker, blocker)
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: a resolve with no recorded writer settled the comment: open = %v", got)
}
// The same words carried by the landing's author do.
e.synced(t, "dan", "dan", ids[0], index.FormatMarker(index.MarkResolved, blocker, ""), blocker)
if got := e.open(t, ids[0]); len(got) != 0 {
t.Fatalf("FAIL: the landing author's synced resolve was not counted: open = %v", got)
}
// Capabilities are read as they are NOW: ann held the accept letter
// when she resolved, and loses it, so her resolve stops counting.
b2, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "again", Blocking: true}, "comment")
if _, err := e.say("ann", landingSayIn{ID: ids[0], Resolves: b2}, "comment"); err != nil {
t.Fatal(err)
}
if got := e.open(t, ids[0]); len(got) != 0 {
t.Fatalf("FAIL: an accepter's resolve did not settle: %v", got)
}
e.letters["ann"] = "3"
if got := e.open(t, ids[0]); len(got) != 1 || got[0] != b2 {
t.Fatalf("FAIL: a resolve by someone who no longer holds the letter still counted: %v", got)
}
}
// An edit is a new version the editor wrote, whoever the post's author
// was. A moderator may edit anyone's post; that does not let the
// moderator speak as the author.
func TestAModeratorsEditOfTheLandingAuthorsRemarkSettlesNothing(t *testing.T) {
e := blockingDeps(t)
ids, ix := e.ids, e.ix
blocker, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
remark, _ := e.say("dan", landingSayIn{ID: ids[0], Message: "looks fine to me"}, "comment")
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("mod", "35"), "forum.edit", forumEditIn{Repo: "test", Post: remark,
Body: index.FormatMarker(index.MarkResolved, blocker, "")})
if err != nil {
t.Fatal(err)
}
edited := got.(forumEditOut).Hash
if p := threadPost(t, ix, ids[0], edited); p.User != "dan" || p.EditedBy != "mod" {
t.Fatalf("FAIL: setup: the edit reads user=%s editedBy=%s", p.User, p.EditedBy)
}
if open := e.open(t, ids[0]); len(open) != 1 {
t.Fatalf("FAIL: a moderator's edit of dan's remark settled the comment: open = %v", open)
}
// dan editing his own remark the same way does.
own, _ := e.say("dan", landingSayIn{ID: ids[0], Message: "another remark"}, "comment")
if _, err := r.Invoke(as("dan", "3"), "forum.edit", forumEditIn{Repo: "test", Post: own,
Body: index.FormatMarker(index.MarkResolved, blocker, "")}); err != nil {
t.Fatal(err)
}
if open := e.open(t, ids[0]); len(open) != 0 {
t.Fatalf("FAIL: the landing author's own edit into a resolve did not count: open = %v", open)
}
}
func TestEditingOrRemovingABlockingPostDoesNotMakeItVanish(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
// A moderator strips the marker, then another reader: still open.
if _, err := r.Invoke(as("mod", "35"), "forum.edit", forumEditIn{Repo: "test", Post: b, Body: "fix it"}); err != nil {
t.Fatal(err)
}
if open := e.open(t, ids[0]); len(open) != 1 {
t.Fatalf("FAIL: a moderator de-prefixing the comment removed it: open = %v", open)
}
// Deleting it is an edit to nothing, and the same.
if _, err := r.Invoke(as("mod", "35"), "forum.delete", forumDeleteIn{Repo: "test", Post: b}); err == nil {
if open := e.open(t, ids[0]); len(open) != 1 {
t.Fatalf("FAIL: a deletion by a non-settler removed the comment: open = %v", open)
}
}
// Its author withdrawing it is allowed: the edit is theirs.
if _, err := r.Invoke(as("eve", "3"), "forum.edit", forumEditIn{Repo: "test", Post: b, Body: "fix it, never mind"}); err != nil {
t.Fatal(err)
}
if open := e.open(t, ids[0]); len(open) != 0 {
t.Fatalf("FAIL: the author's own de-prefixing did not withdraw it: open = %v", open)
}
}
func TestAnEditedBlockingPostKeepsItsResolve(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b}, "comment"); err != nil {
t.Fatal(err)
}
if _, err := r.Invoke(as("eve", "3"), "forum.edit", forumEditIn{Repo: "test", Post: b, Body: "blocking: fix it (typo)"}); err != nil {
t.Fatal(err)
}
if open := e.open(t, ids[0]); len(open) != 0 {
t.Fatalf("FAIL: fixing a typo reopened a resolved comment: open = %v", open)
}
// And it can still be reopened by naming the OLD hash.
if _, err := e.say("dan", landingSayIn{ID: ids[0], Reopens: b}, "comment"); err != nil {
t.Fatalf("FAIL: reopening by an earlier version's hash: %v", err)
}
if open := e.open(t, ids[0]); len(open) != 1 {
t.Fatalf("FAIL: the reopen did not take: %v", open)
}
}
func TestAcceptRefusesWhileBlockingIsOpenAndTheOverrideLetsItThrough(t *testing.T) {
e := blockingDeps(t)
ids, dir, ix := e.ids, e.dir, e.ix
b1, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
_, _ = e.say("eve", landingSayIn{ID: ids[0], Message: "and here", Blocking: true, File: "one.txt", Side: "new", Line: 1}, "comment")
_, err := e.say("ann", landingSayIn{ID: ids[0]}, "accept")
var ref *ops.Refusal
if !errors.As(err, &ref) || ref.Code != RefusalBlockingOpen || !errors.Is(err, ops.ErrForbidden) {
t.Fatalf("FAIL: an accept over open blocking comments answered %v", err)
}
var open []blockingRef
if jerr := json.Unmarshal([]byte(ref.Fields["open"]), &open); jerr != nil || ref.Fields["count"] != "2" || len(open) != 2 {
t.Fatalf("FAIL: the refusal carries %v", ref.Fields)
}
if open[0].Hash != b1 || open[0].File != "" || open[1].File != "one.txt" || open[1].Line != 1 {
t.Errorf("FAIL: the refusal's list is %+v", open)
}
if out := zrun(t, dir, "landing", "ls"); !strings.Contains(out, ids[0][:10]) {
t.Fatalf("FAIL: the landing is not open after a refused accept:\n%s", out)
}
// A reason alone is not the opt-in, even from an admin.
if _, err := e.say("root", landingSayIn{ID: ids[0], Override: "release cut"}, "accept"); refusalCode(err) != RefusalBlockingOpen {
t.Fatalf("FAIL: an admin's reason without overrideBlocking answered %v, want blocking_open", err)
}
// The opt-in needs a reason, and an owner's or an admin's hand.
if _, err := e.say("root", landingSayIn{ID: ids[0], OverrideBlocking: true}, "accept"); !errors.Is(err, ops.ErrInvalid) {
t.Fatalf("FAIL: overrideBlocking with no reason answered %v", err)
}
if _, err := e.say("ann", landingSayIn{ID: ids[0], OverrideBlocking: true, Override: "hotfix"}, "accept"); !errors.Is(err, ops.ErrForbidden) || refusalCode(err) == RefusalBlockingOpen {
t.Fatalf("FAIL: a committer's override answered %v, want a plain forbidden", err)
}
if _, err := e.say("root", landingSayIn{ID: ids[0], OverrideBlocking: true, Override: "release cut"}, "accept"); err != nil {
t.Fatalf("FAIL: an admin's stated override was refused: %v", err)
}
// The reason is on the record, ahead of the merge.
_, posts, _, _ := ix.ForumThreadPosts(ids[0])
said := false
for _, p := range posts {
said = said || (strings.Contains(p.Body, "release cut") && strings.Contains(p.Body, short(b1)))
}
if !said {
t.Errorf("FAIL: the override's reason is not in the thread")
}
}
// A reason given to land over red checks is for red checks. With checks
// red AND a blocking comment open, it clears the first and not the second.
func TestAReasonForRedChecksDoesNotPassOpenBlockingComments(t *testing.T) {
e := newBlockEnv(t, mergeChecks{})
ids := e.ids
e.d.CI = &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
if _, err := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment"); err != nil {
t.Fatal(err)
}
if _, err := e.say("ann", landingSayIn{ID: ids[0], Override: "ship it"}, "accept"); refusalCode(err) != RefusalBlockingOpen {
t.Fatalf("FAIL: an accept with a reason for the checks answered %v, want blocking_open", err)
}
if _, err := e.say("root", landingSayIn{ID: ids[0], Override: "checks are flaky"}, "accept"); refusalCode(err) != RefusalBlockingOpen {
t.Fatalf("FAIL: an admin's reason for the checks passed the blocking comment: %v", err)
}
// Both, said out loud, land it.
if _, err := e.say("root", landingSayIn{ID: ids[0], Override: "checks are flaky", OverrideBlocking: true}, "accept"); err != nil {
t.Fatalf("FAIL: an admin overriding both was refused: %v", err)
}
}
func TestAcceptProceedsOnceEveryBlockingCommentIsResolved(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
b1, _ := e.say("eve", landingSayIn{ID: ids[1], Message: "one", Blocking: true}, "comment")
b2, _ := e.say("eve", landingSayIn{ID: ids[1], Message: "two", Blocking: true}, "comment")
for _, h := range []string{b1, b2} {
if _, err := e.say("ann", landingSayIn{ID: ids[1]}, "accept"); refusalCode(err) != RefusalBlockingOpen {
t.Fatalf("FAIL: accept before resolving %s answered %v", short(h), err)
}
if _, err := e.say("eve", landingSayIn{ID: ids[1], Resolves: h}, "comment"); err != nil {
t.Fatal(err)
}
}
if _, err := e.say("ann", landingSayIn{ID: ids[1]}, "accept"); err != nil {
t.Fatalf("FAIL: the accept after the last resolve was refused: %v", err)
}
}
// Without a capability plane nobody holds the accept letter, on the
// write and on the read alike: only the two authors settle.
func TestWithoutACapabilityPlaneOnlyTheAuthorsSettle(t *testing.T) {
e := blockingDeps(t)
e.d.CapsFor = nil
ids := e.ids
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("ann", landingSayIn{ID: ids[0], Resolves: b}, "comment"); !errors.Is(err, ops.ErrForbidden) {
t.Fatalf("FAIL: without a plane, an accepter's resolve answered %v", err)
}
// A resolve that reached the thread anyway counts as it would be written.
e.synced(t, "ann", "ann", ids[0], index.FormatMarker(index.MarkResolved, b, ""), b)
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: without a plane, a synced resolve by a stranger settled: %v", got)
}
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b}, "comment"); err != nil {
t.Fatalf("FAIL: without a plane, the landing's author was refused: %v", err)
}
if got := e.open(t, ids[0]); len(got) != 0 {
t.Fatalf("FAIL: the landing author's resolve did not settle: %v", got)
}
}
func TestLandingGetCarriesTheOpenBlockingComments(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
h, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it\nmore", Blocking: true, File: "one.txt", Side: "new", Line: 1}, "comment")
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
get := func(id string) landingGetOut {
got, err := r.Invoke(as("eve", "3"), "landing.get", landingGetIn{Repo: "test", ID: id})
if err != nil {
t.Fatal(err)
}
return got.(landingGetOut)
}
out := get(ids[0])
if out.BlockingOpen != 1 || len(out.Blocking) != 1 {
t.Fatalf("FAIL: blockingOpen = %d, blocking = %+v", out.BlockingOpen, out.Blocking)
}
if n := out.Blocking[0]; n.Hash != h || n.User != "eve" || n.File != "one.txt" || n.Line != 1 || n.Text != "fix it" {
t.Errorf("FAIL: the listed comment is %+v", n)
}
raw, _ := json.Marshal(out)
var wire map[string]json.RawMessage
_ = json.Unmarshal(raw, &wire)
if _, ok := wire["blockingOpen"]; !ok {
t.Errorf("FAIL: no blockingOpen key on the wire")
}
if _, ok := wire["blocking"]; !ok {
t.Errorf("FAIL: no blocking key on the wire")
}
if other := get(ids[1]); other.BlockingOpen != 0 || other.Blocking == nil {
t.Errorf("FAIL: a landing with none reads blockingOpen=%d blocking=%v, want 0 and an empty list", other.BlockingOpen, other.Blocking)
}
for _, name := range []string{"landing.comment", "landing.accept"} {
if desc, ok := r.Lookup(name); !ok || !strings.Contains(desc.Doc, "blocking") {
t.Errorf("FAIL: %s's doc does not mention blocking", name)
}
}
}
func TestAnArmedLandingWaitsOnBlockingAndLandsOnceItIsResolved(t *testing.T) {
e := blockingDeps(t)
ids, dir, ix := e.ids, e.dir, e.ix
b, _ := e.say("eve", landingSayIn{ID: ids[1], Message: "fix it", Blocking: true}, "comment")
store := &sweepStub{rows: []oltp.AutoAccept{{Repo: "test", Landing: ids[1], Account: "dan", Tip: ztip(t, dir, "two")}}}
if landed := e.d.AutoAcceptSweep(store); len(landed) != 0 {
t.Fatalf("FAIL: auto-land landed over an open blocking comment: %v", landed)
}
if len(store.waiting) == 0 || !strings.Contains(store.waiting[len(store.waiting)-1], blockingOpenPhrase) {
t.Fatalf("FAIL: the armed landing is not told what it waits on: %v", store.waiting)
}
if _, err := e.say("dan", landingSayIn{ID: ids[1], Resolves: b}, "comment"); err != nil {
t.Fatal(err)
}
// No other trigger: the next sweep reads the thread as it is.
if landed := e.d.AutoAcceptSweep(store); len(landed) != 1 {
t.Fatalf("FAIL: the sweep after the resolve landed %v", landed)
}
if l, _, _ := ix.LandingByPrefix(ids[1]); l.State != "accepted" {
t.Fatalf("FAIL: the landing is %s", l.State)
}
}
func TestAStackWalkStopsAtARungWithOpenBlocking(t *testing.T) {
dir, ids, ix, pipeSync := strataStore(t)
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
e := &blockEnv{dir: dir, ix: ix, db: db, letters: blockingLetters()}
e.d = Deps{Version: "v", ZitVersion: zitcli.MinVersion, Checks: noChecks{}, Notify: &fakeNotify{}, Carriers: db,
CapsFor: func(account, _ string) CapSet { return oltp.CapSet(e.letters[account]) },
Repos: NewRepos(map[string]*Repo{
"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
b, err := e.say("eve", landingSayIn{ID: ids["high"], Message: "fix it", Blocking: true}, "comment")
if err != nil {
t.Fatal(err)
}
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
// A press lands the stack whole or not at all: the clean lower rung stays.
got, err := r.Invoke(as("ann", "3i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
if out := got.(landingLandOut); out.Stopped != ids["high"] || len(out.Landed) != 0 || !strings.Contains(out.Reason, blockingOpenPhrase) {
t.Fatalf("FAIL: the walk over a blocked rung answered %+v", out)
}
if l, _, _ := ix.LandingByPrefix(ids["low"]); l.State != "open" {
t.Fatalf("FAIL: the rung below landed (%s) though the stack was refused", l.State)
}
// A blocked lower rung stops the walk at it, whatever the top says.
bl, _ := e.say("eve", landingSayIn{ID: ids["low"], Message: "and this", Blocking: true}, "comment")
got, err = r.Invoke(as("ann", "3i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
if out := got.(landingLandOut); out.Stopped != ids["low"] || len(out.Landed) != 0 {
t.Fatalf("FAIL: the walk over a blocked base answered %+v", out)
}
for _, h := range []struct{ id, hash string }{{ids["low"], bl}, {ids["high"], b}} {
if _, err := e.say("eve", landingSayIn{ID: h.id, Resolves: h.hash}, "comment"); err != nil {
t.Fatal(err)
}
}
got, err = r.Invoke(as("ann", "3i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
if out := got.(landingLandOut); len(out.Landed) != 2 || out.Stopped != "" {
t.Fatalf("FAIL: the walk after both resolves answered %+v", out)
}
}
// dan resolves, eve reopens, and a moderator then edits eve's reopen:
// the comment stays open.
func TestAModeratorEditingAReopenDoesNotCancelIt(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b}, "comment"); err != nil {
t.Fatal(err)
}
reopen, err := e.say("eve", landingSayIn{ID: ids[0], Reopens: b, Message: "not fixed"}, "comment")
if err != nil {
t.Fatal(err)
}
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: setup: the reopen did not take: %v", got)
}
for _, body := range []string{"tidied", "", "reopened: nonsense"} {
if body == "" {
if _, err := r.Invoke(as("mod", "35"), "forum.delete", forumDeleteIn{Repo: "test", Post: reopen, Reason: "noise"}); err != nil {
continue // only admins delete others' posts; the edits are the case
}
} else if _, err := r.Invoke(as("mod", "35"), "forum.edit", forumEditIn{Repo: "test", Post: reopen, Body: body}); err != nil {
t.Fatal(err)
}
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: a moderator's change (%q) to the reopen cancelled it: open = %v", body, got)
}
}
}
// A deleted account keeps its carrier records: its reopen still counts,
// and keeps its place in time, so a later resolve by a settler can
// still clear it.
func TestAReopenSurvivesTheReopenersAccountBeingDeleted(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
if err := e.db.AddAccount("eve", "a-long-enough-password", false); err != nil {
t.Fatal(err)
}
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b}, "comment"); err != nil {
t.Fatal(err)
}
if _, err := e.say("eve", landingSayIn{ID: ids[0], Reopens: b}, "comment"); err != nil {
t.Fatal(err)
}
if err := e.db.DeleteAccount("eve"); err != nil {
t.Fatal(err)
}
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: the comment closed when the reopener's account was deleted: open = %v", got)
}
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b}, "comment"); err != nil {
t.Fatal(err)
}
if got := e.open(t, ids[0]); len(got) != 0 {
t.Fatalf("FAIL: a resolve after the deleted account's reopen did not clear it: open = %v", got)
}
}
// A held post, once approved, was written by its author: the moderator
// who released it is not the writer of what it says.
func TestAnApprovedHeldResolveIsJudgedByItsAuthor(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
e.d.Forum = e.db
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
hold := func(author string) {
id, err := e.db.StagePost("test", "reply", b, "", index.FormatMarker(index.MarkResolved, b, ""), "", author)
if err != nil {
t.Fatal(err)
}
if _, err := releaseStaged(e.d, "test", id, "mod"); err != nil {
t.Fatal(err)
}
}
hold("mallory")
if got := e.open(t, ids[0]); len(got) != 1 {
t.Fatalf("FAIL: an approved held resolve by a stranger settled the comment: open = %v", got)
}
hold("dan")
if got := e.open(t, ids[0]); len(got) != 0 {
t.Fatalf("FAIL: an approved held resolve by the landing author did not count: open = %v", got)
}
}
// A page draws the state landing.get hands it, so each post says what it
// is: an open or a resolved blocker (with who settled it), or a settle,
// and whether the CALLER may resolve. The marker line never reaches HTML.
func TestLandingGetMarksEachPostWithItsBlockingState(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
open, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "still open", Blocking: true}, "comment")
done, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "settled one", Blocking: true, File: "one.txt", Side: "new", Line: 1}, "comment")
res, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: done, Message: "fixed it"}, "comment")
if err != nil {
t.Fatal(err)
}
// A stranger's resolve, synced in, is not a settle: it stays a remark.
stray := e.synced(t, "mallory", "dan", ids[0], index.FormatMarker(index.MarkResolved, open, "trust me"), open)
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
get := func(user, letters string) map[string]landingPost {
got, err := r.Invoke(as(user, letters), "landing.get", landingGetIn{Repo: "test", ID: ids[0]})
if err != nil {
t.Fatal(err)
}
m := map[string]landingPost{}
for _, p := range got.(landingGetOut).Posts {
m[p.Hash] = p
}
return m
}
posts := get("dan", "3")
if p := posts[open]; p.Blocking != "open" || !p.MaySettle || strings.Contains(p.HTML, "blocking:") || !strings.Contains(p.HTML, "still open") {
t.Errorf("FAIL: the open blocker reads %+v", p)
}
if p := posts[done]; p.Blocking != "resolved" || p.ResolvedBy != "dan" || p.ResolvedAt == "" || p.Line == nil || strings.Contains(p.HTML, "blocking:") {
t.Errorf("FAIL: the resolved line-note blocker reads %+v", p)
}
if p := posts[res]; p.Settle != "resolved" || p.Settles != done || strings.Contains(p.HTML, "resolved:") || !strings.Contains(p.HTML, "fixed it") {
t.Errorf("FAIL: the resolve reads %+v", p)
}
if p := posts[stray]; p.Settle != "" || p.Blocking != "" {
t.Errorf("FAIL: a stranger's synced resolve reads %+v, want a plain remark", p)
}
if p := get("mallory", "3")[open]; p.Blocking != "open" || p.MaySettle {
t.Errorf("FAIL: a stranger is offered the resolve: %+v", p)
}
raw, _ := json.Marshal(posts[open])
var wire map[string]json.RawMessage
_ = json.Unmarshal(raw, &wire)
for _, k := range []string{"blocking", "maySettle"} {
if _, ok := wire[k]; !ok {
t.Errorf("FAIL: no %q key on the wire", k)
}
}
}
// landing.get draws a reply as a settle exactly where the gate counted it:
// from the version it counted, under the writer the forge recorded.
func TestLandingGetDrawsASettleWhereTheGateCountedIt(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
get := func() map[string]landingPost {
got, err := r.Invoke(as("dan", "3"), "landing.get", landingGetIn{Repo: "test", ID: ids[0]})
if err != nil {
t.Fatal(err)
}
m := map[string]landingPost{}
for _, p := range got.(landingGetOut).Posts {
m[p.Hash] = p
}
return m
}
// A moderator's edit of dan's counted resolve changes nothing the gate
// reads, so the page still folds it in as dan's resolve; its words are
// the newest version's, so a clean edit is not undone on the page.
b1, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "first", Blocking: true}, "comment")
res, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b1, Message: "done properly"}, "comment")
if err != nil {
t.Fatal(err)
}
edited, err := r.Invoke(as("mod", "35"), "forum.edit", forumEditIn{Repo: "test", Post: res, Body: "redacted"})
if err != nil {
t.Fatal(err)
}
if open := e.open(t, ids[0]); len(open) != 0 {
t.Fatalf("FAIL: setup: the moderator's edit reopened the comment: %v", open)
}
p := get()[edited.(forumEditOut).Hash]
if p.Settle != "resolved" || p.Settles != b1 || p.SettleBy != "dan" || p.SettleUnverified ||
!strings.Contains(p.HTML, "redacted") || strings.Contains(p.HTML, "done properly") || strings.Contains(p.HTML, "resolved:") {
t.Errorf("FAIL: the edited resolve reads %+v", p)
}
// A resolve written as a line note, as a stock clone can, is counted
// by the gate, so it is a settle and never a note under a line.
b2, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "second", Blocking: true}, "comment")
body := index.FormatLineNote(index.LineNote{File: "one.txt", Side: "new", Line: 1}, index.FormatMarker(index.MarkResolved, b2, "as a line note"))
ln := e.synced(t, "dan", "dan", ids[0], body, b2)
if open := e.open(t, ids[0]); len(open) != 0 {
t.Fatalf("FAIL: setup: the line-note resolve was not counted: %v", open)
}
if p := get()[ln]; p.Settle != "resolved" || p.Settles != b2 || p.Line != nil || !strings.Contains(p.HTML, "as a line note") {
t.Errorf("FAIL: the line-note resolve reads %+v", p)
}
// A reopen no account is recorded for names the artifact's claimed
// user, and says that nobody vouches for it.
ro := e.synced(t, "", "ghost", ids[0], index.FormatMarker(index.MarkReopened, b2, "not so"), b2)
if p := get()[ro]; p.Settle != "reopened" || p.SettleBy != "ghost" || !p.SettleUnverified {
t.Errorf("FAIL: the unrecorded reopen reads %+v", p)
}
}
// An abusive reopen an admin deleted still counts, because a reopen
// counts from any version of its post, so the comment stays open; its row
// says it was removed and does not bring the old words back.
func TestADeletedReopenStillCountsAndItsRowIsNotResurrected(t *testing.T) {
e := blockingDeps(t)
ids := e.ids
r := ops.NewRegistry()
if err := RegisterAll(r, e.d); err != nil {
t.Fatal(err)
}
b, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
if _, err := e.say("dan", landingSayIn{ID: ids[0], Resolves: b}, "comment"); err != nil {
t.Fatal(err)
}
reopen, err := e.say("eve", landingSayIn{ID: ids[0], Reopens: b, Message: "ABUSIVE WORDS"}, "comment")
if err != nil {
t.Fatal(err)
}
del, err := r.Invoke(as("root", "3ia"), "forum.delete", forumDeleteIn{Repo: "test", Post: reopen, Reason: "abuse"})
if err != nil {
t.Fatal(err)
}
if open := e.open(t, ids[0]); len(open) != 1 {
t.Fatalf("FAIL: deleting the reopen settled the comment: open = %v", open)
}
got, err := r.Invoke(as("dan", "3"), "landing.get", landingGetIn{Repo: "test", ID: ids[0]})
if err != nil {
t.Fatal(err)
}
var row *landingPost
for _, p := range got.(landingGetOut).Posts {
if p.Hash == del.(forumEditOut).Hash {
row = &p
}
}
if row == nil || row.Settle != "reopened" || row.Settles != b || !row.Deleted || row.HTML != "" || strings.Contains(row.HTML, "ABUSIVE") {
t.Errorf("FAIL: the deleted reopen's row reads %+v", row)
}
}
// The landing page's "Comment and resolve" posts the reply's to, the
// marker and the note together; the note lands in the same post.
func TestAReplyToTheBlockerCanResolveItWithANote(t *testing.T) {
e := blockingDeps(t)
ids, ix := e.ids, e.ix
h, _ := e.say("eve", landingSayIn{ID: ids[0], Message: "fix it", Blocking: true}, "comment")
r, err := e.say("dan", landingSayIn{ID: ids[0], To: h[:12], Resolves: h, Message: "fixed in the next check-in"}, "comment")
if err != nil {
t.Fatal(err)
}
p := threadPost(t, ix, ids[0], r)
if p.Parent != h || strings.TrimSpace(p.Body) != "resolved: "+h+"\n\nfixed in the next check-in" {
t.Errorf("FAIL: the reply is %q under %s, want the marker and the note under the comment", p.Body, p.Parent)
}
if got := e.open(t, ids[0]); len(got) != 0 {
t.Errorf("FAIL: the comment is still open after a resolving reply: %v", got)
}
}