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

CommandPalette.browser.test.tsx

177 lines6.5 KB

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