| |
|
|
| 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, isDateKey } from '../src/node.mjs'; |
| import { EditSession } from '../src/edit.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 }); |
| const node = new Node({ |
| items: tree.items, |
| tokens, |
| text, |
| path: FAKE_PATH, |
| layer: 'mod', |
| session, |
| }); |
| return { node, session }; |
| } |
|
|
| test('get throws on a duplicated key', () => { |
| const { node } = makeNode('discovered_by = western\ndiscovered_by = eastern\n'); |
| assert.throws(() => node.get('discovered_by'), /occurs 2 times/); |
| }); |
|
|
| test('all returns both values for a duplicated key', () => { |
| const { node } = makeNode('discovered_by = western\ndiscovered_by = eastern\n'); |
| assert.deepEqual(node.all('discovered_by'), ['western', 'eastern']); |
| }); |
|
|
| test('get returns undefined for an absent key (only duplicates throw)', () => { |
| const { node } = makeNode('owner = TLD\n'); |
| assert.equal(node.get('nope'), undefined); |
| }); |
|
|
| test('keys() preserves order and duplicates', () => { |
| const { node } = makeNode('owner = TLD\nadd_core = TLD\nadd_core = CAS\nculture = castillian\n'); |
| assert.deepEqual(node.keys(), ['owner', 'add_core', 'add_core', 'culture']); |
| }); |
|
|
| test('duplicate date blocks are both reachable via blocks(), with and without a key filter', () => { |
| const { node } = makeNode( |
| '1444.11.11 = {\n\towner = CAS\n}\n1444.11.11 = {\n\treligion = catholic\n}\n', |
| ); |
| const all = node.blocks(); |
| assert.equal(all.length, 2); |
| const byKey = node.blocks('1444.11.11'); |
| assert.equal(byKey.length, 2); |
| assert.equal(byKey[0].get('owner'), 'CAS'); |
| assert.equal(byKey[1].get('religion'), 'catholic'); |
| }); |
|
|
| test('at(): plain assignment resolves to the value in effect as of the given date', () => { |
| const { node } = makeNode( |
| 'owner = FRI\n1473.2.23 = {\n\towner = BUR\n}\n1498.1.1 = {\n\towner = SAX\n}\n', |
| ); |
| assert.equal(node.at('1480.1.1').get('owner'), 'BUR'); |
| assert.equal(node.at('1460.1.1').get('owner'), 'FRI'); |
| assert.equal(node.at('1500.1.1').get('owner'), 'SAX'); |
| }); |
|
|
| test('at(): add_X/remove_X accumulate into a bucket named X, in insertion order', () => { |
| const { node } = makeNode( |
| 'add_core = FRI\n' |
| + '1445.1.1 = {\n\tadd_core = BUR\n}\n' |
| + '1477.1.5 = {\n\tadd_core = HAB\n\tremove_core = BUR\n}\n', |
| ); |
| assert.deepEqual(node.at('1500.1.1').all('core'), ['FRI', 'HAB']); |
| }); |
|
|
| test('at() throws a clear error on set/add/remove', () => { |
| const { node } = makeNode('owner = FRI\n1473.2.23 = {\n\towner = BUR\n}\n'); |
| const view = node.at('1480.1.1'); |
| assert.throws(() => view.set('owner', 'X'), /read-only resolved view/); |
| assert.throws(() => view.add('foo', 'X'), /read-only resolved view/); |
| assert.throws(() => view.remove('owner'), /read-only resolved view/); |
| assert.throws(() => view.at('1500.1.1'), /already an at\(\) view/); |
| }); |
|
|
| test("get('owner') returns the bare start-of-game value even when a later date block changes it", () => { |
| const { node } = makeNode( |
| 'owner = FRI\ncontroller = FRI\n1473.2.23 = {\n\towner = BUR\n\tcontroller = BUR\n}\n', |
| ); |
| assert.equal(node.get('owner'), 'FRI'); |
| assert.equal(node.get('controller'), 'FRI'); |
| |
| |
| |
| assert.equal(node.at('1480.1.1').get('owner'), 'BUR'); |
| }); |
|
|
| test('bare value list block parses as a block Node and round-trips untouched', () => { |
| const text = 'historical_idea_groups = {\n\teconomic_ideas\n\toffensive_ideas\n}\n'; |
| const { node } = makeNode(text); |
| const block = node.block('historical_idea_groups'); |
| assert.deepEqual(block.bareValues(), ['economic_ideas', 'offensive_ideas']); |
| assert.equal(block.keys().length, 0); |
| assert.equal(tokenize(text).map((t) => t.raw).join(''), text); |
| }); |
|
|
| test('RGB triple block parses as a block Node with three bare values', () => { |
| const { node } = makeNode('color = { 20 50 210 }\n'); |
| const block = node.block('color'); |
| assert.deepEqual(block.bareValues(), ['20', '50', '210']); |
| }); |
|
|
| test('empty block { } is handled with no keys and no bare values', () => { |
| const { node } = makeNode('chance = { }\n'); |
| const block = node.block('chance'); |
| assert.deepEqual(block.keys(), []); |
| assert.deepEqual(block.bareValues(), []); |
| }); |
|
|
| test('set on a quoted value keeps quoting', () => { |
| const text = 'owner = "TLD"\n'; |
| const { node, session } = makeNode(text); |
| node.set('owner', 'CAS'); |
| assert.equal(session.apply(), 'owner = "CAS"\n'); |
| }); |
|
|
| test('set on a bare value keeps it bare', () => { |
| const text = 'owner = TLD\n'; |
| const { node, session } = makeNode(text); |
| node.set('owner', 'CAS'); |
| assert.equal(session.apply(), 'owner = CAS\n'); |
| }); |
|
|
| test('set throws if the target key has zero or more than one match', () => { |
| const { node: n0 } = makeNode('owner = TLD\n'); |
| assert.throws(() => n0.set('missing', 'X'), /requires exactly one existing entry/); |
|
|
| const { node: n2 } = makeNode('add_core = TLD\nadd_core = CAS\n'); |
| assert.throws(() => n2.set('add_core', 'X'), /requires exactly one existing entry/); |
| }); |
|
|
| test('add appends a new entry with exact resulting text', () => { |
| const text = 'owner = TLD\ncontroller = TLD\n'; |
| const { node, session } = makeNode(text); |
| node.add('culture', 'castillian'); |
| assert.equal(session.apply(), 'owner = TLD\ncontroller = TLD\nculture = castillian\n'); |
| }); |
|
|
| test('add quotes a value that contains whitespace', () => { |
| const text = 'owner = TLD\n'; |
| const { node, session } = makeNode(text); |
| node.add('title', 'City of Toledo'); |
| assert.equal(session.apply(), 'owner = TLD\ntitle = "City of Toledo"\n'); |
| }); |
|
|
| test('remove deletes the exact matching entry and nothing else', () => { |
| const text = 'owner = TLD\ncontroller = TLD\nculture = castillian\n'; |
| const { node, session } = makeNode(text); |
| const count = node.remove('controller'); |
| assert.equal(count, 1); |
| assert.equal(session.apply(), 'owner = TLD\nculture = castillian\n'); |
| }); |
|
|
| test('remove(key, value) removes only the one matching entry, leaving siblings with the same key intact', () => { |
| const text = 'discovered_by = western\ndiscovered_by = eastern\ndiscovered_by = muslim\ndiscovered_by = ottoman\n'; |
| const { node, session } = makeNode(text); |
| const count = node.remove('discovered_by', 'eastern'); |
| assert.equal(count, 1); |
| assert.equal( |
| session.apply(), |
| 'discovered_by = western\ndiscovered_by = muslim\ndiscovered_by = ottoman\n', |
| ); |
| }); |
|
|
| test('isDateKey recognizes real date-shaped keys and rejects ordinary identifiers', () => { |
| assert.ok(isDateKey('1444.11.11')); |
| assert.ok(isDateKey('1.1.1')); |
| assert.ok(!isDateKey('owner')); |
| assert.ok(!isDateKey('add_core')); |
| }); |
|
|