| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 12 | ) |
| 13 | |
| 14 | func TestDirectClaudeAuthDocument(t *testing.T) { |
| 15 | recorded, err := os.ReadFile("testdata/claude-auth-status-2.1.312.json") |
| 16 | mustJournal(t, err) |
| 17 | for _, mode := range []string{"auth", "stream", "other-version", "extra-arg", "tampered-manifest", "other-provider", "duplicate", "trailing", "oversized"} { |
| 18 | t.Run(mode, func(t *testing.T) { |
| 19 | j, l, _, p := directFixture(t, "claude") |
| 20 | args := []string{"/opt/claude", "--setting-sources", "", "--settings", provider.SettingsPath, "--strict-mcp-config", "--mcp-config", provider.MCPPath, "--disable-slash-commands", "auth", "status", "--json"} |
| 21 | raw := string(recorded) |
| 22 | switch mode { |
| 23 | case "stream": |
| 24 | args = []string{"/opt/claude", "-p", "--output-format", "stream-json"} |
| 25 | case "other-version": |
| 26 | l.Binding.Expected.ProviderVersion = "other" |
| 27 | case "other-provider": |
| 28 | l.Selection.Provider = "codex" |
| 29 | case "extra-arg": |
| 30 | args = append(args, "--unknown") |
| 31 | case "duplicate": |
| 32 | raw = "{\n\"loggedIn\":false,\n\"loggedIn\":true\n}\n" |
| 33 | case "trailing": |
| 34 | raw += "{}\n" |
| 35 | case "oversized": |
| 36 | raw = "{\"secret\":\"" + strings.Repeat("x", MaxFrameBytes) + "\"}\n" |
| 37 | } |
| 38 | l.Policy.Manifest, _ = json.Marshal(map[string]any{"Argv": args, "ProviderVersion": "2.1.312"}) |
| 39 | l.Policy.ManifestHash = provider.ManifestDigest(l.Policy.Manifest) |
| 40 | if mode == "tampered-manifest" { |
| 41 | l.Policy.ManifestHash = "wrong" |
| 42 | } |
| 43 | // Replace the fixture's pre-recorded launch before opening capture. |
| 44 | _, err = j.db.Exec("DELETE FROM direct_attempts") |
| 45 | mustJournal(t, err) |
| 46 | p.out = io.NopCloser(strings.NewReader(raw)) |
| 47 | cap, err := NewCaptureFactory(j).Open(context.Background(), p, l) |
| 48 | mustJournal(t, err) |
| 49 | var frames []provider.Frame |
| 50 | for frame := range cap.Frames { |
| 51 | frames = append(frames, frame) |
| 52 | } |
| 53 | err = <-cap.Done |
| 54 | if mode == "auth" { |
| 55 | if err != nil || len(frames) != 1 || !json.Valid(frames[0].Bytes) || p.stopped.Load() { |
| 56 | t.Fatalf("auth document: frames=%d stopped=%v error=%v", len(frames), p.stopped.Load(), err) |
| 57 | } |
| 58 | } else if err == nil || !p.stopped.Load() || len(frames) != 0 { |
| 59 | t.Fatalf("unsafe framing accepted: %v, %d", err, len(frames)) |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |