Files
menu/test/repair.test.ts
tanshu e64f65642f Add ESLint and Prettier
Prettier owns formatting: .prettierrc.json, format and format:check scripts, and
a one-off pass over the repo so the check is green from the start. Embedded
language formatting is off deliberately - the renderers build HTML in html
tagged templates and letting Prettier reflow those would change the markup
customers receive. docs/ is excluded because it is hand-wrapped prose, and shell
scripts have no Prettier parser.

ESLint covers the JavaScript: the two browser scripts, the build and check
scripts, and its own config. no-var is scoped out for the browser scripts on
purpose; they are plain ES5-shaped IIFEs and a hoisting difference would only
show up in a browser.

ESLint cannot lint the TypeScript here: typescript@7 is the native port with no
JavaScript compiler API, and typescript-eslint requires a 4.8-6.1 peer, so it
refuses to install and forcing it through would leave a parser that cannot read
the compiler. tsc --noEmit stays the TypeScript gate; README records the two
ways to change that.

lint and format:check now run in deploy.sh, and VS Code formats on save with
Prettier and surfaces ESLint findings.
2026-09-22 23:15:08 +05:30

99 lines
4.1 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);
});
});