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

Sidebar.browser.test.tsx

87 lines3.2 KB

1import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4​
5import { render } from "../../test/render";
6import { openCommandPalette } from "../commandPaletteBus";
7import { useHandleNewThread } from "../hooks/useHandleNewThread";
8import { __setClientSettingsForTests, getClientSettings } from "../hooks/useSettings";
9import { useProjects } from "../state/entities";
10import type { Project } from "../types";
11import Sidebar from "./Sidebar";
12import { SidebarProvider } from "./ui/sidebar";
13​
14vi.mock("../state/entities", { spy: true });
15vi.mock("../hooks/useHandleNewThread", { spy: true });
16vi.mock("../commandPaletteBus", () => ({
17 openCommandPalette: vi.fn(),
18 isCommandPaletteOpen: () => false,
19}));
20vi.mock("./sidebar/SidebarChrome", () => ({
21 SidebarChromeHeader: () => null,
22 SidebarChromeFooter: () => null,
23}));
24​
25function project(id: string): Project {
26 return {
27 id: ProjectId.make(id),
28 environmentId: EnvironmentId.make("local"),
29 title: id,
30 workspaceRoot: `/projects/${id}`,
31 repositoryIdentity: null,
32 defaultModelSelection: null,
33 createdAt: "2026-01-01T00:00:00.000Z",
34 updatedAt: "2026-01-01T00:00:00.000Z",
35 scripts: [],
36 };
38​
39describe("sidebar header new thread", () => {
40 it.each([
41 { mode: "normal", count: 2, shift: false, picker: true },
42 { mode: "boring", count: 2, shift: false, picker: true },
43 { mode: "boring", count: 2, shift: true, picker: false },
44 { mode: "boring", count: 1, shift: false, picker: false },
45 ] as const)("$mode sidebar, $count projects, shift=$shift: picker=$picker", async (scenario) => {
46 using stack = new DisposableStack();
47 const previousSettings = getClientSettings();
48 stack.defer(() => __setClientSettingsForTests(previousSettings));
49 __setClientSettingsForTests({
50 ...DEFAULT_CLIENT_SETTINGS,
51 boringSidebarEnabled: scenario.mode === "boring",
52 });
53 const projects = [project("active"), project("idle")].slice(0, scenario.count);
54 const currentProjectRef = {
55 environmentId: projects[0]!.environmentId,
56 projectId: projects[0]!.id,
57 };
58 const startThread = vi.fn().mockResolvedValue(null);
59 vi.mocked(useProjects).mockReturnValue(projects);
60 vi.mocked(useHandleNewThread).mockReturnValue({
61 activeDraftThread: null,
62 activeThread: null,
63 defaultProjectRef: currentProjectRef,
64 handleNewThread: startThread,
65 routeDraftId: null,
66 routeThreadRef: null,
67 });
68 vi.mocked(openCommandPalette).mockClear();
69 await using screen = await render(
70 <SidebarProvider>
71 <Sidebar routeReveal={{ observedKey: null, pendingKey: null }} />
72 </SidebarProvider>,
73 );
74​
75 await userEvent.click(screen.getByRole("button", { name: "New thread", exact: true }), {
76 modifiers: scenario.shift ? ["Shift"] : [],
77 });
78​
79 if (scenario.picker) {
80 expect(openCommandPalette).toHaveBeenCalledExactlyOnceWith({ open: "new-thread-in" });
81 expect(startThread).not.toHaveBeenCalled();
82 } else {
83 expect(openCommandPalette).not.toHaveBeenCalled();
84 expect(startThread).toHaveBeenCalledExactlyOnceWith(currentProjectRef);
85 }
86 });
87});