import { afterEach, beforeEach, expect, it, vi } from "vitest"; const ipc = vi.hoisted(() => ({ xcodeFileSync: vi.fn(), runApplySettings: vi.fn() })); const run = vi.hoisted(() => ({ startRun: vi.fn() })); vi.mock("@/platform/ipc", async (importOriginal) => ({ ...(await importOriginal()), isTauri: () => true, commands: ipc, })); vi.mock("@/platform/keymap", async (importOriginal) => ({ ...(await importOriginal()), isMacHost: () => true, })); vi.mock("./store", () => run); import { notifyFileOperation, notifyFilesMoved } from "@/platform/fileOps"; import type { RunConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { provideNotificationService } from "@/workbench/contributions"; import { registerXcodeFiles } from "./xcodeFiles"; const ok = (data: T) => Promise.resolve({ status: "ok" as const, data }); const notify = vi.fn(); provideNotificationService({ notify, expire: vi.fn() }); const act = (label: string) => notify.mock.calls .at(-1)![0] .actions.find((a: { label: string }) => a.label === label) .run(); let dispose: () => void; beforeEach(() => { vi.useFakeTimers(); useProject.setState({ project: { root: "/p", trusted: true } as never }); ipc.runApplySettings.mockImplementation((configs: RunConfig[]) => ok(configs)); run.startRun.mockResolvedValue(null); dispose = registerXcodeFiles(); }); afterEach(() => { dispose(); vi.useRealTimers(); vi.clearAllMocks(); useProject.setState({ project: null }); }); it("checks a burst of file operations once and names the files a classic project misses", async () => { ipc.xcodeFileSync.mockReturnValue(ok({ kind: "notInProject", project: "/p/App.xcodeproj", files: ["New.swift"] })); notifyFileOperation({ kind: "created", paths: ["/p/App/New.swift"] }); notifyFileOperation({ kind: "deleted", paths: ["/p/App/Old.swift"] }); await notifyFilesMoved([{ from: "/p/App/A.swift", to: "/p/App/B.swift" }]); await vi.advanceTimersByTimeAsync(1000); expect(ipc.xcodeFileSync).toHaveBeenCalledTimes(1); expect(ipc.xcodeFileSync).toHaveBeenCalledWith( ["/p/App/New.swift", "/p/App/B.swift"], ["/p/App/Old.swift", "/p/App/A.swift"], ); expect(notify).toHaveBeenCalledTimes(1); expect(notify.mock.calls[0]![0].title).toBe("New.swift isn't in the Xcode project"); act("Open in Xcode"); await vi.waitFor(() => expect(run.startRun).toHaveBeenCalled()); expect(run.startRun.mock.calls[0]![0]).toMatchObject({ command: "open", args: ["-a", "Xcode", "App.xcodeproj"], cwd: "/p", }); }); it("offers to regenerate a generated project", async () => { ipc.xcodeFileSync.mockReturnValue(ok({ kind: "generated", dir: "/p", tool: "tuist" })); notifyFileOperation({ kind: "created", paths: ["/p/Sources/New.swift"] }); await vi.advanceTimersByTimeAsync(1000); expect(notify.mock.calls[0]![0].body).toBe( "Files changed in a project generated by tuist. Run 'tuist generate --no-open' so Xcode builds them.", ); act("Regenerate"); await vi.waitFor(() => expect(run.startRun).toHaveBeenCalled()); expect(run.startRun.mock.calls[0]![0]).toMatchObject({ command: "tuist", args: ["generate", "--no-open"], cwd: "/p", }); }); it("stays quiet when the project picks the files up itself", async () => { ipc.xcodeFileSync.mockReturnValue(ok(null)); notifyFileOperation({ kind: "created", paths: ["/p/App/New.swift"] }); await vi.advanceTimersByTimeAsync(1000); expect(ipc.xcodeFileSync).toHaveBeenCalled(); expect(notify).not.toHaveBeenCalled(); });