Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexThreadRevert.test.ts

92 lines2.5 KB

1import { assert, it } from "@effect/vitest";
2import * as CodexClient from "effect-codex-app-server/client";
3import * as CodexReplay from "effect-codex-app-server/replay";
4import * as Effect from "effect/Effect";
5​
6import { revertCodexThread } from "./CodexThreadRevert.ts";
7​
8const page = (
9 id: number,
10 cursor: string | null,
11 limit: number,
12 turns: string[],
13 nextCursor: string | null,
14) => [
15 {
16 type: "expect_outbound" as const,
17 frame: {
18 id,
19 method: "thread/turns/list",
20 params: {
21 threadId: "thread",
22 cursor,
23 limit,
24 sortDirection: "desc",
25 itemsView: "summary",
26 },
27 },
28 },
29 {
30 type: "emit_inbound" as const,
31 frame: {
32 id,
33 result: {
34 data: turns.map((id) => ({ id, items: [], status: "completed", error: null })),
35 nextCursor,
36 },
37 },
38 },
39];
40​
41it.effect("finds the revert boundary across pages of newest-first turns", () =>
42 Effect.gen(function* () {
43 const client = yield* CodexClient.CodexAppServerClient;
44 const result = yield* revertCodexThread(client, "thread", 3).pipe(Effect.flip);
45 assert.equal(result.message, "boundary reached");
46 }).pipe(
47 Effect.provide(
48 CodexReplay.layerReplay({
49 provider: "codex",
50 protocol: "codex.app-server",
51 version: "test",
52 scenario: "revert-pages",
53 entries: [
54 ...page(1, null, 3, ["newest", "middle"], "older"),
55 ...page(2, "older", 1, ["boundary"], null),
56 {
57 type: "expect_outbound",
58 frame: {
59 id: 3,
60 method: "thread/revert",
61 params: { threadId: "thread", beforeTurnId: "boundary" },
62 },
63 },
64 {
65 type: "emit_inbound",
66 frame: { id: 3, error: { code: -32603, message: "boundary reached" } },
67 },
68 ],
69 }),
70 ),
71 Effect.scoped,
72 ),
73);
74​
75it.effect("rejects repeated cursors instead of looping or reverting incomplete history", () =>
76 Effect.gen(function* () {
77 const client = yield* CodexClient.CodexAppServerClient;
78 const result = yield* revertCodexThread(client, "thread", 3).pipe(Effect.flip);
79 assert.equal(result.message, "Thread history pagination repeated a cursor.");
80 }).pipe(
81 Effect.provide(
82 CodexReplay.layerReplay({
83 provider: "codex",
84 protocol: "codex.app-server",
85 version: "test",
86 scenario: "revert-loop",
87 entries: [...page(1, null, 3, ["newest"], "again"), ...page(2, "again", 2, [], "again")],
88 }),
89 ),
90 Effect.scoped,
91 ),
92);