test(protocol): pin data-model + ready-front production-scar clauses (wy-nzybu)
Beads Protocol v0 conformance corpus, WP3. Seven CLI-level tests for the
normative clauses agents (and the Gas Station worker fleet) already rely on
but that nothing pinned:
- D1.6 bd create does NOT self-assign, --actor included (assignee_test.go)
- D2.4 bd update --status open preserves assignee; only -a "" clears it
- D2.3 the per-parent child-id counter never reuses a number, even after
the child holding it is closed or deleted (child_id_test.go)
- G1.3 dep re-add is idempotent for the same (issue, depends_on, type) and
an error on a type conflict; remove-then-add is the retype path
(dep_identity_test.go)
- G2.2 a merely-open parent epic does not block its children; only a
blocked parent propagates (ready_front_test.go)
- R3 bd ready --parent is transitive over descendants (GH#3396), by
dotted id and by parent-child edge
- R2 ready default order is priority ASC, created_at DESC, id ASC; the
id tiebreak is exercised with an imported pair sharing created_at,
since created_at is second-granular and a bd invocation is not
Also set BD_DISABLE_METRICS=1 in the protocol workspace env: HOME points at
the t.TempDir workspace, so the telemetry emitter wrote eventsData/*.lock
asynchronously under it and raced TempDir cleanup ("directory not empty"),
which flaked tests in this package at random.
This commit is contained in:
parent
303e263fec
commit
b63453f277
5 changed files with 507 additions and 0 deletions
76
cmd/bd/protocol/assignee_test.go
Normal file
76
cmd/bd/protocol/assignee_test.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package protocol
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestProtocol_CreateDoesNotSelfAssign pins protocol clause D1.6: a plain
|
||||
// `bd create` yields an UNASSIGNED issue, even though an actor identity is
|
||||
// resolvable (D2.2) and is recorded in created_by. assignee is the work-queue
|
||||
// field the unassigned-only claim rule (L3.1) reads, so a self-assigning create
|
||||
// would make every freshly created issue unclaimable by a worker fleet — the
|
||||
// folk belief this clause corrects.
|
||||
func TestProtocol_CreateDoesNotSelfAssign(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
|
||||
// The workspace git config sets user.name=protocol-test, so an actor
|
||||
// identity IS resolvable; the point is that it lands in created_by only.
|
||||
id := w.create("--title", "Fresh work", "--type", "task")
|
||||
|
||||
issue := w.showJSON(id)
|
||||
if assignee, ok := issue["assignee"].(string); ok && assignee != "" {
|
||||
t.Errorf("D1.6: bd create self-assigned (assignee = %q); a fresh issue MUST be unassigned", assignee)
|
||||
}
|
||||
assertField(t, issue, "created_by", "protocol-test")
|
||||
|
||||
// The observable consequence: the issue is immediately claimable, i.e. it
|
||||
// shows up in the unassigned ready front a worker claims from.
|
||||
out := w.run("ready", "--unassigned", "--json")
|
||||
if findByID(parseJSONOutput(t, out), id) == nil {
|
||||
t.Errorf("D1.6: freshly created %s is missing from `bd ready --unassigned`; it must be immediately claimable", id)
|
||||
}
|
||||
|
||||
// An explicit --actor does not change this: it selects the actor identity,
|
||||
// not the assignee.
|
||||
id2 := w.create("--title", "Fresh work, explicit actor", "--type", "task", "--actor", "alice")
|
||||
issue2 := w.showJSON(id2)
|
||||
if assignee, ok := issue2["assignee"].(string); ok && assignee != "" {
|
||||
t.Errorf("D1.6: bd create --actor alice set assignee = %q; --actor MUST NOT self-assign", assignee)
|
||||
}
|
||||
assertField(t, issue2, "created_by", "alice")
|
||||
}
|
||||
|
||||
// TestProtocol_StatusUpdatePreservesAssignee pins protocol clause D2.4:
|
||||
// `bd update` mutates only the fields the caller named. Specifically, a status
|
||||
// change MUST NOT touch assignee — the Gas Station fleet relies on this when it
|
||||
// parks an in_progress unit back to open without releasing its owner, and the
|
||||
// inverse (status update silently clearing the assignee) would hand a live
|
||||
// worker's unit to the next claimer.
|
||||
func TestProtocol_StatusUpdatePreservesAssignee(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
id := w.create("--title", "Assigned work", "--type", "task")
|
||||
|
||||
w.run("update", id, "-a", "worker-7", "--status", "in_progress")
|
||||
issue := w.showJSON(id)
|
||||
assertField(t, issue, "assignee", "worker-7")
|
||||
assertField(t, issue, "status", "in_progress")
|
||||
|
||||
// in_progress -> open: status changes, assignee survives.
|
||||
w.run("update", id, "--status", "open")
|
||||
issue = w.showJSON(id)
|
||||
assertField(t, issue, "status", "open")
|
||||
assertField(t, issue, "assignee", "worker-7")
|
||||
|
||||
// The same holds for a priority-only update (no status in the command).
|
||||
w.run("update", id, "-p", "0")
|
||||
issue = w.showJSON(id)
|
||||
assertField(t, issue, "assignee", "worker-7")
|
||||
assertFieldFloat(t, issue, "priority", 0)
|
||||
|
||||
// Only an explicit `-a ""` clears the assignee (D1.6).
|
||||
w.run("update", id, "-a", "")
|
||||
issue = w.showJSON(id)
|
||||
if assignee, ok := issue["assignee"].(string); ok && assignee != "" {
|
||||
t.Errorf(`D1.6: bd update -a "" left assignee = %q, want cleared`, assignee)
|
||||
}
|
||||
}
|
||||
60
cmd/bd/protocol/child_id_test.go
Normal file
60
cmd/bd/protocol/child_id_test.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestProtocol_ChildIDCounterNeverReusesNumbers pins protocol clause D2.3: the
|
||||
// per-parent child-id counter is monotonic — numbers are never reused, even
|
||||
// after the child holding a number is closed or deleted. Reuse would resurrect
|
||||
// a retired id: a stale reference (a commit message, an archived JSONL line, a
|
||||
// worker's claim) would silently point at a different issue.
|
||||
func TestProtocol_ChildIDCounterNeverReusesNumbers(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
parent := w.create("--title", "Counter epic", "--type", "epic")
|
||||
|
||||
c1 := w.create("--title", "child one", "--parent", parent)
|
||||
c2 := w.create("--title", "child two", "--parent", parent)
|
||||
c3 := w.create("--title", "child three", "--parent", parent)
|
||||
|
||||
for i, got := range []string{c1, c2, c3} {
|
||||
want := fmt.Sprintf("%s.%d", parent, i+1)
|
||||
if got != want {
|
||||
t.Fatalf("D2.3: child %d minted id %q, want %q", i+1, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the highest child: the counter must NOT rewind to it.
|
||||
w.run("delete", c3, "--force")
|
||||
if _, err := w.tryRun("show", c3, "--json"); err == nil {
|
||||
t.Fatalf("D2.3 setup: %s still exists after `bd delete --force`", c3)
|
||||
}
|
||||
|
||||
c4 := w.create("--title", "child four", "--parent", parent)
|
||||
if c4 != parent+".4" {
|
||||
t.Errorf("D2.3: after deleting %s, the next child minted %q — want %s.4 (the counter must never reuse a number)", c3, c4, parent)
|
||||
}
|
||||
|
||||
// Closing a child must not rewind the counter either.
|
||||
w.run("close", c4, "--reason", "done")
|
||||
c5 := w.create("--title", "child five", "--parent", parent)
|
||||
if c5 != parent+".5" {
|
||||
t.Errorf("D2.3: after closing %s, the next child minted %q — want %s.5", c4, c5, parent)
|
||||
}
|
||||
|
||||
// Deleting the whole live set must still not rewind: the counter is per
|
||||
// parent and persists independently of which children currently exist.
|
||||
w.run("delete", c1, c2, c5, "--force")
|
||||
c6 := w.create("--title", "child six", "--parent", parent)
|
||||
if c6 != parent+".6" {
|
||||
t.Errorf("D2.3: after deleting every remaining child, the next child minted %q — want %s.6", c6, parent)
|
||||
}
|
||||
|
||||
// Sanity: a dotted child id is <parent>.<N>, not a fresh hash id (J7.2).
|
||||
if !strings.HasPrefix(c6, parent+".") {
|
||||
t.Errorf("D2.3/J7.2: child id %q is not a dotted child of %q", c6, parent)
|
||||
}
|
||||
}
|
||||
84
cmd/bd/protocol/dep_identity_test.go
Normal file
84
cmd/bd/protocol/dep_identity_test.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestProtocol_DepReAddSameTypeIsIdempotent pins the first half of protocol
|
||||
// clause G1.3: an edge is identified by (issue_id, depends_on_id), so re-adding
|
||||
// the same pair with the same type is idempotent — it succeeds and leaves
|
||||
// exactly ONE edge. Agents (and JSONL import) re-assert edges routinely; a
|
||||
// duplicate row would double-count blockers and corrupt the ready front.
|
||||
func TestProtocol_DepReAddSameTypeIsIdempotent(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
blocked := w.create("--title", "Blocked", "--type", "task")
|
||||
blocker := w.create("--title", "Blocker", "--type", "task")
|
||||
|
||||
w.run("dep", "add", blocked, blocker)
|
||||
// Same pair, same type (blocks is the default) — must succeed, not error.
|
||||
w.run("dep", "add", blocked, blocker)
|
||||
// Explicitly naming the same type must also succeed.
|
||||
w.run("dep", "add", blocked, blocker, "--type", "blocks")
|
||||
|
||||
issue := w.showJSON(blocked)
|
||||
requireDepEdgesEqual(t, getObjectSlice(issue, "dependencies"),
|
||||
[]depEdge{{issueID: blocked, dependsOnID: blocker}},
|
||||
"G1.3: dependencies after three identical `dep add` calls")
|
||||
}
|
||||
|
||||
// TestProtocol_DepReAddDifferentTypeIsAnError pins the second half of G1.3: the
|
||||
// same (issue_id, depends_on_id) pair with a DIFFERENT type is an error, not a
|
||||
// silent retype — the caller must remove the edge first. A silent retype would
|
||||
// let `dep add --type discovered-from` quietly demote a blocking edge and make
|
||||
// a blocked issue ready.
|
||||
func TestProtocol_DepReAddDifferentTypeIsAnError(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
from := w.create("--title", "Discovered work", "--type", "task")
|
||||
to := w.create("--title", "Original work", "--type", "task")
|
||||
|
||||
w.run("dep", "add", from, to, "--type", "blocks")
|
||||
|
||||
out, code := w.runExpectError("dep", "add", from, to, "--type", "discovered-from")
|
||||
if code == 0 {
|
||||
t.Fatalf("G1.3: conflicting dep re-add exited 0, want non-zero\n%s", out)
|
||||
}
|
||||
if !strings.Contains(out, "already exists with type") {
|
||||
t.Errorf("G1.3: conflict error should name the existing type; got:\n%s", out)
|
||||
}
|
||||
|
||||
// The original edge is untouched by the rejected call.
|
||||
issue := w.showJSON(from)
|
||||
requireDepEdgesEqual(t, getObjectSlice(issue, "dependencies"),
|
||||
[]depEdge{{issueID: from, dependsOnID: to}},
|
||||
"G1.3: dependencies after a rejected retype")
|
||||
if !depHasType(t, w, from, to, "blocks") {
|
||||
t.Errorf("G1.3: rejected retype changed the edge type of %s -> %s; it must stay \"blocks\"", from, to)
|
||||
}
|
||||
|
||||
// Remove-then-re-add is the sanctioned retype path.
|
||||
w.run("dep", "remove", from, to)
|
||||
w.run("dep", "add", from, to, "--type", "discovered-from")
|
||||
if !depHasType(t, w, from, to, "discovered-from") {
|
||||
t.Errorf("G1.3: after remove + re-add, %s -> %s should have type \"discovered-from\"", from, to)
|
||||
}
|
||||
}
|
||||
|
||||
// depHasType reports whether `bd dep list <id> --json` carries an edge
|
||||
// id -> dependsOn with the given type. A single-id `dep list` emits the
|
||||
// depended-on ISSUES, each carrying the edge type as "dependency_type".
|
||||
func depHasType(t *testing.T, w *workspace, id, dependsOn, want string) bool {
|
||||
t.Helper()
|
||||
out := w.run("dep", "list", id, "--json")
|
||||
for _, obj := range parseJSONOutput(t, out) {
|
||||
if obj["id"] != dependsOn {
|
||||
continue
|
||||
}
|
||||
if typ, ok := obj["dependency_type"].(string); ok && typ == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
@ -215,6 +215,11 @@ func (w *workspace) env() []string {
|
|||
"HOME=" + w.dir,
|
||||
"GIT_CONFIG_NOSYSTEM=1",
|
||||
"BEADS_TEST_MODE=1",
|
||||
// HOME is the t.TempDir workspace, so the telemetry emitter writes its
|
||||
// lock + event files under it — asynchronously, which races TempDir
|
||||
// cleanup and fails the test with "directory not empty". Tests never
|
||||
// want to emit metrics anyway.
|
||||
"BD_DISABLE_METRICS=1",
|
||||
}
|
||||
if testDoltPort > 0 {
|
||||
env = append(env, "BEADS_DOLT_PORT="+strconv.Itoa(testDoltPort))
|
||||
|
|
|
|||
282
cmd/bd/protocol/ready_front_test.go
Normal file
282
cmd/bd/protocol/ready_front_test.go
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestProtocol_OpenParentDoesNotBlockChildren pins protocol clause G2.2:
|
||||
// parent-child is structure, not blocking. A merely-open, unblocked parent epic
|
||||
// does NOT hold its children out of the ready front — only a *blocked* parent
|
||||
// propagates. This is load-bearing for every epic-driven worker fleet: if open
|
||||
// epics blocked their children, the ready front would be empty for all work
|
||||
// that lives under an epic.
|
||||
func TestProtocol_OpenParentDoesNotBlockChildren(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
|
||||
epic := w.create("--title", "Open epic", "--type", "epic")
|
||||
child := w.create("--title", "Child of open epic", "--type", "task", "--parent", epic)
|
||||
grandchild := w.create("--title", "Grandchild of open epic", "--type", "task", "--parent", child)
|
||||
|
||||
ready := parseReadyIDs(t, w)
|
||||
if !ready[child] {
|
||||
t.Errorf("G2.2: child %s of open epic %s is not in the ready front", child, epic)
|
||||
}
|
||||
if !ready[grandchild] {
|
||||
t.Errorf("G2.2: grandchild %s under open epic %s is not in the ready front", grandchild, epic)
|
||||
}
|
||||
if out := w.run("blocked", "--json"); findByID(parseJSONOutput(t, out), child) != nil {
|
||||
t.Errorf("G2.2: child %s of an open epic is reported blocked", child)
|
||||
}
|
||||
|
||||
// Contrast (G2.1): a parent that is itself BLOCKED does propagate — the
|
||||
// child leaves the ready front until the epic's blocker closes. (The
|
||||
// blocker must itself be an epic: cross-type blocking is rejected, GH#1495.)
|
||||
blocker := w.create("--title", "Blocks the epic", "--type", "epic")
|
||||
w.run("dep", "add", epic, blocker)
|
||||
|
||||
ready = parseReadyIDs(t, w)
|
||||
if ready[child] {
|
||||
t.Errorf("G2.1: child %s stayed ready while its parent epic %s is blocked by %s", child, epic, blocker)
|
||||
}
|
||||
|
||||
// Closing the epic's blocker restores the children to the ready front (R6).
|
||||
w.run("close", blocker, "--reason", "done")
|
||||
ready = parseReadyIDs(t, w)
|
||||
if !ready[child] {
|
||||
t.Errorf("G2.2/R6: child %s did not return to the ready front after the epic's blocker closed", child)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProtocol_ReadyParentFilterIsTransitive pins protocol clause R3:
|
||||
// `bd ready --parent <id>` restricts to TRANSITIVE descendants, not just direct
|
||||
// children (GH#3396 — a one-hop subquery silently dropped grandchildren while
|
||||
// the help text promised recursion). A fleet draining an epic by
|
||||
// `bd ready --parent <epic>` would never see work nested one level deeper.
|
||||
func TestProtocol_ReadyParentFilterIsTransitive(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
|
||||
epic := w.create("--title", "Program epic", "--type", "epic")
|
||||
child := w.create("--title", "Work package", "--type", "task", "--parent", epic)
|
||||
grandchild := w.create("--title", "Unit under the work package", "--type", "task", "--parent", child)
|
||||
greatGrandchild := w.create("--title", "Sub-unit", "--type", "task", "--parent", grandchild)
|
||||
|
||||
// An unrelated tree, plus an explicit (non-dotted) parent-child edge, so the
|
||||
// filter is exercised against both id-derived and edge-derived descent.
|
||||
other := w.create("--title", "Unrelated epic", "--type", "epic")
|
||||
otherChild := w.create("--title", "Unrelated work", "--type", "task", "--parent", other)
|
||||
adopted := w.create("--title", "Adopted child (edge, not dotted id)", "--type", "task")
|
||||
w.run("dep", "add", adopted, epic, "--type", "parent-child")
|
||||
|
||||
out := w.run("ready", "--parent", epic, "--json")
|
||||
var got []string
|
||||
for _, item := range parseJSONOutput(t, out) {
|
||||
if id, ok := item["id"].(string); ok {
|
||||
got = append(got, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Every transitive descendant, by dotted id and by parent-child edge; the
|
||||
// epic itself and the unrelated tree are excluded.
|
||||
requireStringSetEqual(t, got,
|
||||
[]string{child, grandchild, greatGrandchild, adopted},
|
||||
"R3: `bd ready --parent "+epic+"` transitive descendants")
|
||||
|
||||
for _, id := range got {
|
||||
if id == other || id == otherChild {
|
||||
t.Errorf("R3: --parent %s leaked an unrelated issue %s", epic, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestProtocol_ReadyDefaultOrdering pins protocol clause R2: the default
|
||||
// listing order is priority ASC, then created_at DESC, then id ASC. Agents that
|
||||
// take "the top item" of `bd ready` depend on this: a P0 filed today must
|
||||
// outrank a P2 filed a year ago, and the tiebreak must be deterministic so two
|
||||
// workers reading the same front see the same head.
|
||||
func TestProtocol_ReadyDefaultOrdering(t *testing.T) {
|
||||
t.Parallel()
|
||||
w := newWorkspace(t)
|
||||
|
||||
// created_at has second granularity, so space the issues whose creation
|
||||
// order the test asserts. Same-second pairs are handled by the id tiebreak,
|
||||
// which the two explicit-id issues below exercise.
|
||||
oldP2 := w.create("--title", "old p2", "-p", "2")
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
oldP1 := w.create("--title", "old p1", "-p", "1")
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
newP1 := w.create("--title", "new p1", "-p", "1")
|
||||
p0 := w.create("--title", "urgent", "-p", "0")
|
||||
|
||||
// Two same-priority issues that share created_at exactly, so ONLY the id
|
||||
// ASC tiebreak can order them. created_at has second granularity and a bd
|
||||
// invocation takes longer than that, so the pair is imported (J6) rather
|
||||
// than created — a CLI-created pair would straddle a second boundary and
|
||||
// silently stop exercising the tiebreak.
|
||||
prefix, _, _ := strings.Cut(oldP2, "-")
|
||||
tieA, tieB := prefix+"-tieaaaa", prefix+"-tiebbbb"
|
||||
w.importIssues(
|
||||
tiedIssueJSONL(tieB, "tie b", 3, "2020-01-01T00:00:00Z"),
|
||||
tiedIssueJSONL(tieA, "tie a", 3, "2020-01-01T00:00:00Z"),
|
||||
)
|
||||
|
||||
items := readyItems(t, w)
|
||||
if len(items) != 6 {
|
||||
t.Fatalf("R2: expected 6 ready issues, got %d: %v", len(items), items)
|
||||
}
|
||||
|
||||
// The observed sequence MUST equal the sequence the R2 comparator produces
|
||||
// from the issues' own priority/created_at/id values.
|
||||
want := make([]readyItem, len(items))
|
||||
copy(want, items)
|
||||
sort.SliceStable(want, func(i, j int) bool { return r2Less(want[i], want[j]) })
|
||||
if gotIDs, wantIDs := ids(items), ids(want); !equalStrings(gotIDs, wantIDs) {
|
||||
t.Errorf("R2: ready order = %v, want %v (priority ASC, created_at DESC, id ASC)\n issues: %v", gotIDs, wantIDs, items)
|
||||
}
|
||||
|
||||
// Spelled-out expectations, so the comparator check above cannot pass
|
||||
// vacuously on a wrong-but-self-consistent order.
|
||||
order := ids(items)
|
||||
if order[0] != p0 {
|
||||
t.Errorf("R2: head of the ready front = %s, want the P0 issue %s (priority ASC dominates)", order[0], p0)
|
||||
}
|
||||
if indexOf(order, newP1) > indexOf(order, oldP1) {
|
||||
t.Errorf("R2: within priority 1, older %s outranked newer %s (created_at must be DESC)", oldP1, newP1)
|
||||
}
|
||||
if indexOf(order, oldP1) > indexOf(order, oldP2) {
|
||||
t.Errorf("R2: P2 %s outranked P1 %s (priority ASC dominates created_at)", oldP2, oldP1)
|
||||
}
|
||||
a, b := itemByID(items, tieA), itemByID(items, tieB)
|
||||
if !a.createdAt.Equal(b.createdAt) {
|
||||
t.Fatalf("R2 setup: imported tie pair should share created_at, got %s vs %s", a.createdAt, b.createdAt)
|
||||
}
|
||||
if indexOf(order, tieA) > indexOf(order, tieB) {
|
||||
t.Errorf("R2: %s and %s share priority and created_at but were ordered %v — the tiebreak must be id ASC",
|
||||
tieA, tieB, order)
|
||||
}
|
||||
|
||||
// Determinism: the same front read twice yields the same order.
|
||||
if second := ids(readyItems(t, w)); !equalStrings(order, second) {
|
||||
t.Errorf("R2: ready order is not stable across reads: %v then %v", order, second)
|
||||
}
|
||||
}
|
||||
|
||||
// tiedIssueJSONL renders one JSONL issue line with an explicit created_at, so a
|
||||
// test can construct issues whose timestamps tie exactly.
|
||||
func tiedIssueJSONL(id, title string, priority int, createdAt string) string {
|
||||
line, err := json.Marshal(map[string]any{
|
||||
"id": id,
|
||||
"title": title,
|
||||
"status": "open",
|
||||
"priority": priority,
|
||||
"issue_type": "task",
|
||||
"created_at": createdAt,
|
||||
"updated_at": createdAt,
|
||||
"created_by": "protocol-test",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(line)
|
||||
}
|
||||
|
||||
// importIssues writes the given JSONL lines to a file in the workspace and
|
||||
// imports them.
|
||||
func (w *workspace) importIssues(lines ...string) {
|
||||
w.t.Helper()
|
||||
path := filepath.Join(w.dir, "import.jsonl")
|
||||
if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0o644); err != nil {
|
||||
w.t.Fatal(err)
|
||||
}
|
||||
w.run("import", path)
|
||||
}
|
||||
|
||||
// readyItem is the (priority, created_at, id) triple the R2 order is defined on.
|
||||
type readyItem struct {
|
||||
id string
|
||||
priority int
|
||||
createdAt time.Time
|
||||
}
|
||||
|
||||
func readyItems(t *testing.T, w *workspace) []readyItem {
|
||||
t.Helper()
|
||||
out := w.run("ready", "--json")
|
||||
var items []readyItem
|
||||
for _, obj := range parseJSONOutput(t, out) {
|
||||
id, _ := obj["id"].(string)
|
||||
p, ok := obj["priority"].(float64)
|
||||
if !ok {
|
||||
t.Fatalf("R2: ready item %s has no numeric priority: %v", id, obj)
|
||||
}
|
||||
raw, _ := obj["created_at"].(string)
|
||||
items = append(items, readyItem{id: id, priority: int(p), createdAt: parseTimestamp(t, raw)})
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func parseTimestamp(t *testing.T, raw string) time.Time {
|
||||
t.Helper()
|
||||
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, "2006-01-02 15:04:05"} {
|
||||
if ts, err := time.Parse(layout, raw); err == nil {
|
||||
return ts.UTC()
|
||||
}
|
||||
}
|
||||
t.Fatalf("R2: cannot parse created_at %q", raw)
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// r2Less is the normative R2 comparator: priority ASC, created_at DESC, id ASC.
|
||||
func r2Less(a, b readyItem) bool {
|
||||
if a.priority != b.priority {
|
||||
return a.priority < b.priority
|
||||
}
|
||||
if !a.createdAt.Equal(b.createdAt) {
|
||||
return a.createdAt.After(b.createdAt)
|
||||
}
|
||||
return a.id < b.id
|
||||
}
|
||||
|
||||
func ids(items []readyItem) []string {
|
||||
out := make([]string, len(items))
|
||||
for i, it := range items {
|
||||
out[i] = it.id
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func itemByID(items []readyItem, id string) readyItem {
|
||||
for _, it := range items {
|
||||
if it.id == id {
|
||||
return it
|
||||
}
|
||||
}
|
||||
return readyItem{}
|
||||
}
|
||||
|
||||
func indexOf(list []string, want string) int {
|
||||
for i, s := range list {
|
||||
if s == want {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func equalStrings(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue