Open History to see the last commit
1import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6
7import { act, render } from "../../test/render";
8import { CommandPalette } from "./CommandPalette";
9import { retainThreadOverlaysForThread } from "./chat/threadOverlayRetention";
10import { toastManager } from "./ui/toast";
11import { projectEnvironment } from "../state/projects";
12import { openThreadWorkspaceDialog } from "../state/threadWorkspace";
13
14const scopes = vi.hoisted(() => ({
15 moveWorkspace: vi.fn(),
16}));
17
18vi.mock("../state/use-atom-command", () => ({
19 useAtomCommand: (command: unknown) =>
20 command === projectEnvironment.moveThreadWorkspace
21 ? scopes.moveWorkspace
22 : async () => ({ _tag: "Success", value: undefined }),
23}));
24vi.mock("../state/environments", async (original) => ({
25 ...(await original<typeof import("../state/environments")>()),
26 useEnvironments: () => ({
27 environments: [
28 {
29 environmentId: "local",
30 label: "Local",
31 connection: { phase: "connected" },
32 entry: { target: { _tag: "PrimaryConnectionTarget" } },
33 serverConfig: {
34 environment: { platform: { os: "linux" }, capabilities: {} },
35 settings: { addProjectBaseDirectory: "/workspace/" },
36 },
37 },
38 ],
39 }),
40 usePrimaryEnvironmentId: () => "local",
41}));
42vi.mock("../state/session", async (original) => ({
43 ...(await original<typeof import("../state/session")>()),
44 useEnvironmentScope: () => true,
45 readEnvironmentScope: () => true,
46}));
47vi.mock("../state/filesystem", async (original) => ({
48 ...(await original<typeof import("../state/filesystem")>()),
49 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
50}));
51
52const threadA = scopeThreadRef(EnvironmentId.make("local"), ThreadId.make("thread"));
53const threadB = scopeThreadRef(EnvironmentId.make("local"), ThreadId.make("other-thread"));
54const folderField = () => page.getByPlaceholder("Choose a workspace folder");
55
56it("closes the picker when leaving its thread and reports a later save failure in a toast", async () => {
57 using toast = vi.spyOn(toastManager, "add");
58 const { promise, resolve } = Promise.withResolvers<unknown>();
59 scopes.moveWorkspace.mockReset().mockReturnValue(promise);
60 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
61 await act(() => openThreadWorkspaceDialog(threadA, "project-directory"));
62 await folderField().fill("/workspace/renamed");
63 await userEvent.keyboard("{Enter}");
64 expect(scopes.moveWorkspace).toHaveBeenCalledOnce();
65
66 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadB)));
67 await expect.element(folderField()).not.toBeInTheDocument();
68 await act(() => resolve({ _tag: "Failure", cause: Cause.fail(new Error("Folder is missing")) }));
69
70 expect(toast).toHaveBeenCalledWith(
71 expect.objectContaining({ type: "error", description: "Folder is missing" }),
72 );
73});