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

handoff.test.ts

185 lines6.2 KB

1import {
2 ContextHandoffId,
3 ProviderInstanceId,
4 RunId,
5 type OrchestrationV2ThreadProjection,
6 type ServerProvider,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9import {
10 resolveHandoffEndpoints,
11 resolvePendingHandoff,
12 type HandoffTimelineRun,
13} from "./handoff.ts";
14​
15const from = ProviderInstanceId.make("codex_personal");
16const to = ProviderInstanceId.make("claudeAgent");
17const item = {
18 runId: RunId.make("target"),
19 fromProviderInstanceIds: [from],
20 toProviderInstanceId: to,
21};
22const run = (
23 id: string,
24 ordinal: number,
25 instanceId: ProviderInstanceId,
26 model: string,
27): HandoffTimelineRun => ({
28 id: RunId.make(id),
29 ordinal,
30 providerInstanceId: instanceId,
31 modelSelection: { instanceId, model },
32});
33​
34describe("handoff endpoints shared by web and mobile", () => {
35 it("preserves stamped models including several models from the same provider", () => {
36 const fromModelSelections = [
37 { instanceId: from, model: "source-a" },
38 { instanceId: from, model: "source-b" },
39 ];
40 expect(
41 resolveHandoffEndpoints({ ...item, fromModelSelections, toModel: "destination" }, [
42 run("target", 2, to, "later-model"),
43 ]),
44 ).toEqual({
45 from: fromModelSelections,
46 to: { instanceId: to, model: "destination" },
47 });
48 });
49​
50 it("recovers legacy models from the handoff run and latest earlier source run", () => {
51 const runs = [
52 run("later", 4, from, "wrong-later-model"),
53 run("old", 1, from, "old-model"),
54 run("target", 3, to, "destination"),
55 run("source", 2, from, "source-model"),
56 ];
57 expect(resolveHandoffEndpoints(item, runs)).toEqual({
58 from: [{ instanceId: from, model: "source-model" }],
59 to: { instanceId: to, model: "destination" },
60 });
61 });
62​
63 it("retains provider identities when historical runs are not loaded", () => {
64 expect(resolveHandoffEndpoints(item, [])).toEqual({
65 from: [{ instanceId: from, model: undefined }],
66 to: { instanceId: to, model: undefined },
67 });
68 });
69​
70 it("does not borrow a target model from another provider", () => {
71 expect(
72 resolveHandoffEndpoints(item, [run("target", 2, from, "wrong-model")]).to.model,
73 ).toBeUndefined();
74 });
75});
76​
77const projection = {
78 runs: [
79 {
80 ...run("source", 1, from, "source"),
81 status: "completed",
82 contextHandoffId: null,
83 startedAt: "2026-10-11T00:00:00Z",
84 },
85 ],
86 visibleTurnItems: [{ item: { type: "user_message" } }],
87} as unknown as OrchestrationV2ThreadProjection;
88​
89describe("pending context handoff", () => {
90 it.each([from, to])("removes the preview while the handoff to %s starts", (instanceId) => {
91 const selection = { instanceId, model: "destination" };
92 const providers = [
93 { instanceId: from, requiresNewThreadForModelChange: true },
94 ] as unknown as ServerProvider[];
95 const starting = {
96 ...projection,
97 runs: [
98 ...projection.runs,
99 {
100 ...projection.runs[0]!,
101 ...run("target", 2, instanceId, "destination"),
102 status: "starting" as const,
103 startedAt: null,
104 contextHandoffId: ContextHandoffId.make("handoff"),
105 },
106 ],
107 };
108​
109 expect(resolvePendingHandoff(projection, selection, providers)).not.toBeNull();
110 expect(resolvePendingHandoff(starting, selection, providers)).toBeNull();
111 expect(
112 resolvePendingHandoff(starting, { instanceId: from, model: "source" }, providers),
113 ).toEqual({
114 from: [selection],
115 to: { instanceId: from, model: "source" },
116 });
117 });
118​
119 it.each(["preparing", "queued", "failed", "cancelled", "rolled_back"] as const)(
120 "keeps the preview when an unstarted handoff run is %s",
121 (status) => {
122 const selection = { instanceId: to, model: "destination" };
123 const unstarted = {
124 ...projection,
125 runs: [
126 ...projection.runs,
127 {
128 ...projection.runs[0]!,
129 ...run("target", 2, to, "destination"),
130 status,
131 startedAt: null,
132 contextHandoffId: ContextHandoffId.make("handoff"),
133 },
134 ],
135 };
136​
137 expect(resolvePendingHandoff(unstarted, selection, [])).toEqual({
138 from: [{ instanceId: from, model: "source" }],
139 to: selection,
140 });
141 },
142 );
143​
144 it("appears on selection, updates the destination, and disappears on returning", () => {
145 expect(resolvePendingHandoff(projection, { instanceId: to, model: "first" }, [])).toEqual({
146 from: [{ instanceId: from, model: "source" }],
147 to: { instanceId: to, model: "first" },
148 });
149 expect(
150 resolvePendingHandoff(projection, { instanceId: to, model: "second" }, [])?.to.model,
151 ).toBe("second");
152 expect(resolvePendingHandoff(projection, { instanceId: from, model: "source" }, [])).toBeNull();
153 expect(resolvePendingHandoff(projection, { instanceId: from, model: "other" }, [])).toBeNull();
154 });
155 it("stays hidden while the thread's timeline is empty", () => {
156 const empty = { ...projection, visibleTurnItems: [] };
157 expect(resolvePendingHandoff(empty, { instanceId: to, model: "first" }, [])).toBeNull();
158 // Paging can leave a window of only tool rows after the prompt.
159 const toolRowsOnly = {
160 ...projection,
161 visibleTurnItems: [{ item: { type: "command_execution" } }],
162 } as unknown as OrchestrationV2ThreadProjection;
163 expect(
164 resolvePendingHandoff(toolRowsOnly, { instanceId: to, model: "first" }, [])?.to.model,
165 ).toBe("first");
166 });
167 it("only previews a same-provider model change when it needs a fresh native thread", () => {
168 const providers = [
169 { instanceId: from, requiresNewThreadForModelChange: true },
170 ] as unknown as ServerProvider[];
171 expect(
172 resolvePendingHandoff(projection, { instanceId: from, model: "other" }, providers)?.to.model,
173 ).toBe("other");
174 });
175 it("resumes compatible account overlays without showing a handoff", () => {
176 const providers = [from, to].map((instanceId) => ({
177 instanceId,
178 driver: "codex",
179 continuation: { groupKey: "shared" },
180 })) as unknown as ServerProvider[];
181 expect(
182 resolvePendingHandoff(projection, { instanceId: to, model: "target" }, providers),
183 ).toBeNull();
184 });
185});