|
| 1 | +import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | +import type { AnyRouter } from "@tanstack/react-router"; |
| 3 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import * as commandPalette from "../commandPaletteBus"; |
| 6 | +import * as settings from "../hooks/useSettings"; |
| 7 | +import * as threadActions from "../lib/chatThreadActions"; |
| 8 | +import * as entities from "../state/entities"; |
| 9 | +import type { Project } from "../types"; |
| 10 | +import { installChatRouteCommands } from "./chatRouteCommands"; |
| 11 | +import * as dispatcher from "./dispatcher"; |
| 12 | + |
| 13 | +function project(id: string): Project { |
| 14 | + return { |
| 15 | + id: ProjectId.make(id), |
| 16 | + environmentId: EnvironmentId.make("local"), |
| 17 | + title: id, |
| 18 | + workspaceRoot: `/projects/${id}`, |
| 19 | + repositoryIdentity: null, |
| 20 | + defaultModelSelection: null, |
| 21 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 22 | + updatedAt: "2026-01-01T00:00:00.000Z", |
| 23 | + scripts: [], |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +describe("new thread commands", () => { |
| 28 | + it.each([ |
| 29 | + { mode: "normal", count: 2, command: "chat.new", picker: true }, |
| 30 | + { mode: "boring", count: 2, command: "chat.new", picker: true }, |
| 31 | + { mode: "boring", count: 1, command: "chat.new", picker: false }, |
| 32 | + { mode: "legacy", count: 2, command: "chat.new", picker: false }, |
| 33 | + { mode: "boring", count: 2, command: "chat.newLocal", picker: false }, |
| 34 | + ] as const)("$mode sidebar, $count projects, $command: picker=$picker", (scenario) => { |
| 35 | + using _settings = vi.spyOn(settings, "getClientSettings").mockReturnValue({ |
| 36 | + ...DEFAULT_CLIENT_SETTINGS, |
| 37 | + boringSidebarEnabled: scenario.mode === "boring", |
| 38 | + legacySidebarEnabled: scenario.mode === "legacy", |
| 39 | + }); |
| 40 | + using _projects = vi |
| 41 | + .spyOn(entities, "readProjects") |
| 42 | + .mockReturnValue([project("active"), project("idle")].slice(0, scenario.count)); |
| 43 | + using openPicker = vi.spyOn(commandPalette, "openCommandPalette").mockImplementation(() => {}); |
| 44 | + using startLocal = vi.spyOn(threadActions, "startNewThreadFromContext").mockResolvedValue(true); |
| 45 | + using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {}); |
| 46 | + const router = { state: { matches: [] } } as unknown as AnyRouter; |
| 47 | + using stack = new DisposableStack(); |
| 48 | + stack.defer(installChatRouteCommands(router)); |
| 49 | + const commands = register.mock.calls[0]![0]; |
| 50 | + const event = { |
| 51 | + preventDefault: vi.fn(), |
| 52 | + stopPropagation: vi.fn(), |
| 53 | + } as unknown as KeyboardEvent; |
| 54 | + |
| 55 | + commands[scenario.command]!(event); |
| 56 | + |
| 57 | + if (scenario.picker) { |
| 58 | + expect(openPicker).toHaveBeenCalledWith({ open: "new-thread-in" }); |
| 59 | + expect(startLocal).not.toHaveBeenCalled(); |
| 60 | + } else { |
| 61 | + expect(openPicker).not.toHaveBeenCalled(); |
| 62 | + expect(startLocal).toHaveBeenCalledOnce(); |
| 63 | + } |
| 64 | + }); |
| 65 | +}); |
0 commit comments