Open History to see the last commit
1import { ProviderReplayNdjsonParseError } from "./ReplayTranscriptNdjson.ts";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5
6import {
7 decodeProviderReplayNdjson,
8 materializeReplayTranscriptWorkspace,
9} from "./ReplayTranscriptNdjson.ts";
10
11const encodeParseError = Schema.encodeUnknownEffect(ProviderReplayNdjsonParseError);
12
13describe("decodeProviderReplayNdjson", () => {
14 it.effect("decodes a self-describing provider replay fixture", () =>
15 Effect.gen(function* () {
16 const transcript = yield* decodeProviderReplayNdjson(`
17 {"type":"transcript_start","provider":"codex","protocol":"codex.app-server","version":"0.120.0","scenario":"simple"}
18 {"type":"expect_outbound","label":"initialize","frame":{"id":1,"method":"initialize","params":{}}}
19 {"type":"emit_inbound","label":"initialized","afterMs":5,"frame":{"id":1,"result":{"ok":true}}}
20 {"type":"runtime_exit","status":"success"}
21 `);
22
23 assert.equal(transcript.provider, "codex");
24 assert.equal(transcript.protocol, "codex.app-server");
25 assert.equal(transcript.scenario, "simple");
26 assert.deepEqual(
27 transcript.entries.map((entry) => entry.type),
28 ["expect_outbound", "emit_inbound", "runtime_exit"],
29 );
30 }),
31 );
32
33 it.effect("decodes entry-only fixtures when metadata is supplied by the test", () =>
34 Effect.gen(function* () {
35 const transcript = yield* decodeProviderReplayNdjson(
36 `
37 {"type":"emit_inbound","frame":{"method":"thread/created","params":{"id":"native-thread"}}}
38 {"type":"runtime_exit","status":"success"}
39 `,
40 {
41 provider: "claudeAgent",
42 protocol: "claude-agent-sdk",
43 version: "0.2.111",
44 scenario: "entry-only",
45 },
46 );
47
48 assert.equal(transcript.provider, "claudeAgent");
49 assert.equal(transcript.entries.length, 2);
50 }),
51 );
52
53 it.effect("returns a schema-serializable typed parse error", () =>
54 Effect.gen(function* () {
55 const error = yield* decodeProviderReplayNdjson(`{"type":`).pipe(Effect.flip);
56 const encoded = yield* encodeParseError(error);
57
58 assert.equal(error._tag, "ProviderReplayNdjsonLineParseError");
59 assert.equal(encoded._tag, "ProviderReplayNdjsonLineParseError");
60 if (encoded._tag !== "ProviderReplayNdjsonLineParseError") {
61 throw new Error("Expected line parse error encoding.");
62 }
63 assert.equal(encoded.lineNumber, 1);
64 assert.equal(encoded.line, '{"type":');
65 const cause = encoded.cause;
66 if (typeof cause !== "object" || cause === null || Array.isArray(cause)) {
67 throw new Error("Expected encoded parse cause to be a JSON object.");
68 }
69 const causeRecord = cause as Record<string, unknown>;
70 assert.equal(causeRecord.name, "SchemaError");
71 assert.doesNotThrow(() => JSON.stringify(encoded));
72 }),
73 );
74
75 it.effect("materializes outbound workspace placeholders without weakening replay frames", () =>
76 Effect.gen(function* () {
77 const transcript = yield* decodeProviderReplayNdjson(`
78 {"type":"transcript_start","provider":"codex","protocol":"codex.app-server","version":"0.120.0","scenario":"workspace"}
79 {"type":"expect_outbound","frame":{"method":"turn/start","params":{"cwd":"<workspace>","nested":["<workspace>"]}}}
80 {"type":"emit_inbound","frame":{"method":"item/completed","params":{"text":"<workspace>"}}}
81 `);
82
83 const materialized = materializeReplayTranscriptWorkspace(transcript, "/tmp/workspace");
84
85 assert.deepEqual(materialized.entries[0], {
86 type: "expect_outbound",
87 frame: {
88 method: "turn/start",
89 params: { cwd: "/tmp/workspace", nested: ["/tmp/workspace"] },
90 },
91 });
92 assert.deepEqual(materialized.entries[1], {
93 type: "emit_inbound",
94 frame: { method: "item/completed", params: { text: "<workspace>" } },
95 });
96 }),
97 );
98});