Open History to see the last commit
1import {
2 ProviderInstanceId,
3 RunId,
4 type OrchestrationV2ThreadProjection,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8import {
9 resolveHandoffEndpoints,
10 resolvePendingHandoff,
11 type HandoffTimelineRun,
12} from "./handoff.ts";
13
14const from = ProviderInstanceId.make("codex_personal");
15const to = ProviderInstanceId.make("claudeAgent");
16const item = {
17 runId: RunId.make("target"),
18 fromProviderInstanceIds: [from],
19 toProviderInstanceId: to,
20};
21const run = (
22 id: string,
23 ordinal: number,
24 instanceId: ProviderInstanceId,
25 model: string,
26): HandoffTimelineRun => ({
27 id: RunId.make(id),
28 ordinal,
29 providerInstanceId: instanceId,
30 modelSelection: { instanceId, model },
31});
32
33describe("handoff endpoints shared by web and mobile", () => {
34 it("preserves stamped models including several models from the same provider", () => {
35 const fromModelSelections = [
36 { instanceId: from, model: "source-a" },
37 { instanceId: from, model: "source-b" },
38 ];
39 expect(
40 resolveHandoffEndpoints({ ...item, fromModelSelections, toModel: "destination" }, [
41 run("target", 2, to, "later-model"),
42 ]),
43 ).toEqual({
44 from: fromModelSelections,
45 to: { instanceId: to, model: "destination" },
46 });
47 });
48
49 it("recovers legacy models from the handoff run and latest earlier source run", () => {
50 const runs = [
51 run("later", 4, from, "wrong-later-model"),
52 run("old", 1, from, "old-model"),
53 run("target", 3, to, "destination"),
54 run("source", 2, from, "source-model"),
55 ];
56 expect(resolveHandoffEndpoints(item, runs)).toEqual({
57 from: [{ instanceId: from, model: "source-model" }],
58 to: { instanceId: to, model: "destination" },
59 });
60 });
61
62 it("retains provider identities when historical runs are not loaded", () => {
63 expect(resolveHandoffEndpoints(item, [])).toEqual({
64 from: [{ instanceId: from, model: undefined }],
65 to: { instanceId: to, model: undefined },
66 });
67 });
68
69 it("does not borrow a target model from another provider", () => {
70 expect(
71 resolveHandoffEndpoints(item, [run("target", 2, from, "wrong-model")]).to.model,
72 ).toBeUndefined();
73 });
74});
75
76const projection = {
77 runs: [{ ...run("source", 1, from, "source"), startedAt: "2026-10-11T00:00:00Z" }],
78 visibleTurnItems: [{ item: { type: "user_message" } }],
79} as unknown as OrchestrationV2ThreadProjection;
80
81describe("pending context handoff", () => {
82 it("appears on selection, updates the destination, and disappears on returning", () => {
83 expect(resolvePendingHandoff(projection, { instanceId: to, model: "first" }, [])).toEqual({
84 from: [{ instanceId: from, model: "source" }],
85 to: { instanceId: to, model: "first" },
86 });
87 expect(
88 resolvePendingHandoff(projection, { instanceId: to, model: "second" }, [])?.to.model,
89 ).toBe("second");
90 expect(resolvePendingHandoff(projection, { instanceId: from, model: "source" }, [])).toBeNull();
91 expect(resolvePendingHandoff(projection, { instanceId: from, model: "other" }, [])).toBeNull();
92 });
93 it("stays hidden while the thread's timeline is empty", () => {
94 const empty = { ...projection, visibleTurnItems: [] };
95 expect(resolvePendingHandoff(empty, { instanceId: to, model: "first" }, [])).toBeNull();
96 // Paging can leave a window of only tool rows after the prompt.
97 const toolRowsOnly = {
98 ...projection,
99 visibleTurnItems: [{ item: { type: "command_execution" } }],
100 } as unknown as OrchestrationV2ThreadProjection;
101 expect(
102 resolvePendingHandoff(toolRowsOnly, { instanceId: to, model: "first" }, [])?.to.model,
103 ).toBe("first");
104 });
105 it("only previews a same-provider model change when it needs a fresh native thread", () => {
106 const providers = [
107 { instanceId: from, requiresNewThreadForModelChange: true },
108 ] as unknown as ServerProvider[];
109 expect(
110 resolvePendingHandoff(projection, { instanceId: from, model: "other" }, providers)?.to.model,
111 ).toBe("other");
112 });
113 it("resumes compatible account overlays without showing a handoff", () => {
114 const providers = [from, to].map((instanceId) => ({
115 instanceId,
116 driver: "codex",
117 continuation: { groupKey: "shared" },
118 })) as unknown as ServerProvider[];
119 expect(
120 resolvePendingHandoff(projection, { instanceId: to, model: "target" }, providers),
121 ).toBeNull();
122 });
123});