|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
| 6 | +import { mkdtempForTestSync } from '../../tmp-dir.fixtures.ts'; |
| 7 | + |
| 8 | +const stdin = vi.hoisted(() => ({ text: '' })); |
| 9 | + |
| 10 | +vi.mock('node:fs', async (importOriginal) => { |
| 11 | + const actual = await importOriginal<typeof import('node:fs')>(); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + readFileSync: (...args: Parameters<typeof actual.readFileSync>) => |
| 15 | + args[0] === 0 ? stdin.text : actual.readFileSync(...args), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +let originalExitCode: typeof process.exitCode; |
| 20 | +let originalArgv: typeof process.argv; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + vi.resetModules(); |
| 24 | + originalExitCode = process.exitCode; |
| 25 | + originalArgv = process.argv; |
| 26 | + process.argv = [...process.argv]; |
| 27 | + process.exitCode = undefined; |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + vi.restoreAllMocks(); |
| 32 | + process.exitCode = originalExitCode; |
| 33 | + process.argv = originalArgv; |
| 34 | +}); |
| 35 | + |
| 36 | +function captureOutput() { |
| 37 | + const chunks: string[] = []; |
| 38 | + const written = new Promise<void>((resolve) => { |
| 39 | + vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => { |
| 40 | + chunks.push(String(chunk)); |
| 41 | + resolve(); |
| 42 | + return true; |
| 43 | + }); |
| 44 | + }); |
| 45 | + return { written, text: () => chunks.join('') }; |
| 46 | +} |
| 47 | + |
| 48 | +test('the VM entry reads its stdin request and writes the output envelope', async () => { |
| 49 | + const scriptPath = path.join(mkdtempForTestSync('run-script-entry-'), 'setup.js'); |
| 50 | + fs.writeFileSync(scriptPath, 'output.answer = VALUE.toUpperCase();'); |
| 51 | + stdin.text = JSON.stringify({ scriptPath, env: { VALUE: 'child' }, publicNetworkOnly: false }); |
| 52 | + const output = captureOutput(); |
| 53 | + process.argv[1] = fileURLToPath(new URL('../run-script-child.ts', import.meta.url)); |
| 54 | + |
| 55 | + await import('../run-script-child.ts'); |
| 56 | + |
| 57 | + await output.written; |
| 58 | + assert.deepEqual(JSON.parse(output.text()), { outputEnv: { 'output.answer': 'CHILD' } }); |
| 59 | +}); |
| 60 | + |
| 61 | +test('the HTTP entry writes typed refusals as JSON without failing the process', async () => { |
| 62 | + stdin.text = JSON.stringify({ |
| 63 | + method: 'POST', |
| 64 | + url: 'not a URL', |
| 65 | + headers: {}, |
| 66 | + publicNetworkOnly: true, |
| 67 | + }); |
| 68 | + const output = captureOutput(); |
| 69 | + const stderr = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 70 | + process.argv[1] = fileURLToPath(new URL('../run-script-http-child.ts', import.meta.url)); |
| 71 | + |
| 72 | + await import('../run-script-http-child.ts'); |
| 73 | + |
| 74 | + await output.written; |
| 75 | + assert.deepEqual(JSON.parse(output.text()), { |
| 76 | + error: { code: 'INVALID_ARGS', message: 'Invalid Maestro runScript HTTP URL' }, |
| 77 | + }); |
| 78 | + assert.equal(stderr.mock.calls.length, 0); |
| 79 | + assert.equal(process.exitCode, undefined); |
| 80 | +}); |
| 81 | + |
| 82 | +test('the HTTP entry reports malformed protocol input on stderr with a failing exit code', async () => { |
| 83 | + stdin.text = '{'; |
| 84 | + const stdout = vi.spyOn(process.stdout, 'write').mockReturnValue(true); |
| 85 | + const failure = new Promise<unknown>((resolve) => { |
| 86 | + vi.spyOn(console, 'error').mockImplementation((value) => resolve(value)); |
| 87 | + }); |
| 88 | + process.argv[1] = fileURLToPath(new URL('../run-script-http-child.ts', import.meta.url)); |
| 89 | + |
| 90 | + await import('../run-script-http-child.ts'); |
| 91 | + |
| 92 | + assert.match(String(await failure), /SyntaxError/); |
| 93 | + assert.equal(stdout.mock.calls.length, 0); |
| 94 | + assert.equal(process.exitCode, 1); |
| 95 | +}); |
0 commit comments