Open History to see the last commit
1import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4
5import { act, render } from "../../test/render";
6import { openCommandPalette } from "../commandPaletteBus";
7import { useCommandPaletteStore } from "../commandPaletteStore";
8import { LAST_USED_PROJECT_STORAGE_KEY } from "../hooks/useHandleNewThread";
9import { removeLocalStorageItem } from "../hooks/useLocalStorage";
10import { makeThreadFixture } from "../test-fixtures";
11import type { Project, Thread } from "../types";
12import { CommandPalette } from "./CommandPalette";
13
14const entities = vi.hoisted(() => ({
15 projects: [] as ReadonlyArray<Project>,
16 threads: [] as ReadonlyArray<Thread>,
17}));
18vi.mock("../state/entities", async (original) => ({
19 ...(await original<typeof import("../state/entities")>()),
20 useProjects: () => entities.projects,
21 useVisibleThreadShells: () => entities.threads,
22}));
23
24function setup(state: { projects?: ReadonlyArray<Project>; threads?: ReadonlyArray<Thread> } = {}) {
25 // Earlier browser test files share this origin's localStorage; a remembered project
26 // that is not listed here hides the default "New thread in <project>" row.
27 removeLocalStorageItem(LAST_USED_PROJECT_STORAGE_KEY);
28 useCommandPaletteStore.setState(useCommandPaletteStore.getInitialState(), true);
29 entities.projects = state.projects ?? [];
30 entities.threads = state.threads ?? [];
31}
32
33describe("CommandPalette open intents", () => {
34 it("drags a searched thread into the current composer without navigating", async () => {
35 const environmentId = EnvironmentId.make("remote");
36 const threadId = ThreadId.make("dragged-thread");
37 setup({
38 threads: [makeThreadFixture({ environmentId, id: threadId, title: "Drag this thread" })],
39 });
40 const onDrop = vi.fn();
41 await using screen = await render(
42 <CommandPalette>
43 <div
44 data-thread-context-drop="true"
45 ref={(node) => {
46 node?.addEventListener("t3-thread-context-drop", onDrop);
47 }}
48 style={{ position: "fixed", bottom: 0, width: 300, height: 100 }}
49 >
50 Composer
51 </div>
52 </CommandPalette>,
53 );
54 const pathname = screen.router?.state.location.pathname;
55 openCommandPalette({ query: "Drag this thread" });
56 const row = page.getByRole("option", { name: /Drag this thread/ });
57 await expect.element(row).toBeVisible();
58 const bounds = row.element().getBoundingClientRect();
59 const pointer = (type: string, x: number, y: number) =>
60 new PointerEvent(type, {
61 bubbles: true,
62 cancelable: true,
63 pointerId: 1,
64 isPrimary: true,
65 button: 0,
66 buttons: type === "pointerup" ? 0 : 1,
67 clientX: x,
68 clientY: y,
69 });
70 await act(() => {
71 row.element().dispatchEvent(pointer("pointerdown", bounds.x + 20, bounds.y + 10));
72 document.dispatchEvent(pointer("pointermove", bounds.x + 30, bounds.y + 20));
73 });
74 expect(useCommandPaletteStore.getState().open).toBe(false);
75 await act(() => {
76 document.dispatchEvent(pointer("pointermove", 50, window.innerHeight - 30));
77 document.dispatchEvent(pointer("pointerup", 50, window.innerHeight - 30));
78 });
79 expect(onDrop).toHaveBeenCalledOnce();
80 expect((onDrop.mock.calls[0]![0] as CustomEvent).detail).toEqual([{ environmentId, threadId }]);
81 expect(screen.router?.state.location.pathname).toBe(pathname);
82 });
83
84 it("opens New thread in... with focus in the search input, not the Back button", async () => {
85 setup();
86 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
87
88 openCommandPalette({ open: "new-thread-in" });
89
90 await expect.element(page.getByPlaceholder("Search...")).toHaveFocus();
91 });
92
93 it("highlights the first project when New thread in... opens from the root list", async () => {
94 const environmentId = EnvironmentId.make("local");
95 const project = (id: string, title: string): Project => ({
96 id: ProjectId.make(id),
97 environmentId,
98 title,
99 workspaceRoot: `/work/${id}`,
100 repositoryIdentity: null,
101 defaultModelSelection: null,
102 createdAt: "2026-03-01T00:00:00.000Z",
103 updatedAt: "2026-03-01T00:00:00.000Z",
104 scripts: [],
105 });
106 setup({
107 projects: [project("acme", "Acme"), project("beta", "Beta"), project("gamma", "Gamma")],
108 });
109 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
110
111 openCommandPalette({});
112 await expect
113 .element(page.getByPlaceholder("Search commands, projects, and threads..."))
114 .toHaveFocus();
115 await userEvent.keyboard("{ArrowDown}");
116 await expect
117 .element(page.getByRole("option", { name: /^New thread in\.\.\./ }))
118 .toHaveAttribute("data-highlighted");
119 await userEvent.keyboard("{Enter}");
120
121 await expect.element(page.getByPlaceholder("Search...")).toHaveFocus();
122 await expect.element(page.getByRole("option").first()).toHaveAttribute("data-highlighted");
123 await expect.element(page.getByRole("option").nth(1)).not.toHaveAttribute("data-highlighted");
124 });
125
126 it("opens a search intent with its query in the focused input", async () => {
127 setup();
128 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
129
130 openCommandPalette({ query: "acme" });
131
132 const input = page.getByPlaceholder("Search commands, projects, and threads...");
133 await expect.element(input).toHaveFocus();
134 await expect.element(input).toHaveValue("acme");
135 });
136
137 it("opens a searched project's latest listed thread, not a newer subagent", async () => {
138 const environmentId = EnvironmentId.make("local");
139 const projectId = ProjectId.make("acme");
140 const parentId = ThreadId.make("parent");
141 setup({
142 projects: [
143 {
144 id: projectId,
145 environmentId,
146 title: "Acme",
147 workspaceRoot: "/work/acme",
148 repositoryIdentity: null,
149 defaultModelSelection: null,
150 createdAt: "2026-03-01T00:00:00.000Z",
151 updatedAt: "2026-03-01T00:00:00.000Z",
152 scripts: [],
153 },
154 ],
155 threads: [
156 makeThreadFixture({
157 id: parentId,
158 environmentId,
159 projectId,
160 latestUserMessageAt: "2026-03-02T00:00:00.000Z",
161 }),
162 makeThreadFixture({
163 id: ThreadId.make("subagent"),
164 environmentId,
165 projectId,
166 updatedAt: "2026-03-03T00:00:00.000Z",
167 lineage: {
168 rootThreadId: parentId,
169 parentThreadId: parentId,
170 relationshipToParent: "subagent",
171 },
172 }),
173 ],
174 });
175 await using screen = await render(<CommandPalette>{null}</CommandPalette>);
176
177 openCommandPalette({ query: "Acme" });
178 await page.getByRole("option", { name: /Acme/ }).first().click();
179
180 await vi.waitFor(() => expect(screen.router?.state.location.pathname).toBe("/local/parent"));
181 });
182});