| 54 | * Create a test environment with temporary config and service container |
| 55 | */ |
| 56 | export async function createTestEnvironment(): Promise<TestEnvironment> { |
| 57 | // Create temporary directory for test config |
| 58 | const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "mux-test-")); |
| 59 | |
| 60 | // Create config with temporary directory |
| 61 | const config = new Config(tempDir); |
| 62 | |
| 63 | // Some UI tests render ProjectPage, which now hard-blocks workspace creation when no providers |
| 64 | // are configured. For non-integration tests, seed a dummy provider so the UI can render. |
| 65 | // |
| 66 | // For integration tests (TEST_INTEGRATION=1), do NOT write dummy keys here (they would override |
| 67 | // real env-backed credentials used by tests like name generation). |
| 68 | if (!shouldRunIntegrationTests()) { |
| 69 | config.saveProvidersConfig({ |
| 70 | anthropic: { apiKey: "test-key-for-ui-tests" }, |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | // Create mock BrowserWindow |
| 75 | const mockWindow = createMockBrowserWindow(); |
| 76 | |
| 77 | // Create ServiceContainer instance |
| 78 | const services = new ServiceContainer(config); |
| 79 | // IPC tests run SSH against Docker containers with ephemeral host keys and no |
| 80 | // interactive UI for host-key approval. Reset to headless-fallback so the |
| 81 | // ServiceContainer's "strict" mode doesn't block Docker SSH connections. |
| 82 | setOpenSSHHostKeyPolicyMode("headless-fallback"); |
| 83 | await services.initialize(); |
| 84 | |
| 85 | // Wire services to the mock BrowserWindow |
| 86 | // Note: Events are consumed via ORPC subscriptions (StreamCollector), not windowService.send() |
| 87 | services.windowService.setMainWindow(mockWindow); |
| 88 | |
| 89 | const orpcContext: ORPCContext = services.toORPCContext(); |
| 90 | const orpc = createOrpcTestClient(orpcContext); |
| 91 | |
| 92 | return { |
| 93 | config, |
| 94 | services, |
| 95 | mockWindow, |
| 96 | tempDir, |
| 97 | orpc, |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Cleanup test environment (remove temporary directory) with retry logic |