Files
menu/test/validate.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

104 lines
4.1 KiB
TypeScript

import { strict as assert } from 'node:assert';
import { readFile, readdir, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { afterEach, beforeEach, describe, it } from 'node:test';
import { MANIFEST_FILE } from '../src/storage/manifest.ts';
import { sitePaths } from '../src/storage/sets.ts';
import { hasFatal, validateSite } from '../src/storage/validate.ts';
import { SECTIONS, makeStore, pngBytes, tempDir } from './support/harness.ts';
let dataDir: string;
beforeEach(async () => {
dataDir = await tempDir('validate');
});
afterEach(async () => {
await rm(dataDir, { recursive: true, force: true });
});
function validate() {
return validateSite({
dataDir,
sections: { has: (slug) => (SECTIONS as readonly string[]).includes(slug) },
});
}
describe('validateSite', () => {
it('passes an empty Site and a healthy published set', async () => {
const { store } = makeStore(dataDir);
assert.deepEqual(await validate(), []);
const set = await store.create('bar', { label: 'Bar Menu' });
await store.addPage(set.id, { data: pngBytes('one'), originalName: 'a.jpg' });
await store.publish(set.id);
assert.deepEqual(await validate(), []);
});
it('warns about a broken Draft but fails on a broken published set', async () => {
const { store } = makeStore(dataDir);
const draft = await store.create('bar', { label: 'Bar Menu' });
const page = await store.addPage(draft.id, { data: pngBytes('one'), originalName: 'a.jpg' });
const names = await readdir(draft.dir);
const thumbnail = names.find((name) => name.startsWith('thumb-'))!;
await rm(path.join(draft.dir, thumbnail));
const draftProblems = await validate();
assert.equal(draftProblems.length, 1);
assert.equal(draftProblems[0]?.level, 'warning');
assert.equal(hasFatal(draftProblems), false);
await store.publish(draft.id);
const publishedProblems = await validate();
assert.equal(publishedProblems[0]?.level, 'fatal');
assert.equal(hasFatal(publishedProblems), true);
assert.match(publishedProblems[0]?.message ?? '', new RegExp(page.id));
});
it('fails when a manifest disagrees with its own directory', async () => {
const { store } = makeStore(dataDir);
const set = await store.create('bar', { label: 'Bar Menu' });
const file = path.join(set.dir, MANIFEST_FILE);
const manifest = JSON.parse(await readFile(file, 'utf8')) as Record<string, unknown>;
manifest['section'] = 'food';
await writeFile(file, JSON.stringify(manifest));
const problems = await validate();
assert.equal(hasFatal(problems), true);
assert.ok(problems.some((problem) => problem.message.includes('does not match its directory')));
});
it('fails on a manifest that cannot be parsed', async () => {
const { store } = makeStore(dataDir);
const set = await store.create('bar', { label: 'Bar Menu' });
await writeFile(path.join(set.dir, MANIFEST_FILE), '{ not json');
const problems = await validate();
assert.equal(hasFatal(problems), true);
assert.ok(problems.some((problem) => problem.message.includes('not valid JSON')));
});
it('fails on a published set with no Pages', async () => {
const { store } = makeStore(dataDir);
const set = await store.create('bar', { label: 'Bar Menu' });
await store.addPage(set.id, { data: pngBytes('one'), originalName: 'a.jpg' });
const published = await store.publish(set.id);
const manifestPath = path.join(published.dir, MANIFEST_FILE);
const manifest = JSON.parse(await readFile(manifestPath, 'utf8')) as Record<string, unknown>;
manifest['pages'] = [];
await writeFile(manifestPath, JSON.stringify(manifest));
const problems = await validate();
assert.equal(hasFatal(problems), true);
assert.ok(problems.some((problem) => problem.message.includes('no Pages')));
});
it('reports the data directory layout it inspected', async () => {
const paths = sitePaths(dataDir);
assert.equal(paths.drafts, path.join(dataDir, 'drafts'));
assert.equal(paths.published, path.join(dataDir, 'published'));
assert.equal(paths.originals, path.join(dataDir, 'originals'));
});
});