140 lines
7.8 KiB
JavaScript
140 lines
7.8 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_craft_import.js', import.meta.url), 'utf8');
|
|
const craftFixtures = ['example-craft-forge.json', 'example-craft-alchemy.json'].map(name =>
|
|
JSON.parse(readFileSync(new URL(`../build/${name}`, 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, isFinite,
|
|
});
|
|
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);
|
|
}
|
|
|
|
test('imports all supported craft item types into a selected character', () => {
|
|
const game = session();
|
|
const character = game.createObj('character', { name: 'Ariane' });
|
|
const token = game.createObj('graphic', { represents: character.id });
|
|
const data = { format: 'ravenous-roll20-craft', version: 1,
|
|
craft: { name: 'Lot ardent', discipline: 'forge', element: 'Feu', level: 3 },
|
|
items: [
|
|
{ type: 'attack', name: 'Lame', dice: 5, base_damage: 8, notes: 'Feu' },
|
|
{ type: 'protection', name: 'Targe', protection: 3, integrity: 2, integrity_max: 2 },
|
|
{ type: 'consumable', name: 'Huile', kind: 'Autre', quantity: 2 },
|
|
{ type: 'modifier', name: 'Piège', duration: '1 tour', description: 'Entrave' },
|
|
] };
|
|
game.send(`!ravenous-import-craft ${JSON.stringify(data)}`, 'gm',
|
|
[{ _type: 'graphic', _id: token.id }]);
|
|
const attrs = attributes(game, character.id);
|
|
assert.equal(attrs.filter(attr => attr.get('name').endsWith('_name')).length, 4);
|
|
assert.ok(attrs.some(attr => /^repeating_attacks_.*_base_damage$/.test(attr.get('name')) && attr.get('current') === '8'));
|
|
assert.ok(attrs.some(attr => /^repeating_protections_.*_integrity_max$/.test(attr.get('name'))));
|
|
assert.ok(attrs.some(attr => /^repeating_consumables_.*_kind$/.test(attr.get('name')) && attr.get('current') === 'Autre'));
|
|
assert.ok(attrs.some(attr => /^repeating_modifiers_.*_description$/.test(attr.get('name'))));
|
|
assert.match(game.chat.at(-1).content, /Imported 4 craft item/);
|
|
});
|
|
|
|
test('a player can export craft rows and import the resulting handout', () => {
|
|
const game = session();
|
|
const player = game.createObj('player', { _displayname: 'Alice' });
|
|
const source = game.createObj('character', { name: 'Artisane', controlledby: player.id });
|
|
game.createObj('attribute', { characterid: source.id, name: 'repeating_attacks_row_name', current: 'Lame', max: '' });
|
|
game.createObj('attribute', { characterid: source.id, name: 'repeating_attacks_row_base_damage', current: '8', max: '' });
|
|
game.createObj('attribute', { characterid: source.id, name: 'repeating_attacks_row_unrelated', current: 'ignore', max: '' });
|
|
game.createObj('attribute', { characterid: source.id, name: 'repeating_consumables_food_name', current: 'Soupe', max: '' });
|
|
game.createObj('attribute', { characterid: source.id, name: 'repeating_consumables_food_kind', current: 'Nourriture', max: '' });
|
|
game.send(`!ravenous-export-craft --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'), /"name": "Lame"/);
|
|
assert.doesNotMatch(handout.get('notes'), /unrelated/);
|
|
|
|
const destination = game.createObj('character', { name: 'Destination', controlledby: player.id });
|
|
game.send(`!ravenous-import-craft --id ${destination.id} --handout ${handout.id}`, player.id);
|
|
const imported = attributes(game, destination.id);
|
|
assert.ok(imported.some(attribute => attribute.get('current') === 'Lame'));
|
|
assert.ok(imported.some(attribute => attribute.get('current') === 'Soupe'));
|
|
assert.match(game.chat.at(-1).content, /Imported 2 craft item/);
|
|
});
|
|
|
|
test('generated craft fixtures import into all supported repeating sections', () => {
|
|
const game = session();
|
|
const character = game.createObj('character', { name: 'Fixture' });
|
|
const token = game.createObj('graphic', { represents: character.id });
|
|
for (const fixture of craftFixtures) {
|
|
game.send(`!ravenous-import-craft ${JSON.stringify(fixture)}`, 'gm',
|
|
[{ _type: 'graphic', _id: token.id }]);
|
|
}
|
|
const names = attributes(game, character.id).map(attribute => attribute.get('name'));
|
|
for (const section of ['attacks', 'protections', 'consumables', 'modifiers']) {
|
|
assert.ok(names.some(name => name.startsWith(`repeating_${section}_`)), section);
|
|
}
|
|
});
|
|
|
|
test('supports explicit character and HTML-escaped handout JSON', () => {
|
|
const game = session();
|
|
const character = game.createObj('character', { name: 'Arangar' });
|
|
const data = { format: 'ravenous-roll20-craft', version: 1,
|
|
items: [{ type: 'consumable', name: 'A & B', kind: 'Potion', quantity: 1 }] };
|
|
const escaped = JSON.stringify(data).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
const handout = game.createObj('handout', { gmnotes: `<pre>${escaped}</pre>` });
|
|
game.send(`!ravenous-import-craft --id ${character.id} --handout ${handout.id}`);
|
|
assert.ok(attributes(game, character.id).some(attr => attr.get('current') === 'A & B'));
|
|
});
|
|
|
|
test('rejects unauthorized players, unknown fields, invalid kinds, malformed JSON, and missing targets', () => {
|
|
const game = session();
|
|
const character = game.createObj('character', { name: 'Ariane' });
|
|
const valid = { format: 'ravenous-roll20-craft', version: 1,
|
|
items: [{ type: 'consumable', name: 'Potion', kind: 'Potion' }] };
|
|
game.send(`!ravenous-import-craft --id ${character.id} ${JSON.stringify(valid)}`, 'player');
|
|
game.send(`!ravenous-import-craft --id ${character.id} {bad}`);
|
|
game.send(`!ravenous-import-craft --id ${character.id} ${JSON.stringify({ ...valid,
|
|
items: [{ type: 'consumable', name: 'Potion', kind: 'Weapon' }] })}`);
|
|
game.send(`!ravenous-import-craft --id ${character.id} ${JSON.stringify({ ...valid, extra: true })}`);
|
|
game.send(`!ravenous-import-craft ${JSON.stringify(valid)}`);
|
|
assert.equal(attributes(game, character.id).length, 0);
|
|
assert.match(game.chat.at(-1).content, /Select one token/);
|
|
});
|
|
|
|
test('a player can import into a controlled character from an accessible handout', () => {
|
|
const game = session();
|
|
const character = game.createObj('character', { name: 'Ariane', controlledby: 'player' });
|
|
const token = game.createObj('graphic', { represents: character.id });
|
|
const data = { format: 'ravenous-roll20-craft', version: 1,
|
|
items: [{ type: 'consumable', name: 'Potion', kind: 'Potion' }] };
|
|
const handout = game.createObj('handout', { controlledby: 'player', notes: JSON.stringify(data) });
|
|
game.send(`!ravenous-import-craft --handout ${handout.id}`, 'player',
|
|
[{ _type: 'graphic', _id: token.id }]);
|
|
assert.ok(attributes(game, character.id).some(attr => attr.get('current') === 'Potion'));
|
|
});
|