import { describe, expect, it } from 'vitest'; import { previewRebuildTargets, startPreviewRebuild } from './previewRebuild.mjs'; describe('preview rebuild targets', () => { it('takes live camera raws and nothing else', () => { const photos = [ { id: 'raw', raw: true }, { id: 'jpeg', raw: false }, { id: 'trashed', raw: true, deletedAt: 1 }, { id: 'unknown' }, ]; expect(previewRebuildTargets(photos).map((photo) => photo.id)).toEqual(['raw']); }); }); describe('preview rebuild job', () => { it('walks every target in order and counts the 16-bit sidecars', async () => { const seen = []; const job = startPreviewRebuild({ targets: [{ id: 'a' }, { id: 'b' }], regenerate: async (photo) => { seen.push(photo.id); return { sidecar: photo.id === 'a' }; }, now: () => 7, }); expect(job.snapshot()).toMatchObject({ state: 'running', total: 2, done: 0 }); await job.finished; expect(seen).toEqual(['a', 'b']); expect(job.snapshot()).toMatchObject({ state: 'done', done: 2, failed: 0, sidecars: 1, startedAt: 7, finishedAt: 7, }); }); it('keeps going after a photo that cannot be decoded', async () => { const job = startPreviewRebuild({ targets: [{ id: 'a', name: 'A.CR2' }, { id: 'b', name: 'B.CR2' }], regenerate: async (photo) => { if (photo.id === 'a') throw new Error('the original is missing'); return { sidecar: false }; }, }); await job.finished; const snapshot = job.snapshot(); expect(snapshot).toMatchObject({ state: 'done', done: 2, failed: 1, sidecars: 0 }); expect(snapshot.errors[0]).toContain('A.CR2'); expect(snapshot.errors[0]).toContain('the original is missing'); }); it('hands out copies, so a caller cannot edit the live progress', async () => { const job = startPreviewRebuild({ targets: [{ id: 'a' }], regenerate: async () => ({ sidecar: false }), }); const snapshot = job.snapshot(); snapshot.errors.push('tampered'); await job.finished; expect(job.snapshot().errors).toEqual([]); }); it('finishes immediately when there is nothing to rebuild', async () => { const job = startPreviewRebuild({ targets: [], regenerate: async () => ({ sidecar: false }) }); await job.finished; expect(job.snapshot()).toMatchObject({ state: 'done', total: 0, done: 0, failed: 0 }); }); });