File size: 7,192 Bytes
082058f
 
37a34fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b6aaf
37a34fd
 
 
 
 
 
d3b6aaf
 
 
 
 
 
37a34fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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');
});