|
| 1 | +import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | +import { userEvent } from "vite-plus/test/browser"; |
| 4 | + |
| 5 | +import { render } from "../../test/render"; |
| 6 | +import { openCommandPalette } from "../commandPaletteBus"; |
| 7 | +import { useHandleNewThread } from "../hooks/useHandleNewThread"; |
| 8 | +import { __setClientSettingsForTests, getClientSettings } from "../hooks/useSettings"; |
| 9 | +import { useProjects } from "../state/entities"; |
| 10 | +import type { Project } from "../types"; |
| 11 | +import Sidebar from "./Sidebar"; |
| 12 | +import { SidebarProvider } from "./ui/sidebar"; |
| 13 | + |
| 14 | +vi.mock("../state/entities", { spy: true }); |
| 15 | +vi.mock("../hooks/useHandleNewThread", { spy: true }); |
| 16 | +vi.mock("../commandPaletteBus", () => ({ |
| 17 | + openCommandPalette: vi.fn(), |
| 18 | + isCommandPaletteOpen: () => false, |
| 19 | +})); |
| 20 | +vi.mock("./sidebar/SidebarChrome", () => ({ |
| 21 | + SidebarChromeHeader: () => null, |
| 22 | + SidebarChromeFooter: () => null, |
| 23 | +})); |
| 24 | + |
| 25 | +function 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 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +describe("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 | + { mode: "legacy", count: 2, shift: false, picker: false }, |
| 46 | + ] as const)("$mode sidebar, $count projects, shift=$shift: picker=$picker", async (scenario) => { |
| 47 | + using stack = new DisposableStack(); |
| 48 | + const previousSettings = getClientSettings(); |
| 49 | + stack.defer(() => __setClientSettingsForTests(previousSettings)); |
| 50 | + __setClientSettingsForTests({ |
| 51 | + ...DEFAULT_CLIENT_SETTINGS, |
| 52 | + boringSidebarEnabled: scenario.mode === "boring", |
| 53 | + legacySidebarEnabled: scenario.mode === "legacy", |
| 54 | + }); |
| 55 | + const projects = [project("active"), project("idle")].slice(0, scenario.count); |
| 56 | + const currentProjectRef = { |
| 57 | + environmentId: projects[0]!.environmentId, |
| 58 | + projectId: projects[0]!.id, |
| 59 | + }; |
| 60 | + const startThread = vi.fn().mockResolvedValue(null); |
| 61 | + vi.mocked(useProjects).mockReturnValue(projects); |
| 62 | + vi.mocked(useHandleNewThread).mockReturnValue({ |
| 63 | + activeDraftThread: null, |
| 64 | + activeThread: null, |
| 65 | + defaultProjectRef: currentProjectRef, |
| 66 | + handleNewThread: startThread, |
| 67 | + routeDraftId: null, |
| 68 | + routeThreadRef: null, |
| 69 | + }); |
| 70 | + vi.mocked(openCommandPalette).mockClear(); |
| 71 | + await using screen = await render( |
| 72 | + <SidebarProvider> |
| 73 | + <Sidebar routeReveal={{ observedKey: null, pendingKey: null }} /> |
| 74 | + </SidebarProvider>, |
| 75 | + ); |
| 76 | + |
| 77 | + await userEvent.click(screen.getByRole("button", { name: "New thread", exact: true }), { |
| 78 | + modifiers: scenario.shift ? ["Shift"] : [], |
| 79 | + }); |
| 80 | + |
| 81 | + if (scenario.picker) { |
| 82 | + expect(openCommandPalette).toHaveBeenCalledExactlyOnceWith({ open: "new-thread-in" }); |
| 83 | + expect(startThread).not.toHaveBeenCalled(); |
| 84 | + } else { |
| 85 | + expect(openCommandPalette).not.toHaveBeenCalled(); |
| 86 | + expect(startThread).toHaveBeenCalledExactlyOnceWith(currentProjectRef); |
| 87 | + } |
| 88 | + }); |
| 89 | +}); |
0 commit comments