File size: 7,088 Bytes
082058f
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Targeted cases for the Node API (node.mjs), 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, 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');
  // The literal add_core/owner-style fields stay reachable through the
  // normal (non-at) read path completely unaffected by at()'s bucket
  // renaming or overwrite resolution.
  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'));
});