* fix(issueops): enforce the dotted-child dependency rule on every leg The refusal of an explicit edge from a dotted-id child to its own ancestor (bd-abc.1 -> bd-abc; only the parent-child edge to the immediate dotted parent is allowed) lived only in cmd/bd (isDisallowedHierarchicalDependency). HTTP dependencies:add, batch:apply dep_add items and `bd create --graph`'s own edges skipped it. It is now one library rule, issueops.CheckDottedChildDependency, returning *issueops.DottedChildDependencyError (unwraps to ErrValidation, same message the CLI printed): - ValidateAddDependenciesRequest applies it, so DependencyEditor refuses on dolt, embedded, uow and the server behind httpapi (400 invalid_argument); - both BatchApplier bodies (issueops and uow) apply it to each dep_add item on the resolved ids, so a key bound to a minted dotted child is refused too; - the HTTP client checks it before the dial, as it does self-dependency, so the typed error survives the wire. cmd/bd keeps no copy: single-edge dep add / dep --blocks rely on the role; bd link (still on the store verb) and the --file bulk reporter call the library rule. CLI output is byte-identical (checked against origin/main). Conformance: RunDependencyEditorRefusesADottedChildGatedOnItsOwnParent and RunBatchApplyRefusesADottedChildGatedOnItsOwnParent wired on dolt, embedded, uow and http, plus a raw-wire served test past the client check and handler mapping cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(batch): refuse a dotted child gated on its own ancestor in bd batch Review fixup for PR #7424 (iterations 1 and 2). Major: a bd batch script line `dep add bd-abc.1 bd-abc` still wrote the edge this PR refuses everywhere else, on both backends. Neither batch dispatch writes through the DependencyEditor role that enforces the dotted-child rule: the classic arm calls storage.Transaction.AddDependency and the proxied arm the unit of work's AddDependencies verb. Both arms now build the edge with one helper, batchDepAddEdge, which keeps the default type and the arity and type errors, and asks publicops.CheckDottedChildDependency the way bd link does. A refused line rolls the whole batch back. Tests: TestBatchDepAddEdge (default and explicit types, arity and type errors, three refusals typed *DottedChildDependencyError matching ErrValidation, and the immediate parent-child edge allowed), and TestProxiedServerBatchDepAddRefusesADottedChildGatedOnItsParent (classic and proxied: the refusal names line 2, rolls back an earlier update line and writes no edge, and `dep add C P parent-child` still lands exactly one), registered in the proxied shard manifest. A mutant that drops the rule from the shared helper fails both; one that bypasses the helper in the proxied arm alone fails the proxied leg. Minors: - For a child whose parent-child edge is already stored (a `bd create --parent` child) the rule answers ahead of the hierarchy and type conflicts, so an HTTP caller now gets 400 invalid_argument where it got 409 dependency_cycle (blocking types) or dependency_exists (any other type). Documented in the DependencyEditor role doc, the CHANGELOG entry and the httpapi batch-apply test comment, and pinned by a stored-hierarchy phase in the DependencyEditor and BatchApplier dotted-child conformance contracts on every leg; a mutant that drops the rule gets the hierarchy conflict instead (409 dependency_cycle on the wire). - The bd create --graph dry-run note now says the live create may also refuse an edge that gates a dotted-id child on its own ancestor. - The DottedChildDependencyError comment says the message is kept verbatim and says "deadlock" whatever the type. Not changed: bd relate (relates-to does not schedule) and the edges a create writes with its issue (bd create --deps/--waits-for, and over HTTP an explicit dotted id with a non-immediate ParentID) still write through paths that skip the rule. The scheduling shapes need an explicit dotted id or a grandparent target. Follow-up bd-25s1wh tracks moving the rule down to the domain/store add verbs, with a structural bypass for reparent and template edges. Iteration 2 (minor): the CheckDottedChildDependency doc no longer implies bd link and bd batch are the only writers outside the roles. It names bd relate and the create edges as not applying the rule yet and cites bd-25s1wh. Complexity (cyclomatic/cognitive): runBatchOp 23/35 -> 21/31, runBatchOpUOW 28/48 -> 26/44, batchDepAddEdge 5/4. Touching them brings both pre-existing dispatch switches into the measured set; splitting them is out of scope here. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(issueops,httpapi): document the dotted-child 400 and widen its gap list Review fixup for PR #7424 (relaunch, iteration 1, minors). - The OpenAPI spec now describes the wire change this PR makes. The dependencies:add and issues:batchApply 400 vocabularies name an edge from a dotted-id child to its own ancestor (anything but the parent-child edge to its immediate parent; param `edges` and `items`). dependencies:add says why that is request-intrinsic, and both 409s say it is never dependency_cycle or dependency_exists, even for a child whose parent-child edge is stored. Response descriptions only: the generated types and the wireshape digest are unchanged (make api-check). - The CheckDottedChildDependency doc no longer reads as a complete list of the writers that skip the rule. Besides bd relate and the create edges it names bd duplicate, bd supersede, bd duplicates --auto-merge (its related link and its reparenting) and the tracker engine's createDependencies, and says the molecule, template and swarm writers are unaudited. bd-25s1wh's notes carry the widened scope, including `bd duplicate bd-a.1 --of bd-a`. Declined (nit): unifying the root issueops import alias across the PR's cmd/bd files. batch.go is the one file that imports both issueops packages, so one of them needs an alias there; it uses publicops, the repo's usual alias for the root package, and aliasing the other side instead would rewrite base-owned lines. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
374 lines
9.8 KiB
Go
374 lines
9.8 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
publicops "github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
func TestTokenizeBatchLine(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want []string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "simple",
|
|
in: "close bd-1 done",
|
|
want: []string{"close", "bd-1", "done"},
|
|
},
|
|
{
|
|
name: "tabs and spaces",
|
|
in: "close\tbd-1 done",
|
|
want: []string{"close", "bd-1", "done"},
|
|
},
|
|
{
|
|
name: "quoted with spaces",
|
|
in: `update bd-2 title="hello world"`,
|
|
want: []string{"update", "bd-2", "title=hello world"},
|
|
},
|
|
{
|
|
name: "escaped quote",
|
|
in: `create bug 1 "say \"hi\""`,
|
|
want: []string{"create", "bug", "1", `say "hi"`},
|
|
},
|
|
{
|
|
name: "escaped backslash",
|
|
in: `create bug 1 "back\\slash"`,
|
|
want: []string{"create", "bug", "1", `back\slash`},
|
|
},
|
|
{
|
|
name: "unterminated quote",
|
|
in: `close bd-1 "oops`,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty string",
|
|
in: "",
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "empty quoted",
|
|
in: `update bd-1 title=""`,
|
|
want: []string{"update", "bd-1", "title="},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tokenizeBatchLine(tt.in)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("tokenizeBatchLine(%q) err = %v, wantErr %v", tt.in, err, tt.wantErr)
|
|
}
|
|
if tt.wantErr {
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("tokenizeBatchLine(%q) = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript(t *testing.T) {
|
|
script := `# leading comment
|
|
# another
|
|
close bd-1 stale
|
|
|
|
update bd-2 status=in_progress priority=1
|
|
create task 2 "triage the backlog"
|
|
dep add bd-3 bd-4
|
|
dep add bd-3 bd-5 related
|
|
dep remove bd-6 bd-7
|
|
dep rm bd-8 bd-9
|
|
`
|
|
ops, err := parseBatchScript(strings.NewReader(script))
|
|
if err != nil {
|
|
t.Fatalf("parseBatchScript: %v", err)
|
|
}
|
|
wantCmds := []string{
|
|
"close",
|
|
"update",
|
|
"create",
|
|
"dep.add",
|
|
"dep.add",
|
|
"dep.remove",
|
|
"dep.remove",
|
|
}
|
|
if len(ops) != len(wantCmds) {
|
|
t.Fatalf("got %d ops, want %d: %+v", len(ops), len(wantCmds), ops)
|
|
}
|
|
for i, op := range ops {
|
|
if op.cmd != wantCmds[i] {
|
|
t.Errorf("op %d: cmd = %q, want %q", i, op.cmd, wantCmds[i])
|
|
}
|
|
if op.line == 0 {
|
|
t.Errorf("op %d: line number not set", i)
|
|
}
|
|
}
|
|
|
|
// Spot check: create title is one token via quoting
|
|
create := ops[2]
|
|
if create.cmd != "create" {
|
|
t.Fatalf("expected create, got %q", create.cmd)
|
|
}
|
|
if len(create.args) != 3 {
|
|
t.Fatalf("create args = %v, want 3 tokens", create.args)
|
|
}
|
|
if create.args[2] != "triage the backlog" {
|
|
t.Errorf("create title = %q, want %q", create.args[2], "triage the backlog")
|
|
}
|
|
|
|
// dep.add with type
|
|
depWithType := ops[4]
|
|
if depWithType.cmd != "dep.add" || len(depWithType.args) != 3 || depWithType.args[2] != "related" {
|
|
t.Errorf("dep.add with type mismatch: %+v", depWithType)
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_Empty(t *testing.T) {
|
|
ops, err := parseBatchScript(strings.NewReader(""))
|
|
if err != nil {
|
|
t.Fatalf("parseBatchScript empty: %v", err)
|
|
}
|
|
if len(ops) != 0 {
|
|
t.Errorf("expected 0 ops, got %d", len(ops))
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_OnlyCommentsAndBlank(t *testing.T) {
|
|
ops, err := parseBatchScript(strings.NewReader("# comment\n\n \n# another\n"))
|
|
if err != nil {
|
|
t.Fatalf("parseBatchScript: %v", err)
|
|
}
|
|
if len(ops) != 0 {
|
|
t.Errorf("expected 0 ops, got %d", len(ops))
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_UnsupportedCommand(t *testing.T) {
|
|
_, err := parseBatchScript(strings.NewReader("show bd-1\n"))
|
|
if err == nil {
|
|
t.Fatal("expected error for unsupported command")
|
|
}
|
|
if !strings.Contains(err.Error(), "unsupported batch command") {
|
|
t.Errorf("error should mention unsupported command, got: %v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "line 1") {
|
|
t.Errorf("error should include line number, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_UnsupportedCommandOnLaterLine(t *testing.T) {
|
|
script := "close bd-1\nshow bd-2\n"
|
|
_, err := parseBatchScript(strings.NewReader(script))
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if !strings.Contains(err.Error(), "line 2") {
|
|
t.Errorf("error should include line 2, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_DepRequiresSubcommand(t *testing.T) {
|
|
_, err := parseBatchScript(strings.NewReader("dep\n"))
|
|
if err == nil {
|
|
t.Fatal("expected error for bare 'dep'")
|
|
}
|
|
if !strings.Contains(err.Error(), "subcommand") {
|
|
t.Errorf("error should mention subcommand, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_DepUnknownSubcommand(t *testing.T) {
|
|
_, err := parseBatchScript(strings.NewReader("dep list bd-1\n"))
|
|
if err == nil {
|
|
t.Fatal("expected error for unknown dep subcommand")
|
|
}
|
|
if !strings.Contains(err.Error(), "dep subcommand") {
|
|
t.Errorf("error should mention dep subcommand, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseBatchScript_UnterminatedQuote(t *testing.T) {
|
|
_, err := parseBatchScript(strings.NewReader(`create task 1 "oops`))
|
|
if err == nil {
|
|
t.Fatal("expected error for unterminated quote")
|
|
}
|
|
}
|
|
|
|
func TestParseUpdateKVs(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in []string
|
|
want map[string]interface{}
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "status and priority",
|
|
in: []string{"status=in_progress", "priority=1"},
|
|
want: map[string]interface{}{"status": "in_progress", "priority": 1},
|
|
},
|
|
{
|
|
name: "title",
|
|
in: []string{"title=new title"},
|
|
want: map[string]interface{}{"title": "new title"},
|
|
},
|
|
{
|
|
name: "assignee blank allowed (unassign)",
|
|
in: []string{"assignee="},
|
|
want: map[string]interface{}{"assignee": ""},
|
|
},
|
|
{
|
|
name: "unsupported key",
|
|
in: []string{"description=foo"},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "missing equals",
|
|
in: []string{"status"},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid priority",
|
|
in: []string{"priority=high"},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty status",
|
|
in: []string{"status="},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty title",
|
|
in: []string{"title="},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := parseUpdateKVs(tt.in)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("parseUpdateKVs err = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
if tt.wantErr {
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("parseUpdateKVs = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBatchDepAddEdge pins the edge a `dep add` line builds on both backends,
|
|
// and that the line asks the dotted-id hierarchy rule before either backend
|
|
// writes: the refusal is the role's own typed error, so a batch prints the
|
|
// sentence `bd dep add` prints and rolls back on it.
|
|
func TestBatchDepAddEdge(t *testing.T) {
|
|
tooLong := strings.Repeat("x", types.MaxDependencyTypeLen+1)
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
want *types.Dependency
|
|
wantErr string
|
|
wantDotted bool
|
|
}{
|
|
{
|
|
name: "default type is blocks",
|
|
args: []string{"bd-1", "bd-2"},
|
|
want: &types.Dependency{IssueID: "bd-1", DependsOnID: "bd-2", Type: types.DepBlocks},
|
|
},
|
|
{
|
|
name: "explicit type",
|
|
args: []string{"bd-1", "bd-2", "related"},
|
|
want: &types.Dependency{IssueID: "bd-1", DependsOnID: "bd-2", Type: types.DepRelated},
|
|
},
|
|
{
|
|
name: "missing to-id",
|
|
args: []string{"bd-1"},
|
|
wantErr: "dep add requires <from-id> <to-id>",
|
|
},
|
|
{
|
|
name: "invalid type",
|
|
args: []string{"bd-1", "bd-2", tooLong},
|
|
wantErr: `dep add: invalid dependency type "` + tooLong + `"`,
|
|
},
|
|
{
|
|
name: "dotted child gated on its own parent",
|
|
args: []string{"bd-abc.1", "bd-abc"},
|
|
wantDotted: true,
|
|
},
|
|
{
|
|
name: "any explicit type up the tree, not only blocking ones",
|
|
args: []string{"bd-abc.1", "bd-abc", "related"},
|
|
wantDotted: true,
|
|
},
|
|
{
|
|
name: "parent-child past the immediate parent",
|
|
args: []string{"bd-abc.1.2", "bd-abc", "parent-child"},
|
|
wantDotted: true,
|
|
},
|
|
{
|
|
name: "parent-child to the immediate parent is the hierarchy itself",
|
|
args: []string{"bd-abc.1", "bd-abc", "parent-child"},
|
|
want: &types.Dependency{IssueID: "bd-abc.1", DependsOnID: "bd-abc", Type: types.DepParentChild},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := batchDepAddEdge(tt.args)
|
|
switch {
|
|
case tt.wantDotted:
|
|
var dotted *publicops.DottedChildDependencyError
|
|
if !errors.As(err, &dotted) {
|
|
t.Fatalf("batchDepAddEdge(%q) = %+v, %v; want *DottedChildDependencyError", tt.args, got, err)
|
|
}
|
|
if !errors.Is(err, publicops.ErrValidation) {
|
|
t.Errorf("refusal %v does not match ErrValidation", err)
|
|
}
|
|
if dotted.IssueID != tt.args[0] || dotted.DependsOnID != tt.args[1] {
|
|
t.Errorf("refusal names %s -> %s, want %s -> %s", dotted.IssueID, dotted.DependsOnID, tt.args[0], tt.args[1])
|
|
}
|
|
case tt.wantErr != "":
|
|
if err == nil || err.Error() != tt.wantErr {
|
|
t.Fatalf("batchDepAddEdge(%q) err = %v, want %q", tt.args, err, tt.wantErr)
|
|
}
|
|
default:
|
|
if err != nil {
|
|
t.Fatalf("batchDepAddEdge(%q): %v", tt.args, err)
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("batchDepAddEdge(%q) = %+v, want %+v", tt.args, got, tt.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBatchCmd_Registered(t *testing.T) {
|
|
// Ensure 'bd batch' is wired into rootCmd with the correct group.
|
|
cmd, _, err := rootCmd.Find([]string{"batch"})
|
|
if err != nil {
|
|
t.Fatalf("rootCmd.Find batch: %v", err)
|
|
}
|
|
if cmd == nil || cmd.Name() != "batch" {
|
|
t.Fatalf("expected batch command, got %+v", cmd)
|
|
}
|
|
if cmd.GroupID != "maint" {
|
|
t.Errorf("batch GroupID = %q, want %q", cmd.GroupID, "maint")
|
|
}
|
|
if cmd.Flags().Lookup("file") == nil {
|
|
t.Error("batch missing --file flag")
|
|
}
|
|
if cmd.Flags().Lookup("dry-run") == nil {
|
|
t.Error("batch missing --dry-run flag")
|
|
}
|
|
if cmd.Flags().Lookup("message") == nil {
|
|
t.Error("batch missing --message flag")
|
|
}
|
|
}
|