c7b01b31cb
The job only lived in memory, so every deploy discarded hours of proxy work and the next run started at the first photo. Mirror progress to the storage root after each photo and pick the pending ids back up on boot. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
88 lines
3.0 KiB
JavaScript
88 lines
3.0 KiB
JavaScript
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('reports progress and what is still pending after each photo', async () => {
|
|
const steps = [];
|
|
const job = startPreviewRebuild({
|
|
targets: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
regenerate: async () => ({ sidecar: false }),
|
|
onProgress: (snapshot, pending) =>
|
|
steps.push({ done: snapshot.done, pending: pending.map((photo) => photo.id) }),
|
|
});
|
|
await job.finished;
|
|
expect(steps).toEqual([
|
|
{ done: 1, pending: ['b', 'c'] },
|
|
{ done: 2, pending: ['c'] },
|
|
{ done: 3, pending: [] },
|
|
]);
|
|
});
|
|
|
|
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 });
|
|
});
|
|
});
|