|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// writeCodexRollout creates a Codex CLI rollout JSONL file under |
| 13 | +// CODEX_HOME/sessions/2026/07/25/rollout-<timestamp>-<thread>.jsonl and writes |
| 14 | +// the given raw lines (one JSON record per line). It returns the file path. |
| 15 | +func writeCodexRollout(t *testing.T, codexHome, thread, timestamp string, lines []string) string { |
| 16 | + t.Helper() |
| 17 | + dir := filepath.Join(codexHome, "sessions", "2026", "07", "25") |
| 18 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 19 | + t.Fatalf("mkdir rollout dir: %v", err) |
| 20 | + } |
| 21 | + path := filepath.Join(dir, fmt.Sprintf("rollout-%s-%s.jsonl", timestamp, thread)) |
| 22 | + f, err := os.Create(path) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("create rollout: %v", err) |
| 25 | + } |
| 26 | + defer f.Close() |
| 27 | + w := bufio.NewWriter(f) |
| 28 | + for _, l := range lines { |
| 29 | + if _, err := w.WriteString(l + "\n"); err != nil { |
| 30 | + t.Fatalf("write rollout line: %v", err) |
| 31 | + } |
| 32 | + } |
| 33 | + if err := w.Flush(); err != nil { |
| 34 | + t.Fatalf("flush rollout: %v", err) |
| 35 | + } |
| 36 | + return path |
| 37 | +} |
| 38 | + |
| 39 | +// turnContextLine returns a JSONL record for a Codex turn_context entry with |
| 40 | +// the given turn_id and model. |
| 41 | +func turnContextLine(turnID, model string) string { |
| 42 | + rec := map[string]interface{}{ |
| 43 | + "type": "turn_context", |
| 44 | + "payload": map[string]interface{}{ |
| 45 | + "turn_id": turnID, |
| 46 | + "model": model, |
| 47 | + }, |
| 48 | + } |
| 49 | + b, err := json.Marshal(rec) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + return string(b) |
| 54 | +} |
| 55 | + |
| 56 | +func TestResolveCodexModel_MatchesExactTurn(t *testing.T) { |
| 57 | + codexHome := t.TempDir() |
| 58 | + writeCodexRollout(t, codexHome, "abc", "20260725-180000", []string{ |
| 59 | + turnContextLine("turn-old", "gpt-5.5"), |
| 60 | + turnContextLine("turn-7", "gpt-5.6-sol"), |
| 61 | + }) |
| 62 | + t.Setenv("CODEX_HOME", codexHome) |
| 63 | + got := resolveCodexModel("abc", "turn-7") |
| 64 | + if got != "gpt-5.6-sol" { |
| 65 | + t.Errorf("resolveCodexModel = %q, want gpt-5.6-sol", got) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestResolveCodexModel_DoesNotUseAnotherTurn(t *testing.T) { |
| 70 | + codexHome := t.TempDir() |
| 71 | + writeCodexRollout(t, codexHome, "abc", "20260725-180000", []string{ |
| 72 | + turnContextLine("turn-old", "gpt-5.5"), |
| 73 | + }) |
| 74 | + t.Setenv("CODEX_HOME", codexHome) |
| 75 | + got := resolveCodexModel("abc", "turn-7") |
| 76 | + if got != "" { |
| 77 | + t.Errorf("resolveCodexModel = %q, want empty (must not fall back to another turn)", got) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestResolveCodexModel_IgnoresBadIdentifiersAndMalformedRecords(t *testing.T) { |
| 82 | + t.Run("malformed JSON skipped", func(t *testing.T) { |
| 83 | + codexHome := t.TempDir() |
| 84 | + writeCodexRollout(t, codexHome, "abc", "20260725-180000", []string{ |
| 85 | + "{not valid json", |
| 86 | + turnContextLine("turn-7", "gpt-5.6-sol"), |
| 87 | + }) |
| 88 | + t.Setenv("CODEX_HOME", codexHome) |
| 89 | + got := resolveCodexModel("abc", "turn-7") |
| 90 | + if got != "gpt-5.6-sol" { |
| 91 | + t.Errorf("resolveCodexModel = %q, want gpt-5.6-sol after skipping malformed line", got) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("path traversal thread rejected", func(t *testing.T) { |
| 96 | + codexHome := t.TempDir() |
| 97 | + writeCodexRollout(t, codexHome, "abc", "20260725-180000", []string{ |
| 98 | + turnContextLine("turn-7", "gpt-5.6-sol"), |
| 99 | + }) |
| 100 | + t.Setenv("CODEX_HOME", codexHome) |
| 101 | + got := resolveCodexModel("../abc", "turn-7") |
| 102 | + if got != "" { |
| 103 | + t.Errorf("resolveCodexModel = %q for ../thread, want empty", got) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("empty turn rejected", func(t *testing.T) { |
| 108 | + codexHome := t.TempDir() |
| 109 | + writeCodexRollout(t, codexHome, "abc", "20260725-180000", []string{ |
| 110 | + turnContextLine("turn-7", "gpt-5.6-sol"), |
| 111 | + }) |
| 112 | + t.Setenv("CODEX_HOME", codexHome) |
| 113 | + got := resolveCodexModel("abc", "") |
| 114 | + if got != "" { |
| 115 | + t.Errorf("resolveCodexModel = %q for empty turn, want empty", got) |
| 116 | + } |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func TestModelFromCodexRollout_UnreadablePathIsEmpty(t *testing.T) { |
| 121 | + dir := t.TempDir() |
| 122 | + // Passing a directory: os.Open succeeds but reading fails; must return empty. |
| 123 | + got := modelFromCodexRollout(dir, "turn-7") |
| 124 | + if got != "" { |
| 125 | + t.Errorf("modelFromCodexRollout(dir) = %q, want empty", got) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestCodexNotify_ResolvesModelFromSession(t *testing.T) { |
| 130 | + codexHome := t.TempDir() |
| 131 | + writeCodexRollout(t, codexHome, "abc", "20260725-180000", []string{ |
| 132 | + turnContextLine("turn-old", "gpt-5.5"), |
| 133 | + turnContextLine("turn-7", "gpt-5.6-sol"), |
| 134 | + }) |
| 135 | + t.Setenv("CODEX_HOME", codexHome) |
| 136 | + in := []byte(`{"type":"agent-turn-complete","thread-id":"abc","turn-id":"turn-7","last-assistant-message":"Built OK."}`) |
| 137 | + m, err := CodexNotifyFromBytes(in) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("unexpected error: %v", err) |
| 140 | + } |
| 141 | + if m.Model != "gpt-5.6-sol" { |
| 142 | + t.Errorf("model = %q, want gpt-5.6-sol resolved from session rollout", m.Model) |
| 143 | + } |
| 144 | +} |
0 commit comments