Open History to see the last commit
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("removes the preview while the handoff starts", () => {
91 const selection = { instanceId: to, model: "destination" };
92 const providers = [{ instanceId: from }] as unknown as ServerProvider[];
93 const starting = {
94 ...projection,
95 runs: [
96 ...projection.runs,
97 {
98 ...projection.runs[0]!,
99 ...run("target", 2, to, "destination"),
100 status: "starting" as const,
101 startedAt: null,
102 contextHandoffId: ContextHandoffId.make("handoff"),
103 },
104 ],
105 };
106
107 expect(resolvePendingHandoff(projection, selection, providers)).not.toBeNull();
108 expect(resolvePendingHandoff(starting, selection, providers)).toBeNull();
109 expect(
110 resolvePendingHandoff(starting, { instanceId: from, model: "source" }, providers),
111 ).toEqual({
112 from: [selection],
113 to: { instanceId: from, model: "source" },
114 });
115 });
116
117 it.each(["preparing", "queued", "failed", "cancelled", "rolled_back"] as const)(
118 "keeps the preview when an unstarted handoff run is %s",
119 (status) => {
120 const selection = { instanceId: to, model: "destination" };
121 const unstarted = {
122 ...projection,
123 runs: [
124 ...projection.runs,
125 {
126 ...projection.runs[0]!,
127 ...run("target", 2, to, "destination"),
128 status,
129 startedAt: null,
130 contextHandoffId: ContextHandoffId.make("handoff"),
131 },
132 ],
133 };
134
135 expect(resolvePendingHandoff(unstarted, selection, [])).toEqual({
136 from: [{ instanceId: from, model: "source" }],
137 to: selection,
138 });
139 },
140 );
141
142 it("appears on selection, updates the destination, and disappears on returning", () => {
143 expect(resolvePendingHandoff(projection, { instanceId: to, model: "first" }, [])).toEqual({
144 from: [{ instanceId: from, model: "source" }],
145 to: { instanceId: to, model: "first" },
146 });
147 expect(
148 resolvePendingHandoff(projection, { instanceId: to, model: "second" }, [])?.to.model,
149 ).toBe("second");
150 expect(resolvePendingHandoff(projection, { instanceId: from, model: "source" }, [])).toBeNull();
151 expect(resolvePendingHandoff(projection, { instanceId: from, model: "other" }, [])).toBeNull();
152 });
153 it("stays hidden while the thread's timeline is empty", () => {
154 const empty = { ...projection, visibleTurnItems: [] };
155 expect(resolvePendingHandoff(empty, { instanceId: to, model: "first" }, [])).toBeNull();
156 // Paging can leave a window of only tool rows after the prompt.
157 const toolRowsOnly = {
158 ...projection,
159 visibleTurnItems: [{ item: { type: "command_execution" } }],
160 } as unknown as OrchestrationV2ThreadProjection;
161 expect(
162 resolvePendingHandoff(toolRowsOnly, { instanceId: to, model: "first" }, [])?.to.model,
163 ).toBe("first");
164 });
165 it("resumes compatible account overlays without showing a handoff", () => {
166 const providers = [from, to].map((instanceId) => ({
167 instanceId,
168 driver: "codex",
169 continuation: { groupKey: "shared" },
170 })) as unknown as ServerProvider[];
171 expect(
172 resolvePendingHandoff(projection, { instanceId: to, model: "target" }, providers),
173 ).toBeNull();
174 });
175});