Open History to see the last commit
1import type { AnyRouter } from "@tanstack/react-router";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4import { subscribePreviewAction, type PreviewAction } from "../components/preview/previewActionBus";
5import { isMacPlatform } from "../lib/utils";
6import * as session from "../state/session";
7import { useThreadSelectionStore } from "../threadSelectionStore";
8import { installChatRouteCommands } from "./chatRouteCommands";
9
10vi.mock(import("../state/session"), { spy: true });
11
12type Match = {
13 readonly routeId: string;
14 readonly pathname: string;
15 readonly params?: { readonly environmentId: string; readonly threadId: string };
16};
17
18function routerAt(matches: Match[]) {
19 const state = { matches };
20 return {
21 router: { state } as unknown as AnyRouter,
22 navigate: (next: Match[]) => {
23 state.matches = next;
24 },
25 };
26}
27
28const CHAT: Match[] = [
29 { routeId: "__root__", pathname: "/" },
30 { routeId: "/_chat", pathname: "/" },
31];
32
33function pressEscape(): KeyboardEvent {
34 const event = new KeyboardEvent("keydown", { key: "Escape", cancelable: true, bubbles: true });
35 window.dispatchEvent(event);
36 return event;
37}
38
39function pressMod(key: string): KeyboardEvent {
40 const mac = isMacPlatform(navigator.platform);
41 const event = new KeyboardEvent("keydown", {
42 key,
43 metaKey: mac,
44 ctrlKey: !mac,
45 cancelable: true,
46 bubbles: true,
47 });
48 document.activeElement?.dispatchEvent(event);
49 return event;
50}
51
52function selectThread(threadKey: string) {
53 useThreadSelectionStore.getState().clearSelection();
54 useThreadSelectionStore.getState().toggleThread(threadKey);
55}
56
57describe("chat route commands", () => {
58 it("clears a selection made after install, reading the store at keypress time", () => {
59 const { router } = routerAt(CHAT);
60 using stack = new DisposableStack();
61 stack.defer(installChatRouteCommands(router));
62 stack.defer(() => useThreadSelectionStore.getState().clearSelection());
63
64 selectThread("env:thread-1");
65 const event = pressEscape();
66
67 expect(event.defaultPrevented).toBe(true);
68 expect(useThreadSelectionStore.getState().selectedThreadKeys.size).toBe(0);
69 // With nothing selected, Escape is left for other handlers.
70 expect(pressEscape().defaultPrevented).toBe(false);
71 });
72
73 it("stays out of the way off the chat route and under modal pages", () => {
74 const { router, navigate } = routerAt([{ routeId: "__root__", pathname: "/pair" }]);
75 using stack = new DisposableStack();
76 stack.defer(installChatRouteCommands(router));
77 stack.defer(() => useThreadSelectionStore.getState().clearSelection());
78 selectThread("env:thread-1");
79
80 expect(pressEscape().defaultPrevented).toBe(false);
81
82 navigate([...CHAT, { routeId: "/_chat/settings", pathname: "/settings" }]);
83 expect(pressEscape().defaultPrevented).toBe(false);
84 expect(useThreadSelectionStore.getState().selectedThreadKeys.size).toBe(1);
85
86 navigate(CHAT);
87 expect(pressEscape().defaultPrevented).toBe(true);
88 });
89
90 it("leaves zoom keys to the app unless the focused right panel shows a browser", () => {
91 const { router } = routerAt(CHAT);
92 using stack = new DisposableStack();
93 stack.defer(installChatRouteCommands(router));
94 const panel = document.createElement("div");
95 panel.setAttribute("data-preview-panel-mode", "inline");
96 const pullRequestTab = document.createElement("button");
97 panel.append(pullRequestTab);
98 document.body.append(panel);
99 stack.defer(() => panel.remove());
100 pullRequestTab.focus();
101
102 for (const key of ["=", "+", "-", "0"]) expect(pressMod(key).defaultPrevented).toBe(false);
103
104 const browser = document.createElement("div");
105 browser.setAttribute("data-preview-view", "");
106 panel.append(browser);
107 for (const key of ["=", "+", "-", "0"]) expect(pressMod(key).defaultPrevented).toBe(true);
108 });
109
110 it("leaves Find unclaimed on Diff but routes it to Files and Browser on a thread", () => {
111 const { router } = routerAt([
112 ...CHAT,
113 {
114 routeId: "/_chat/$environmentId/$threadId",
115 pathname: "/local/thread-1",
116 params: { environmentId: "local", threadId: "thread-1" },
117 },
118 ]);
119 using _scope = vi.spyOn(session, "readEnvironmentScope").mockReturnValue(true);
120 using stack = new DisposableStack();
121 stack.defer(installChatRouteCommands(router));
122 const actions: PreviewAction[] = [];
123 stack.defer(subscribePreviewAction((action) => actions.push(action)));
124 const panel = document.createElement("div");
125 panel.setAttribute("data-preview-panel-mode", "inline");
126 const tab = document.createElement("button");
127 tab.textContent = "Diff";
128 const files = document.createElement("div");
129 files.setAttribute("data-panel-find-responder", "");
130 files.hidden = true;
131 panel.append(tab, files);
132 document.body.append(panel);
133 stack.defer(() => panel.remove());
134 tab.focus();
135
136 expect(pressMod("f").defaultPrevented).toBe(false);
137 expect(actions).toEqual([]);
138
139 tab.textContent = "Files";
140 files.hidden = false;
141 expect(pressMod("f").defaultPrevented).toBe(true);
142 expect(actions.splice(0)).toEqual(["find"]);
143
144 files.hidden = true;
145 const browser = document.createElement("div");
146 browser.setAttribute("data-preview-view", "");
147 browser.tabIndex = 0;
148 panel.append(browser);
149 browser.focus();
150
151 expect(pressMod("f").defaultPrevented).toBe(true);
152 expect(actions).toEqual(["find"]);
153 });
154});