The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | import type { AnyRouter } from "@tanstack/react-router"; |
| 3 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | |
| 5 | import * as commandPalette from "../commandPaletteBus"; |
| 6 | import { DraftId, useComposerDraftStore } from "../composerDraftStore"; |
| 7 | import * as previewActions from "../components/preview/previewActionBus"; |
| 8 | import * as settings from "../hooks/useSettings"; |
| 9 | import * as storage from "../hooks/useLocalStorage"; |
| 10 | import * as threadActions from "../lib/chatThreadActions"; |
| 11 | import * as entities from "../state/entities"; |
| 12 | import * as projectsState from "../state/projects"; |
| 13 | import * as session from "../state/session"; |
| 14 | import { Atom } from "effect/reactivity"; |
| 15 | import type { Project } from "../types"; |
| 16 | import { installChatRouteCommands } from "./chatRouteCommands"; |
| 17 | import * as dispatcher from "./dispatcher"; |
| 18 | |
| 19 | function project(id: string): Project { |
| 20 | return { |
| 21 | id: ProjectId.make(id), |
| 22 | environmentId: EnvironmentId.make("local"), |
| 23 | title: id, |
| 24 | workspaceRoot: `/projects/${id}`, |
| 25 | repositoryIdentity: null, |
| 26 | defaultModelSelection: null, |
| 27 | createdAt: "2026-01-01T00:00:00.000Z", |
| 28 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 29 | scripts: [], |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | describe("new thread commands", () => { |
| 34 | it.each([ |
| 35 | { mode: "normal", count: 2, command: "chat.new", picker: true }, |
| 36 | { mode: "boring", count: 2, command: "chat.new", picker: true }, |
| 37 | { mode: "boring", count: 1, command: "chat.new", picker: false }, |
| 38 | { mode: "boring", count: 2, command: "chat.newLocal", picker: false }, |
| 39 | ] as const)("$mode sidebar, $count projects, $command: picker=$picker", (scenario) => { |
| 40 | using _settings = vi.spyOn(settings, "getClientSettings").mockReturnValue({ |
| 41 | ...DEFAULT_CLIENT_SETTINGS, |
| 42 | boringSidebarEnabled: scenario.mode === "boring", |
| 43 | }); |
| 44 | using _projects = vi |
| 45 | .spyOn(entities, "readProjects") |
| 46 | .mockReturnValue([project("active"), project("idle")].slice(0, scenario.count)); |
| 47 | using _ready = vi |
| 48 | .spyOn(projectsState, "defaultProjectFallbackReadyAtom") |
| 49 | .mockReturnValue(Atom.make(true)); |
| 50 | using storedProject = vi.spyOn(storage, "getLocalStorageItem").mockReturnValue("local:active"); |
| 51 | using openPicker = vi.spyOn(commandPalette, "openCommandPalette").mockImplementation(() => {}); |
| 52 | using startLocal = vi.spyOn(threadActions, "startNewThreadFromContext").mockResolvedValue(true); |
| 53 | using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {}); |
| 54 | const router = { state: { matches: [] } } as unknown as AnyRouter; |
| 55 | using stack = new DisposableStack(); |
| 56 | stack.defer(installChatRouteCommands(router)); |
| 57 | const commands = register.mock.calls[0]![0]; |
| 58 | const event = { |
| 59 | preventDefault: vi.fn(), |
| 60 | stopPropagation: vi.fn(), |
| 61 | } as unknown as KeyboardEvent; |
| 62 | |
| 63 | storedProject.mockReturnValue("local:idle"); |
| 64 | commands[scenario.command]!(event); |
| 65 | |
| 66 | if (scenario.picker) { |
| 67 | expect(openPicker).toHaveBeenCalledWith({ open: "new-thread-in" }); |
| 68 | expect(startLocal).not.toHaveBeenCalled(); |
| 69 | } else { |
| 70 | expect(openPicker).not.toHaveBeenCalled(); |
| 71 | expect(startLocal).toHaveBeenCalledOnce(); |
| 72 | expect(startLocal.mock.calls[0]![0].defaultProjectRef).toEqual({ |
| 73 | environmentId: "local", |
| 74 | projectId: scenario.count === 1 ? "active" : "idle", |
| 75 | }); |
| 76 | } |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | describe("preview commands", () => { |
| 81 | it.each([ |
| 82 | { command: "preview.find", action: "find" }, |
| 83 | { command: "preview.refresh", action: "refresh" }, |
| 84 | { command: "preview.focusUrl", action: "focus-url" }, |
| 85 | ] as const)("$command reaches the right panel on a draft thread", ({ command, action }) => { |
| 86 | const draftId = DraftId.make("draft-1"); |
| 87 | useComposerDraftStore |
| 88 | .getState() |
| 89 | .setProjectDraftThreadId( |
| 90 | { environmentId: EnvironmentId.make("local"), projectId: ProjectId.make("active") }, |
| 91 | draftId, |
| 92 | ); |
| 93 | using stack = new DisposableStack(); |
| 94 | stack.defer(() => useComposerDraftStore.getState().clearDraftThread(draftId)); |
| 95 | using _scope = vi.spyOn(session, "readEnvironmentScope").mockReturnValue(true); |
| 96 | using dispatch = vi.spyOn(previewActions, "dispatchPreviewAction").mockImplementation(() => {}); |
| 97 | using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {}); |
| 98 | const router = { |
| 99 | state: { matches: [{ routeId: "/_chat/draft/$draftId", params: { draftId } }] }, |
| 100 | } as unknown as AnyRouter; |
| 101 | stack.defer(installChatRouteCommands(router)); |
| 102 | const event = { preventDefault: vi.fn(), stopPropagation: vi.fn() } as unknown as KeyboardEvent; |
| 103 | |
| 104 | register.mock.calls[0]![0][command]!(event); |
| 105 | |
| 106 | expect(dispatch).toHaveBeenCalledWith(action); |
| 107 | }); |
| 108 | }); |