import { afterEach, beforeEach, expect, test } from 'bun:test'; import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { type LastRun, readLastRun, writeLastRun } from './last-run'; let dir: string; beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'e2e-last-')); process.env.CELILO_E2E_LAST_RUN_PATH = join(dir, 'last-run.json'); }); afterEach(() => { rmSync(dir, { recursive: true, force: true }); delete process.env.CELILO_E2E_LAST_RUN_PATH; }); const RUN: LastRun = { runId: 'abc-123', resultsDir: '/repo/e2e/results/2026-08-12T10-00-00', startedAt: '2026-08-12T17:00:00.000Z', status: 'completed', total: 3, passed: 3, failed: 0, skipped: 0, durationMs: 600_000, }; test('with no run recorded, readLastRun is null rather than throwing', () => { expect(readLastRun()).toBeNull(); }); test('a recorded run round-trips every field `cele2e last --json` promises', () => { writeLastRun(RUN); expect(readLastRun()).toEqual(RUN); }); test('the record is overwritten, so `last` never returns a stale earlier run', () => { // The whole point: inferring "my results dir" from `ls -t results | head -1` // hands back the PREVIOUS run's numbers when a run refuses to start, and a // suite that never executed then reads as a clean pass. writeLastRun(RUN); writeLastRun({ ...RUN, runId: 'def-456', resultsDir: '/repo/e2e/results/later', failed: 2 }); const last = readLastRun(); expect(last?.runId).toBe('def-456'); expect(last?.resultsDir).toBe('/repo/e2e/results/later'); expect(last?.failed).toBe(2); }); test('a run still in flight is recorded as running, so its logs are findable', () => { writeLastRun({ ...RUN, status: 'running', passed: 0, total: 3 }); expect(readLastRun()?.status).toBe('running'); expect(readLastRun()?.resultsDir).toBe(RUN.resultsDir); }); test('a corrupt record is null, not fatal', () => { writeLastRun(RUN); Bun.write(process.env.CELILO_E2E_LAST_RUN_PATH as string, 'not json{'); expect(readLastRun()).toBeNull(); });