Files
crosscode-platform/apps/website/backend/tests/media.test.ts
T
joshuaandClaude Opus 5.5 a9215ac4e4 Website 1.7.0: move and zoom photos in their frame; delete unused photos
- A section's photo (hero, welcome, invitation, image-text) shows in the
  shape of its frame for the current template and layout, with a Move and
  zoom dialog: drag or arrow keys to choose what stays in view, zoom to
  300%. Saved with the section as data.framing ({ x, y, zoom } by media
  id), a draft until published. The site draws it with object-position
  and scale() around that point; Bold split/collage frames now clip, and
  a zoomed photo stays inside Heritage's mat. Framing for photos a section
  no longer shows is dropped on save.
- Media library: each photo says where it's used; one used nowhere can be
  deleted with its files (DELETE /admin/media/:id, 409 IN_USE otherwise,
  audited as media_deleted).

Checked: Website backend tests (135) and typecheck, frontend typecheck,
lint and production build, and an e2e test of both flows in Chromium.
Lighthouse not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 23:34:10 +02:00

59 lines
3.5 KiB
TypeScript

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);
});
});