|
| 1 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import http from 'node:http'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 8 | + |
| 9 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..'); |
| 10 | +const CLI = path.join(ROOT, 'dist/index.js'); |
| 11 | +const URLS = [41, 42].map((n) => `https://github.com/team/repo/pull/${n}`); |
| 12 | + |
| 13 | +describe('import --from-mr with interrupted GitHub responses', () => { |
| 14 | + let sandbox: string; |
| 15 | + let preload: string; |
| 16 | + |
| 17 | + beforeAll(() => { |
| 18 | + sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'teamai-mr-response-')); |
| 19 | + const repo = path.join(sandbox, 'repo'); |
| 20 | + fs.mkdirSync(repo); |
| 21 | + fs.writeFileSync(path.join(repo, 'teamai.yaml'), 'team: response-errors\nrepo: https://example.com/team.git\nprovider: git\n'); |
| 22 | + fs.mkdirSync(path.join(sandbox, '.teamai')); |
| 23 | + fs.writeFileSync(path.join(sandbox, '.teamai/config.yaml'), [ |
| 24 | + 'repo:', ` localPath: ${JSON.stringify(repo)}`, ' remote: https://example.com/team.git', |
| 25 | + 'username: test-user', 'scope: user', '', |
| 26 | + ].join('\n')); |
| 27 | + preload = path.join(sandbox, 'github-fixture.mjs'); |
| 28 | + // Only replace the external boundaries: the built CLI reads real HTTP |
| 29 | + // responses, including IncomingMessage's error after a socket closes early. |
| 30 | + fs.writeFileSync(preload, ` |
| 31 | +import childProcess from 'node:child_process'; |
| 32 | +import http from 'node:http'; |
| 33 | +import https from 'node:https'; |
| 34 | +import { syncBuiltinESMExports } from 'node:module'; |
| 35 | +const execSync = childProcess.execSync; |
| 36 | +childProcess.execSync = (command, ...args) => { |
| 37 | + if (command.startsWith('gh pr ')) throw new Error('gh unavailable in fixture'); |
| 38 | + return execSync(command, ...args); |
| 39 | +}; |
| 40 | +const request = https.request; |
| 41 | +https.request = (options, callback) => options.hostname === 'api.github.com' |
| 42 | + ? http.request({ ...options, protocol: 'http:', hostname: '127.0.0.1', port: process.env.FIXTURE_PORT }, callback) |
| 43 | + : request(options, callback); |
| 44 | +syncBuiltinESMExports(); |
| 45 | +`); |
| 46 | + }); |
| 47 | + |
| 48 | + afterAll(() => { if (sandbox) fs.rmSync(sandbox, { recursive: true, force: true }); }); |
| 49 | + |
| 50 | + it.each(['metadata', 'commits', 'diff'])( |
| 51 | + 'reports an interrupted %s response and continues to the next PR', async (endpoint) => { |
| 52 | + const interrupted: string[] = []; |
| 53 | + const server = http.createServer((req, res) => { |
| 54 | + const stage = req.headers.accept?.includes('.diff') ? 'diff' |
| 55 | + : req.url?.includes('/commits?') ? 'commits' : 'metadata'; |
| 56 | + if (stage === endpoint) { |
| 57 | + interrupted.push(req.url!); |
| 58 | + res.writeHead(200, { 'Content-Length': '10000' }); |
| 59 | + res.write('partial response'); |
| 60 | + setImmediate(() => res.destroy()); |
| 61 | + return; |
| 62 | + } |
| 63 | + res.end(JSON.stringify(stage === 'metadata' |
| 64 | + ? { title: 'Test PR', body: '', user: { login: 'dev' }, merged_at: null } |
| 65 | + : [{ sha: 'abc123', commit: { message: 'Test commit' } }])); |
| 66 | + }); |
| 67 | + await new Promise<void>((resolve) => { server.listen(0, '127.0.0.1', resolve); }); |
| 68 | + try { |
| 69 | + const address = server.address(); |
| 70 | + if (!address || typeof address === 'string') throw new Error('Expected a TCP address'); |
| 71 | + const result = await new Promise<{ code: number | null; output: string }>((resolve, reject) => { |
| 72 | + const child = spawn(process.execPath, [ |
| 73 | + '--import', pathToFileURL(preload).href, CLI, 'import', '--from-mr', ...URLS, |
| 74 | + '--all', '--output', path.join(sandbox, 'output'), |
| 75 | + ], { |
| 76 | + cwd: sandbox, |
| 77 | + env: { |
| 78 | + ...process.env, HOME: sandbox, USERPROFILE: sandbox, FORCE_COLOR: '0', |
| 79 | + GITHUB_TOKEN: '', GH_TOKEN: '', FIXTURE_PORT: String(address.port), |
| 80 | + }, |
| 81 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 82 | + }); |
| 83 | + let output = ''; |
| 84 | + child.stdout.on('data', (chunk: Buffer) => { output += chunk.toString(); }); |
| 85 | + child.stderr.on('data', (chunk: Buffer) => { output += chunk.toString(); }); |
| 86 | + const timer = setTimeout(() => { child.kill(); reject(new Error(`CLI timed out: ${output}`)); }, 20_000); |
| 87 | + child.on('error', (error) => { clearTimeout(timer); reject(error); }); |
| 88 | + child.on('close', (code) => { clearTimeout(timer); resolve({ code, output }); }); |
| 89 | + }); |
| 90 | + expect(result.code, result.output).toBe(1); |
| 91 | + expect(result.output).toContain('Import from MR complete: 0 succeeded, 2 failed'); |
| 92 | + for (const url of URLS) expect(result.output).toContain(`import from MR failed: ${url}`); |
| 93 | + expect(result.output).not.toContain("Unhandled 'error' event"); |
| 94 | + expect(result.output).not.toContain('AI analysis in progress'); |
| 95 | + expect(interrupted).toHaveLength(2); |
| 96 | + expect(fs.existsSync(path.join(sandbox, 'output'))).toBe(false); |
| 97 | + } finally { |
| 98 | + server.closeAllConnections(); |
| 99 | + await new Promise<void>((resolve, reject) => { server.close((error) => error ? reject(error) : resolve()); }); |
| 100 | + } |
| 101 | + }, |
| 102 | + ); |
| 103 | +}); |
0 commit comments