|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const fs = require('node:fs'); |
| 4 | +const os = require('node:os'); |
| 5 | +const path = require('node:path'); |
| 6 | +const { spawnSync } = require('node:child_process'); |
| 7 | + |
| 8 | +const RUNNER = path.join(__dirname, 'run-copilot-inference.cjs'); |
| 9 | + |
| 10 | +function makeFakeCopilot(source) { |
| 11 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fake-copilot-')); |
| 12 | + const file = path.join(dir, 'copilot'); |
| 13 | + fs.writeFileSync(file, `#!/usr/bin/env node\n${source}\n`, { mode: 0o755 }); |
| 14 | + return { dir, file }; |
| 15 | +} |
| 16 | + |
| 17 | +function outputValue(file, key) { |
| 18 | + const text = fs.readFileSync(file, 'utf8'); |
| 19 | + const match = text.match(new RegExp(`${key}<<([^\\n]+)\\n([\\s\\S]*?)\\n\\1(?:\\n|$)`)); |
| 20 | + assert.ok(match, `missing ${key} output in ${text}`); |
| 21 | + return match[2]; |
| 22 | +} |
| 23 | + |
| 24 | +test('streams a large prompt over stdin and maps the Copilot token to GITHUB_TOKEN', () => { |
| 25 | + const fake = makeFakeCopilot(` |
| 26 | + const fs = require('node:fs'); |
| 27 | + const input = fs.readFileSync(0, 'utf8'); |
| 28 | + const argvBytes = Buffer.byteLength(process.argv.slice(2).join(' ')); |
| 29 | + if (argvBytes > 8192) { |
| 30 | + console.error('prompt leaked into argv'); |
| 31 | + process.exit(91); |
| 32 | + } |
| 33 | + process.stdout.write(JSON.stringify({ |
| 34 | + inputBytes: Buffer.byteLength(input), |
| 35 | + argv: process.argv.slice(2), |
| 36 | + githubToken: process.env.GITHUB_TOKEN || '', |
| 37 | + })); |
| 38 | + `); |
| 39 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'copilot-runner-test-')); |
| 40 | + const promptFile = path.join(dir, 'prompt.txt'); |
| 41 | + const outputFile = path.join(dir, 'output.txt'); |
| 42 | + const prompt = 'x'.repeat(512 * 1024); |
| 43 | + fs.writeFileSync(promptFile, prompt); |
| 44 | + fs.writeFileSync(outputFile, ''); |
| 45 | + |
| 46 | + const result = spawnSync(process.execPath, [RUNNER, promptFile], { |
| 47 | + encoding: 'utf8', |
| 48 | + env: { |
| 49 | + ...process.env, |
| 50 | + PATH: `${fake.dir}${path.delimiter}${process.env.PATH}`, |
| 51 | + GITHUB_OUTPUT: outputFile, |
| 52 | + COPILOT_SYSTEM_PROMPT: 'system instruction', |
| 53 | + COPILOT_GITHUB_TOKEN: 'test-token', |
| 54 | + GITHUB_TOKEN: '', |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + assert.equal(result.status, 0, result.stderr); |
| 59 | + const response = JSON.parse(outputValue(outputFile, 'response')); |
| 60 | + assert.ok(response.inputBytes > Buffer.byteLength(prompt)); |
| 61 | + assert.deepEqual(response.argv, ['-s', '--no-ask-user', '--no-custom-instructions', '--no-auto-update']); |
| 62 | + assert.equal(response.githubToken, 'test-token'); |
| 63 | +}); |
| 64 | + |
| 65 | +test('surfaces Copilot stderr and preserves a non-zero exit code', () => { |
| 66 | + const fake = makeFakeCopilot(` |
| 67 | + process.stdin.resume(); |
| 68 | + process.stdin.on('end', () => { |
| 69 | + console.error('copilot auth failed: test diagnostic'); |
| 70 | + process.exit(7); |
| 71 | + }); |
| 72 | + `); |
| 73 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'copilot-runner-test-')); |
| 74 | + const promptFile = path.join(dir, 'prompt.txt'); |
| 75 | + const outputFile = path.join(dir, 'output.txt'); |
| 76 | + fs.writeFileSync(promptFile, 'hello'); |
| 77 | + fs.writeFileSync(outputFile, ''); |
| 78 | + |
| 79 | + const result = spawnSync(process.execPath, [RUNNER, promptFile], { |
| 80 | + encoding: 'utf8', |
| 81 | + env: { |
| 82 | + ...process.env, |
| 83 | + PATH: `${fake.dir}${path.delimiter}${process.env.PATH}`, |
| 84 | + GITHUB_OUTPUT: outputFile, |
| 85 | + COPILOT_SYSTEM_PROMPT: 'system instruction', |
| 86 | + COPILOT_GITHUB_TOKEN: 'test-token', |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + assert.equal(result.status, 7); |
| 91 | + assert.match(result.stderr, /copilot auth failed: test diagnostic/); |
| 92 | + assert.equal(fs.readFileSync(outputFile, 'utf8'), ''); |
| 93 | +}); |
| 94 | + |
| 95 | +test('kills a hung Copilot process at the configured timeout', () => { |
| 96 | + const fake = makeFakeCopilot(` |
| 97 | + process.stderr.write('copilot started\\n'); |
| 98 | + setInterval(() => {}, 1000); |
| 99 | + `); |
| 100 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'copilot-runner-test-')); |
| 101 | + const promptFile = path.join(dir, 'prompt.txt'); |
| 102 | + const outputFile = path.join(dir, 'output.txt'); |
| 103 | + fs.writeFileSync(promptFile, 'hello'); |
| 104 | + fs.writeFileSync(outputFile, ''); |
| 105 | + |
| 106 | + const result = spawnSync(process.execPath, [RUNNER, promptFile], { |
| 107 | + encoding: 'utf8', |
| 108 | + timeout: 5000, |
| 109 | + env: { |
| 110 | + ...process.env, |
| 111 | + PATH: `${fake.dir}${path.delimiter}${process.env.PATH}`, |
| 112 | + GITHUB_OUTPUT: outputFile, |
| 113 | + COPILOT_SYSTEM_PROMPT: 'system instruction', |
| 114 | + COPILOT_GITHUB_TOKEN: 'test-token', |
| 115 | + COPILOT_TIMEOUT_MS: '75', |
| 116 | + }, |
| 117 | + }); |
| 118 | + |
| 119 | + assert.notEqual(result.status, 0); |
| 120 | + assert.match(result.stderr, /ETIMEDOUT/); |
| 121 | + assert.match(result.stderr, /SIGKILL/); |
| 122 | + assert.equal(fs.readFileSync(outputFile, 'utf8'), ''); |
| 123 | +}); |
0 commit comments