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

thread-provider-switching.test.ts

70 lines2.3 KB

1import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { threadAllowsProviderSwitch } from "./thread-provider-switching";
5​
6const startedThread = {
7 latestRun: { id: "run-1" },
8 latestUserMessageAt: "2026-06-01T00:00:00.000Z",
9 runtime: { status: "idle" },
10} as never;
11​
12function projectionWithSession(supportsProviderSwitchingViaHandoff: boolean) {
13 return {
14 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
15 runs: [],
16 providerThreads: [
17 { id: "provider-thread", appThreadId: "thread", providerSessionId: "provider-session" },
18 ],
19 providerSessions: [
20 {
21 id: "provider-session",
22 status: "running",
23 capabilities: { sessions: { supportsProviderSwitchingViaHandoff } },
24 },
25 ],
26 } as unknown as OrchestrationV2ThreadProjection;
28​
29describe("threadAllowsProviderSwitch", () => {
30 it("keeps a started thread on its provider when the session cannot hand off", () => {
31 expect(
32 threadAllowsProviderSwitch({
33 thread: startedThread,
34 projection: projectionWithSession(false),
35 }),
36 ).toBe(false);
37 });
38​
39 it("allows an imported thread to hand off before opening a provider session", () => {
40 expect(
41 threadAllowsProviderSwitch({
42 thread: startedThread,
43 projection: {
44 thread: { id: "thread", activeProviderThreadId: null, historyOrigin: "v1_import" },
45 runs: [],
46 providerThreads: [],
47 providerSessions: [],
48 } as unknown as OrchestrationV2ThreadProjection,
49 }),
50 ).toBe(true);
51 });
52​
53 it("keeps a preparing turn bound before its provider session appears", () => {
54 expect(
55 threadAllowsProviderSwitch({
56 thread: startedThread,
57 projection: {
58 thread: { id: "thread", activeProviderThreadId: null, historyOrigin: "v1_import" },
59 runs: [{ status: "preparing", providerThreadId: "provider-thread" }],
60 providerThreads: [],
61 providerSessions: [],
62 } as unknown as OrchestrationV2ThreadProjection,
63 }),
64 ).toBe(false);
65 });
66​
67 it("keeps a started thread bound until its projection resolves a session", () => {
68 expect(threadAllowsProviderSwitch({ thread: startedThread, projection: null })).toBe(false);
69 });
70});