Files
menu/test/repair.test.ts
T
tanshu c31b73d553 Let the owner manage categories, and give the menus a shelf
Categories were baked into config/<site>.site.json, so adding one meant a
rebuild and a redeploy. They are now data: data/sections.json, seeded from the
config the first time, and managed in the admin (add, rename, reorder, remove).
A category's web address is fixed when it is created because that is what a QR
code points at, and a category cannot be removed while it still holds menus.

The landing and the top bar were the ugliest parts. The landing was a column of
hairlines with tiny meta — a broadsheet, and the paper that makes this app worth
looking at was nowhere on it. It is now a shelf: each category is a row whose
right edge is a sliver of that menu's first page, bleeding off the screen, with
the shelf edge in brass and the name in heavy tight Archivo. The bar was a row
of teal pills that would not survive more categories: it now names the menu you
are reading and opens a sheet listing the rest, each with its own paper sliver,
and the current one is the only teal thing on screen. The sheet is a link to the
landing page, so navigating still works without the script.

Also fixes the category manager's own layout, which I shipped unstyled, and the
markup contract the route tests described.
2026-09-22 22:24:53 +05:30

96 lines
4.0 KiB
TypeScript

import { strict as assert } from 'node:assert';
import { readdir, readFile, rm, unlink, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { afterEach, beforeEach, describe, it } from 'node:test';
import { sitePaths } from '../src/storage/sets.ts';
import { repairSite } from '../src/storage/repair.ts';
import { hasFatal, validateSite } from '../src/storage/validate.ts';
import { FakePipeline, SECTIONS, makeStore, pngBytes, tempDir } from './support/harness.ts';
let dataDir: string;
beforeEach(async () => {
dataDir = await tempDir('repair');
});
afterEach(async () => {
await rm(dataDir, { recursive: true, force: true });
});
async function publishedSetWithPages(count = 2) {
const { store } = makeStore(dataDir);
const set = await store.create('bar', { label: 'Bar Menu', validUntil: '2026-06-26' });
for (let index = 0; index < count; index += 1) {
await store.addPage(set.id, { data: pngBytes(`page-${index}`), originalName: `${index}.jpg` });
}
const published = await store.publish(set.id);
return { store, set: published };
}
function validate() {
return validateSite({ dataDir, sections: { has: (slug) => (SECTIONS as readonly string[]).includes(slug) } });
}
describe('repairSite', () => {
it('reports nothing to do on a healthy Site', async () => {
await publishedSetWithPages();
const report = await repairSite({ dataDir, pipeline: new FakePipeline() });
assert.deepEqual(report, { fixed: [], unresolved: [] });
});
it('rebuilds a missing Thumbnail and a missing Viewing image from the Original', async () => {
const { set } = await publishedSetWithPages(1);
const page = set.manifest.pages[0]!;
const thumbnail = (await readdir(set.dir)).find((name) => name.startsWith('thumb-'))!;
const webp = (await readdir(set.dir)).find(
(name) => name.endsWith('.webp') && !name.startsWith('thumb-'),
)!;
const thumbBytes = await readFile(path.join(set.dir, thumbnail));
await unlink(path.join(set.dir, thumbnail));
await unlink(path.join(set.dir, webp));
assert.equal(hasFatal(await validate()), true);
const report = await repairSite({ dataDir, pipeline: new FakePipeline() });
assert.equal(report.fixed.length, 2);
assert.deepEqual(report.unresolved, []);
assert.deepEqual(await validate(), []);
assert.deepEqual(await readFile(path.join(set.dir, thumbnail)), thumbBytes);
assert.equal(page.hash.length > 0, true);
});
it('leaves a Page unresolved when its Original is gone', async () => {
const { set } = await publishedSetWithPages(1);
const page = set.manifest.pages[0]!;
const originals = path.join(sitePaths(dataDir).originals, 'bar', set.manifest.id);
const original = (await readdir(originals))[0]!;
await unlink(path.join(originals, original));
const thumbnail = (await readdir(set.dir)).find((name) => name.startsWith('thumb-'))!;
await unlink(path.join(set.dir, thumbnail));
const report = await repairSite({ dataDir, pipeline: new FakePipeline() });
assert.equal(report.fixed.length, 0);
assert.equal(report.unresolved.length, 1);
assert.match(report.unresolved[0] ?? '', /Original is gone/);
assert.match(report.unresolved[0] ?? '', new RegExp(page.id));
});
it('notices when an Original no longer matches its recorded hash', async () => {
const { set } = await publishedSetWithPages(1);
const originals = path.join(sitePaths(dataDir).originals, 'bar', set.manifest.id);
const original = (await readdir(originals))[0]!;
await rm(path.join(originals, original));
const page = set.manifest.pages[0]!;
await writeFile(path.join(originals, original), pngBytes('swapped'));
const thumbnail = (await readdir(set.dir)).find((name) => name.startsWith('thumb-'))!;
await unlink(path.join(set.dir, thumbnail));
const report = await repairSite({ dataDir, pipeline: new FakePipeline() });
assert.equal(report.fixed.length, 0);
assert.equal(report.unresolved.length, 1);
assert.match(report.unresolved[0] ?? '', /no longer matches/);
assert.equal(page.hash.length > 0, true);
});
});