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

chatRouteCommands.test.ts

149 lines6.7 KB

1import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import type { AnyRouter } from "@tanstack/react-router";
4import { describe, expect, it, vi } from "vite-plus/test";
5​
6import * as commandPalette from "../commandPaletteBus";
7import { DraftId, useComposerDraftStore } from "../composerDraftStore";
8import * as previewActions from "../components/preview/previewActionBus";
9import * as settings from "../hooks/useSettings";
10import * as storage from "../hooks/useLocalStorage";
11import * as threadActions from "../lib/chatThreadActions";
12import * as previewFocus from "../lib/previewFocus";
13import { useRightPanelStore } from "../rightPanelStore";
14import * as entities from "../state/entities";
15import * as projectsState from "../state/projects";
16import * as session from "../state/session";
17import { Atom } from "effect/reactivity";
18import type { Project } from "../types";
19import { installChatRouteCommands } from "./chatRouteCommands";
20import * as dispatcher from "./dispatcher";
21​
22function project(id: string): Project {
23 return {
24 id: ProjectId.make(id),
25 environmentId: EnvironmentId.make("local"),
26 title: id,
27 workspaceRoot: `/projects/${id}`,
28 repositoryIdentity: null,
29 defaultModelSelection: null,
30 createdAt: "2026-01-01T00:00:00.000Z",
31 updatedAt: "2026-01-01T00:00:00.000Z",
32 scripts: [],
33 };
35​
36describe("new thread commands", () => {
37 it.each([
38 { mode: "normal", count: 2, command: "chat.new", picker: true },
39 { mode: "boring", count: 2, command: "chat.new", picker: true },
40 { mode: "boring", count: 1, command: "chat.new", picker: false },
41 { mode: "boring", count: 2, command: "chat.newLocal", picker: false },
42 ] as const)("$mode sidebar, $count projects, $command: picker=$picker", (scenario) => {
43 using _settings = vi.spyOn(settings, "getClientSettings").mockReturnValue({
44 ...DEFAULT_CLIENT_SETTINGS,
45 boringSidebarEnabled: scenario.mode === "boring",
46 });
47 using _projects = vi
48 .spyOn(entities, "readProjects")
49 .mockReturnValue([project("active"), project("idle")].slice(0, scenario.count));
50 using _ready = vi
51 .spyOn(projectsState, "defaultProjectFallbackReadyAtom")
52 .mockReturnValue(Atom.make(true));
53 using storedProject = vi.spyOn(storage, "getLocalStorageItem").mockReturnValue("local:active");
54 using openPicker = vi.spyOn(commandPalette, "openCommandPalette").mockImplementation(() => {});
55 using startLocal = vi.spyOn(threadActions, "startNewThreadFromContext").mockResolvedValue(true);
56 using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {});
57 const router = { state: { matches: [] } } as unknown as AnyRouter;
58 using stack = new DisposableStack();
59 stack.defer(installChatRouteCommands(router));
60 const commands = register.mock.calls[0]![0];
61 const event = {
62 preventDefault: vi.fn(),
63 stopPropagation: vi.fn(),
64 } as unknown as KeyboardEvent;
65​
66 storedProject.mockReturnValue("local:idle");
67 commands[scenario.command]!(event);
68​
69 if (scenario.picker) {
70 expect(openPicker).toHaveBeenCalledWith({ open: "new-thread-in" });
71 expect(startLocal).not.toHaveBeenCalled();
72 } else {
73 expect(openPicker).not.toHaveBeenCalled();
74 expect(startLocal).toHaveBeenCalledOnce();
75 expect(startLocal.mock.calls[0]![0].defaultProjectRef).toEqual({
76 environmentId: "local",
77 projectId: scenario.count === 1 ? "active" : "idle",
78 });
79 }
80 });
81});
82​
83describe("preview commands", () => {
84 it("reads the active thread's right panel visibility at shortcut time", () => {
85 const threadRef = scopeThreadRef(EnvironmentId.make("local"), ThreadId.make("panel-thread"));
86 const router = {
87 state: { matches: [{ params: threadRef }] },
88 } as unknown as AnyRouter;
89 using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {});
90 using stack = new DisposableStack();
91 const panels = useRightPanelStore.getState();
92 stack.defer(() => useRightPanelStore.setState(panels, true));
93 stack.defer(installChatRouteCommands(router));
94 const readContext = register.mock.calls[0]![1]!.context!;
95​
96 panels.toggle(threadRef, "diff");
97 expect(readContext()).toMatchObject({ rightPanelOpen: true, previewOpen: false });
98​
99 panels.close(threadRef);
100 expect(readContext()).toMatchObject({ rightPanelOpen: false, previewOpen: false });
101 });
102​
103 it.each([
104 { command: "preview.find", action: "find" },
105 { command: "preview.refresh", action: "refresh" },
106 { command: "preview.focusUrl", action: "focus-url" },
107 ] as const)("$command reaches the right panel on a draft thread", ({ command, action }) => {
108 const draftId = DraftId.make("draft-1");
109 useComposerDraftStore
110 .getState()
111 .setProjectDraftThreadId(
112 { environmentId: EnvironmentId.make("local"), projectId: ProjectId.make("active") },
113 draftId,
114 );
115 using stack = new DisposableStack();
116 stack.defer(() => useComposerDraftStore.getState().clearDraftThread(draftId));
117 using _focus = vi.spyOn(previewFocus, "isPanelFindAvailable").mockReturnValue(true);
118 using _scope = vi.spyOn(session, "readEnvironmentScope").mockReturnValue(true);
119 using dispatch = vi.spyOn(previewActions, "dispatchPreviewAction").mockImplementation(() => {});
120 using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {});
121 const router = {
122 state: { matches: [{ routeId: "/_chat/draft/$draftId", params: { draftId } }] },
123 } as unknown as AnyRouter;
124 stack.defer(installChatRouteCommands(router));
125 const event = { preventDefault: vi.fn(), stopPropagation: vi.fn() } as unknown as KeyboardEvent;
126​
127 register.mock.calls[0]![0][command]!(event);
128​
129 expect(dispatch).toHaveBeenCalledWith(action);
130 });
131​
132 it("preview.find leaves the key unclaimed on non-thread chat routes", () => {
133 using _focus = vi.spyOn(previewFocus, "isPanelFindAvailable").mockReturnValue(true);
134 using dispatch = vi.spyOn(previewActions, "dispatchPreviewAction").mockImplementation(() => {});
135 using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {});
136 const router = {
137 state: { matches: [{ routeId: "/_chat/pull-requests", params: {} }] },
138 } as unknown as AnyRouter;
139 using stack = new DisposableStack();
140 stack.defer(installChatRouteCommands(router));
141 const event = { preventDefault: vi.fn(), stopPropagation: vi.fn() } as unknown as KeyboardEvent;
142​
143 const result = register.mock.calls[0]![0]["preview.find"]!(event);
144​
145 expect(result).toBe(false);
146 expect(event.preventDefault).not.toHaveBeenCalled();
147 expect(dispatch).not.toHaveBeenCalled();
148 });
149});