|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as os from 'node:os'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +const hoisted = vi.hoisted(() => ({ |
| 7 | + buildFn: vi.fn(), |
| 8 | + scaffoldFn: vi.fn(), |
| 9 | + constructorArgs: [] as Array<{ baseUrl?: string; apiKey?: string | null }>, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('../credentials.js', async () => { |
| 13 | + const actual = await vi.importActual<typeof import('../credentials.js')>('../credentials.js'); |
| 14 | + return { ...actual, resolveApiKey: vi.fn() }; |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock('../http-client.js', () => { |
| 18 | + return { |
| 19 | + EngineClient: class { |
| 20 | + constructor(opts: { baseUrl?: string; apiKey?: string | null }) { |
| 21 | + hoisted.constructorArgs.push(opts); |
| 22 | + } |
| 23 | + build = hoisted.buildFn; |
| 24 | + scaffold = hoisted.scaffoldFn; |
| 25 | + health = vi.fn(); |
| 26 | + catalog = vi.fn(); |
| 27 | + }, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +import { resolveApiKey } from '../credentials.js'; |
| 32 | +import { architectCommand } from '../commands/architect.js'; |
| 33 | +import { runCommand } from '../commands/run.js'; |
| 34 | +import type { CLIOptions } from '../index.js'; |
| 35 | + |
| 36 | +const mockedResolveApiKey = vi.mocked(resolveApiKey); |
| 37 | + |
| 38 | +const options: CLIOptions = { |
| 39 | + format: 'json', |
| 40 | + configPath: '.charter', |
| 41 | + ciMode: false, |
| 42 | + yes: true, |
| 43 | +}; |
| 44 | + |
| 45 | +function fakeBuildResult() { |
| 46 | + return { |
| 47 | + stack: [], |
| 48 | + compatibility: { |
| 49 | + pairs: [], |
| 50 | + totalScore: 0, |
| 51 | + normalizedScore: 0, |
| 52 | + dominant: '', |
| 53 | + tensions: [], |
| 54 | + }, |
| 55 | + scaffold: {}, |
| 56 | + seed: 1, |
| 57 | + receipt: 'receipt', |
| 58 | + requirements: { |
| 59 | + description: 'anything', |
| 60 | + keywords: [], |
| 61 | + constraints: {}, |
| 62 | + complexity: 'moderate', |
| 63 | + }, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function fakeScaffoldResult() { |
| 68 | + return { |
| 69 | + files: [], |
| 70 | + fileSource: 'engine' as const, |
| 71 | + nextSteps: [], |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +let tmpCwd: string; |
| 76 | + |
| 77 | +beforeEach(() => { |
| 78 | + tmpCwd = fs.mkdtempSync(path.join(os.tmpdir(), 'charter-wiring-')); |
| 79 | + process.chdir(tmpCwd); |
| 80 | + fs.mkdirSync(path.join(tmpCwd, '.charter'), { recursive: true }); |
| 81 | + hoisted.buildFn.mockReset().mockResolvedValue(fakeBuildResult()); |
| 82 | + hoisted.scaffoldFn.mockReset().mockResolvedValue(fakeScaffoldResult()); |
| 83 | + hoisted.constructorArgs.length = 0; |
| 84 | + mockedResolveApiKey.mockReset(); |
| 85 | + vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 86 | + vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 87 | + vi.spyOn(process.stderr, 'write').mockImplementation(() => true); |
| 88 | +}); |
| 89 | + |
| 90 | +afterEach(() => { |
| 91 | + vi.restoreAllMocks(); |
| 92 | + process.chdir(os.tmpdir()); |
| 93 | + fs.rmSync(tmpCwd, { recursive: true, force: true }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('architect — auth wiring', () => { |
| 97 | + it('forwards the env-sourced API key (and custom baseUrl) to EngineClient', async () => { |
| 98 | + mockedResolveApiKey.mockReturnValue({ |
| 99 | + apiKey: 'ea_env_wiring', |
| 100 | + source: 'env', |
| 101 | + baseUrl: 'https://engine.example', |
| 102 | + }); |
| 103 | + |
| 104 | + await architectCommand(options, ['a simple project description']); |
| 105 | + |
| 106 | + expect(hoisted.constructorArgs).toHaveLength(1); |
| 107 | + expect(hoisted.constructorArgs[0].apiKey).toBe('ea_env_wiring'); |
| 108 | + expect(hoisted.constructorArgs[0].baseUrl).toBe('https://engine.example'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('passes apiKey=null to EngineClient when resolveApiKey returns null', async () => { |
| 112 | + mockedResolveApiKey.mockReturnValue(null); |
| 113 | + |
| 114 | + await architectCommand(options, ['unauthenticated fallback']); |
| 115 | + |
| 116 | + expect(hoisted.constructorArgs[0].apiKey).toBeNull(); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('run — gateway vs engine routing', () => { |
| 121 | + it('uses the gateway (scaffold) when the env var provides an API key', async () => { |
| 122 | + mockedResolveApiKey.mockReturnValue({ apiKey: 'ea_env_gateway', source: 'env' }); |
| 123 | + |
| 124 | + await runCommand(options, ['a description', '--dry-run']); |
| 125 | + |
| 126 | + expect(hoisted.scaffoldFn).toHaveBeenCalledTimes(1); |
| 127 | + expect(hoisted.buildFn).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('falls back to engine /build when no API key is resolved', async () => { |
| 131 | + mockedResolveApiKey.mockReturnValue(null); |
| 132 | + |
| 133 | + await runCommand(options, ['a description', '--dry-run']); |
| 134 | + |
| 135 | + expect(hoisted.buildFn).toHaveBeenCalledTimes(1); |
| 136 | + expect(hoisted.scaffoldFn).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('uses the gateway when login-stored credentials are resolved (parity with env path)', async () => { |
| 140 | + mockedResolveApiKey.mockReturnValue({ apiKey: 'sb_live_stored', source: 'credentials' }); |
| 141 | + |
| 142 | + await runCommand(options, ['a description', '--dry-run']); |
| 143 | + |
| 144 | + expect(hoisted.scaffoldFn).toHaveBeenCalledTimes(1); |
| 145 | + expect(hoisted.buildFn).not.toHaveBeenCalled(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments