import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import request from 'supertest'; import { hashToken } from '../src/lib/crypto.js'; import { parseSectionData } from '../src/site/sections.js'; import { harness, SITE } from './support/harness.js'; async function site() { const uploads = await fs.mkdtemp(path.join(os.tmpdir(), 'ccw-media-')); const h = await harness({ linked: false, env: { UPLOAD_DIR: uploads } }); const id = '00000000-0000-4000-8000-00000000000a'; await h.db.websiteUser.create({ data: { id, name: 'Ann', email: 'ann@hope.test', role: 'editor' } }); await h.db.session.create({ data: { tokenHash: hashToken('ann'), userId: id, expiresAt: new Date(Date.now() + 864e5) } }); const go = (m: 'get' | 'delete', p: string) => request(h.app)[m](`/api/v1/admin${p}`).set('Cookie', 'ccw_session=ann').set('Origin', SITE); return { ...h, go, uploads }; } describe('deleting photos', () => { it('deletes one that is not used, with its files, and refuses one that is, saying where', async () => { const { go, db, uploads, ctx } = await site(); const dir = path.resolve(ctx.config.uploadDir ?? uploads); await fs.mkdir(path.join(dir, '2026/11'), { recursive: true }); for (const f of ['old.jpg', 'old-480.webp', 'old-480.jpg']) await fs.writeFile(path.join(dir, '2026/11', f), 'x'); const old = await db.mediaAsset.create({ data: { filename: '2026/11/old.jpg', mime: 'image/jpeg', bytes: 3, width: 960, sizes: [{ w: 480, webp: 'old-480.webp', jpg: 'old-480.jpg' }] } }); const used = await db.mediaAsset.create({ data: { filename: '2026/11/used.jpg', mime: 'image/jpeg', bytes: 3 } }); const page = await db.page.create({ data: { kind: 'home', slug: 'home', title: 'Home' } }); await db.section.create({ data: { pageId: page.id, type: 'hero', data: { headline: 'Hi', images: [used.id] } } }); const list = (await go('get', '/media')).body.items as { id: string; usedIn: string[] }[]; assert.deepEqual(list.find((m) => m.id === used.id)!.usedIn, ['Home page']); assert.deepEqual(list.find((m) => m.id === old.id)!.usedIn, []); const refused = await go('delete', `/media/${used.id}`); assert.equal(refused.status, 409); assert.match(refused.body.error.message, /Home page/); assert.ok(await db.mediaAsset.findUnique({ where: { id: used.id } })); assert.equal((await go('delete', `/media/${old.id}`)).status, 200); assert.equal(await db.mediaAsset.findUnique({ where: { id: old.id } }), null); assert.deepEqual(await fs.readdir(path.join(dir, '2026/11')), []); assert.equal((await go('delete', `/media/${old.id}`)).status, 404); }); }); describe('framing a photo in its frame', () => { it('keeps where each photo sits and how far it is zoomed, and refuses nonsense', () => { const id = '11111111-1111-4111-8111-111111111111'; const ok = parseSectionData('hero', { headline: 'Hi', images: [id], framing: { [id]: { x: 30, y: 60, zoom: 1.5 } } }); assert.deepEqual(ok?.framing, { [id]: { x: 30, y: 60, zoom: 1.5 } }); assert.equal(parseSectionData('image-text', {})?.framing, undefined); // A photo taken out of the section takes its framing with it (or it would count as "in use"). assert.deepEqual(parseSectionData('hero', { headline: 'Hi', images: [], framing: { [id]: { x: 30, y: 60, zoom: 1 } } })?.framing, {}); assert.equal(parseSectionData('hero', { headline: 'Hi', framing: { [id]: { x: 300, y: 0, zoom: 1 } } }), null); }); });