import { beforeEach, expect, it, vi } from "vitest"; import { IpcError } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { provideProjectTrustPrompt } from "@/platform/projectTrust"; import { useRemote } from "@/platform/remote"; const mocks = vi.hoisted(() => ({ open: vi.fn(), notify: vi.fn(), confirm: vi.fn(), checkRequirements: vi.fn(), readDir: vi.fn(), tauri: false, })); vi.mock("@/workbench/projectWindow", () => ({ openRemoteProject: mocks.open })); vi.mock("@/workbench/contributions", () => ({ notifications: () => ({ notify: mocks.notify }) })); vi.mock("@/workbench/messages", () => ({ confirm: mocks.confirm })); vi.mock("@/platform/ipc", async (original) => { const actual = await original(); return { ...actual, isTauri: () => mocks.tauri, commands: { ...actual.commands, remoteDevcontainerCheckRequirements: mocks.checkRequirements, fsReadDir: mocks.readDir, }, }; }); import { installDevContainerNotifications, openDevContainer, reopenInContainer } from "./container"; const project = (root: string, trusted = true) => ({ root, name: "app", trusted }) as never; beforeEach(() => { vi.clearAllMocks(); mocks.tauri = false; useRemote.setState({ session: null }); useProject.setState({ project: project("/Users/me/app") }); }); it("opens the local project's dev container", async () => { mocks.open.mockResolvedValue(true); await reopenInContainer(); expect(mocks.open).toHaveBeenCalledWith( { kind: "devContainer", folder: "/Users/me/app", config: "" }, "/Users/me/app", ); expect(mocks.notify).not.toHaveBeenCalled(); }); it("requires trust before checking requirements or starting the container", async () => { mocks.tauri = true; const trust = vi.fn().mockResolvedValue(false); const dispose = provideProjectTrustPrompt(trust); try { await expect(openDevContainer("/unknown", ".devcontainer/special/devcontainer.json")).rejects.toMatchObject({ kind: "cancelled", }); expect(trust).toHaveBeenCalledWith("/unknown", "local", true); expect(mocks.checkRequirements).not.toHaveBeenCalled(); expect(mocks.open).not.toHaveBeenCalled(); } finally { dispose(); } }); it("opens the configuration from the editor and ignores unrelated paths", async () => { const root = "/Users/me/app"; const config = ".devcontainer/python/devcontainer.json"; mocks.readDir.mockResolvedValue({ status: "ok", data: [] }); await reopenInContainer(`${root}/example/devcontainer.json`); expect(mocks.open).not.toHaveBeenCalled(); await reopenInContainer(`${root}/${config}`); expect(mocks.open).toHaveBeenCalledWith({ kind: "devContainer", folder: root, config }, root); }); it("offers a trusted project once and remembers Don't ask again", async () => { const root = "/container-notifications"; const preference = `openidea.devContainer.ignore:${root}`; localStorage.removeItem(preference); useProject.setState({ project: project(root, false) }); mocks.readDir.mockResolvedValue({ status: "ok", data: [{ name: ".devcontainer.json", kind: "file" }] }); const dispose = installDevContainerNotifications(); try { expect(mocks.readDir).not.toHaveBeenCalled(); useProject.setState({ project: project(root) }); await vi.waitFor(() => expect(mocks.notify).toHaveBeenCalledTimes(1)); useProject.setState({ project: null }); useProject.setState({ project: project(root) }); expect(mocks.readDir).toHaveBeenCalledTimes(1); mocks.notify.mock.calls[0]![0].actions[1].run(); expect(localStorage.getItem(preference)).toBe("1"); } finally { dispose(); } const restart = installDevContainerNotifications(); try { expect(mocks.readDir).toHaveBeenCalledTimes(1); } finally { restart(); localStorage.removeItem(preference); } }); it("does nothing in a remote window or without a project", async () => { useRemote.setState({ session: { target: { kind: "ssh", config: "a" }, root: "/srv/app", host: "a", os: "linux", commands: [] }, }); await reopenInContainer(); useRemote.setState({ session: null }); useProject.setState({ project: null }); await reopenInContainer(); expect(mocks.open).not.toHaveBeenCalled(); }); it("reports why the container could not be opened, but not a cancel", async () => { mocks.open.mockRejectedValueOnce(new Error("The Dev Containers CLI is not installed.")); await reopenInContainer(); expect(mocks.notify).toHaveBeenCalledWith( expect.objectContaining({ level: "error", body: "The Dev Containers CLI is not installed." }), ); mocks.notify.mockClear(); mocks.open.mockRejectedValueOnce(new IpcError({ kind: "cancelled", message: "Project opening cancelled" })); await reopenInContainer(); expect(mocks.notify).not.toHaveBeenCalled(); }); it("warns before building when hostRequirements fall short and respects cancellation", async () => { mocks.tauri = true; mocks.checkRequirements.mockResolvedValue({ status: "ok", data: { met: false, configPath: "/Users/me/app/.devcontainer/devcontainer.json", requirements: { cpus: 8, memoryBytes: 16 * 1024 ** 3, storageBytes: null }, environment: { hostCpus: 12, hostMemoryBytes: 32 * 1024 ** 3, hostStorageAvailableBytes: 100 * 1024 ** 3, dockerCpus: 4, dockerMemoryBytes: 8 * 1024 ** 3, dockerOs: "Docker Desktop", }, shortfalls: [ { resource: "cpus", source: "dockerVm", required: "8 CPUs", available: "4 CPUs", message: "Requires 8 CPUs, but the Docker VM has 4 CPUs (this machine has 12 CPUs).", }, ], }, }); mocks.confirm.mockResolvedValueOnce(false); await reopenInContainer(); expect(mocks.confirm).toHaveBeenCalledWith( expect.objectContaining({ title: "Host Requirements Not Met", message: expect.stringContaining("Docker VM has 4 CPUs"), ok: "Continue Anyway", }), ); expect(mocks.open).not.toHaveBeenCalled(); mocks.confirm.mockResolvedValueOnce(true); mocks.open.mockResolvedValueOnce(true); await reopenInContainer(); expect(mocks.open).toHaveBeenCalledOnce(); });