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

CommandPalette.browser.test.tsx

127 lines4.6 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 { 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("opens New thread in... with focus in the search input, not the Back button", async () => {
30 setup();
31 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
32​
33 openCommandPalette({ open: "new-thread-in" });
34​
35 await expect.element(page.getByPlaceholder("Search...")).toHaveFocus();
36 });
37​
38 it("highlights the first project when New thread in... opens from the root list", async () => {
39 const environmentId = EnvironmentId.make("local");
40 const project = (id: string, title: string): Project => ({
41 id: ProjectId.make(id),
42 environmentId,
43 title,
44 workspaceRoot: `/work/${id}`,
45 repositoryIdentity: null,
46 defaultModelSelection: null,
47 createdAt: "2026-03-01T00:00:00.000Z",
48 updatedAt: "2026-03-01T00:00:00.000Z",
49 scripts: [],
50 });
51 setup({
52 projects: [project("acme", "Acme"), project("beta", "Beta"), project("gamma", "Gamma")],
53 });
54 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
55​
56 openCommandPalette({});
57 await expect
58 .element(page.getByPlaceholder("Search commands, projects, and threads..."))
59 .toHaveFocus();
60 await userEvent.keyboard("{ArrowDown}");
61 await expect
62 .element(page.getByRole("option", { name: /^New thread in\.\.\./ }))
63 .toHaveAttribute("data-highlighted");
64 await userEvent.keyboard("{Enter}");
65​
66 await expect.element(page.getByPlaceholder("Search...")).toHaveFocus();
67 await expect.element(page.getByRole("option").first()).toHaveAttribute("data-highlighted");
68 await expect.element(page.getByRole("option").nth(1)).not.toHaveAttribute("data-highlighted");
69 });
70​
71 it("opens a search intent with its query in the focused input", async () => {
72 setup();
73 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
74​
75 openCommandPalette({ query: "acme" });
76​
77 const input = page.getByPlaceholder("Search commands, projects, and threads...");
78 await expect.element(input).toHaveFocus();
79 await expect.element(input).toHaveValue("acme");
80 });
81​
82 it("opens a searched project's latest listed thread, not a newer subagent", async () => {
83 const environmentId = EnvironmentId.make("local");
84 const projectId = ProjectId.make("acme");
85 const parentId = ThreadId.make("parent");
86 setup({
87 projects: [
88 {
89 id: projectId,
90 environmentId,
91 title: "Acme",
92 workspaceRoot: "/work/acme",
93 repositoryIdentity: null,
94 defaultModelSelection: null,
95 createdAt: "2026-03-01T00:00:00.000Z",
96 updatedAt: "2026-03-01T00:00:00.000Z",
97 scripts: [],
98 },
99 ],
100 threads: [
101 makeThreadFixture({
102 id: parentId,
103 environmentId,
104 projectId,
105 latestUserMessageAt: "2026-03-02T00:00:00.000Z",
106 }),
107 makeThreadFixture({
108 id: ThreadId.make("subagent"),
109 environmentId,
110 projectId,
111 updatedAt: "2026-03-03T00:00:00.000Z",
112 lineage: {
113 rootThreadId: parentId,
114 parentThreadId: parentId,
115 relationshipToParent: "subagent",
116 },
117 }),
118 ],
119 });
120 await using screen = await render(<CommandPalette>{null}</CommandPalette>);
121​
122 openCommandPalette({ query: "Acme" });
123 await page.getByRole("option", { name: /Acme/ }).first().click();
124​
125 await vi.waitFor(() => expect(screen.router?.state.location.pathname).toBe("/local/parent"));
126 });
127});