- Blocks get layouts (BLOCK_VARIANTS: text standard/centred; image and text photo left, right or above; quote centred/with a line; call to action centred/banner), stored on Section.variant and checked against the type; an unknown one draws as the default, as every block looked before. The demo's image-text block id is now image-left. - Each block has its own background (Section.background, migration 20261201000000_block_background), in the published source only when set, put back by Discard and Restore, drawn as the homepage's (data-bg). - MP4 uploads up to 20 MB for the hero's Video layout: QuickTime refused, metadata boxes (udta, meta, uuid) blanked into free boxes of the same size so where it was filmed is gone and the frame offsets hold; size read from tkhd. GET /media lists mime. - Admin: a block's Layout and Background when open; the hero's Video field (only videos; photo fields only photos); the media library shows and takes videos. An upload over the limit says so instead of "That request isn't valid." - Tests: backend (152, three new); e2e for block layouts and a video upload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
6.8 KiB
TypeScript
128 lines
6.8 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 { cleanVideo } from '../src/admin/media.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);
|
|
});
|
|
});
|
|
|
|
/** An MP4 box: its size, its type, then what's in it. */
|
|
const box = (type: string, ...parts: Buffer[]) => {
|
|
const body = Buffer.concat(parts);
|
|
const head = Buffer.alloc(8);
|
|
head.writeUInt32BE(8 + body.length, 0);
|
|
head.write(type, 4, 'latin1');
|
|
return Buffer.concat([head, body]);
|
|
};
|
|
/** A version 0 track header for a picture `w` by `h` (16.16 fixed point at its end). */
|
|
const tkhd = (w: number, h: number) => {
|
|
const b = Buffer.alloc(84);
|
|
b.writeUInt32BE(w << 16, 76);
|
|
b.writeUInt32BE(h << 16, 80);
|
|
return box('tkhd', b);
|
|
};
|
|
const PLACE = '+52.3676+004.9041/';
|
|
/** A small MP4 filmed somewhere: where in `udta` (©xyz) and Apple's `meta`, as phones save them. */
|
|
function mp4(brand = 'isom') {
|
|
return Buffer.concat([
|
|
box('ftyp', Buffer.from(`${brand}\0\0\0\0isommp42`, 'latin1')),
|
|
box('moov',
|
|
box('mvhd', Buffer.alloc(100)),
|
|
box('trak', tkhd(640, 360), box('mdia', box('mdhd', Buffer.alloc(24)))),
|
|
box('udta', box('©xyz', Buffer.from(PLACE, 'latin1'))),
|
|
box('meta', Buffer.from(`com.apple.quicktime.location.ISO6709${PLACE}`, 'latin1'))),
|
|
box('mdat', Buffer.from('frames go here')),
|
|
]);
|
|
}
|
|
|
|
describe('a video for the top of the page', () => {
|
|
it('takes away where it was filmed without moving anything, and reads its size', () => {
|
|
const v = mp4();
|
|
const clean = cleanVideo(v)!;
|
|
assert.equal(clean.bytes.length, v.length);
|
|
assert.equal(clean.bytes.includes(Buffer.from(PLACE)), false);
|
|
assert.equal(clean.bytes.includes(Buffer.from('com.apple.quicktime.location')), false);
|
|
assert.ok(clean.bytes.includes(Buffer.from('frames go here')));
|
|
// The frames sit where they did, so the offsets to them still hold.
|
|
assert.equal(clean.bytes.indexOf('mdat'), v.indexOf('mdat'));
|
|
assert.deepEqual([clean.width, clean.height], [640, 360]);
|
|
// The upload itself isn't changed.
|
|
assert.ok(v.includes(Buffer.from(PLACE)));
|
|
// Boxes that don't add up, or no moov: not a video.
|
|
assert.equal(cleanVideo(Buffer.concat([v.subarray(0, 40), Buffer.from([0, 0, 1, 0])])), null);
|
|
assert.equal(cleanVideo(box('ftyp', Buffer.from('isom\0\0\0\0', 'latin1'))), null);
|
|
});
|
|
|
|
it('uploads an MP4 to the library, and refuses QuickTime and one too large', async () => {
|
|
const { app, db, uploads } = await site();
|
|
const post = (b: Buffer) => request(app).post('/api/v1/admin/media?alt=People%20arriving').set('Cookie', 'ccw_session=ann').set('Origin', SITE).set('Content-Type', 'application/octet-stream').send(b);
|
|
const ok = await post(mp4());
|
|
assert.equal(ok.status, 200, JSON.stringify(ok.body));
|
|
assert.equal(ok.body.mime, 'video/mp4');
|
|
assert.match(ok.body.url, /\.mp4$/);
|
|
const row = await db.mediaAsset.findUnique({ where: { id: ok.body.id } });
|
|
assert.deepEqual([row?.width, row?.height, row?.alt], [640, 360, 'People arriving']);
|
|
const saved = await fs.readFile(path.join(uploads, String(row!.filename)));
|
|
assert.equal(saved.includes(Buffer.from(PLACE)), false);
|
|
|
|
const mov = await post(mp4('qt '));
|
|
assert.equal(mov.status, 415);
|
|
assert.match(mov.body.error.message, /MP4/);
|
|
const big = await post(Buffer.concat([mp4(), Buffer.alloc(20 * 1024 * 1024)]));
|
|
assert.equal(big.status, 413);
|
|
assert.match(big.body.error.message, /20 MB/);
|
|
});
|
|
});
|