193 lines
11 KiB
JavaScript
193 lines
11 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('../roll20_mod/ravenous_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); },
|
|
data,
|
|
};
|
|
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(obj => obj.type === query.type &&
|
|
obj.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 };
|
|
}
|
|
|
|
test('export handout and import preserve notes and repeating attributes', () => {
|
|
const game = session();
|
|
const original = game.createObj('character', { name: 'Arangar', bio: 'Bio é',
|
|
gmnotes: 'GM only', controlledby: 'player1', inplayerjournals: 'player1' });
|
|
game.createObj('attribute', { characterid: original.id, name: 'force', current: '10', max: '' });
|
|
game.createObj('attribute', { characterid: original.id, name: 'repeating_attacks_row1_name', current: 'Griffe', max: '' });
|
|
game.createObj('attribute', { characterid: original.id, name: 'hp', current: '0', max: '20' });
|
|
game.createObj('ability', { characterid: original.id, name: 'Roar', action: '/em Roars', istokenaction: true });
|
|
game.send(`!ravenous-export --id ${original.id}`);
|
|
const handout = [...game.objects.values()].find(obj => obj.type === 'handout');
|
|
assert.ok(handout);
|
|
assert.equal(handout.get('inplayerjournals'), '');
|
|
assert.match(handout.get('gmnotes'), /"format": "ravenous-roll20-character"/);
|
|
game.send(`!ravenous-import --handout ${handout.id}`);
|
|
const characters = [...game.objects.values()].filter(obj => obj.type === 'character');
|
|
assert.equal(characters.length, 2);
|
|
const imported = characters[1];
|
|
assert.equal(imported.get('name'), 'Arangar');
|
|
assert.equal(imported.get('bio'), 'Bio é');
|
|
assert.equal(imported.get('gmnotes'), 'GM only');
|
|
assert.equal(imported.get('controlledby'), 'player1');
|
|
const attrs = [...game.objects.values()].filter(obj => obj.type === 'attribute' && obj.get('characterid') === imported.id);
|
|
assert.deepEqual(attrs.map(obj => [obj.get('name'), obj.get('current'), obj.get('max')]), [
|
|
['force', '10', ''], ['hp', '0', '20'], ['repeating_attacks_row1_name', 'Griffe', ''],
|
|
]);
|
|
const ability = [...game.objects.values()].find(obj => obj.type === 'ability' && obj.get('characterid') === imported.id);
|
|
assert.equal(ability.get('action'), '/em Roars');
|
|
});
|
|
|
|
test('export uses the selected token by default and requires one linked token', () => {
|
|
const game = session();
|
|
const character = game.createObj('character', { name: 'Ariane' });
|
|
const linked = game.createObj('graphic', { represents: character.id });
|
|
const unlinked = game.createObj('graphic', { represents: '' });
|
|
game.send('!ravenous-export');
|
|
game.send('!ravenous-export', 'gm', [{ _type: 'graphic', _id: unlinked.id }]);
|
|
game.send('!ravenous-export', 'gm', [{ _type: 'graphic', _id: linked.id }, { _type: 'graphic', _id: unlinked.id }]);
|
|
assert.equal([...game.objects.values()].filter(obj => obj.type === 'handout').length, 0);
|
|
game.send('!ravenous-export', 'gm', [{ _type: 'graphic', _id: linked.id }]);
|
|
assert.equal([...game.objects.values()].filter(obj => obj.type === 'handout').length, 1);
|
|
});
|
|
|
|
test('inline JSON imports a character, while non-GM and invalid input do not', () => {
|
|
const game = session();
|
|
const json = JSON.stringify({ format: 'ravenous-roll20-character', version: 1,
|
|
name: 'Creature', attributes: [{ name: 'force', current: '5', max: '' }] });
|
|
game.send(`!ravenous-import ${json}`, 'player');
|
|
game.send('!ravenous-import {bad}');
|
|
game.send('!ravenous-import {"format":"wrong","name":"Bad","attributes":[]}');
|
|
assert.equal(game.objects.size, 0);
|
|
game.send(`!ravenous-import ${json}`);
|
|
assert.equal([...game.objects.values()].filter(obj => obj.type === 'character').length, 1);
|
|
assert.match(game.chat.at(-1).content, /Imported 1 attributes/);
|
|
});
|
|
|
|
test('a player can export and replace a controlled character in place', () => {
|
|
const game = session();
|
|
game.createObj('player', { _displayname: 'Alice' }).id = 'player';
|
|
const character = game.createObj('character', { name: 'Avant', bio: 'Ancienne bio',
|
|
gmnotes: 'Secret MJ', controlledby: 'player', inplayerjournals: 'player' });
|
|
const token = game.createObj('graphic', { represents: character.id });
|
|
game.createObj('attribute', { characterid: character.id, name: 'force', current: '2', max: '' });
|
|
game.createObj('attribute', { characterid: character.id, name: 'obsolete', current: 'oui', max: '' });
|
|
game.createObj('ability', { characterid: character.id, name: 'Ancienne', action: '/em test', istokenaction: false });
|
|
|
|
game.send('!ravenous-export', 'player', [{ _type: 'graphic', _id: token.id }]);
|
|
const exported = [...game.objects.values()].find(obj => obj.type === 'handout');
|
|
assert.equal(exported.get('controlledby'), 'player');
|
|
assert.equal(exported.get('gmnotes'), '');
|
|
assert.match(exported.get('notes'), /"gmnotes": ""/);
|
|
|
|
const replacement = { format: 'ravenous-roll20-character', version: 1, name: 'Après',
|
|
bio: 'Nouvelle bio', gmnotes: 'Tentative joueur', controlledby: 'intrus', inplayerjournals: 'intrus',
|
|
attributes: [{ name: 'force', current: '9', max: '' }],
|
|
abilities: [{ name: 'Nouvelle', action: '/em nouveau', istokenaction: true }] };
|
|
exported.set('notes', encodeURIComponent(`<pre>${JSON.stringify(replacement)}</pre>`));
|
|
game.send(`!ravenous-import --replace --handout ${exported.id}`, 'player',
|
|
[{ _type: 'graphic', _id: token.id }]);
|
|
|
|
assert.equal(character.get('name'), 'Après');
|
|
assert.equal(character.get('bio'), 'Nouvelle bio');
|
|
assert.equal(character.get('gmnotes'), 'Secret MJ');
|
|
assert.equal(character.get('controlledby'), 'player');
|
|
assert.equal(character.get('inplayerjournals'), 'player');
|
|
assert.equal(token.get('represents'), character.id);
|
|
const attrs = [...game.objects.values()].filter(obj => obj.type === 'attribute' &&
|
|
obj.get('characterid') === character.id);
|
|
assert.deepEqual(attrs.map(obj => [obj.get('name'), obj.get('current')]), [['force', '9']]);
|
|
const abilities = [...game.objects.values()].filter(obj => obj.type === 'ability' &&
|
|
obj.get('characterid') === character.id);
|
|
assert.deepEqual(abilities.map(obj => obj.get('name')), ['Nouvelle']);
|
|
assert.match(game.chat.at(-1).content, /Replaced 1 attributes/);
|
|
});
|
|
|
|
test('players cannot export or replace characters and handouts they cannot access', () => {
|
|
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 payload = { format: 'ravenous-roll20-character', version: 1,
|
|
name: 'Intrusion', attributes: [] };
|
|
const privateHandout = game.createObj('handout', { controlledby: 'someone-else',
|
|
notes: JSON.stringify(payload) });
|
|
game.send('!ravenous-export', player.id, [{ _type: 'graphic', _id: token.id }]);
|
|
game.send(`!ravenous-import --replace --handout ${privateHandout.id}`, player.id,
|
|
[{ _type: 'graphic', _id: token.id }]);
|
|
assert.equal(other.get('name'), 'Autre');
|
|
assert.equal([...game.objects.values()].filter(obj => obj.type === 'handout').length, 1);
|
|
assert.match(game.chat.at(-1).content, /Permission denied/);
|
|
});
|
|
|
|
test('a GM can import JSON pasted into a handout and HTML escaped by Roll20', () => {
|
|
const game = session();
|
|
const json = JSON.stringify({ format: 'ravenous-roll20-character', version: 1,
|
|
name: 'A & B', bio: '<b>Test</b>', attributes: [] });
|
|
const handout = game.createObj('handout', { gmnotes: `<p>${json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')}</p>` });
|
|
game.send(`!ravenous-import --handout ${handout.id}`);
|
|
const character = [...game.objects.values()].find(obj => obj.type === 'character');
|
|
assert.equal(character.get('name'), 'A & B');
|
|
assert.equal(character.get('bio'), '<b>Test</b>');
|
|
});
|
|
|
|
test('a converted database monster imports with its body parts and abilities',
|
|
{ skip: !process.env.RAVENOUS_MONSTER_JSON }, () => {
|
|
const data = JSON.parse(readFileSync(process.env.RAVENOUS_MONSTER_JSON, 'utf8'));
|
|
const game = session();
|
|
const handout = game.createObj('handout', { gmnotes: `<pre>${JSON.stringify(data).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')}</pre>` });
|
|
game.send(`!ravenous-import --handout ${handout.id}`);
|
|
const character = [...game.objects.values()].find(obj => obj.type === 'character');
|
|
assert.equal(character.get('name'), data.name);
|
|
const attrs = [...game.objects.values()].filter(obj => obj.type === 'attribute' && obj.get('characterid') === character.id);
|
|
assert.equal(attrs.length, data.attributes.length);
|
|
assert.ok(attrs.some(obj => /^repeating_locations_dbpart\d+_name$/.test(obj.get('name'))));
|
|
assert.ok(attrs.some(obj => /^repeating_attacks_dbability\d+_name$/.test(obj.get('name'))));
|
|
assert.ok(attrs.some(obj => /^repeating_powers_dbability\d+_name$/.test(obj.get('name'))));
|
|
});
|
|
|
|
test('a generated fuzz character imports with all representative repeating sections',
|
|
{ skip: !process.env.RAVENOUS_CHARACTER_JSON }, () => {
|
|
const data = JSON.parse(readFileSync(process.env.RAVENOUS_CHARACTER_JSON, 'utf8'));
|
|
const game = session();
|
|
game.send(`!ravenous-import ${JSON.stringify(data)}`);
|
|
const character = [...game.objects.values()].find(obj => obj.type === 'character');
|
|
assert.equal(character.get('name'), data.name);
|
|
const attrs = [...game.objects.values()].filter(obj => obj.type === 'attribute' &&
|
|
obj.get('characterid') === character.id);
|
|
assert.equal(attrs.length, data.attributes.length);
|
|
for (const section of ['locations', 'customskills', 'attacks', 'protections', 'consumables',
|
|
'modifiers', 'techniques', 'powers']) {
|
|
assert.ok(attrs.some(obj => obj.get('name').startsWith(`repeating_${section}_`)), section);
|
|
}
|
|
});
|