File size: 4,466 Bytes
37a34fd
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
// Targeted cases for EditSession (edit.mjs): splice composition, overlap
// detection, and comment-aware brace balance verification.

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, readFile, stat, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { tokenize } from '../src/lexer.mjs';
import { EditSession, countBraces } from '../src/edit.mjs';
import { writeGameText } from '../src/codec.mjs';

test('a single splice produces byte-identical output outside the touched span', () => {
  const text = 'owner = TLD\t\t#Juan II of Castille\ncontroller = TLD\n';
  const tokens = tokenize(text);
  const session = new EditSession('fixture.txt', text, tokens, { bom: false });
  const ownerValueToken = tokens.find((t) => t.kind === 'bare' && t.value === 'TLD');
  session.splice(ownerValueToken.index, ownerValueToken.length, 'CAS');
  const out = session.apply();
  assert.equal(out, 'owner = CAS\t\t#Juan II of Castille\ncontroller = TLD\n');
});

test('two non-overlapping splices compose correctly regardless of registration order', () => {
  const text = 'a = 1\nb = 2\nc = 3\n';
  const tokens = tokenize(text);
  const session = new EditSession('fixture.txt', text, tokens, { bom: false });
  const bToken = tokens.find((t) => t.kind === 'bare' && t.value === '2');
  const cToken = tokens.find((t) => t.kind === 'bare' && t.value === '3');
  // Register in reverse-of-file order deliberately, to prove application
  // order doesn't depend on registration order.
  session.splice(cToken.index, cToken.length, '30');
  session.splice(bToken.index, bToken.length, '20');
  assert.equal(session.apply(), 'a = 1\nb = 20\nc = 30\n');
});

test('overlapping splices throw', () => {
  const text = 'owner = TLD\n';
  const tokens = tokenize(text);
  const session = new EditSession('fixture.txt', text, tokens, { bom: false });
  const token = tokens.find((t) => t.kind === 'bare' && t.value === 'TLD');
  session.splice(token.index, token.length, 'CAS');
  session.splice(token.index, token.length + 1, 'XXX');
  assert.throws(() => session.apply(), /overlapping splices/);
});

test('comment-aware brace balance ignores a stray "}" inside a comment (# The Eden Agreement } case)', () => {
  const text = 'title = "Eden"\t# The Eden Agreement }\nowner = TLD\n';
  const braces = countBraces(text);
  assert.equal(braces.open, 0);
  assert.equal(braces.close, 0);

  const tokens = tokenize(text);
  const session = new EditSession('fixture.txt', text, tokens, { bom: false });
  const token = tokens.find((t) => t.kind === 'bare' && t.value === 'TLD');
  session.splice(token.index, token.length, 'CAS');
  const out = session.apply();
  // The comment-aware count must still see zero real braces after the edit
  // — a naive scan would have counted the comment's "}" as a real close.
  const after = countBraces(out);
  assert.equal(after.open, 0);
  assert.equal(after.close, 0);
});

test('save() on a session with zero splices is a true no-op (bytes and mtime untouched)', async () => {
  const dir = await mkdtemp(join(tmpdir(), 'eu4-edit-test-'));
  const path = join(dir, 'fixture.txt');
  const text = 'owner = TLD\n';
  await writeGameText(path, text, { bom: false });

  const before = await stat(path);
  const beforeBytes = await readFile(path);

  const tokens = tokenize(text);
  const session = new EditSession(path, text, tokens, { bom: false });
  assert.equal(session.dirty, false);
  await session.save();

  const after = await stat(path);
  const afterBytes = await readFile(path);
  assert.equal(afterBytes.equals(beforeBytes), true);
  assert.equal(after.mtimeMs, before.mtimeMs);

  await rm(dir, { recursive: true, force: true });
});

test('save() writes verified output for a legitimate single splice', async () => {
  const dir = await mkdtemp(join(tmpdir(), 'eu4-edit-test-'));
  const path = join(dir, 'fixture.txt');
  const text = 'owner = TLD\ncontroller = TLD\n';
  await writeGameText(path, text, { bom: false });

  const tokens = tokenize(text);
  const session = new EditSession(path, text, tokens, { bom: false });
  const token = tokens.find((t) => t.kind === 'bare' && t.value === 'TLD');
  session.splice(token.index, token.length, 'CAS');
  await session.save();

  const out = await readFile(path, 'utf-8');
  assert.equal(out, 'owner = CAS\ncontroller = TLD\n');

  await rm(dir, { recursive: true, force: true });
});