website 1.11.0: layouts and backgrounds for the blocks; a video for the top of the page
- 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>
This commit is contained in:
@@ -4,6 +4,7 @@ import request from 'supertest';
|
||||
import { hashToken } from '../src/lib/crypto.js';
|
||||
import { previewContent, siteContent } from '../src/site/content.js';
|
||||
import { publish } from '../src/site/snapshot.js';
|
||||
import { changes, discard } from '../src/site/workingCopy.js';
|
||||
import { harness, SITE } from './support/harness.js';
|
||||
|
||||
async function site() {
|
||||
@@ -85,3 +86,42 @@ describe('an address that changed (§6)', () => {
|
||||
assert.deepEqual(after.mode === 'published' && after.snapshot.redirects, { 'youth-camp': '/summer-camp' });
|
||||
});
|
||||
});
|
||||
|
||||
describe("a block's layout and background", () => {
|
||||
it('saves one of its own layouts and a background from the church\'s colours, previews them, and discards them', async () => {
|
||||
const { ctx, db, as } = await site();
|
||||
const page = await db.page.create({ data: { kind: 'custom', slug: 'join', title: 'Join us' } });
|
||||
const block = await db.section.create({ data: { pageId: page.id, type: 'cta', data: { heading: 'Come along', text: null, button: null } } });
|
||||
await db.section.create({ data: { pageId: page.id, type: 'text', variant: 'something-old', data: { heading: null, body: 'Hi' } } });
|
||||
await publish(ctx, { id: 'x', role: 'system' });
|
||||
|
||||
// Drawn with its type's default until it has a layout; an id it doesn't know is drawn so too.
|
||||
const shown = async () => {
|
||||
const c = await previewContent(ctx);
|
||||
assert.ok(c.mode === 'published');
|
||||
return c.snapshot.pages.find((p) => p.slug === 'join')!.sections.map((s) => ({ type: s.type, variant: s.variant, background: s.background }));
|
||||
};
|
||||
assert.deepEqual(await shown(), [{ type: 'cta', variant: 'centred', background: undefined }, { type: 'text', variant: 'standard', background: undefined }]);
|
||||
|
||||
const put = (b: object) => as('ann')('put', `/pages/${page.id}/blocks/${block.id}`, b);
|
||||
assert.equal((await put({ variant: 'banner' })).status, 200);
|
||||
assert.equal((await put({ background: 'dark' })).status, 200);
|
||||
const wrong = await put({ variant: 'image-left' });
|
||||
assert.equal(wrong.status, 400);
|
||||
assert.match(wrong.body.error.message, /isn't a layout of this block/);
|
||||
assert.equal((await put({ background: 'pink' })).status, 400);
|
||||
assert.deepEqual((await shown())[0], { type: 'cta', variant: 'banner', background: 'dark' });
|
||||
|
||||
// A change like any other: listed, and discarded back to what's published.
|
||||
const pending = (await changes(ctx)).find((c) => c.key === `page:${page.id}`);
|
||||
assert.ok(pending);
|
||||
await discard(ctx, `page:${page.id}`, { id: 'x', role: 'editor' });
|
||||
const back = await db.section.findUnique({ where: { id: block.id } });
|
||||
assert.equal(back?.background, null);
|
||||
assert.equal(back?.variant, null);
|
||||
// Back to null follows the style again.
|
||||
assert.equal((await put({ background: 'tint' })).status, 200);
|
||||
assert.equal((await put({ background: null })).status, 200);
|
||||
assert.equal((await shown())[0]!.background, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ 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';
|
||||
|
||||
@@ -56,3 +57,71 @@ describe('framing a photo in its frame', () => {
|
||||
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/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ const DEFAULTS: Record<string, () => Row> = {
|
||||
leader: () => ({ role: null, bio: null, photoId: null, framing: null, position: 0, archivedAt: null, updatedBy: null }),
|
||||
messageSeries: () => ({ summary: null, imageId: null }),
|
||||
page: () => ({ seoTitle: null, description: null, navLabel: null, inNav: true, position: 0, imageId: null, archivedAt: null, updatedBy: null }),
|
||||
section: () => ({ variant: null, position: 0, hidden: false, updatedBy: null }),
|
||||
section: () => ({ variant: null, background: null, position: 0, hidden: false, updatedBy: null }),
|
||||
message: () => ({ externalId: null, thumbnailUrl: null, speaker: null, passage: null, notes: null, seriesId: null, hidden: false, publishedAt: null, updatedBy: null }),
|
||||
simpleEvent: () => ({ endsAt: null, allDay: false, timezone: 'Africa/Johannesburg', location: null, description: null, imageId: null, framing: null, link: null, cancelled: false, publishedAt: null, updatedBy: null }),
|
||||
mediaAsset: () => ({ width: null, height: null, alt: '', sizes: null, uploadedBy: null }),
|
||||
|
||||
Reference in New Issue
Block a user