agent-import.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { spawnSync } from 'node:child_process';
  2. import { createRequire } from 'node:module';
  3. import { fileURLToPath, pathToFileURL } from 'node:url';
  4. import { expect, it } from 'vitest';
  5. const require = createRequire(import.meta.url);
  6. it('rejects unterminated TOML comments without blocking agent import', () => {
  7. const serviceUrl = new URL('../../../src/service/agent/application/agent-import.ts', import.meta.url);
  8. const root = fileURLToPath(new URL('../../../../../', import.meta.url));
  9. // A synchronous parser loop cannot be interrupted by a Vitest timeout.
  10. // Run the real import service in a directly owned, killable child process.
  11. const result = spawnSync(process.execPath, [
  12. '--import', pathToFileURL(require.resolve('tsx')).href,
  13. '--input-type=module', '--eval', `
  14. import assert from 'node:assert/strict';
  15. import { AgentImportService } from ${JSON.stringify(serviceUrl.href)};
  16. const service = new AgentImportService();
  17. assert.equal(service.preview('codex', 'name="sample"').proposedName, 'sample');
  18. for (const content of ['a=[1 #', 'a={b=1 #']) {
  19. assert.throws(() => service.preview('codex', content), {
  20. code: 'AGENT_CONFIG_INVALID',
  21. });
  22. }
  23. console.log('import-rejected-malformed-toml');
  24. `,
  25. ], {
  26. cwd: root,
  27. env: {
  28. ...process.env,
  29. TSX_TSCONFIG_PATH: fileURLToPath(new URL('../../../../../tsconfig.standalone.json', import.meta.url)),
  30. },
  31. encoding: 'utf8',
  32. timeout: 10_000,
  33. killSignal: 'SIGKILL',
  34. });
  35. expect(result.error, result.stderr).toBeUndefined();
  36. expect(result.status, result.stderr).toBe(0);
  37. expect(result.stdout.trim()).toBe('import-rejected-malformed-toml');
  38. }, 15_000);