|
| 1 | +package intent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// testHelper is a test binary approach: when invoked as a subprocess, it |
| 12 | +// writes the value of TEST_CLAUDE_OUTPUT to stdout and exits with TEST_CLAUDE_EXIT. |
| 13 | +func TestHelperProcess(t *testing.T) { |
| 14 | + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { |
| 15 | + return |
| 16 | + } |
| 17 | + out := os.Getenv("TEST_CLAUDE_OUTPUT") |
| 18 | + _, _ = os.Stdout.WriteString(out) |
| 19 | + if os.Getenv("TEST_CLAUDE_EXIT") == "1" { |
| 20 | + os.Exit(1) |
| 21 | + } |
| 22 | + os.Exit(0) |
| 23 | +} |
| 24 | + |
| 25 | +func fakeCommandContext(output string, exitErr bool) func(ctx context.Context, name string, args ...string) *exec.Cmd { |
| 26 | + return func(ctx context.Context, name string, args ...string) *exec.Cmd { |
| 27 | + cs := []string{"-test.run=TestHelperProcess", "--"} |
| 28 | + cs = append(cs, args...) |
| 29 | + cmd := exec.CommandContext(ctx, os.Args[0], cs...) |
| 30 | + cmd.Env = append(os.Environ(), |
| 31 | + "GO_WANT_HELPER_PROCESS=1", |
| 32 | + "TEST_CLAUDE_OUTPUT="+output, |
| 33 | + ) |
| 34 | + if exitErr { |
| 35 | + cmd.Env = append(cmd.Env, "TEST_CLAUDE_EXIT=1") |
| 36 | + } |
| 37 | + return cmd |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestClassify_Success(t *testing.T) { |
| 42 | + // Envelope wrapping actual JSON result. |
| 43 | + envelope := `{"result":"{\"argv\":[\"run\",\"plans/auth.md\"],\"confidence\":0.95,\"reasoning\":\"user wants to run auth plan\"}"}` |
| 44 | + |
| 45 | + orig := CommandContext |
| 46 | + CommandContext = fakeCommandContext(envelope, false) |
| 47 | + defer func() { CommandContext = orig }() |
| 48 | + |
| 49 | + r, err := Classify(context.Background(), "run the auth plan") |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("unexpected error: %v", err) |
| 52 | + } |
| 53 | + if len(r.Argv) != 2 || r.Argv[0] != "run" || r.Argv[1] != "plans/auth.md" { |
| 54 | + t.Fatalf("unexpected argv: %v", r.Argv) |
| 55 | + } |
| 56 | + if r.Confidence < 0.9 { |
| 57 | + t.Fatalf("unexpected confidence: %f", r.Confidence) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestClassify_NoClaude(t *testing.T) { |
| 62 | + // Override PATH so claude is not found. |
| 63 | + orig := os.Getenv("PATH") |
| 64 | + os.Setenv("PATH", "") |
| 65 | + defer os.Setenv("PATH", orig) |
| 66 | + |
| 67 | + _, err := Classify(context.Background(), "anything") |
| 68 | + if !errors.Is(err, ErrNoClaude) { |
| 69 | + t.Fatalf("expected ErrNoClaude, got: %v", err) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestClassify_ExitError(t *testing.T) { |
| 74 | + orig := CommandContext |
| 75 | + CommandContext = fakeCommandContext("something went wrong", true) |
| 76 | + defer func() { CommandContext = orig }() |
| 77 | + |
| 78 | + _, err := Classify(context.Background(), "do something") |
| 79 | + if !errors.Is(err, ErrClassificationFailed) { |
| 80 | + t.Fatalf("expected ErrClassificationFailed, got: %v", err) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestClassify_MalformedJSON(t *testing.T) { |
| 85 | + orig := CommandContext |
| 86 | + CommandContext = fakeCommandContext(`{"result":"not json at all"}`, false) |
| 87 | + defer func() { CommandContext = orig }() |
| 88 | + |
| 89 | + _, err := Classify(context.Background(), "do something") |
| 90 | + if !errors.Is(err, ErrClassificationFailed) { |
| 91 | + t.Fatalf("expected ErrClassificationFailed, got: %v", err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestClassify_EmptyArgv(t *testing.T) { |
| 96 | + orig := CommandContext |
| 97 | + CommandContext = fakeCommandContext(`{"result":"{\"argv\":[],\"confidence\":0.1,\"reasoning\":\"unclear\"}"}`, false) |
| 98 | + defer func() { CommandContext = orig }() |
| 99 | + |
| 100 | + _, err := Classify(context.Background(), "do something") |
| 101 | + if !errors.Is(err, ErrClassificationFailed) { |
| 102 | + t.Fatalf("expected ErrClassificationFailed, got: %v", err) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestClassify_CodeFencedJSON(t *testing.T) { |
| 107 | + // Claude sometimes wraps JSON in code fences. |
| 108 | + fenced := "{\"result\":\"```json\\n{\\\"argv\\\":[\\\"runs\\\"],\\\"confidence\\\":0.9,\\\"reasoning\\\":\\\"list runs\\\"}\\n```\"}" |
| 109 | + |
| 110 | + orig := CommandContext |
| 111 | + CommandContext = fakeCommandContext(fenced, false) |
| 112 | + defer func() { CommandContext = orig }() |
| 113 | + |
| 114 | + r, err := Classify(context.Background(), "show my runs") |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("unexpected error: %v", err) |
| 117 | + } |
| 118 | + if len(r.Argv) != 1 || r.Argv[0] != "runs" { |
| 119 | + t.Fatalf("unexpected argv: %v", r.Argv) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestParseResponse_DirectJSON(t *testing.T) { |
| 124 | + // No envelope, direct JSON. |
| 125 | + r, err := parseResponse(`{"argv":["status","abc123"],"confidence":0.85,"reasoning":"check status"}`) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("unexpected error: %v", err) |
| 128 | + } |
| 129 | + if len(r.Argv) != 2 || r.Argv[0] != "status" { |
| 130 | + t.Fatalf("unexpected argv: %v", r.Argv) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestStripCodeFences(t *testing.T) { |
| 135 | + tests := []struct { |
| 136 | + name string |
| 137 | + in string |
| 138 | + want string |
| 139 | + }{ |
| 140 | + {"no fences", `{"argv":["runs"]}`, `{"argv":["runs"]}`}, |
| 141 | + {"with fences", "```json\n{\"argv\":[\"runs\"]}\n```", `{"argv":["runs"]}`}, |
| 142 | + {"with bare fences", "```\n{\"argv\":[\"runs\"]}\n```", `{"argv":["runs"]}`}, |
| 143 | + } |
| 144 | + for _, tt := range tests { |
| 145 | + t.Run(tt.name, func(t *testing.T) { |
| 146 | + got := stripCodeFences(tt.in) |
| 147 | + if got != tt.want { |
| 148 | + t.Fatalf("stripCodeFences(%q) = %q, want %q", tt.in, got, tt.want) |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments