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

threads.test.ts

110 lines3.8 KB

1import {
2 CommandId,
3 EnvironmentId,
4 MessageId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8} from "@t3tools/contracts";
9import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
10​
11import { appAtomRegistry } from "./atom-registry";
12import { pendingThreadCreationOutcomesAtom } from "./pending-thread-creation";
13import { threadOutboxManager } from "./thread-outbox";
14import type { QueuedThreadMessage } from "./thread-outbox-model";
15import { startOpeningEnvironmentThread } from "./threads";
16​
17const loads = vi.hoisted(() => [] as string[]);
18​
19vi.mock("../connection/runtime", () => ({ connectionAtomRuntime: {} }));
20vi.mock("../connection/catalog", () => ({ environmentCatalog: {} }));
21vi.mock("./shell", () => ({ environmentShell: {} }));
22vi.mock("@t3tools/client-runtime/state/threads", async (importOriginal) => {
23 const { Atom } = await import("effect/reactivity");
24 const stateAtom = Atom.family((key: string) =>
25 Atom.make(() => {
26 loads.push(key);
27 return null;
28 }),
29 );
30 return {
31 ...(await importOriginal<object>()),
32 createThreadFamilyAtoms: () => ({}),
33 createOlderSettledThreadAtoms: () => ({}),
34 createThreadEnvironmentAtoms: () => ({}),
35 createEnvironmentThreadDetailAtoms: () => ({}),
36 createEnvironmentThreadShellAtoms: () => ({}),
37 createEnvironmentThreadStateAtoms: () => ({
38 stateAtom: (environmentId: string, threadId: string) =>
39 stateAtom(`${environmentId}:${threadId}`),
40 }),
41 };
42});
43​
44const environmentId = EnvironmentId.make("environment-1");
45const threadId = ThreadId.make("thread-1");
46const threadKey = `${environmentId}:${threadId}`;
47const creation: QueuedThreadMessage = {
48 environmentId,
49 threadId,
50 messageId: MessageId.make("message-1"),
51 commandId: CommandId.make("command-1"),
52 text: "Fix the flaky login test",
53 attachments: [],
54 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
55 creation: {
56 projectId: ProjectId.make("project-1"),
57 workspaceMode: "local",
58 branch: "main",
59 worktreePath: null,
60 },
61 createdAt: "2026-08-24T12:00:00.000Z",
62};
63​
64describe("startOpeningEnvironmentThread", () => {
65 beforeEach(() => {
66 vi.useFakeTimers();
67 });
68​
69 afterEach(() => {
70 vi.runAllTimers();
71 vi.useRealTimers();
72 loads.length = 0;
73 appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {});
74 appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {});
75 });
76​
77 it("waits for a queued thread creation to be delivered before loading the thread", () => {
78 appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {
79 [threadKey]: [creation],
80 });
81 startOpeningEnvironmentThread(environmentId, threadId);
82 expect(loads).toEqual([]);
83​
84 appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {});
85 appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {
86 [threadKey]: { kind: "delivered", message: creation },
87 });
88 startOpeningEnvironmentThread(environmentId, threadId);
89 expect(loads).toEqual([threadKey]);
90 });
91​
92 it("does not load a thread whose creation failed", () => {
93 appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {
94 [threadKey]: { kind: "failed", message: creation, reason: "Checkout failed" },
95 });
96 startOpeningEnvironmentThread(environmentId, threadId);
97 expect(loads).toEqual([]);
98 });
99​
100 it("loads threads without a pending creation", () => {
101 const { creation: _creation, ...followUp } = creation;
102 const otherThreadId = ThreadId.make("thread-2");
103 appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {
104 [threadKey]: [followUp],
105 });
106 startOpeningEnvironmentThread(environmentId, threadId);
107 startOpeningEnvironmentThread(environmentId, otherThreadId);
108 expect(loads).toEqual([threadKey, `${environmentId}:${otherThreadId}`]);
109 });
110});