test: align Audit comparison expectations and backup i18n fixtures

This commit is contained in:
martino
2026-09-23 10:42:15 +02:00
parent dee18df711
commit 6cf3f4a520
4 changed files with 121 additions and 69 deletions
+32 -24
View File
@@ -1,13 +1,8 @@
// Offline actual JSX/provider seams; no component/backend imports or effects.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { createRequire } = require('node:module');
const { translator, catalogs } = require('./test_backup_archives_empty.cjs');
const app = path.resolve(__dirname, '../AppImage');
const ts = createRequire(path.join(app, 'package.json'))('typescript');
const source = ts.createSourceFile('host-backup.tsx', fs.readFileSync(path.join(app, 'components/host-backup.tsx'), 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
const printer = ts.createPrinter();
const { ts, read, printer, translator, catalogs } = require('./backup_i18n_seam.cjs');
const source = ts.createSourceFile('host-backup.tsx', read('components/host-backup.tsx'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
const React = { createElement: (tag, props, ...children) => children.flat(Infinity).filter(x => x != null && x !== false).join('') };
function find(predicate) {
const found = [];
@@ -25,32 +20,45 @@ function render(node, locale, backups = 1, messages = catalogs) {
}
const kept = find(node => ts.isBinaryExpression(node) && node.left.getText(source) === 'backups > 0');
const keptMessage = 'Existing backup archives are not deleted.';
for (const count of [1, 3]) assert.equal(render(kept, 'it', count), keptMessage, 'Italian catalog uses English fallback');
assert.equal(catalogs.en.backup.destinations.backupsKeptMessage, keptMessage);
assert.equal(render(kept, 'en', 1), keptMessage);
assert.equal(render(kept, 'en', 3), keptMessage);
assert.equal(render(kept, 'en', 0), false);
for (const locale of Object.keys(catalogs)) {
if (catalogs[locale].backup?.destinations?.backupsKeptMessage !== undefined && locale !== 'en') continue;
for (const count of [1, 3]) assert.equal(render(kept, locale, count), keptMessage, `${locale}: kept whole-message fallback`);
}
console.log('PASS: destination removal JSX guard 0/1/N and whole-message locale fallback');
// A missing locale key must use English regardless of shipped catalog completeness.
const missing = { ...catalogs, missing: {} };
for (const count of [1, 3]) assert.equal(render(kept, 'missing', count, missing), keptMessage);
console.log('PASS: destination removal JSX guard 0/1/N and missing-key fallback');
// Select the actual help paragraph, keeping its literal paths/JSX whitespace.
const help = find(node => ts.isJsxElement(node) && node.openingElement.tagName.getText(source) === 'p' &&
/backup\.destinations\.localAttachHelp(?:Before|Message)/.test(node.getText(source)));
const helpMessage = 'Mount USB drives first. Leave blank to use the PVE storage path + /dump (fallback: /var/lib/vz/dump).';
assert.equal(render(help, 'it'), helpMessage, 'Italian catalog uses English fallback');
for (const locale of Object.keys(catalogs)) {
if (catalogs[locale].backup?.destinations?.localAttachHelpMessage !== undefined && locale !== 'en') continue;
assert.equal(render(help, locale), helpMessage, `${locale}: local attach help fallback`);
}
// Missing-key fallback must remain covered after shipped catalogs are complete.
const missing = { ...catalogs, missing: {} };
assert.equal(render(kept, 'missing', 1, missing), keptMessage);
assert.equal(catalogs.en.backup.destinations.localAttachHelpMessage, helpMessage);
assert.equal(render(help, 'en'), helpMessage);
assert.equal(render(help, 'missing', 1, missing), helpMessage);
// Synthetic locale proves complete messages are translatable, not hardcoded.
// Synthetic whole messages prove both JSX consumers use the provider rather than hardcoded English.
const synthetic = { ...catalogs, fixture: { backup: { destinations: {
backupsKeptMessage: 'Fixture kept.', localAttachHelpMessage: 'Fixture help.',
} } } };
assert.equal(render(kept, 'fixture', 1, synthetic), 'Fixture kept.');
assert.equal(render(kept, 'fixture', 3, synthetic), 'Fixture kept.');
assert.equal(render(help, 'fixture', 1, synthetic), 'Fixture help.');
const locales = Object.keys(catalogs);
assert.ok(locales.length > 1, 'shipped multilingual catalogs discovered');
for (const locale of locales) {
const destinations = catalogs[locale].backup?.destinations;
for (const count of [1, 3]) {
assert.equal(render(kept, locale, count), destinations?.backupsKeptMessage ?? keptMessage,
`${locale}: actual kept message or English fallback`);
}
assert.equal(render(help, locale), destinations?.localAttachHelpMessage ?? helpMessage,
`${locale}: actual local attach help or English fallback`);
}
for (const [key, english] of [['backupsKeptMessage', keptMessage], ['localAttachHelpMessage', helpMessage]]) {
assert.ok(locales.some(locale => locale !== 'en' &&
typeof catalogs[locale].backup?.destinations?.[key] === 'string' &&
catalogs[locale].backup.destinations[key] !== english),
`${key}: localized shipped message exercised`);
}
assert.equal(catalogs.en.backup.destinations.kept, 'kept');
assert.equal(catalogs.en.backup.destinations.localAttachHelpMiddle, 'USB drives');
console.log('PASS: local attach JSX whole-message fallback and literal paths');
console.log('PASS: destination whole-message JSX, shipped locales, synthetic translation/fallback and literal paths');