Rewrites ValidateAgentID to scan right-to-left for known role tokens instead of relying on fixed position parsing. This allows rig names containing hyphens (e.g., ob-my-project-witness). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
275 lines
8.0 KiB
Go
275 lines
8.0 KiB
Go
package validation
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func TestParsePriority(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
expected int
|
|
}{
|
|
// Numeric format
|
|
{"0", 0},
|
|
{"1", 1},
|
|
{"2", 2},
|
|
{"3", 3},
|
|
{"4", 4},
|
|
|
|
// P-prefix format (uppercase)
|
|
{"P0", 0},
|
|
{"P1", 1},
|
|
{"P2", 2},
|
|
{"P3", 3},
|
|
{"P4", 4},
|
|
|
|
// P-prefix format (lowercase)
|
|
{"p0", 0},
|
|
{"p1", 1},
|
|
{"p2", 2},
|
|
|
|
// With whitespace
|
|
{" 1 ", 1},
|
|
{" P1 ", 1},
|
|
|
|
// Invalid cases (returns -1)
|
|
{"5", -1}, // Out of range
|
|
{"-1", -1}, // Negative
|
|
{"P5", -1}, // Out of range with prefix
|
|
{"abc", -1}, // Not a number
|
|
{"P", -1}, // Just the prefix
|
|
{"PP1", -1}, // Double prefix
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
got := ParsePriority(tt.input)
|
|
if got != tt.expected {
|
|
t.Errorf("ParsePriority(%q) = %d, want %d", tt.input, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidatePriority(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
wantValue int
|
|
wantError bool
|
|
}{
|
|
{"0", 0, false},
|
|
{"2", 2, false},
|
|
{"P1", 1, false},
|
|
{"5", -1, true},
|
|
{"abc", -1, true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
got, err := ValidatePriority(tt.input)
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ValidatePriority(%q) error = %v, wantError %v", tt.input, err, tt.wantError)
|
|
return
|
|
}
|
|
if got != tt.wantValue {
|
|
t.Errorf("ValidatePriority(%q) = %d, want %d", tt.input, got, tt.wantValue)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateIDFormat(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
wantPrefix string
|
|
wantError bool
|
|
}{
|
|
{"", "", false},
|
|
{"bd-a3f8e9", "bd", false},
|
|
{"bd-42", "bd", false},
|
|
{"bd-a3f8e9.1", "bd", false},
|
|
{"foo-bar", "foo", false},
|
|
{"nohyphen", "", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
got, err := ValidateIDFormat(tt.input)
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ValidateIDFormat(%q) error = %v, wantError %v", tt.input, err, tt.wantError)
|
|
return
|
|
}
|
|
if got != tt.wantPrefix {
|
|
t.Errorf("ValidateIDFormat(%q) = %q, want %q", tt.input, got, tt.wantPrefix)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseIssueType(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantType types.IssueType
|
|
wantError bool
|
|
errorContains string
|
|
}{
|
|
// Valid issue types
|
|
{"bug type", "bug", types.TypeBug, false, ""},
|
|
{"feature type", "feature", types.TypeFeature, false, ""},
|
|
{"task type", "task", types.TypeTask, false, ""},
|
|
{"epic type", "epic", types.TypeEpic, false, ""},
|
|
{"chore type", "chore", types.TypeChore, false, ""},
|
|
{"merge-request type", "merge-request", types.TypeMergeRequest, false, ""},
|
|
{"molecule type", "molecule", types.TypeMolecule, false, ""},
|
|
{"gate type", "gate", types.TypeGate, false, ""},
|
|
{"agent type", "agent", types.TypeAgent, false, ""},
|
|
{"role type", "role", types.TypeRole, false, ""},
|
|
{"message type", "message", types.TypeMessage, false, ""},
|
|
|
|
// Case sensitivity (function is case-sensitive)
|
|
{"uppercase bug", "BUG", types.TypeTask, true, "invalid issue type"},
|
|
{"mixed case feature", "FeAtUrE", types.TypeTask, true, "invalid issue type"},
|
|
|
|
// With whitespace
|
|
{"bug with spaces", " bug ", types.TypeBug, false, ""},
|
|
{"feature with tabs", "\tfeature\t", types.TypeFeature, false, ""},
|
|
|
|
// Invalid issue types
|
|
{"invalid type", "invalid", types.TypeTask, true, "invalid issue type"},
|
|
{"empty string", "", types.TypeTask, true, "invalid issue type"},
|
|
{"whitespace only", " ", types.TypeTask, true, "invalid issue type"},
|
|
{"numeric type", "123", types.TypeTask, true, "invalid issue type"},
|
|
{"special chars", "bug!", types.TypeTask, true, "invalid issue type"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseIssueType(tt.input)
|
|
|
|
// Check error conditions
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ParseIssueType(%q) error = %v, wantError %v", tt.input, err, tt.wantError)
|
|
return
|
|
}
|
|
|
|
if err != nil && tt.errorContains != "" {
|
|
if !strings.Contains(err.Error(), tt.errorContains) {
|
|
t.Errorf("ParseIssueType(%q) error message = %q, should contain %q", tt.input, err.Error(), tt.errorContains)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Check return value
|
|
if got != tt.wantType {
|
|
t.Errorf("ParseIssueType(%q) = %v, want %v", tt.input, got, tt.wantType)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidatePrefix(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
requestedPrefix string
|
|
dbPrefix string
|
|
force bool
|
|
wantError bool
|
|
}{
|
|
{"matching prefixes", "bd", "bd", false, false},
|
|
{"empty db prefix", "bd", "", false, false},
|
|
{"mismatched with force", "foo", "bd", true, false},
|
|
{"mismatched without force", "foo", "bd", false, true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := ValidatePrefix(tt.requestedPrefix, tt.dbPrefix, tt.force)
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ValidatePrefix() error = %v, wantError %v", err, tt.wantError)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateAgentID(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
id string
|
|
wantError bool
|
|
errorContains string
|
|
}{
|
|
// Town-level agents (no rig)
|
|
{"valid mayor", "gt-mayor", false, ""},
|
|
{"valid deacon", "gt-deacon", false, ""},
|
|
|
|
// Per-rig agents (canonical format: gt-<rig>-<role>)
|
|
{"valid witness gastown", "gt-gastown-witness", false, ""},
|
|
{"valid refinery beads", "gt-beads-refinery", false, ""},
|
|
|
|
// Named agents (canonical format: gt-<rig>-<role>-<name>)
|
|
{"valid polecat", "gt-gastown-polecat-nux", false, ""},
|
|
{"valid crew", "gt-beads-crew-dave", false, ""},
|
|
{"valid polecat with complex name", "gt-gastown-polecat-war-boy-1", false, ""},
|
|
|
|
// Valid: alternative prefixes (beads uses bd-)
|
|
{"valid bd-mayor", "bd-mayor", false, ""},
|
|
{"valid bd-beads-polecat-pearl", "bd-beads-polecat-pearl", false, ""},
|
|
{"valid bd-beads-witness", "bd-beads-witness", false, ""},
|
|
|
|
// Valid: hyphenated rig names (GH#854)
|
|
{"hyphenated rig witness", "ob-my-project-witness", false, ""},
|
|
{"hyphenated rig refinery", "gt-foo-bar-refinery", false, ""},
|
|
{"hyphenated rig crew", "bd-my-cool-project-crew-fang", false, ""},
|
|
{"hyphenated rig polecat", "gt-some-long-rig-name-polecat-nux", false, ""},
|
|
{"hyphenated rig and name", "gt-my-rig-polecat-war-boy", false, ""},
|
|
{"multi-hyphen rig crew", "ob-a-b-c-d-crew-dave", false, ""},
|
|
|
|
// Invalid: no prefix (missing hyphen)
|
|
{"no prefix", "mayor", true, "must have a prefix followed by '-'"},
|
|
|
|
// Invalid: empty
|
|
{"empty id", "", true, "agent ID is required"},
|
|
|
|
// Invalid: unknown role in position 2
|
|
{"unknown role", "gt-gastown-admin", true, "invalid agent format"},
|
|
|
|
// Invalid: town-level with rig (put role first)
|
|
{"mayor with rig suffix", "gt-gastown-mayor", true, "cannot have rig/name suffixes"},
|
|
{"deacon with rig suffix", "gt-beads-deacon", true, "cannot have rig/name suffixes"},
|
|
|
|
// Invalid: per-rig role without rig
|
|
{"witness alone", "gt-witness", true, "requires rig"},
|
|
{"refinery alone", "gt-refinery", true, "requires rig"},
|
|
|
|
// Invalid: named agent without name
|
|
{"crew no name", "gt-beads-crew", true, "requires name"},
|
|
{"polecat no name", "gt-gastown-polecat", true, "requires name"},
|
|
|
|
// Invalid: witness/refinery with extra parts
|
|
{"witness with name", "gt-gastown-witness-extra", true, "cannot have name suffix"},
|
|
{"refinery with name", "gt-beads-refinery-extra", true, "cannot have name suffix"},
|
|
|
|
// Invalid: empty components
|
|
{"empty after prefix", "gt-", true, "must include content after prefix"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := ValidateAgentID(tt.id)
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ValidateAgentID(%q) error = %v, wantError %v", tt.id, err, tt.wantError)
|
|
return
|
|
}
|
|
if err != nil && tt.errorContains != "" {
|
|
if !strings.Contains(err.Error(), tt.errorContains) {
|
|
t.Errorf("ValidateAgentID(%q) error = %q, should contain %q", tt.id, err.Error(), tt.errorContains)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|