beads/cmd/bd/comment_test.go
Athos Bernardes 6ec150f8b9
Reject "list"/"add" as the id in the singular "comment" shorthand (#5369)
"bd comment <id> [text...]" had no validation on the id positional
argument. Typing "bd comment list <text>" (confusing the singular
shorthand with the plural "bd comments list" guard) parsed as
id="list", text=[<text>...], and since no issue is literally named
"list", ResolvePartialID's substring/prefix fallback would silently
match "list" against whatever existing bead or wisp id happened to
contain it as a prefix (after wisp- stripping) and post the comment
there instead of erroring — no warning, no error, wrong issue mutated.

- Add validateCommentArgs, wired as commentCmd's Args validator
  (mirrors comments.go's validateCommentsArgs), rejecting "list" and
  "add" as the id with a message pointing at the correct invocation.
  Runs before RunE's usesProxiedServer() dispatch, so both backends
  reject identically.
- Note the restriction in commentCmd's --help text.
- Add TestValidateCommentArgs (pure unit test, no store needed) plus
  CLI-level regression tests mirroring the existing
  TestCLI_CommentsListMisplacedSyntax /
  TestCLI_CommentsSwappedAddRejectedBeforeStoreOpen /
  ...RejectedInProxiedServerMode trio for the plural sibling, plus one
  confirming text that merely starts with "list"/"add" still works
  (only the id slot is checked, never the text).

Manually verified end-to-end against both a pre-fix and post-fix
build: with a bead whose id contains "list" (mirroring a real
production incident), the unpatched binary silently posts the stray
comment there (exit 0); the fixed binary rejects the command (exit 1)
and the bead's comment count stays at zero.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-07 05:49:46 -07:00

42 lines
1.9 KiB
Go

package main
import "testing"
// TestValidateCommentArgs is a pure unit test for the singular "comment"
// shorthand's Args guard — it calls the validator directly with no store, no
// Dolt, and no cobra dispatch, so it runs regardless of cgo/Docker
// availability. Message-content assertions for the two rejected cases live in
// the CLI-level TestCLI_Comment{List,Add}MisplacedSyntax tests alongside the
// plural sibling's equivalents; this test only pins down which shapes are
// accepted vs rejected.
func TestValidateCommentArgs(t *testing.T) {
origJSONOutput := jsonOutput
jsonOutput = false
t.Cleanup(func() { jsonOutput = origJSONOutput })
cases := []struct {
name string
args []string
wantErr bool
}{
{name: "bare list is rejected (the reported typo)", args: []string{"list", "some text"}, wantErr: true},
{name: "bare list with no text is still rejected", args: []string{"list"}, wantErr: true},
{name: "bare add is rejected (mirrors comments swapped-add)", args: []string{"add", "some text"}, wantErr: true},
{name: "real id with text starting with the word list is fine", args: []string{"test-abc123", "list", "of", "things", "to", "do"}, wantErr: false},
{name: "real id with text starting with the word add is fine", args: []string{"test-abc123", "add", "one", "more", "item"}, wantErr: false},
{name: "real id alone (text comes from --stdin/--file) is fine", args: []string{"test-abc123"}, wantErr: false},
{name: "no args at all is rejected by the base MinimumNArgs check", args: []string{}, wantErr: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := validateCommentArgs(commentCmd, tc.args)
if tc.wantErr && err == nil {
t.Fatalf("validateCommentArgs(%q): expected an error, got nil", tc.args)
}
if !tc.wantErr && err != nil {
t.Fatalf("validateCommentArgs(%q): expected no error, got %v", tc.args, err)
}
})
}
}