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

decider.turnDiffComplete.test.ts

127 lines3.7 KB

1import {
2 CheckpointRef,
3 CommandId,
4 MessageId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8 TurnId,
9 type OrchestrationCheckpointSummary,
10 type OrchestrationReadModel,
11} from "@t3tools/contracts";
12import * as NodeServices from "@effect/platform-node/NodeServices";
13import { expect, it } from "@effect/vitest";
14import * as Effect from "effect/Effect";
15import * as Exit from "effect/Exit";
16​
17import { decideOrchestrationCommand } from "./decider.ts";
18​
19const NOW = "2026-01-01T00:00:00.000Z";
20const THREAD_ID = ThreadId.make("thread-1");
21const TURN_ID = TurnId.make("turn-1");
22​
23function makeReadModel(checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>) {
24 return {
25 snapshotSequence: 0,
26 projects: [],
27 threads: [
28 {
29 id: THREAD_ID,
30 projectId: ProjectId.make("project-1"),
31 title: "Thread",
32 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
33 runtimeMode: "full-access",
34 interactionMode: "default",
35 branch: null,
36 worktreePath: null,
37 pullRequests: [],
38 latestTurn: null,
39 createdAt: NOW,
40 updatedAt: NOW,
41 archivedAt: null,
42 settledOverride: null,
43 settledAt: null,
44 snoozedUntil: null,
45 snoozedAt: null,
46 pinnedAt: null,
47 pinOrderKey: null,
48 deletedAt: null,
49 messages: [],
50 proposedPlans: [],
51 activities: [],
52 checkpoints,
53 session: null,
54 },
55 ],
56 updatedAt: NOW,
57 } satisfies OrchestrationReadModel;
59​
60function makeCheckpoint(status: OrchestrationCheckpointSummary["status"]) {
61 return {
62 turnId: TURN_ID,
63 checkpointTurnCount: 1,
64 checkpointRef: CheckpointRef.make(`existing:${status}`),
65 status,
66 files: [],
67 assistantMessageId: null,
68 completedAt: NOW,
69 } satisfies OrchestrationCheckpointSummary;
71​
72function placeholderCommand() {
73 return {
74 type: "thread.turn.diff.complete",
75 commandId: CommandId.make("cmd-diff-placeholder"),
76 threadId: THREAD_ID,
77 turnId: TURN_ID,
78 completedAt: NOW,
79 checkpointRef: CheckpointRef.make("provider-diff:event-1"),
80 status: "missing",
81 files: [],
82 assistantMessageId: MessageId.make("assistant:turn-1"),
83 checkpointTurnCount: 2,
84 createdAt: NOW,
85 } as const;
87​
88it.layer(NodeServices.layer)("turn diff complete decider", (it) => {
89 it.effect("rejects a placeholder when the turn already has a captured checkpoint", () =>
90 Effect.gen(function* () {
91 const exit = yield* Effect.exit(
92 decideOrchestrationCommand({
93 command: placeholderCommand(),
94 readModel: makeReadModel([makeCheckpoint("ready")]),
95 }),
96 );
97 expect(Exit.isFailure(exit)).toBe(true);
98 }),
99 );
100​
101 it.effect("accepts a placeholder when the turn only has a placeholder", () =>
102 Effect.gen(function* () {
103 const event = yield* decideOrchestrationCommand({
104 command: placeholderCommand(),
105 readModel: makeReadModel([makeCheckpoint("missing")]),
106 });
107 const events = Array.isArray(event) ? event : [event];
108 expect(events).toHaveLength(1);
109 expect(events[0]?.type).toBe("thread.turn-diff-completed");
110 }),
111 );
112​
113 it.effect("lets a captured checkpoint replace an earlier placeholder", () =>
114 Effect.gen(function* () {
115 const event = yield* decideOrchestrationCommand({
116 command: {
117 ...placeholderCommand(),
118 checkpointRef: CheckpointRef.make("refs/t3/checkpoints/turn-1"),
119 status: "ready",
120 },
121 readModel: makeReadModel([makeCheckpoint("missing")]),
122 });
123 const events = Array.isArray(event) ? event : [event];
124 expect(events[0]?.type).toBe("thread.turn-diff-completed");
125 }),
126 );
127});