|
1 | | -/* eslint-disable local/no-sync-fs-methods -- Test file uses sync fs for simplicity */ |
2 | | -import { describe, test, beforeEach, afterEach } from "bun:test"; |
| 1 | +import { describe, test } from "bun:test"; |
3 | 2 | import { ExtensionManager } from "./extensionManager"; |
4 | 3 | import type { WorkspaceMetadata } from "@/types/workspace"; |
5 | 4 | import type { RuntimeConfig } from "@/types/runtime"; |
6 | | -import * as fs from "fs"; |
| 5 | +import * as fs from "fs/promises"; |
7 | 6 | import * as path from "path"; |
8 | 7 | import * as os from "os"; |
9 | 8 |
|
10 | | - |
11 | | -describe("ExtensionManager", () => { |
12 | | - let manager: ExtensionManager; |
13 | | - let tempDir: string; |
14 | | - let projectPath: string; |
15 | | - let workspaceMetadata: WorkspaceMetadata; |
16 | | - let runtimeConfig: RuntimeConfig; |
17 | | - |
18 | | - beforeEach(() => { |
19 | | - |
20 | | - // Create temp directory for test |
21 | | - tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "ext-mgr-test-")); |
22 | | - projectPath = path.join(tempDir, "project"); |
23 | | - fs.mkdirSync(projectPath, { recursive: true }); |
24 | | - |
25 | | - workspaceMetadata = { |
26 | | - id: "test-workspace", |
27 | | - name: "test-branch", |
28 | | - projectName: "test-project", |
29 | | - projectPath, |
30 | | - }; |
31 | | - |
32 | | - runtimeConfig = { |
33 | | - type: "local", |
34 | | - srcBaseDir: path.join(tempDir, "src"), |
35 | | - }; |
36 | | - |
37 | | - manager = new ExtensionManager(); |
38 | | - }); |
39 | | - |
40 | | - afterEach(() => { |
| 9 | +/** |
| 10 | + * Create a fresh test context with isolated temp directory and manager instance |
| 11 | + */ |
| 12 | +async function createTestContext() { |
| 13 | + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ext-mgr-test-")); |
| 14 | + const projectPath = path.join(tempDir, "project"); |
| 15 | + await fs.mkdir(projectPath, { recursive: true }); |
| 16 | + |
| 17 | + const workspaceMetadata: WorkspaceMetadata = { |
| 18 | + id: "test-workspace", |
| 19 | + name: "test-branch", |
| 20 | + projectName: "test-project", |
| 21 | + projectPath, |
| 22 | + }; |
| 23 | + |
| 24 | + const runtimeConfig: RuntimeConfig = { |
| 25 | + type: "local", |
| 26 | + srcBaseDir: path.join(tempDir, "src"), |
| 27 | + }; |
| 28 | + |
| 29 | + const manager = new ExtensionManager(); |
| 30 | + |
| 31 | + const cleanup = async () => { |
41 | 32 | manager.shutdown(); |
42 | | - if (fs.existsSync(tempDir)) { |
43 | | - fs.rmSync(tempDir, { recursive: true, force: true }); |
| 33 | + try { |
| 34 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 35 | + } catch (error) { |
| 36 | + // Ignore cleanup errors |
44 | 37 | } |
45 | | - }); |
| 38 | + }; |
46 | 39 |
|
47 | | - test("initializeGlobal should do nothing when no extensions found", async () => { |
48 | | - // No extensions in the global directory |
49 | | - await manager.initializeGlobal(); |
50 | | - |
51 | | - // No extension host should be spawned - postToolUse should work without error |
52 | | - await manager.postToolUse("test-workspace", { |
53 | | - toolName: "bash", |
54 | | - toolCallId: "test-call", |
55 | | - args: {}, |
56 | | - result: {}, |
57 | | - workspaceId: "test-workspace", |
58 | | - timestamp: Date.now(), |
59 | | - }); |
60 | | - |
61 | | - // If no error thrown, test passes |
62 | | - }); |
| 40 | + return { manager, tempDir, projectPath, workspaceMetadata, runtimeConfig, cleanup }; |
| 41 | +} |
63 | 42 |
|
64 | | - test("initializeGlobal should not spawn multiple hosts", async () => { |
65 | | - // Create an extension in global directory |
66 | | - const globalExtDir = path.join(os.homedir(), ".cmux", "ext"); |
67 | | - fs.mkdirSync(globalExtDir, { recursive: true }); |
68 | | - fs.writeFileSync(path.join(globalExtDir, "test.js"), "export default { onPostToolUse() {} }"); |
69 | | - |
70 | | - // Call initializeGlobal twice |
71 | | - const promise1 = manager.initializeGlobal(); |
72 | | - const promise2 = manager.initializeGlobal(); |
73 | | - |
74 | | - await Promise.all([promise1, promise2]); |
75 | | - |
76 | | - // Cleanup global extension |
77 | | - fs.rmSync(path.join(globalExtDir, "test.js")); |
| 43 | +describe("ExtensionManager", () => { |
78 | 44 |
|
79 | | - // Should work without errors (testing for no crash) |
| 45 | + test.concurrent("initializeGlobal should do nothing when no extensions found", async () => { |
| 46 | + const { manager, cleanup } = await createTestContext(); |
| 47 | + try { |
| 48 | + // No extensions in the global directory |
| 49 | + await manager.initializeGlobal(); |
| 50 | + |
| 51 | + // No extension host should be spawned - postToolUse should work without error |
| 52 | + await manager.postToolUse("test-workspace", { |
| 53 | + toolName: "bash", |
| 54 | + toolCallId: "test-call", |
| 55 | + args: {}, |
| 56 | + result: {}, |
| 57 | + workspaceId: "test-workspace", |
| 58 | + timestamp: Date.now(), |
| 59 | + }); |
| 60 | + |
| 61 | + // If no error thrown, test passes |
| 62 | + } finally { |
| 63 | + await cleanup(); |
| 64 | + } |
80 | 65 | }); |
81 | 66 |
|
82 | | - test("registerWorkspace and unregisterWorkspace should work", async () => { |
83 | | - // Create an extension in global directory |
84 | | - const globalExtDir = path.join(os.homedir(), ".cmux", "ext"); |
85 | | - fs.mkdirSync(globalExtDir, { recursive: true }); |
86 | | - fs.writeFileSync(path.join(globalExtDir, "test.js"), "export default { onPostToolUse() {} }"); |
| 67 | + test.concurrent("initializeGlobal should not spawn multiple hosts", async () => { |
| 68 | + const { manager, cleanup } = await createTestContext(); |
| 69 | + try { |
| 70 | + // Note: This test is limited because ExtensionManager hardcodes ~/.cmux/ext |
| 71 | + // For now, we test the idempotency without actually loading extensions |
87 | 72 |
|
88 | | - // Initialize global host |
89 | | - await manager.initializeGlobal(); |
| 73 | + // Call initializeGlobal twice |
| 74 | + const promise1 = manager.initializeGlobal(); |
| 75 | + const promise2 = manager.initializeGlobal(); |
90 | 76 |
|
91 | | - // Register workspace |
92 | | - await manager.registerWorkspace("test-workspace", workspaceMetadata, runtimeConfig, "/tmp"); |
| 77 | + await Promise.all([promise1, promise2]); |
93 | 78 |
|
94 | | - // Unregister workspace |
95 | | - await manager.unregisterWorkspace("test-workspace"); |
96 | | - |
97 | | - // Cleanup |
98 | | - fs.rmSync(path.join(globalExtDir, "test.js")); |
99 | | - |
100 | | - // Should work without errors |
| 79 | + // Should work without errors (testing for no crash) |
| 80 | + } finally { |
| 81 | + await cleanup(); |
| 82 | + } |
101 | 83 | }); |
102 | 84 |
|
103 | | - test("shutdown should clean up the global host", async () => { |
104 | | - // Create an extension in global directory |
105 | | - const globalExtDir = path.join(os.homedir(), ".cmux", "ext"); |
106 | | - fs.mkdirSync(globalExtDir, { recursive: true }); |
107 | | - fs.writeFileSync(path.join(globalExtDir, "test.js"), "export default { onPostToolUse() {} }"); |
108 | | - |
109 | | - // Initialize global host |
110 | | - await manager.initializeGlobal(); |
111 | | - |
112 | | - // Shutdown |
113 | | - manager.shutdown(); |
114 | | - |
115 | | - // Cleanup |
116 | | - fs.rmSync(path.join(globalExtDir, "test.js")); |
117 | | - |
118 | | - // Should work without errors |
| 85 | + test.concurrent( |
| 86 | + "registerWorkspace and unregisterWorkspace should work", |
| 87 | + async () => { |
| 88 | + const { manager, workspaceMetadata, runtimeConfig, cleanup } = await createTestContext(); |
| 89 | + try { |
| 90 | + // Note: This test is limited because ExtensionManager hardcodes ~/.cmux/ext |
| 91 | + // For now, we test workspace registration without actually loading extensions |
| 92 | + |
| 93 | + // Initialize global host |
| 94 | + await manager.initializeGlobal(); |
| 95 | + |
| 96 | + // Register workspace |
| 97 | + await manager.registerWorkspace("test-workspace", workspaceMetadata, runtimeConfig, "/tmp"); |
| 98 | + |
| 99 | + // Unregister workspace |
| 100 | + await manager.unregisterWorkspace("test-workspace"); |
| 101 | + |
| 102 | + // Should work without errors |
| 103 | + } finally { |
| 104 | + await cleanup(); |
| 105 | + } |
| 106 | + }, |
| 107 | + 10000 |
| 108 | + ); |
| 109 | + |
| 110 | + test.concurrent("shutdown should clean up the global host", async () => { |
| 111 | + const { manager, cleanup } = await createTestContext(); |
| 112 | + try { |
| 113 | + // Note: This test is limited because ExtensionManager hardcodes ~/.cmux/ext |
| 114 | + // For now, we test shutdown without actually loading extensions |
| 115 | + |
| 116 | + // Initialize global host |
| 117 | + await manager.initializeGlobal(); |
| 118 | + |
| 119 | + // Shutdown |
| 120 | + manager.shutdown(); |
| 121 | + |
| 122 | + // Should work without errors |
| 123 | + } finally { |
| 124 | + await cleanup(); |
| 125 | + } |
119 | 126 | }); |
120 | 127 |
|
121 | | - test("postToolUse should do nothing when no host initialized", async () => { |
122 | | - await manager.postToolUse("nonexistent-workspace", { |
123 | | - toolName: "bash", |
124 | | - toolCallId: "test-call", |
125 | | - args: {}, |
126 | | - result: {}, |
127 | | - workspaceId: "nonexistent-workspace", |
128 | | - timestamp: Date.now(), |
129 | | - }); |
130 | | - |
131 | | - // Should not throw |
| 128 | + test.concurrent("postToolUse should do nothing when no host initialized", async () => { |
| 129 | + const { manager, cleanup } = await createTestContext(); |
| 130 | + try { |
| 131 | + await manager.postToolUse("nonexistent-workspace", { |
| 132 | + toolName: "bash", |
| 133 | + toolCallId: "test-call", |
| 134 | + args: {}, |
| 135 | + result: {}, |
| 136 | + workspaceId: "nonexistent-workspace", |
| 137 | + timestamp: Date.now(), |
| 138 | + }); |
| 139 | + |
| 140 | + // Should not throw |
| 141 | + } finally { |
| 142 | + await cleanup(); |
| 143 | + } |
132 | 144 | }); |
133 | 145 | }); |
0 commit comments