ravenous_roll20/tests/test_effects_import.mjs

165 lines
8.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('../roll20_mod/ravenous_effects_import.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; }, 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(object => object.type === query.type &&
object.get('characterid') === query.characterid),
sendChat: (speaker, content) => chat.push({ speaker, content }),
Date, Math, Number, parseInt, String,
});
const send = (content, playerid = 'gm', selected = []) => handler({ type: 'api', playerid, content, selected });
return { createObj, objects, chat, send };
}
function attributes(game, characterId) {
return [...game.objects.values()].filter(object => object.type === 'attribute' &&
object.get('characterid') === characterId);
}
function payload(effects) {
return { format: 'ravenous-roll20-effects', version: 1, effects };
}
test('imports complete reusable effect rows into a selected character', () => {
const game = session();
const character = game.createObj('character', { name: 'Ariane' });
const token = game.createObj('graphic', { represents: character.id });
const effect = {
enabled: false, effect_id: 'combustion', type: 'dot', name: 'Combustion', duration: '3 tours',
description: 'Perdez quatre points de vie.', duration_type: 'turns', remaining: 3,
countdown_phase: 'start', start_reminder: 'Subissez 4 dégâts.', end_reminder: 'Testez la résistance.',
apply_reminder: 'La cible prend feu.', removal_reminder: 'Les flammes cessent.'
};
const library = { ...payload([effect]), name: 'Feux favoris', catalog: '0123456789ab' };
game.send(`!ravenous-import-effects ${JSON.stringify(library)}`, 'gm',
[{ _type: 'graphic', _id: token.id }]);
const attrs = attributes(game, character.id);
assert.equal(attrs.length, 13, JSON.stringify(game.chat));
const values = Object.fromEntries(attrs.map(attribute => [
attribute.get('name').replace(/^repeating_sharedeffects_[^_]+_/, ''), attribute.get('current')
]));
assert.deepEqual(values, {
enabled: '0', effect_id: 'combustion', type: 'dot', name: 'Combustion', duration: '3 tours',
description: 'Perdez quatre points de vie.', duration_type: 'turns', remaining: '3',
countdown_phase: 'start', start_reminder: 'Subissez 4 dégâts.', end_reminder: 'Testez la résistance.',
apply_reminder: 'La cible prend feu.', removal_reminder: 'Les flammes cessent.'
});
assert.match(game.chat.at(-1).content, /Imported 1 reusable effect/);
});
test('fills every sheet field with safe defaults', () => {
const game = session();
const character = game.createObj('character', { name: 'Ariane' });
game.send(`!ravenous-import-effects --id ${character.id} ${JSON.stringify(payload([
{ effect_id: 'inspiration', type: 'bonus', name: 'Inspiration' }
]))}`);
const attrs = attributes(game, character.id);
assert.equal(attrs.length, 13);
const values = attrs.map(attribute => attribute.get('current'));
assert.ok(values.includes('manual'));
assert.ok(values.includes('end'));
assert.ok(values.includes('1'));
assert.ok(values.includes('0'));
});
test('exports reusable effects to a private handout that can be imported again', () => {
const game = session();
const player = game.createObj('player', { _displayname: 'Alice' });
const source = game.createObj('character', { name: 'Ariane', controlledby: player.id });
const fields = { enabled: '1', effect_id: 'al_combustion', type: 'dot', name: 'Combustion',
duration: '2 tours', description: 'Brûle.', duration_type: 'turns', remaining: '2',
countdown_phase: 'start', start_reminder: 'Subissez 4 dégâts.', end_reminder: '',
apply_reminder: '', removal_reminder: 'Les flammes cessent.' };
for (const [field, current] of Object.entries(fields)) {
game.createObj('attribute', { characterid: source.id,
name: `repeating_sharedeffects_row1_${field}`, current, max: '' });
}
game.send(`!ravenous-export-effects --id ${source.id}`, player.id);
const handout = [...game.objects.values()].find(object => object.type === 'handout');
assert.equal(handout.get('controlledby'), player.id);
assert.match(handout.get('notes'), /"effect_id": "al_combustion"/);
assert.match(game.chat.at(-1).content, /Exported 1 reusable effect/);
const destination = game.createObj('character', { name: 'Destination', controlledby: player.id });
game.send(`!ravenous-import-effects --id ${destination.id} --handout ${handout.id}`, player.id);
assert.ok(attributes(game, destination.id).some(attribute => attribute.get('current') === 'Combustion'));
});
test('a player can import an HTML-escaped accessible handout into a controlled character', () => {
const game = session();
const player = game.createObj('player', { _displayname: 'Alice' });
const character = game.createObj('character', { name: 'Ariane', controlledby: player.id });
const data = payload([{ effect_id: 'a-b', type: 'alteration', name: 'A & B', description: '<rare>' }]);
const escaped = JSON.stringify(data).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
const handout = game.createObj('handout', { inplayerjournals: player.id, notes: `<pre>${escaped}</pre>` });
game.send(`!ravenous-import-effects --id ${character.id} --handout ${handout.id}`, player.id);
assert.ok(attributes(game, character.id).some(attribute => attribute.get('current') === 'A & B'), JSON.stringify(game.chat));
assert.ok(attributes(game, character.id).some(attribute => attribute.get('current') === '<rare>'));
});
test('rejects unauthorized, malformed, unknown, oversized, and out-of-range payloads', () => {
const invalidEffects = [
{ effect_id: 'bad', type: 'unknown', name: 'Bad' },
{ effect_id: 'bad', type: 'dot', name: 'Bad', remaining: 1000 },
{ effect_id: 'bad', type: 'dot', name: 'Bad', enabled: 'yes' },
{ effect_id: 'bad', type: 'dot', name: 'Bad', extra: true },
{ effect_id: '', type: 'dot', name: 'Bad' },
];
for (const effect of invalidEffects) {
const game = session();
const character = game.createObj('character', { name: 'Ariane' });
game.send(`!ravenous-import-effects --id ${character.id} ${JSON.stringify(payload([effect]))}`);
assert.equal(attributes(game, character.id).length, 0);
assert.match(game.chat.at(-1).content, /Invalid or unsupported/);
}
const game = session();
const character = game.createObj('character', { name: 'Ariane' });
game.send(`!ravenous-import-effects --id ${character.id} ${JSON.stringify(payload([
{ effect_id: 'ok', type: 'other', name: 'Okay' }
]))}`, 'intruder');
game.send(`!ravenous-import-effects --id ${character.id} {bad}`);
game.send(`!ravenous-import-effects --id ${character.id} ${JSON.stringify({ ...payload([
{ effect_id: 'ok', type: 'other', name: 'Okay' }
]), extra: true })}`);
assert.equal(attributes(game, character.id).length, 0);
});
test('rejects a handout the player cannot read and a character they cannot control', () => {
const game = session();
const player = game.createObj('player', { _displayname: 'Alice' });
const own = game.createObj('character', { name: 'Ariane', controlledby: player.id });
const other = game.createObj('character', { name: 'Autre' });
const handout = game.createObj('handout', { notes: JSON.stringify(payload([
{ effect_id: 'ok', type: 'other', name: 'Okay' }
])) });
game.send(`!ravenous-import-effects --id ${own.id} --handout ${handout.id}`, player.id);
game.send(`!ravenous-import-effects --id ${other.id} ${handout.get('notes')}`, player.id);
assert.equal(attributes(game, own.id).length, 0);
assert.equal(attributes(game, other.id).length, 0);
assert.match(game.chat.at(-1).content, /Permission denied/);
});