|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestResolveAgent_nilProducesSafeFalse(t *testing.T) { |
| 11 | + got := resolveAgent(Command{Name: "lint", Usage: "Run linters"}) |
| 12 | + if got.Safe { |
| 13 | + t.Errorf("Safe = true, want false for nil Agent") |
| 14 | + } |
| 15 | + if got.Description != "Run linters" { |
| 16 | + t.Errorf("Description = %q, want fallback to Usage", got.Description) |
| 17 | + } |
| 18 | + if got.Reads != nil || got.Writes != nil { |
| 19 | + t.Errorf("Reads/Writes = %v/%v, want nil for nil Agent", got.Reads, got.Writes) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestResolveAgent_safeTrueRoundTrips(t *testing.T) { |
| 24 | + got := resolveAgent(Command{ |
| 25 | + Name: "test", |
| 26 | + Usage: "Run tests", |
| 27 | + Agent: &Agent{Safe: true}, |
| 28 | + }) |
| 29 | + if !got.Safe { |
| 30 | + t.Error("Safe = false, want true") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestResolveAgent_descriptionOverridesUsage(t *testing.T) { |
| 35 | + got := resolveAgent(Command{ |
| 36 | + Name: "deploy", |
| 37 | + Usage: "deploy", |
| 38 | + Agent: &Agent{Description: "Deploy the service to prod — destructive"}, |
| 39 | + }) |
| 40 | + if got.Description != "Deploy the service to prod — destructive" { |
| 41 | + t.Errorf("Description = %q, want explicit override", got.Description) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestResolveAgent_emptyDescriptionFallsBackToUsage(t *testing.T) { |
| 46 | + got := resolveAgent(Command{ |
| 47 | + Name: "test", |
| 48 | + Usage: "Run tests", |
| 49 | + Agent: &Agent{Safe: true}, // Description left empty |
| 50 | + }) |
| 51 | + if got.Description != "Run tests" { |
| 52 | + t.Errorf("Description = %q, want fallback to Usage when Agent.Description empty", got.Description) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestResolveAgent_fallsBackToNameWhenUsageAndDescriptionEmpty(t *testing.T) { |
| 57 | + // Schema only requires name+tasks, so a valid command can have neither |
| 58 | + // usage nor agent.description. MCP clients still need a non-empty |
| 59 | + // descriptor, so the command name is the last-resort fallback. |
| 60 | + got := resolveAgent(Command{Name: "lint"}) |
| 61 | + if got.Description != "lint" { |
| 62 | + t.Errorf("Description = %q, want fallback to Name", got.Description) |
| 63 | + } |
| 64 | + |
| 65 | + got = resolveAgent(Command{Name: "deploy", Agent: &Agent{Safe: false}}) |
| 66 | + if got.Description != "deploy" { |
| 67 | + t.Errorf("Description = %q, want fallback to Name when Agent present but empty", got.Description) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestResolveAgent_readsWritesPreserved(t *testing.T) { |
| 72 | + reads := []string{"src/**/*.go", "go.mod"} |
| 73 | + writes := []string{"dist/"} |
| 74 | + got := resolveAgent(Command{ |
| 75 | + Name: "build", |
| 76 | + Usage: "Build", |
| 77 | + Agent: &Agent{Safe: false, Reads: reads, Writes: writes}, |
| 78 | + }) |
| 79 | + if !reflect.DeepEqual(got.Reads, reads) { |
| 80 | + t.Errorf("Reads = %v, want %v", got.Reads, reads) |
| 81 | + } |
| 82 | + if !reflect.DeepEqual(got.Writes, writes) { |
| 83 | + t.Errorf("Writes = %v, want %v", got.Writes, writes) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestWorkspaceAgent_JSONAlwaysEmitsSafeField(t *testing.T) { |
| 88 | + // Public-contract assertion: even when the Command has no Agent block, |
| 89 | + // the marshalled WorkspaceCommand must include "safe":false. MCP |
| 90 | + // clients rely on reading agent.safe without checking presence first. |
| 91 | + wc := WorkspaceCommand{ |
| 92 | + Name: "lint", |
| 93 | + Description: "Lint everything", |
| 94 | + Agent: resolveAgent(Command{Name: "lint", Usage: "Lint everything"}), |
| 95 | + } |
| 96 | + buf, err := json.Marshal(wc) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("Marshal: %v", err) |
| 99 | + } |
| 100 | + if !strings.Contains(string(buf), `"safe":false`) { |
| 101 | + t.Errorf("JSON missing safe field: %s", buf) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestWorkspaceAgent_omitsEmptySlicesAndDescription(t *testing.T) { |
| 106 | + // A nil-Agent command falls back to Usage for Description; Reads and |
| 107 | + // Writes should be omitted from the JSON since they're nil. |
| 108 | + wc := WorkspaceCommand{ |
| 109 | + Name: "lint", |
| 110 | + Agent: resolveAgent(Command{Name: "lint", Usage: "Lint"}), |
| 111 | + } |
| 112 | + buf, err := json.Marshal(wc) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("Marshal: %v", err) |
| 115 | + } |
| 116 | + got := string(buf) |
| 117 | + if strings.Contains(got, "reads") || strings.Contains(got, "writes") { |
| 118 | + t.Errorf("JSON should omit empty reads/writes: %s", got) |
| 119 | + } |
| 120 | +} |
0 commit comments