Files
glenloch-media/e2e/photo-archive.spec.ts
T
2026-11-20 04:07:02 +00:00

150 lines
4.7 KiB
TypeScript

import { test, expect } from "@playwright/test";
import { access, mkdir, readFile, readdir, rm } from "node:fs/promises";
import path from "node:path";
import sharp from "sharp";
import { signInAs, resetDb, BASE } from "./dev";
/**
* The finish-workflow APIs: save an edit, export a delivery size, archive the
* event to the mounted destination, and remove the live event only after the
* archive is complete. Runs against the isolated `media_e2e` database.
*/
const ARCHIVE_ROOT = process.env.ARCHIVE_DIR ?? "/tmp/glenloch-e2e-archive";
const ARCHIVE_FOLDER = "E2E Archive";
const UPLOAD_DIR = process.env.UPLOAD_DIR ?? "/tmp/glenloch-e2e-uploads";
let photoBytes: Buffer;
test.beforeAll(async ({ browser }) => {
const context = await browser.newContext();
const page = await context.newPage();
await resetDb(page);
await context.close();
photoBytes = await sharp({
create: {
width: 1200,
height: 800,
channels: 3,
background: { r: 80, g: 120, b: 160 },
},
})
.jpeg({ quality: 95 })
.toBuffer();
await mkdir(ARCHIVE_ROOT, { recursive: true });
await rm(path.join(ARCHIVE_ROOT, ARCHIVE_FOLDER), {
recursive: true,
force: true,
});
});
test("exports a delivery size, then archives and removes the event", async ({
page,
}) => {
await page.setViewportSize({ width: 1200, height: 900 });
await signInAs(page, "admin@glenloch.local");
const configured = await page.request.put(`${BASE}/api/dev/storage`, {
data: {
nfsShare: "e2e-nas:/export/church",
archivePath: ARCHIVE_ROOT,
archiveFolder: ARCHIVE_FOLDER,
},
});
expect(configured.ok()).toBe(true);
const storage = (await configured.json()) as { status?: { ready: boolean } };
expect(storage.status?.ready).toBe(true);
const eventResponse = await page.request.post(`${BASE}/api/events`, {
data: {
title: "E2E archive workflow",
eventType: "photoshoot",
startsAtLocal: "2026-11-20T10:00",
location: "Main campus",
},
});
expect(eventResponse.ok()).toBe(true);
const event = (await eventResponse.json()) as { event: { id: string } };
const folderResponse = await page.request.post(
`${BASE}/api/events/${event.event.id}/folders`,
{ data: { name: "Originals", kind: "originals" } },
);
expect(folderResponse.ok()).toBe(true);
const folder = (await folderResponse.json()) as { folder: { id: string } };
const upload = await page.request.post(
`${BASE}/api/events/${event.event.id}/folders/${folder.folder.id}/photos?name=archive-test.jpg`,
{
headers: { "content-type": "image/jpeg" },
data: photoBytes,
},
);
expect(upload.ok()).toBe(true);
const uploaded = (await upload.json()) as { photo: { id: string } };
const edit = await page.request.post(
`${BASE}/api/events/${event.event.id}/folders/${folder.folder.id}/photos/${uploaded.photo.id}/edit`,
{
data: {
adjustments: {
exposure: 0.4,
contrast: 8,
blackPoint: 0,
brightness: 0,
curves: [
{ x: 0, y: 0 },
{ x: 1, y: 1 },
],
},
},
},
);
expect(edit.ok()).toBe(true);
const exported = await page.request.get(
`${BASE}/api/events/${event.event.id}/folders/${folder.folder.id}/photos/${uploaded.photo.id}/export?size=640`,
);
expect(exported.ok()).toBe(true);
expect(exported.headers()["content-disposition"]).toContain(
"archive-test-640.jpg",
);
const exportMeta = await sharp(await exported.body()).metadata();
expect(exportMeta.width).toBe(640);
expect(exportMeta.height).toBe(427);
const archived = await page.request.post(
`${BASE}/api/events/${event.event.id}/archive`,
);
expect(archived.ok()).toBe(true);
const archive = (await archived.json()) as {
archivePath: string;
files: number;
};
expect(archive.files).toBe(3);
const manifest = JSON.parse(
await readFile(path.join(archive.archivePath, "manifest.json"), "utf8"),
) as {
photos: { original: string; edited: string | null; editJson: string | null }[];
};
expect(manifest.photos).toHaveLength(1);
expect(manifest.photos[0].original).toContain("archive-test.jpg");
expect(manifest.photos[0].edited).toContain("archive-test-edited.jpg");
expect(manifest.photos[0].editJson).toContain('"exposure":0.4');
const entries = await readdir(archive.archivePath, { recursive: true });
expect(entries.some((entry) => entry.endsWith("archive-test.jpg"))).toBe(true);
expect(
entries.some((entry) => entry.endsWith("archive-test-edited.jpg")),
).toBe(true);
const gone = await page.goto(`${BASE}/events/${event.event.id}`);
expect(gone?.status()).toBe(404);
await expect(
access(path.join(UPLOAD_DIR, "events", event.event.id)),
).rejects.toThrow();
});