import { describe, expect, test, vi } from 'vitest';

import {
  abortManagedCodexProcess,
  managedCodexCommand,
} from '../../src/codex-process';

describe('managed Codex process', () => {
  test('launches Codex in a job-specific process session', () => {
    const command = managedCodexCommand({
      jobId: 'job-1',
      pidFile: '/home/alice/.codex/job-1.pid',
      command: ['codex', 'exec', '--json', 'fix it'],
    });

    expect(command.slice(0, 2)).toEqual(['python3', '-c']);
    expect(command).toContain('/home/alice/.codex/job-1.pid');
    expect(command).toContain('job-1');
    expect(command.slice(-4)).toEqual(['codex', 'exec', '--json', 'fix it']);
  });

  test('invokes an isolated in-box kill for the matching job', async () => {
    const execute = vi.fn().mockResolvedValue({ exitCode: 0, output: '' });

    await abortManagedCodexProcess({
      jobId: 'job-1',
      pidFile: '/home/alice/.codex/job-1.pid',
      execute,
    });

    expect(execute).toHaveBeenCalledOnce();
    const [command] = execute.mock.calls[0] as [string[]];
    expect(command.slice(0, 2)).toEqual(['python3', '-c']);
    expect(command).toContain('/home/alice/.codex/job-1.pid');
    expect(command).toContain('job-1');
  });
});
