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

useStartSideChat.test.ts

84 lines3.5 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5const device = vi.hoisted(() => ({
6 secureStore: new Map<string, string>(),
7 shells: new Map<string, { latestRun: null; latestUserMessageAt: string | null }>(),
8 forkCommands: new Map<string, string>(),
9 turnTargets: [] as string[],
10 loseForkReply: true,
11 ids: 0,
12}));
13​
14vi.mock("expo-secure-store", () => ({
15 WHEN_UNLOCKED_THIS_DEVICE_ONLY: 0,
16 getItem: (key: string) => device.secureStore.get(key) ?? null,
17 setItem: (key: string, value: string) => void device.secureStore.set(key, value),
18 deleteItemAsync: async (key: string) => void device.secureStore.delete(key),
19}));
20vi.mock("gt-react-native", () => ({
21 msg: (text: string) => text,
22 useGT: () => (strings: TemplateStringsArray) => strings.join(""),
23}));
24vi.mock("../../lib/uuid", () => ({ uuidv4: () => `id-${++device.ids}` }));
25vi.mock("../../i18n/messages", () => ({ m: (text: string) => text }));
26vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() }));
27vi.mock("./useOpenLinkedThread", () => ({ useOpenLinkedThread: () => vi.fn() }));
28vi.mock("./threadForkNavigation", () => ({
29 waitForThreadShell: async (ref: { threadId: string }) => device.shells.has(ref.threadId),
30}));
31vi.mock("../../state/atom-registry", () => ({
32 appAtomRegistry: { get: (threadId: string) => device.shells.get(threadId) ?? null },
33}));
34vi.mock("../../state/threads", () => ({
35 environmentThreadShells: { threadShellAtom: (ref: { threadId: string }) => ref.threadId },
36 threadEnvironment: {
37 forkSideChat: "fork",
38 delete: "delete",
39 setRuntimeMode: "setRuntimeMode",
40 startTurn: "startTurn",
41 },
42}));
43// The server dedupes forks by command id, so a resent command reuses the side chat it made.
44vi.mock("../../state/use-atom-command", () => ({
45 useAtomCommand:
46 (command: string) =>
47 async (call: { input: { commandId: string; targetThreadId: string; threadId: string } }) => {
48 if (command === "fork") {
49 const { commandId, targetThreadId } = call.input;
50 if (!device.forkCommands.has(commandId)) {
51 device.forkCommands.set(commandId, targetThreadId);
52 device.shells.set(targetThreadId, { latestRun: null, latestUserMessageAt: null });
53 }
54 if (device.loseForkReply) return { _tag: "Failure", cause: Cause.interrupt() };
55 }
56 if (command === "startTurn") device.turnTargets.push(call.input.threadId);
57 return { _tag: "Success", value: undefined };
58 },
59}));
60​
61describe("useStartSideChat", () => {
62 it("resends the fork whose reply was lost after the app restarts", async () => {
63 const source = {
64 environmentId: EnvironmentId.make("environment"),
65 threadId: ThreadId.make("parent"),
66 };
67 const message = { text: "What should we check?", attachments: [], titleSeed: "Check" };
68 const settings = {
69 runtimeMode: "full-access",
70 modelSelection: { instanceId: "replay", model: "replay" },
71 } as unknown as Parameters<ReturnType<typeof useStartSideChat>>[2];
72​
73 let { useStartSideChat } = await import("./useStartSideChat");
74 expect(await useStartSideChat()(source, message, settings)).toBe(false);
75​
76 vi.resetModules();
77 device.loseForkReply = false;
78 ({ useStartSideChat } = await import("./useStartSideChat"));
79 expect(await useStartSideChat()(source, message, settings)).toBe(true);
80​
81 expect(device.forkCommands.size).toBe(1);
82 expect(device.turnTargets).toEqual([...device.forkCommands.values()]);
83 });
84});