The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | CommandId, |
| 3 | EnvironmentId, |
| 4 | MessageId, |
| 5 | ProjectId, |
| 6 | ProviderInstanceId, |
| 7 | ThreadId, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 10 | |
| 11 | import { appAtomRegistry } from "./atom-registry"; |
| 12 | import { pendingThreadCreationOutcomesAtom } from "./pending-thread-creation"; |
| 13 | import { threadOutboxManager } from "./thread-outbox"; |
| 14 | import type { QueuedThreadMessage } from "./thread-outbox-model"; |
| 15 | import { startOpeningEnvironmentThread } from "./threads"; |
| 16 | |
| 17 | const loads = vi.hoisted(() => [] as string[]); |
| 18 | |
| 19 | vi.mock("../connection/runtime", () => ({ connectionAtomRuntime: {} })); |
| 20 | vi.mock("../connection/catalog", () => ({ environmentCatalog: {} })); |
| 21 | vi.mock("./shell", () => ({ environmentShell: {} })); |
| 22 | vi.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 | |
| 44 | const environmentId = EnvironmentId.make("environment-1"); |
| 45 | const threadId = ThreadId.make("thread-1"); |
| 46 | const threadKey = `${environmentId}:${threadId}`; |
| 47 | const 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 | |
| 64 | describe("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 | }); |