#!/usr/bin/env bun import assert from 'node:assert/strict' import { spawnSync } from 'node:child_process' import { EventEmitter } from 'node:events' import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' import { render } from 'ink' import React from 'react' import * as builderOnboardingCommand from '../src/build/onboarding/command.ts' import { discoverCapacitorProjects, hasCapacitorConfig, } from '../src/build/onboarding/project-discovery.ts' import { projectCandidateLabel, selectCapacitorProject } from '../src/build/onboarding/project-selection.ts' import { builderProjectNotFoundMessage, shouldDiscoverBuilderProject, } from '../src/build/onboarding/command.ts' import { loadConfigTarget, setConfigWriteTarget, writeConfig } from '../src/config/index.ts' import { BuilderProjectDiscoveryApp, DEFAULT_BUILDER_PROJECT_DISCOVERY_TIMING, } from '../src/build/onboarding/ui/project-discovery.tsx' const fixtureRoots = [] let failures = 0 function fixture(name) { const root = mkdtempSync(join(tmpdir(), `capgo-builder-${name}-`)) fixtureRoots.push(root) return root } function writeText(path, value) { mkdirSync(dirname(path), { recursive: true }) writeFileSync(path, value) } function writeJson(path, value) { writeText(path, `${JSON.stringify(value, null, 2)}\n`) } function addPackage(root, relativeDir, name) { const dir = join(root, relativeDir) writeJson(join(dir, 'package.json'), { name, version: '1.0.0' }) return dir } function addCapacitorApp(root, relativeDir, name, appId) { const dir = addPackage(root, relativeDir, name) writeText(join(dir, 'capacitor.config.ts'), `export default { appId: '${appId}' }\n`) return dir } function makeStdout(cols = 100, rows = 50, beforeWrite = () => {}) { const stream = new EventEmitter() stream.columns = cols stream.rows = rows stream.isTTY = true stream.frames = [] stream.frameTimes = [] stream.lastFrame = '' stream.write = (frame) => { beforeWrite(frame) stream.frameTimes.push(Date.now()) stream.frames.push(frame) stream.lastFrame = frame return true } return stream } function makeStdin() { const stream = new EventEmitter() stream.isTTY = true stream.setEncoding = () => {} stream.setRawMode = () => {} stream.resume = () => {} stream.pause = () => {} stream.ref = () => {} stream.unref = () => {} stream.read = () => null return stream } async function waitForFrame(stdout, pattern, timeoutMs = 2000) { const deadline = Date.now() + timeoutMs while (!stdout.frames.some(frame => pattern.test(frame)) && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 5)) assert.ok(stdout.frames.some(frame => pattern.test(frame)), `No frame matched ${pattern}`) } async function test(name, run) { try { await run() console.log(`✓ ${name}`) } catch (error) { failures += 1 console.error(`✗ ${name}`) console.error(error) } } try { await test('does not paint search status when discovery completes within 100ms', async () => { const candidate = { dir: '/workspace/apps/mobile', relativeDir: 'apps/mobile', packageName: '@example/mobile', appId: 'com.example.mobile', } const stdout = makeStdout() const instance = render( React.createElement(BuilderProjectDiscoveryApp, { searchRoot: '/workspace', onDecision: () => {}, discoverProjects: async () => ({ candidates: [candidate], nxDetected: false }), }), { stdout, stderr: makeStdout(), stdin: makeStdin(), debug: true, exitOnCtrlC: false, patchConsole: false, }, ) try { await waitForFrame(stdout, /Is this the correct app\?/) assert.equal(stdout.frames.some(frame => /Looking for a Capacitor app/.test(frame)), false) } finally { instance.unmount() } }) for (const { renderDelayMs, debug } of [ { renderDelayMs: 0, debug: true }, { renderDelayMs: 120, debug: true }, { renderDelayMs: 0, debug: false }, { renderDelayMs: 120, debug: false }, ]) { await test(`keeps search status visible after ${renderDelayMs}ms delay with debug=${debug}`, async () => { const candidate = { dir: '/workspace/apps/mobile', relativeDir: 'apps/mobile', packageName: '@example/mobile', appId: 'com.example.mobile', } let resolveDiscovery const discoveryPromise = new Promise((resolve) => { resolveDiscovery = resolve }) let delayed = false const stdout = makeStdout(100, 50, (frame) => { if (delayed || !/Looking for a Capacitor app/.test(frame)) return delayed = true // Simulate a slow terminal commit, before the status becomes visible. const until = Date.now() + renderDelayMs while (Date.now() < until) {} }) const instance = render( React.createElement(BuilderProjectDiscoveryApp, { searchRoot: '/workspace', onDecision: () => {}, discoverProjects: () => discoveryPromise, timing: { searchStatusDelayMs: 20, minimumSearchStatusMs: 80, timeoutMs: 5000, }, }), { stdout, stderr: makeStdout(), stdin: makeStdin(), debug, interactive: true, exitOnCtrlC: false, patchConsole: false, }, ) try { await waitForFrame(stdout, /Looking for a Capacitor app/, 2000) const statusShownAt = stdout.frameTimes[stdout.frames.findIndex(frame => /Looking for a Capacitor app/.test(frame))] resolveDiscovery({ candidates: [candidate], nxDetected: false }) await waitForFrame(stdout, /Is this the correct app\?/, 2000) const selectionShownAt = stdout.frameTimes[stdout.frames.findIndex(frame => /Is this the correct app\?/.test(frame))] assert.ok(selectionShownAt - statusShownAt >= 70, `search status was visible for only ${selectionShownAt - statusShownAt}ms`) } finally { instance.unmount() } }) } await test('stops project discovery after five seconds', async () => { assert.deepEqual(DEFAULT_BUILDER_PROJECT_DISCOVERY_TIMING, { searchStatusDelayMs: 100, minimumSearchStatusMs: 1000, timeoutMs: 5000, }) const stdout = makeStdout() let resolveDecision const decisionPromise = new Promise((resolve) => { resolveDecision = resolve }) const instance = render( React.createElement(BuilderProjectDiscoveryApp, { searchRoot: '/workspace', onDecision: resolveDecision, discoverProjects: () => new Promise(() => {}), timing: { searchStatusDelayMs: 10, minimumSearchStatusMs: 20, timeoutMs: 60, }, }), { stdout, stderr: makeStdout(), stdin: makeStdin(), debug: true, exitOnCtrlC: false, patchConsole: false, }, ) try { const decision = await Promise.race([ decisionPromise, new Promise(resolve => setTimeout(() => resolve({ kind: 'test-timeout' }), 250)), ]) assert.deepEqual(decision, { kind: 'timed-out' }) assert.ok(stdout.frames.some(frame => /Looking for a Capacitor app/.test(frame))) } finally { instance.unmount() } }) await test('accepts a Capacitor app in the invocation directory without requiring package.json', async () => { const root = fixture('current-app') writeText(join(root, 'capacitor.config.json'), '{"appId":"com.example.current"}\n') assert.equal(hasCapacitorConfig(root), true) const result = await discoverCapacitorProjects(root) assert.equal(result.reason, undefined) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['.']) assert.equal(result.candidates[0].appId, 'com.example.current') }) await test('does not search when the invocation directory has no package.json', async () => { const root = fixture('no-package-json') addCapacitorApp(root, 'nested/mobile', '@example/mobile', 'com.example.mobile') const result = await discoverCapacitorProjects(root) assert.equal(result.reason, 'missing-package-json') assert.deepEqual(result.candidates, []) }) await test('discovers generic array-form workspaces without requiring a lockfile', async () => { const root = fixture('generic') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) addPackage(root, 'apps/web', '@example/web') addCapacitorApp(root, 'apps/mobile', '@example/mobile', 'com.example.mobile') const result = await discoverCapacitorProjects(root) assert.equal(result.reason, undefined) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/mobile']) assert.equal(result.candidates[0].packageName, '@example/mobile') assert.equal(result.candidates[0].appId, 'com.example.mobile') }) await test('unwraps a CommonJS default export from capacitor.config.js', async () => { const root = fixture('javascript-cjs-default') const configPath = join(root, 'capacitor.config.js') writeText(configPath, "exports.default = { appId: 'com.example.cjs-default' }\n") assert.equal((await loadConfigTarget(configPath)).appId, 'com.example.cjs-default') writeText(configPath, "exports.default = { appId: 'com.example.cjs-next' }\n") assert.equal((await loadConfigTarget(configPath)).appId, 'com.example.cjs-next') }) await test('reloads a JavaScript Capacitor config after it is saved', async () => { const root = fixture('javascript-config-reload') const configPath = join(root, 'capacitor.config.js') writeText(configPath, "module.exports = { appId: 'com.example.first' }\n") assert.equal((await loadConfigTarget(configPath)).appId, 'com.example.first') writeText(configPath, "module.exports = { appId: 'com.example.second', plugins: { CapgoBuilder: { capgoBuilderAppId: 'com.example.capgo' } } }\n") const reloaded = await loadConfigTarget(configPath) assert.equal(reloaded.appId, 'com.example.second') assert.equal(reloaded.plugins.CapgoBuilder.capgoBuilderAppId, 'com.example.capgo') }) await test('saves a type module capacitor.config.js as ESM and reloads it', async () => { const root = fixture('javascript-esm-config-write') writeJson(join(root, 'package.json'), { private: true, type: 'module' }) const configPath = join(root, 'capacitor.config.js') writeText(configPath, "export default { appId: 'com.example.first', webDir: 'www' }\n") setConfigWriteTarget(configPath) try { await writeConfig('CapacitorUpdater', { path: configPath, config: { appId: 'com.example.first', webDir: 'www', plugins: { CapacitorUpdater: { appId: 'com.example.capgo' } }, }, }) } finally { setConfigWriteTarget(undefined) } const source = readFileSync(configPath, 'utf8') assert.match(source, /export default config/) assert.equal(source.includes('module.exports'), false) const reloaded = await loadConfigTarget(configPath) assert.equal(reloaded.appId, 'com.example.first') assert.equal(reloaded.plugins.CapacitorUpdater.appId, 'com.example.capgo') }) await test('reloads an ES module Capacitor config after it is saved', async () => { const root = fixture('javascript-esm-config-reload') writeJson(join(root, 'package.json'), { private: true, type: 'module' }) const configPath = join(root, 'capacitor.config.js') writeText(configPath, "export default { appId: 'com.example.first' }\n") assert.equal((await loadConfigTarget(configPath)).appId, 'com.example.first') writeText(configPath, "export default { appId: 'com.example.second', plugins: { CapgoBuilder: { capgoBuilderAppId: 'com.example.capgo' } } }\n") const reloaded = await loadConfigTarget(configPath) assert.equal(reloaded.appId, 'com.example.second') assert.equal(reloaded.plugins.CapgoBuilder.capgoBuilderAppId, 'com.example.capgo') }) await test('node reloads an ES module config after require cached it', async () => { const node = spawnSync('node', ['-e', 'process.exit(0)'], { encoding: 'utf8' }) if (node.error || node.status !== 0) return const root = fixture('javascript-esm-config-node') const script = ` import { createRequire } from 'node:module' import { writeFileSync } from 'node:fs' import { pathToFileURL } from 'node:url' const root = process.argv[1] const configPath = root + '/capacitor.config.js' writeFileSync(root + '/package.json', JSON.stringify({ private: true, type: 'module' })) writeFileSync(configPath, "export default { appId: 'com.example.first' }\\n") const targetRequire = createRequire(configPath) let resolved try { resolved = targetRequire.resolve(configPath) targetRequire(configPath) } catch (error) { if (error && error.code === 'ERR_REQUIRE_ESM') process.exit(0) console.error(error) process.exit(1) } writeFileSync(configPath, "export default { appId: 'com.example.second' }\\n") delete targetRequire.cache[resolved] const fresh = await import(pathToFileURL(resolved).href + '?capgo-reload=' + Date.now()) const appId = fresh.default?.appId ?? fresh.appId if (appId !== 'com.example.second') { console.error(appId) process.exit(1) } ` const result = spawnSync('node', ['--input-type=module', '-e', script, root], { encoding: 'utf8' }) assert.equal(result.status, 0, result.stderr || result.stdout) }) await test('node saves a type module config through writeConfig and reloads it', async () => { const node = spawnSync('node', ['-e', 'process.exit(0)'], { encoding: 'utf8' }) if (node.error || node.status !== 0) return const root = fixture('javascript-esm-config-node-write') writeJson(join(root, 'package.json'), { private: true, type: 'module' }) const configPath = join(root, 'capacitor.config.js') writeText(configPath, "export default { appId: 'com.example.first', webDir: 'www' }\n") const bundlePath = join(root, 'config-bundle.mjs') const sourcePath = fileURLToPath(new URL('../src/config/index.ts', import.meta.url)) const build = spawnSync('bun', ['build', sourcePath, '--outfile', bundlePath, '--target', 'node', '--format=esm'], { encoding: 'utf8' }) assert.equal(build.status, 0, build.stderr || build.stdout) const script = ` import { loadConfigTarget, setConfigWriteTarget, writeConfig } from ${JSON.stringify(pathToFileURL(bundlePath).href)} const configPath = process.argv[1] setConfigWriteTarget(configPath) await writeConfig('CapacitorUpdater', { path: configPath, config: { appId: 'com.example.first', webDir: 'www', plugins: { CapacitorUpdater: { appId: 'com.example.capgo' } }, }, }) const reloaded = await loadConfigTarget(configPath) if (reloaded.appId !== 'com.example.first' || reloaded.plugins?.CapacitorUpdater?.appId !== 'com.example.capgo') { console.error(JSON.stringify({ appId: reloaded.appId, updater: reloaded.plugins?.CapacitorUpdater?.appId })) process.exit(1) } ` const result = spawnSync('node', ['--input-type=module', '-e', script, configPath], { encoding: 'utf8' }) assert.equal(result.status, 0, result.stderr || result.stdout) const source = readFileSync(configPath, 'utf8') assert.match(source, /export default config/) assert.equal(source.includes('module.exports'), false) }) await test('reads appId from a JavaScript config without project TypeScript', async () => { const root = fixture('javascript-config') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) const appDir = addPackage(root, 'apps/mobile', '@example/mobile') writeText(join(appDir, 'capacitor.config.js'), "module.exports = { appId: 'com.example.javascript' }\n") const result = await discoverCapacitorProjects(root) assert.equal(result.reason, undefined) assert.equal(result.candidates[0].appId, 'com.example.javascript') }) await test('shows the configured Builder ID in workspace candidates', async () => { const root = fixture('builder-app-id') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) const jsDir = addPackage(root, 'apps/javascript', '@example/javascript') const jsonDir = addPackage(root, 'apps/json', '@example/json') writeText(join(jsDir, 'capacitor.config.js'), "module.exports = { appId: 'com.example.native', plugins: { CapgoBuilder: { capgoBuilderAppId: 'com.example.builder' } } }\n") writeJson(join(jsonDir, 'capacitor.config.json'), { appId: 'com.example.native-json', plugins: { CapgoBuilder: { capgoBuilderAppId: 'com.example.builder-json' } }, }) const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.appId), ['com.example.builder', 'com.example.builder-json']) }) await test('reads literal appId metadata without executing candidate configuration code', async () => { const root = fixture('non-executing-config-discovery') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) const appDir = addPackage(root, 'apps/mobile', '@example/mobile') const executionMarker = join(root, 'candidate-config-executed') writeText( join(appDir, 'capacitor.config.js'), `require('node:fs').writeFileSync(${JSON.stringify(executionMarker)}, 'executed')\nmodule.exports = { appId: 'com.example.safe-discovery' }\n`, ) const result = await discoverCapacitorProjects(root) assert.equal(result.reason, undefined) assert.equal(result.candidates[0].appId, 'com.example.safe-discovery') assert.equal(existsSync(executionMarker), false) }) await test('falls back to the CLI compiler when a resolved TypeScript module is unusable', async () => { const root = fixture('invalid-project-typescript') const configPath = join(root, 'capacitor.config.ts') writeText(configPath, "export default { appId: 'com.example.fallback' }\n") writeJson(join(root, 'node_modules/typescript/package.json'), { name: 'typescript', version: '0.0.0', main: 'index.js' }) writeText(join(root, 'node_modules/typescript/index.js'), "module.exports = { version: '0.0.0' }\n") const config = await loadConfigTarget(configPath) assert.equal(config.appId, 'com.example.fallback') }) await test('sorts multiple Capacitor apps by relative workspace path', async () => { const root = fixture('multiple') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) addCapacitorApp(root, 'apps/mobile', '@example/mobile', 'com.example.mobile') addCapacitorApp(root, 'apps/admin', '@example/admin', 'com.example.admin') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/admin', 'apps/mobile']) }) await test('discovers Yarn object-form workspaces', async () => { const root = fixture('yarn') writeJson(join(root, 'package.json'), { private: true, packageManager: 'yarn@1.22.22', workspaces: { packages: ['packages/*'] }, }) addCapacitorApp(root, 'packages/mobile', '@example/yarn-mobile', 'com.example.yarn') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['packages/mobile']) }) await test('discovers pnpm-workspace.yaml packages', async () => { const root = fixture('pnpm') writeJson(join(root, 'package.json'), { private: true, packageManager: 'pnpm@10.0.0' }) writeText(join(root, 'pnpm-workspace.yaml'), "packages:\n - 'apps/*'\n") addCapacitorApp(root, 'apps/mobile', '@example/pnpm-mobile', 'com.example.pnpm') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/mobile']) }) await test('discovers Bun workspaces from package-manager metadata', async () => { const root = fixture('bun') writeJson(join(root, 'package.json'), { private: true, packageManager: 'bun@1.3.0', workspaces: ['apps/*'], }) addCapacitorApp(root, 'apps/mobile', '@example/bun-mobile', 'com.example.bun') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/mobile']) }) await test('discovers Lerna package globs', async () => { const root = fixture('lerna') writeJson(join(root, 'package.json'), { private: true }) writeJson(join(root, 'lerna.json'), { packages: ['products/*'] }) addCapacitorApp(root, 'products/mobile', '@example/lerna-mobile', 'com.example.lerna') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['products/mobile']) }) await test('prefers root npm workspaces when lerna.json omits package globs', async () => { const root = fixture('lerna-workspaces') writeJson(join(root, 'package.json'), { private: true, packageManager: 'npm@10.9.2', workspaces: ['apps/*', 'packages/*'], }) writeJson(join(root, 'lerna.json'), { version: 'independent', npmClient: 'npm' }) addCapacitorApp(root, 'apps/mobile', '@example/lerna-mobile', 'com.example.lerna') addPackage(root, 'packages/shared', '@example/shared') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/mobile']) }) await test('discovers Rush project folders', async () => { const root = fixture('rush') writeJson(join(root, 'package.json'), { private: true }) writeJson(join(root, 'rush.json'), { projects: [{ packageName: '@example/rush-mobile', projectFolder: 'apps/mobile' }], }) addCapacitorApp(root, 'apps/mobile', '@example/rush-mobile', 'com.example.rush') const result = await discoverCapacitorProjects(root) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/mobile']) }) await test('reports Nx-only repositories as unsupported', async () => { const root = fixture('nx-only') writeJson(join(root, 'package.json'), { private: true }) writeJson(join(root, 'nx.json'), {}) writeJson(join(root, 'apps/mobile/project.json'), { name: 'mobile', root: 'apps/mobile' }) writeText(join(root, 'apps/mobile/capacitor.config.ts'), "export default { appId: 'com.example.nx' }\n") const result = await discoverCapacitorProjects(root) assert.equal(result.reason, 'unsupported-workspace') assert.equal(result.nxDetected, true) assert.deepEqual(result.candidates, []) }) await test('supports Nx repositories that also declare package workspaces', async () => { const root = fixture('nx-workspaces') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) writeJson(join(root, 'nx.json'), {}) addCapacitorApp(root, 'apps/mobile', '@example/nx-mobile', 'com.example.nx') const result = await discoverCapacitorProjects(root) assert.equal(result.nxDetected, true) assert.deepEqual(result.candidates.map(candidate => candidate.relativeDir), ['apps/mobile']) }) await test('rejects workspace package symlinks that escape the invocation root', async () => { const root = fixture('symlink-root') const outside = fixture('symlink-outside') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) addCapacitorApp(outside, '.', '@example/outside', 'com.example.outside') mkdirSync(join(root, 'apps'), { recursive: true }) symlinkSync(outside, join(root, 'apps', 'outside'), 'dir') const result = await discoverCapacitorProjects(root) assert.equal(result.reason, 'no-capacitor-app') assert.deepEqual(result.candidates, []) }) await test('asks for confirmation when one app is discovered', async () => { const only = { dir: '/workspace/apps/mobile', relativeDir: 'apps/mobile', packageName: '@example/mobile' } let selectCalled = false const selected = await selectCapacitorProject([only], { confirm: async candidate => candidate.dir === only.dir, select: async () => { selectCalled = true return Symbol('cancel') }, }) assert.equal(selected?.dir, only.dir) assert.equal(selectCalled, false) }) await test('returns no app when the single candidate is rejected', async () => { const only = { dir: '/workspace/apps/mobile', relativeDir: 'apps/mobile' } const selected = await selectCapacitorProject([only], { confirm: async () => false, select: async () => assert.fail('select must not run'), }) assert.equal(selected, undefined) }) await test('selects among multiple apps and handles prompt cancellation', async () => { const candidates = [ { dir: '/workspace/apps/admin', relativeDir: 'apps/admin' }, { dir: '/workspace/apps/mobile', relativeDir: 'apps/mobile' }, ] const selected = await selectCapacitorProject(candidates, { confirm: async () => assert.fail('confirm must not run'), select: async options => options[1].dir, }) const cancelled = await selectCapacitorProject(candidates, { confirm: async () => assert.fail('confirm must not run'), select: async () => Symbol('cancel'), }) assert.equal(selected?.dir, candidates[1].dir) assert.equal(cancelled, undefined) }) await test('labels project choices with path and Capacitor appId when available', () => { assert.equal( projectCandidateLabel({ dir: '/workspace/apps/mobile', relativeDir: 'apps/mobile', packageName: '@example/mobile', appId: 'com.example.mobile', }), 'apps/mobile — appId: com.example.mobile', ) assert.equal( projectCandidateLabel({ dir: '/workspace/apps/admin', relativeDir: 'apps/admin', packageName: '@example/admin', }), '@example/admin — apps/admin', ) }) await test('changes into the selected app and keeps the invocation root on cancellation', async () => { assert.equal(typeof builderOnboardingCommand.discoverBuilderProjectFromInvocationRoot, 'function') const root = fixture('command-handoff') writeJson(join(root, 'package.json'), { private: true, workspaces: ['apps/*'] }) const appDir = addCapacitorApp(root, 'apps/mobile', '@example/mobile', 'com.example.mobile') const previousCwd = process.cwd() try { process.chdir(root) const selected = await builderOnboardingCommand.discoverBuilderProjectFromInvocationRoot( { enableProjectDiscovery: true }, { confirm: async () => true, select: async () => assert.fail('select must not run'), }, ) assert.equal(selected, 'ready') assert.equal(realpathSync(process.cwd()), realpathSync(appDir)) process.chdir(root) const cancelled = await builderOnboardingCommand.discoverBuilderProjectFromInvocationRoot( { enableProjectDiscovery: true }, { confirm: async () => false, select: async () => assert.fail('select must not run'), }, ) assert.equal(cancelled, 'cancelled') assert.equal(realpathSync(process.cwd()), realpathSync(root)) } finally { process.chdir(previousCwd) } }) await test('enables workspace discovery only for the direct build init entrypoint', () => { assert.equal(shouldDiscoverBuilderProject({ enableProjectDiscovery: true }), true) assert.equal(shouldDiscoverBuilderProject({}), false) }) await test('provides actionable generic and Nx-specific failure messages', () => { const generic = builderProjectNotFoundMessage(false) const nx = builderProjectNotFoundMessage(true) assert.match(generic, /npx @capgo\/cli@latest build init/) assert.doesNotMatch(generic, /Nx repositories/) assert.match(nx, /Nx repositories.*not currently supported/s) }) await test('provides an actionable five-second discovery timeout message', () => { const message = builderOnboardingCommand.builderProjectTimeoutMessage() assert.match(message, /timed out after 5 seconds/i) assert.match(message, /npx @capgo\/cli@latest build init/) }) } finally { for (const root of fixtureRoots) rmSync(root, { recursive: true, force: true }) } if (failures > 0) { console.error(`\n${failures} builder project discovery test(s) failed`) process.exit(1) } console.log('\nBuilder project discovery tests passed')