import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { resolve, dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { mkdtempSync, writeFileSync, rmSync, existsSync, readdirSync, readFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { execFileSync } from 'node:child_process'; import { createIsolatedEnv, updateIsolatedConfig, type IsolatedEnv, } from '../helpers/isolated-env'; import { runTakt } from '../helpers/takt-runner'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); function createLocalRepo(): { path: string; cleanup: () => void } { const repoPath = mkdtempSync(join(tmpdir(), 'takt-e2e-report-file-')); execFileSync('git', ['init'], { cwd: repoPath, stdio: 'pipe' }); execFileSync('git', ['config', 'user.email', 'test@example.com'], { cwd: repoPath, stdio: 'pipe' }); execFileSync('git', ['config', 'user.name', 'Test'], { cwd: repoPath, stdio: 'pipe' }); writeFileSync(join(repoPath, 'README.md'), '# test\n'); execFileSync('git', ['add', '.'], { cwd: repoPath, stdio: 'pipe' }); execFileSync('git', ['commit', '-m', 'init'], { cwd: repoPath, stdio: 'pipe' }); return { path: repoPath, cleanup: () => { try { rmSync(repoPath, { recursive: true, force: true }); } catch { /* best-effort */ } }, }; } // E2E更新時は docs/testing/e2e.md も更新すること describe('E2E: Report file output (mock)', () => { let isolatedEnv: IsolatedEnv; let repo: { path: string; cleanup: () => void }; beforeEach(() => { isolatedEnv = createIsolatedEnv(); updateIsolatedConfig(isolatedEnv.taktDir, { provider: 'mock', }); repo = createLocalRepo(); }); afterEach(() => { try { repo.cleanup(); } catch { /* best-effort */ } try { isolatedEnv.cleanup(); } catch { /* best-effort */ } }); it('should write report file to .takt/runs/*/reports with expected content', () => { const piecePath = resolve(__dirname, '../fixtures/pieces/report-judge.yaml'); const scenarioPath = resolve(__dirname, '../fixtures/scenarios/report-judge.json'); const result = runTakt({ args: [ '--task', 'Test report output', '--piece', piecePath, '--create-worktree', 'no', '--provider', 'mock', ], cwd: repo.path, env: { ...isolatedEnv.env, TAKT_MOCK_SCENARIO: scenarioPath, }, timeout: 240_000, }); expect(result.exitCode).toBe(0); const runsDir = join(repo.path, '.takt', 'runs'); expect(existsSync(runsDir)).toBe(true); const runDirs = readdirSync(runsDir).sort(); expect(runDirs.length).toBeGreaterThan(0); const latestRun = runDirs[runDirs.length - 1]!; const reportPath = join(runsDir, latestRun, 'reports', 'report.md'); expect(existsSync(reportPath)).toBe(true); const report = readFileSync(reportPath, 'utf-8'); expect(report).toContain('Report summary: OK'); }, 240_000); });