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'); 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, 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('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, '>'); const handout = game.createObj('handout', { gmnotes: `
${escaped}` });
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', gmnotes: 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'));
});