|
| 1 | +import { shouldRunIntegrationTests, createTestEnvironment, cleanupTestEnvironment } from "./setup"; |
| 2 | +import { IPC_CHANNELS } from "../../src/constants/ipc-constants"; |
| 3 | +import { |
| 4 | + createTempGitRepo, |
| 5 | + cleanupTempGitRepo, |
| 6 | + createWorkspace, |
| 7 | + generateBranchName, |
| 8 | + waitForFileNotExists, |
| 9 | +} from "./helpers"; |
| 10 | +import * as fs from "fs/promises"; |
| 11 | + |
| 12 | +// Skip all tests if TEST_INTEGRATION is not set |
| 13 | +const describeIntegration = shouldRunIntegrationTests() ? describe : describe.skip; |
| 14 | + |
| 15 | +describeIntegration("IpcMain remove workspace integration tests", () => { |
| 16 | + test.concurrent( |
| 17 | + "should successfully remove workspace and git worktree", |
| 18 | + async () => { |
| 19 | + const env = await createTestEnvironment(); |
| 20 | + const tempGitRepo = await createTempGitRepo(); |
| 21 | + |
| 22 | + try { |
| 23 | + const branchName = generateBranchName("remove-test"); |
| 24 | + |
| 25 | + // Create a workspace |
| 26 | + const createResult = await createWorkspace(env.mockIpcRenderer, tempGitRepo, branchName); |
| 27 | + expect(createResult.success).toBe(true); |
| 28 | + if (!createResult.success) { |
| 29 | + throw new Error("Failed to create workspace"); |
| 30 | + } |
| 31 | + |
| 32 | + const { metadata } = createResult; |
| 33 | + const workspacePath = metadata.workspacePath; |
| 34 | + |
| 35 | + // Verify the worktree exists |
| 36 | + const worktreeExistsBefore = await fs |
| 37 | + .access(workspacePath) |
| 38 | + .then(() => true) |
| 39 | + .catch(() => false); |
| 40 | + expect(worktreeExistsBefore).toBe(true); |
| 41 | + |
| 42 | + // Remove the workspace |
| 43 | + const removeResult = await env.mockIpcRenderer.invoke( |
| 44 | + IPC_CHANNELS.WORKSPACE_REMOVE, |
| 45 | + metadata.id |
| 46 | + ); |
| 47 | + expect(removeResult.success).toBe(true); |
| 48 | + |
| 49 | + // Verify the worktree no longer exists |
| 50 | + const worktreeRemoved = await waitForFileNotExists(workspacePath, 5000); |
| 51 | + expect(worktreeRemoved).toBe(true); |
| 52 | + |
| 53 | + // Verify workspace is no longer in config |
| 54 | + const config = env.config.loadConfigOrDefault(); |
| 55 | + const project = config.projects.get(tempGitRepo); |
| 56 | + if (project) { |
| 57 | + const workspaceStillInConfig = project.workspaces.some((w) => w.path === workspacePath); |
| 58 | + expect(workspaceStillInConfig).toBe(false); |
| 59 | + } |
| 60 | + } finally { |
| 61 | + await cleanupTestEnvironment(env); |
| 62 | + await cleanupTempGitRepo(tempGitRepo); |
| 63 | + } |
| 64 | + }, |
| 65 | + 15000 |
| 66 | + ); |
| 67 | + |
| 68 | + test.concurrent( |
| 69 | + "should handle removal of non-existent workspace gracefully", |
| 70 | + async () => { |
| 71 | + const env = await createTestEnvironment(); |
| 72 | + |
| 73 | + try { |
| 74 | + // Try to remove a workspace that doesn't exist |
| 75 | + const removeResult = await env.mockIpcRenderer.invoke( |
| 76 | + IPC_CHANNELS.WORKSPACE_REMOVE, |
| 77 | + "non-existent-workspace-id" |
| 78 | + ); |
| 79 | + |
| 80 | + // Should succeed (idempotent operation) |
| 81 | + expect(removeResult.success).toBe(true); |
| 82 | + } finally { |
| 83 | + await cleanupTestEnvironment(env); |
| 84 | + } |
| 85 | + }, |
| 86 | + 15000 |
| 87 | + ); |
| 88 | + |
| 89 | + test.concurrent( |
| 90 | + "should handle removal when worktree directory is already deleted", |
| 91 | + async () => { |
| 92 | + const env = await createTestEnvironment(); |
| 93 | + const tempGitRepo = await createTempGitRepo(); |
| 94 | + |
| 95 | + try { |
| 96 | + const branchName = generateBranchName("remove-deleted"); |
| 97 | + |
| 98 | + // Create a workspace |
| 99 | + const createResult = await createWorkspace(env.mockIpcRenderer, tempGitRepo, branchName); |
| 100 | + expect(createResult.success).toBe(true); |
| 101 | + if (!createResult.success) { |
| 102 | + throw new Error("Failed to create workspace"); |
| 103 | + } |
| 104 | + |
| 105 | + const { metadata } = createResult; |
| 106 | + const workspacePath = metadata.workspacePath; |
| 107 | + |
| 108 | + // Manually delete the worktree directory (simulating external deletion) |
| 109 | + await fs.rm(workspacePath, { recursive: true, force: true }); |
| 110 | + |
| 111 | + // Verify it's gone |
| 112 | + const worktreeExists = await fs |
| 113 | + .access(workspacePath) |
| 114 | + .then(() => true) |
| 115 | + .catch(() => false); |
| 116 | + expect(worktreeExists).toBe(false); |
| 117 | + |
| 118 | + // Remove the workspace via IPC - should succeed and prune stale worktree |
| 119 | + const removeResult = await env.mockIpcRenderer.invoke( |
| 120 | + IPC_CHANNELS.WORKSPACE_REMOVE, |
| 121 | + metadata.id |
| 122 | + ); |
| 123 | + expect(removeResult.success).toBe(true); |
| 124 | + |
| 125 | + // Verify workspace is no longer in config |
| 126 | + const config = env.config.loadConfigOrDefault(); |
| 127 | + const project = config.projects.get(tempGitRepo); |
| 128 | + if (project) { |
| 129 | + const workspaceStillInConfig = project.workspaces.some((w) => w.path === workspacePath); |
| 130 | + expect(workspaceStillInConfig).toBe(false); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + await cleanupTestEnvironment(env); |
| 134 | + await cleanupTempGitRepo(tempGitRepo); |
| 135 | + } |
| 136 | + }, |
| 137 | + 15000 |
| 138 | + ); |
| 139 | +}); |
0 commit comments