Ab-Integro / tools /eu4_clausewitz /test /validate.test.mjs
Ame
fix(validation): restore Naga adjective and filter sea zones
d3b6aaf
Raw
History Blame Contribute Delete
7.19 kB
// Targeted cases for src/validate.mjs's rule engine and concrete registry
// rules, using small inline fixtures.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { tokenize } from '../src/lexer.mjs';
import { parse } from '../src/ast.mjs';
import { Node } from '../src/node.mjs';
import { EditSession } from '../src/edit.mjs';
import { runRules, refFieldRules } from '../src/validate.mjs';
import provincesRegistry from '../registry/provinces.mjs';
import { unownedLandRule, provinceMissingLocRule } from '../registry/provinces.rules.mjs';
import { tagMissingDisplayNameRule } from '../registry/countries.rules.mjs';
import { noBomRule, noAstralRule, braceBalanceRule } from '../registry/encoding.rules.mjs';
const FAKE_PATH = 'fixture.txt';
function makeNode(text) {
const tokens = tokenize(text);
const tree = parse(tokens);
const session = new EditSession(FAKE_PATH, text, tokens, { bom: false });
return new Node({
items: tree.items,
tokens,
text,
path: FAKE_PATH,
layer: 'mod',
session,
});
}
// --- unowned-land ---------------------------------------------------------
test('unowned-land: no owner, no native_size, no add_core -> warn', () => {
const node = makeNode('base_tax = 1\ntrade_goods = grain\n');
const findings = runRules([unownedLandRule], [{ node, id: '9001' }], {});
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'warn');
assert.equal(findings[0].id, 'unowned-land');
});
test('unowned-land: sea zone without trade_goods -> no finding', () => {
const node = makeNode('base_tax = 1\n');
const findings = runRules([unownedLandRule], [{ node, id: '1250' }], {});
assert.equal(findings.length, 0);
});
test('unowned-land: Wild-Fields shape (no owner, add_core present) -> no finding', () => {
const node = makeNode('add_core = CRI\nculture = crimean\nreligion = sunni\nnative_size = 5\n');
const findings = runRules([unownedLandRule], [{ node, id: '282' }], {});
assert.equal(findings.length, 0, 'a province with add_core (and native_size) must never warn even with no owner');
});
test('unowned-land: no owner but has native_size (uncolonized native land) -> no finding', () => {
const node = makeNode('native_size = 5\nculture = crimean\n');
const findings = runRules([unownedLandRule], [{ node, id: '9002' }], {});
assert.equal(findings.length, 0);
});
test('unowned-land: has owner -> no finding', () => {
const node = makeNode('owner = TLD\ncontroller = TLD\n');
const findings = runRules([unownedLandRule], [{ node, id: '219' }], {});
assert.equal(findings.length, 0);
});
// --- province-missing-loc --------------------------------------------------
test('province-missing-loc: land province missing both keys -> warn', () => {
const node = makeNode('owner = TLD\n');
const ctx = { definedSets: { locKeys: new Set() } };
const findings = runRules([provinceMissingLocRule], [{ node, id: '9003' }], ctx);
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'warn');
});
test('province-missing-loc: land province with both keys present -> no finding', () => {
const node = makeNode('owner = TLD\n');
const ctx = { definedSets: { locKeys: new Set(['PROV9003', 'PROV_ADJ9003']) } };
const findings = runRules([provinceMissingLocRule], [{ node, id: '9003' }], ctx);
assert.equal(findings.length, 0);
});
test('province-missing-loc: not a land province (no owner, no native_size) -> rule does not apply', () => {
const node = makeNode('base_tax = 1\n');
const ctx = { definedSets: { locKeys: new Set() } };
const findings = runRules([provinceMissingLocRule], [{ node, id: '9004' }], ctx);
assert.equal(findings.length, 0, 'rule only applies to provinces with an owner or native_size');
});
// --- tag-missing-display-name ---------------------------------------------
test('tag-missing-display-name: tag missing both keys -> warn', () => {
const node = makeNode('government = monarchy\n');
const ctx = { definedSets: { locKeys: new Set() } };
const findings = runRules([tagMissingDisplayNameRule], [{ node, id: 'ZZZ' }], ctx);
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'warn');
});
test('tag-missing-display-name: tag with both keys present -> no finding', () => {
const node = makeNode('government = monarchy\n');
const ctx = { definedSets: { locKeys: new Set(['ZZZ', 'ZZZ_ADJ']) } };
const findings = runRules([tagMissingDisplayNameRule], [{ node, id: 'ZZZ' }], ctx);
assert.equal(findings.length, 0);
});
// --- generic ref-field-defined rules (error level) -------------------------
test('refFieldRules: undefined reference value -> error', () => {
const rules = refFieldRules(provincesRegistry);
const fwd = { registry: 'provinces', id: '9005', field: 'owner', value: 'ZZZ', target: 'tags', ok: false };
const findings = runRules(rules, [fwd], {});
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'error');
assert.equal(findings[0].id, 'provinces-owner-defined');
});
test('refFieldRules: defined reference value -> no finding', () => {
const rules = refFieldRules(provincesRegistry);
const fwd = { registry: 'provinces', id: '9005', field: 'owner', value: 'TLD', target: 'tags', ok: true };
const findings = runRules(rules, [fwd], {});
assert.equal(findings.length, 0);
});
// --- encoding rules ---------------------------------------------------------
test('no-bom: file with a BOM -> error', () => {
const findings = runRules([noBomRule], [{ path: FAKE_PATH, text: 'owner = TLD\n', bom: true }], {});
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'error');
});
test('no-bom: file without a BOM -> no finding', () => {
const findings = runRules([noBomRule], [{ path: FAKE_PATH, text: 'owner = TLD\n', bom: false }], {});
assert.equal(findings.length, 0);
});
test('no-astral: file with an astral character -> error', () => {
const astral = String.fromCodePoint(0x20000);
const findings = runRules([noAstralRule], [{ path: FAKE_PATH, text: `name = "${astral}"\n` }], {});
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'error');
});
test('no-astral: plain ASCII text -> no finding', () => {
const findings = runRules([noAstralRule], [{ path: FAKE_PATH, text: 'owner = TLD\n' }], {});
assert.equal(findings.length, 0);
});
test('brace-balance: balanced braces -> no finding', () => {
const findings = runRules([braceBalanceRule], [{ path: FAKE_PATH, text: 'core = {\n\towner = TLD\n}\n' }], {});
assert.equal(findings.length, 0);
});
test('brace-balance: unbalanced braces -> error', () => {
const findings = runRules([braceBalanceRule], [{ path: FAKE_PATH, text: 'core = {\n\towner = TLD\n' }], {});
assert.equal(findings.length, 1);
assert.equal(findings[0].level, 'error');
});
test('brace-balance: a brace inside a comment does not throw off the count (comment-aware)', () => {
const text = 'core = {\n\towner = TLD\n} # The Eden Agreement }\n';
const findings = runRules([braceBalanceRule], [{ path: FAKE_PATH, text }], {});
assert.equal(findings.length, 0, 'the stray } inside the comment must not be counted');
});