|
| 1 | +import XCTest |
| 2 | + |
| 3 | +@testable import StackNudgePanelCore |
| 4 | + |
| 5 | +// CodexTranscriptReader parses Codex CLI rollout JSONL into the same |
| 6 | +// TranscriptStats the Claude reader produces. The schema is fixed against |
| 7 | +// Codex's own TokenUsage definition: context occupancy is |
| 8 | +// last_token_usage.total_tokens - reasoning_output_tokens, and cached input |
| 9 | +// is a subset of input (never summed). Fixtures here encode that contract so |
| 10 | +// a Codex schema drift, or a regression to the cached-double-count bug, is |
| 11 | +// caught without needing a live Codex session. |
| 12 | +final class CodexTranscriptReaderTests: XCTestCase { |
| 13 | + |
| 14 | + private func writeRollout(_ lines: [String], name: String = "rollout-test.jsonl") -> String { |
| 15 | + let dir = NSTemporaryDirectory() + "codex-rollout-\(UUID().uuidString)/" |
| 16 | + try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true) |
| 17 | + let path = dir + name |
| 18 | + try? (lines.joined(separator: "\n") + "\n") |
| 19 | + .write(toFile: path, atomically: true, encoding: .utf8) |
| 20 | + return path |
| 21 | + } |
| 22 | + |
| 23 | + func test_read_usesContextOccupancyFromLatestTokenCount() { |
| 24 | + let path = writeRollout([ |
| 25 | + #"{"type":"session_meta","payload":{"id":"s1","model":"gpt-5-codex"}}"#, |
| 26 | + #"{"type":"turn_context","payload":{"model":"gpt-5-codex"}}"#, |
| 27 | + #"{"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":10000,"cached_input_tokens":2000,"output_tokens":500,"reasoning_output_tokens":300,"total_tokens":10800},"last_token_usage":{"input_tokens":10000,"cached_input_tokens":2000,"output_tokens":500,"reasoning_output_tokens":300,"total_tokens":10800},"model_context_window":272000}}}"#, |
| 28 | + #"{"type":"response_item","payload":{"role":"assistant"}}"#, |
| 29 | + #"{"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":60000,"cached_input_tokens":50000,"output_tokens":2000,"reasoning_output_tokens":1000,"total_tokens":63000},"last_token_usage":{"input_tokens":52000,"cached_input_tokens":48000,"output_tokens":1500,"reasoning_output_tokens":1000,"total_tokens":54500},"model_context_window":272000}}}"#, |
| 30 | + ]) |
| 31 | + |
| 32 | + let actual = CodexTranscriptReader.read(path: path) |
| 33 | + |
| 34 | + // Latest token_count: last_token_usage.total_tokens - reasoning = 54500 - 1000. |
| 35 | + XCTAssertEqual(actual?.tokens, 53500) |
| 36 | + XCTAssertEqual(actual?.model, "gpt-5-codex") |
| 37 | + } |
| 38 | + |
| 39 | + func test_read_ignoresCumulativeTotalAndCachedSubset() { |
| 40 | + // A single turn where cached == input. If the reader wrongly summed |
| 41 | + // cached into input, or used the cumulative total_token_usage, the |
| 42 | + // number would differ. Occupancy must be last.total - last.reasoning. |
| 43 | + let path = writeRollout([ |
| 44 | + #"{"type":"turn_context","payload":{"model":"gpt-5"}}"#, |
| 45 | + #"{"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":900000,"cached_input_tokens":0,"output_tokens":40000,"reasoning_output_tokens":12000,"total_tokens":940000},"last_token_usage":{"input_tokens":30000,"cached_input_tokens":30000,"output_tokens":800,"reasoning_output_tokens":200,"total_tokens":30800},"model_context_window":400000}}}"#, |
| 46 | + ]) |
| 47 | + |
| 48 | + let actual = CodexTranscriptReader.read(path: path) |
| 49 | + |
| 50 | + XCTAssertEqual(actual?.tokens, 30600) // 30800 - 200, not 940000-ish, not +cached |
| 51 | + XCTAssertEqual(actual?.model, "gpt-5") |
| 52 | + } |
| 53 | + |
| 54 | + func test_read_returnsNilWhenNoTokenCount() { |
| 55 | + let path = writeRollout([ |
| 56 | + #"{"type":"session_meta","payload":{"model":"gpt-5-codex"}}"#, |
| 57 | + #"{"type":"response_item","payload":{"role":"user"}}"#, |
| 58 | + ]) |
| 59 | + |
| 60 | + XCTAssertNil(CodexTranscriptReader.read(path: path)) |
| 61 | + } |
| 62 | + |
| 63 | + func test_read_returnsNilForMissingFile() { |
| 64 | + XCTAssertNil(CodexTranscriptReader.read(path: "/nonexistent/rollout-x.jsonl")) |
| 65 | + } |
| 66 | + |
| 67 | + func test_dispatch_routesRolloutFilenameToCodexReader() { |
| 68 | + // A Claude-shaped assistant line written into a rollout-* file. If the |
| 69 | + // dispatcher routed by filename to the Codex reader (correct), it finds |
| 70 | + // no token_count and returns nil. If it fell through to the Claude |
| 71 | + // reader, it would parse the usage block and return tokens. nil proves |
| 72 | + // the routing. |
| 73 | + let path = writeRollout([ |
| 74 | + #"{"type":"assistant","message":{"model":"claude-x","usage":{"input_tokens":5,"cache_creation_input_tokens":0,"cache_read_input_tokens":0}}}"#, |
| 75 | + ], name: "rollout-abc.jsonl") |
| 76 | + |
| 77 | + XCTAssertNil(TranscriptReader.read(path: path)) |
| 78 | + } |
| 79 | +} |
0 commit comments