import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { createHash } from 'node:crypto' import { describe, expect, it } from 'vitest' import { buildBrowserScreenshotPlan, hasDistinctScreenshotHashes } from '../../src/commands/test/browser-evidence.ts' function md5Of(value: string): string { return createHash('md5').update(value).digest('hex') } describe('browser record plan', () => { it('plans three checkpoints with stable distinct filenames', () => { const dir = mkdtempSync(join(tmpdir(), 'dacli-shots-')) try { const plan = buildBrowserScreenshotPlan(dir, 'workflow', '852451069744119813') expect(plan).toHaveLength(3) expect(plan[0]).toContain('workflow-852451069744119813-01-page-loaded.png') expect(plan[1]).toContain('workflow-852451069744119813-02-after-checks.png') expect(plan[2]).toContain('workflow-852451069744119813-03-final.png') } finally { rmSync(dir, { recursive: true, force: true }) } }) it('treats three identical captures as a degenerate artifact set', () => { const dir = mkdtempSync(join(tmpdir(), 'dacli-shots-')) try { mkdirSync(dir, { recursive: true }) const plan = buildBrowserScreenshotPlan(dir, 'workflow', '852451069744119813') const artifacts: Array<{ path: string; md5: string }> = [] for (const path of plan) { writeFileSync(path, 'aaaa') artifacts.push({ path, md5: md5Of(readFileSync(path, 'utf-8')) }) } expect(artifacts).toHaveLength(3) expect(hasDistinctScreenshotHashes(artifacts)).toBe(false) } finally { rmSync(dir, { recursive: true, force: true }) } }) }) describe('browser record md5', () => { it('marks distinct content as distinct even when filenames repeat', () => { const artifacts: Array<{ path: string; md5: string }> = [ { path: 'a.png', md5: md5Of('payload-1') }, { path: 'b.png', md5: md5Of('payload-2') }, { path: 'c.png', md5: md5Of('payload-3') }, ] expect(hasDistinctScreenshotHashes(artifacts)).toBe(true) }) })