/** * Regression guard — reopen→re-release export integrity + the stale-keepsake data-loss fix (H1). * * Two related bugs on this path: * * (1) Temp-file race: a second worker spawned by a re-release could stomp the first * worker's FIXED temp path → a corrupt/truncated keepsake ZIP. * * (2) Stale keepsake (silent data loss): a worker from the PRIOR release, still streaming a * snapshot taken BEFORE a reopen, would finish and unconditionally re-flip * `export_*_ready = TRUE` on an archive that predated the reopen — so uploads added * during the reopen window were permanently missing from a "ready" keepsake. * * The fix: a per-(event,type) generation counter `release_seq`, bumped on every (re)release. * A worker captures the seq it claimed and writes per-generation temp/final paths * (`Gallery..zip`); its finalize and ready-flag flip are guarded by `release_seq`, so a * superseded worker discards its output instead of resurrecting a stale keepsake. The * download follows the current `done` row's `file_path`, never a fixed name. * * A second, narrower window on the SAME class of bug (fixed alongside): a reopen (`open_event`) * landing between a *current* worker's `finalize_job` and its ready-flag flip. `open_event` * clears `export_released_at` + both ready flags but leaves the `export_job` row `done` at the * current seq — so the seq-guarded flip would still match and resurrect `export_zip_ready=TRUE` * on a pre-reopen snapshot, and the next re-release would `if ready { continue }` and skip * regeneration. The flip UPDATEs are therefore additionally anchored on * `export_released_at IS NOT NULL`, making them a no-op once a reopen has landed. The * "completeness" test below exercises the real reopen→re-release worker path end-to-end; the * sub-millisecond finalize↔flip interleave itself isn't deterministically forceable with the * fast fixtures, so that exact window is covered by the SQL guard + code review rather than a * timing-dependent assertion. * * Coverage: * - churn integrity: rapid reopen→re-release yields exactly one INTACT ZIP, no stuck jobs. * - completeness: an upload added during the reopen window IS present in the re-released * keepsake (the direct data-loss regression). * - supersession: a re-release SUPERSEDES an in-flight (running) job — it bumps the * generation and regenerates, rather than leaving the stale worker to finalize. */ import { test, expect } from '../../fixtures/test'; import { Client } from 'pg'; import { execFileSync } from 'node:child_process'; import { writeFileSync, mkdtempSync, readFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { seedUpload } from '../../helpers/seed'; import { uploadRaw } from '../../helpers/upload-client'; const BASE = process.env.E2E_FRONTEND_URL ?? 'http://localhost:3101'; const SLUG = 'e2e-test-event'; const N_UPLOADS = 6; const PG = { host: process.env.E2E_DB_HOST ?? 'localhost', port: Number(process.env.E2E_DB_PORT ?? '55432'), user: process.env.E2E_DB_USER ?? 'eventsnap_test', password: process.env.E2E_DB_PASSWORD ?? 'eventsnap_test', database: process.env.E2E_DB_NAME ?? 'eventsnap_test', }; async function pgQuery(sql: string): Promise { const c = new Client(PG); await c.connect(); try { return (await c.query(sql)).rows as T[]; } finally { await c.end(); } } function post(path: string, jwt: string) { return fetch(BASE + path, { method: 'POST', headers: { Authorization: `Bearer ${jwt}` } }); } async function exportStatus(jwt: string): Promise { const res = await fetch(BASE + '/api/v1/export/status', { headers: { Authorization: `Bearer ${jwt}` } }); return res.json(); } async function mintTicket(jwt: string): Promise { const res = await post('/api/v1/export/ticket', jwt); return (await res.json()).ticket; } async function waitExportDone(jwt: string) { await expect .poll(async () => { const s = await exportStatus(jwt); return s.released === true && s.zip?.status === 'done' && s.html?.status === 'done'; }, { timeout: 60_000, intervals: [500] }) .toBe(true); } /** Download the current ZIP keepsake and return its (non-directory) entry names. */ async function downloadZipEntries(jwt: string): Promise { const ticket = await mintTicket(jwt); const res = await fetch(BASE + '/api/v1/export/zip?ticket=' + encodeURIComponent(ticket)); expect(res.status).toBe(200); const bytes = Buffer.from(await res.arrayBuffer()); expect(bytes.subarray(0, 2).toString('latin1')).toBe('PK'); // ZIP magic — not a stomped file const dir = mkdtempSync(join(tmpdir(), 'es-zip-')); const zipPath = join(dir, 'Gallery.zip'); writeFileSync(zipPath, bytes); execFileSync('unzip', ['-t', zipPath], { stdio: 'pipe' }); // CRC integrity; throws on corruption return execFileSync('unzip', ['-Z1', zipPath], { encoding: 'utf8' }) .split('\n') .map((l) => l.trim()) .filter((l) => l.length > 0 && !l.endsWith('/')); } async function zipReleaseSeq(): Promise { const [row] = await pgQuery<{ release_seq: string }>( `SELECT ej.release_seq FROM export_job ej JOIN event e ON e.id = ej.event_id WHERE e.slug = '${SLUG}' AND ej.type = 'zip'` ); return Number(row.release_seq); } test.describe('Flow re-review — reopen→re-release export integrity + stale-keepsake (H1)', () => { // The real export job runs image processing; give it head-room over the tiny fixtures. test.setTimeout(120_000); test('rapid release/reopen/re-release yields exactly one INTACT ZIP and no stuck jobs', async ({ host, }) => { // Seed real, decodable uploads while the event is open. for (let i = 0; i < N_UPLOADS; i++) { await seedUpload(host.jwt, { caption: `pic ${i}` }); } // First release → export starts and uploads lock (release ⇒ lock). expect((await post('/api/v1/host/gallery/release', host.jwt)).status).toBe(204); // Post-release uploads must be rejected (belt-and-suspenders on top of the lock). const rejected = await uploadRaw(host.jwt, readFileSync(join(process.cwd(), 'fixtures', 'media', 'sample.jpg')), { filename: 'late.jpg', contentType: 'image/jpeg', }); expect(rejected.status).toBe(403); // Churn: reopen (clears release + ready flags) then re-release, several times in // quick succession. This is the path that used to be able to double-spawn workers // over the shared temp file. End on a release so the gallery is left released. for (let i = 0; i < 4; i++) { expect((await post('/api/v1/host/event/open', host.jwt)).status).toBe(204); const rel = await post('/api/v1/host/gallery/release', host.jwt); expect(rel.status).toBe(204); } await waitExportDone(host.jwt); // No export_job may be left stuck 'running'. const jobs = await pgQuery<{ type: string; status: string }>( `SELECT ej.type::text AS type, ej.status::text AS status FROM export_job ej JOIN event e ON e.id = ej.event_id WHERE e.slug = '${SLUG}'` ); expect(jobs.length).toBe(2); for (const j of jobs) expect(j.status, `${j.type} job status`).toBe('done'); // Exactly one entry per seeded upload — no entry lost to a stomped temp file, none // duplicated by a second worker. const listing = await downloadZipEntries(host.jwt); expect(listing.length, `zip entries:\n${listing.join('\n')}`).toBe(N_UPLOADS); }); test('an upload added during the reopen window IS present in the re-released keepsake (H1)', async ({ host, }) => { // Release an initial keepsake of 3 uploads. for (let i = 0; i < 3; i++) await seedUpload(host.jwt, { caption: `orig ${i}` }); expect((await post('/api/v1/host/gallery/release', host.jwt)).status).toBe(204); await waitExportDone(host.jwt); expect((await downloadZipEntries(host.jwt)).length).toBe(3); // Reopen (invalidates the release) → add a 4th upload while unlocked → re-release. expect((await post('/api/v1/host/event/open', host.jwt)).status).toBe(204); await seedUpload(host.jwt, { caption: 'added-during-reopen' }); expect((await post('/api/v1/host/gallery/release', host.jwt)).status).toBe(204); await waitExportDone(host.jwt); // The re-released keepsake MUST contain all 4 — the stale-keepsake bug would have left // the download at the pre-reopen snapshot of 3, silently dropping the new upload. const listing = await downloadZipEntries(host.jwt); expect(listing.length, `zip entries:\n${listing.join('\n')}`).toBe(4); }); test('re-release SUPERSEDES an in-flight (running) export — bumps the generation', async ({ host, }) => { // Deterministic proof of the generation guard, independent of export timing. We pin the // zip job to `running` with a sentinel to mimic a worker from a prior release that is // still mid-export, then drive reopen→re-release. // // OLD behavior (the bug): the re-enqueue left a `running` row alone, so the stale worker // would eventually finalize and re-flip the ready flag on its pre-reopen snapshot. // NEW behavior (the fix): the re-enqueue bumps `release_seq` and resets the row, so the // stale generation is superseded — its sentinel is wiped and a fresh worker regenerates. await seedUpload(host.jwt, { caption: 'a' }); await seedUpload(host.jwt, { caption: 'b' }); expect((await post('/api/v1/host/gallery/release', host.jwt)).status).toBe(204); await waitExportDone(host.jwt); const seqBefore = await zipReleaseSeq(); // Simulate an in-flight worker owning the zip job at the current generation. await pgQuery( `UPDATE export_job ej SET status = 'running', progress_pct = 77 FROM event e WHERE e.id = ej.event_id AND e.slug = '${SLUG}' AND ej.type = 'zip'` ); // Reopen (clears release + ready flags; does NOT touch job rows) then re-release. expect((await post('/api/v1/host/event/open', host.jwt)).status).toBe(204); expect((await post('/api/v1/host/gallery/release', host.jwt)).status).toBe(204); // The fresh generation must settle to done at a HIGHER release_seq — proving the stale // running row was superseded (not left to finalize) and the sentinel is gone. await waitExportDone(host.jwt); const [row] = await pgQuery<{ status: string; progress_pct: number; release_seq: string }>( `SELECT ej.status::text AS status, ej.progress_pct, ej.release_seq FROM export_job ej JOIN event e ON e.id = ej.event_id WHERE e.slug = '${SLUG}' AND ej.type = 'zip'` ); expect(row.status, 'zip job status').toBe('done'); expect(Number(row.progress_pct), 'zip job progress').toBe(100); expect(Number(row.release_seq), 'release_seq advanced past the superseded generation').toBeGreaterThan( seqBefore ); }); });