beads/internal/workapi/delete_test.go
CI Bot 67d38d2620 Trim this series' comments toward the repo's own density
Ten agents over 216 files, one directory-balanced group each, working to a
rubric that named what to cut (duplication narration, restatement of the next
line, the governing rule echoed in every command file, throat-clearing) and
what to keep (traps, invariants, security, tracker ids, behaviour changes, and
the "why a naive simplification destroys this assertion" rationale).

Net 1,523 lines. Density against this repo's own per-directory norm on main:

  cmd/bd              33.6% -> 27.3%   (main 10.7%)
  internal/storage    26.1% -> 23.1%   (main 14.1%)
  backend/conformance 34.0% -> 29.6%   (main 22.8%)
  internal/httpapi    30.9% -> 27.8%   (main 30.6%, now below)
  overall             34.7% -> 31.6%

Every agent landed at 18-25% rather than the 30% asked for, and every one gave
the same reason independently: what remained was on the keep list. Ten separate
judgements agreeing is the most useful result here — the prose is dense, but it
is mostly load-bearing, and the shortfall is the honest answer rather than a
failure to try.

TWO REVERTS, both to protect something worth more than the lines.

issueops/ is back to its pre-trim state, giving up 610 lines. The conformance
contracts cite the leaf docs BY LINE — "pins treewalker.go:52-66" — 155 times.
Trimming the cited files silently repointed those at whatever text moved into
those numbers, across agent boundaries so no agent could see it. A semantic
remapper recovered 141 of them, but 23 needed judgement and several landed on
struct fields. The leaf docs are the spec the contracts are written against;
they are the one place in this change where dense prose IS the deliverable, and
keeping them stable is worth more than the 610 lines. tree_walker_contract.go
and version_reconciler_contract.go went back for the same reason: an agent had
deleted a paragraph containing a citation, so positional restoration could not
align them.

After the reverts, exactly 5 citations point into a still-trimmed file and 2 of
those are weak — both in cmd/bd/config_guard_test.go, which this series never
touched and whose citations were already wrong on main (init.go:361 pointed at
a closing brace; the guard it names is at init.go:1499).

VERIFICATION. A guard strips every comment and all whitespace from each of the
193 modified files at HEAD and in the worktree and requires the two to be
byte-identical, so a code change cannot hide in a comment diff. It handles `//`
inside string and raw-string literals, and was tested against a deliberate code
change before being trusted. gofmt clean, go build, go vet, golangci-lint 0
issues, and the two AST-based tests that PARSE trimmed files — the timed
provider binding pin over claim.go and the serve database-source gate over
serve.go — both pass.

One real defect found while trimming: checkDatabaseSource's doc block ran into
`// sourceRoles is ...` with no blank line, so godoc attached thirty-one lines
of the wrong prose to func sourceRoles and left checkDatabaseSource
undocumented. Fixed by separating them.
2026-08-06 08:06:59 +00:00

98 lines
3.6 KiB
Go

package workapi
import (
"errors"
"reflect"
"testing"
"github.com/steveyegge/beads/issueops"
)
// The database-free half of issueops.Deleter, pinned in milliseconds. Every
// backend runs these functions, so a disagreement about what a malformed
// request means is caught here rather than three times over in conformance.
func TestValidateDeleteRequest(t *testing.T) {
for _, test := range []struct {
name string
request issueops.DeleteRequest
wantErr bool
}{
{"one id", issueops.DeleteRequest{IDs: []string{"bd-1"}}, false},
{"several ids", issueops.DeleteRequest{IDs: []string{"bd-1", "bd-2"}}, false},
{"untrimmed id is accepted and normalized later", issueops.DeleteRequest{IDs: []string{" bd-1 "}}, false},
{"nil ids", issueops.DeleteRequest{}, true},
{"empty ids", issueops.DeleteRequest{IDs: []string{}}, true},
{"blank id", issueops.DeleteRequest{IDs: []string{""}}, true},
{"whitespace id", issueops.DeleteRequest{IDs: []string{"\t "}}, true},
{"blank beside a real one", issueops.DeleteRequest{IDs: []string{"bd-1", " "}}, true},
} {
t.Run(test.name, func(t *testing.T) {
err := ValidateDeleteRequest(test.request)
if test.wantErr {
if !errors.Is(err, issueops.ErrValidation) {
t.Fatalf("error = %v, want ErrValidation", err)
}
return
}
if err != nil {
t.Fatalf("error = %v, want nil", err)
}
})
}
}
// TestNormalizeDeleteIDs pins FIRST-MENTION order rather than sorted order. It
// is the order both front doors echo back — in "issues not found" and in the
// `--force` hint they print — so a caller re-reading its own `--from-file` list
// against that output should not have to re-sort it.
func TestNormalizeDeleteIDs(t *testing.T) {
for _, test := range []struct {
name string
in []string
want []string
}{
{"unchanged", []string{"bd-1", "bd-2"}, []string{"bd-1", "bd-2"}},
{"duplicates collapse to the first mention", []string{"bd-2", "bd-1", "bd-2"}, []string{"bd-2", "bd-1"}},
{"whitespace is trimmed before comparing", []string{" bd-1", "bd-1 "}, []string{"bd-1"}},
{"order is not sorted", []string{"bd-z", "bd-a"}, []string{"bd-z", "bd-a"}},
} {
t.Run(test.name, func(t *testing.T) {
if got := NormalizeDeleteIDs(test.in); !reflect.DeepEqual(got, test.want) {
t.Errorf("NormalizeDeleteIDs(%v) = %v, want %v", test.in, got, test.want)
}
})
}
}
// TestNormalizeDeleteIDsDoesNotMutateTheCaller is the pin behind
// issueops.DeleteRequest's "IDs is read, never written through": normalizing in
// place is the most natural way to write this function and would hand the
// caller back a shorter, reordered version of its own slice.
func TestNormalizeDeleteIDsDoesNotMutateTheCaller(t *testing.T) {
in := []string{" bd-2 ", "bd-1", "bd-2"}
snapshot := append([]string(nil), in...)
NormalizeDeleteIDs(in)
if !reflect.DeepEqual(in, snapshot) {
t.Errorf("caller's slice changed: got %v, want %v", in, snapshot)
}
}
// TestSortedDeleteIDs pins the ascending order DeleteResult.Orphaned and
// DependentsOutsideRequestError.Dependents promise. Both are collected out of
// maps, so without this they would publish Go's map order and a caller
// diffing two runs would see spurious changes.
func TestSortedDeleteIDs(t *testing.T) {
if got := SortedDeleteIDs(nil); got != nil {
t.Errorf("SortedDeleteIDs(nil) = %v, want nil", got)
}
if got := SortedDeleteIDs(map[string]bool{}); got != nil {
t.Errorf("SortedDeleteIDs(empty) = %v, want nil", got)
}
got := SortedDeleteIDs(map[string]bool{"bd-c": true, "bd-a": true, "bd-b": true})
if want := []string{"bd-a", "bd-b", "bd-c"}; !reflect.DeepEqual(got, want) {
t.Errorf("SortedDeleteIDs() = %v, want %v", got, want)
}
}