Ab-Integro / tools /eu4_clausewitz /test /edit.test.mjs
Ame
chore(tooling): tighten docs and prune tests
082058f
Raw
History Blame Contribute Delete
4.47 kB
// 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 });
});