d10_cs_generator/tests/test_character_io.mjs

131 lines
6.3 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { test } from 'node:test';
import vm from 'node:vm';
const script = readFileSync(new URL('../scripts/d10_character_io.js', import.meta.url), 'utf8');
function session() {
const objects = new Map();
const chat = [];
let nextId = 0;
let handler;
const createObj = (type, fields) => {
const data = { ...fields };
const object = {
id: `id${++nextId}`,
type,
get(key, callback) {
const value = data[key] ?? '';
if (callback) callback(value);
else return value;
},
set(key, value) { data[key] = value; },
remove() { objects.delete(object.id); },
};
objects.set(object.id, object);
return object;
};
vm.runInNewContext(script, {
on(event, callback) { assert.equal(event, 'chat:message'); handler = callback; },
playerIsGM: id => id === 'gm',
createObj,
getObj: (type, id) => objects.get(id)?.type === type ? objects.get(id) : undefined,
findObjs: query => [...objects.values()].filter(object => object.type === query.type &&
object.get('characterid') === query.characterid),
sendChat: (speaker, content) => chat.push({ speaker, content }),
encodeURIComponent,
decodeURIComponent,
});
const send = (content, playerid = 'gm', selected = []) =>
handler({ type: 'api', playerid, content, selected });
return { createObj, objects, chat, send };
}
const objectsOf = (game, type) => [...game.objects.values()].filter(object => object.type === type);
test('GM export and import preserve repeating attributes, notes, access, and abilities', () => {
const game = session();
const original = game.createObj('character', { name: 'Ariane', bio: 'Bio é',
gmnotes: 'Secret', controlledby: 'player1', inplayerjournals: 'player1' });
game.createObj('attribute', { characterid: original.id, name: 'base-Force', current: '4', max: '' });
game.createObj('attribute', { characterid: original.id,
name: 'repeating_distance_row1_name', current: 'Pistolet', max: '' });
game.createObj('attribute', { characterid: original.id, name: 'Life_Points', current: '7', max: '10' });
game.createObj('ability', { characterid: original.id, name: 'Action', action: '/em agit', istokenaction: true });
game.send(`!d10-export --id ${original.id}`);
const handout = objectsOf(game, 'handout')[0];
assert.ok(handout);
assert.match(handout.get('gmnotes'), /"format": "d10-roll20-character"/);
game.send(`!d10-import --handout ${handout.id}`);
const imported = objectsOf(game, 'character')[1];
assert.equal(imported.get('name'), 'Ariane');
assert.equal(imported.get('bio'), 'Bio é');
assert.equal(imported.get('gmnotes'), 'Secret');
assert.equal(imported.get('controlledby'), 'player1');
assert.deepEqual(objectsOf(game, 'attribute').filter(item => item.get('characterid') === imported.id)
.map(item => [item.get('name'), item.get('current'), item.get('max')]), [
['base-Force', '4', ''], ['Life_Points', '7', '10'],
['repeating_distance_row1_name', 'Pistolet', ''],
]);
assert.equal(objectsOf(game, 'ability').find(item => item.get('characterid') === imported.id)
.get('action'), '/em agit');
});
test('player export and replace preserve character identity and protected metadata', () => {
const game = session();
const player = game.createObj('player', { _displayname: 'Alice' });
const character = game.createObj('character', { name: 'Avant', bio: 'Avant', gmnotes: 'MJ',
controlledby: player.id, inplayerjournals: player.id });
const token = game.createObj('graphic', { represents: character.id });
game.createObj('attribute', { characterid: character.id, name: 'obsolete', current: 'oui', max: '' });
game.send('!d10-export', player.id, [{ _type: 'graphic', _id: token.id }]);
const handout = objectsOf(game, 'handout')[0];
assert.equal(handout.get('controlledby'), player.id);
assert.match(handout.get('notes'), /"gmnotes": ""/);
const replacement = { format: 'd10-roll20-character', version: 1, name: 'Après',
bio: 'Après', gmnotes: 'Tentative', controlledby: 'intrus', inplayerjournals: 'intrus',
attributes: [{ name: 'base-Force', current: '9', max: '' }], abilities: [] };
handout.set('notes', encodeURIComponent(`<pre>${JSON.stringify(replacement)}</pre>`));
game.send(`!d10-import --replace --handout ${handout.id}`, player.id,
[{ _type: 'graphic', _id: token.id }]);
assert.equal(character.get('name'), 'Après');
assert.equal(character.get('gmnotes'), 'MJ');
assert.equal(character.get('controlledby'), player.id);
assert.equal(token.get('represents'), character.id);
assert.deepEqual(objectsOf(game, 'attribute').map(item => item.get('name')), ['base-Force']);
});
test('permissions and strict version validation reject unsafe imports', () => {
const game = session();
const player = game.createObj('player', { _displayname: 'Alice' });
const other = game.createObj('character', { name: 'Autre', controlledby: 'someone-else' });
const token = game.createObj('graphic', { represents: other.id });
const valid = { format: 'd10-roll20-character', version: 1, name: 'Nouveau', attributes: [] };
const privateHandout = game.createObj('handout', { controlledby: 'someone-else', notes: JSON.stringify(valid) });
game.send(`!d10-import ${JSON.stringify(valid)}`, player.id);
game.send(`!d10-import --replace --handout ${privateHandout.id}`, player.id,
[{ _type: 'graphic', _id: token.id }]);
game.send('!d10-import {"format":"wrong","version":1,"name":"Bad","attributes":[]}');
assert.equal(objectsOf(game, 'character').length, 1);
assert.equal(other.get('name'), 'Autre');
assert.match(game.chat.at(-2).content, /Permission denied/);
assert.match(game.chat.at(-1).content, /Invalid or unsupported/);
});
test('GM handout import decodes Roll20 HTML escaping', () => {
const game = session();
const payload = { format: 'd10-roll20-character', version: 1,
name: 'A & B', bio: '<b>Test</b>', attributes: [] };
const escaped = JSON.stringify(payload).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
const handout = game.createObj('handout', { gmnotes: `<p>${escaped}</p>` });
game.send(`!d10-import --handout ${handout.id}`);
assert.equal(objectsOf(game, 'character')[0].get('name'), 'A & B');
assert.equal(objectsOf(game, 'character')[0].get('bio'), '<b>Test</b>');
});