// A param that holds several values (MODULE_API 1.14.0; Rust PLAN_REDESIGNS // §12.3): what the multi-select stores, when it gives way to JSON, and how the // run console reads a list back. import { test } from 'node:test' import assert from 'node:assert/strict' import { blankStep, paramsMode, PARAM_FORM, PARAM_JSON, listValue, setParamList, addToList, filterOptions, missingParams, describeParams, } from '../src/lib/eventAuthoring.js' const LEND = { id: 'test.perm.lend', params: [ { name: 'server', type: 'string', required: true, example: 'main' }, { name: 'perms', type: 'string', required: true, example: 'nightvision.allowed', multiple: true, max: 3 }, { name: 'ids', type: 'int', required: false, example: 7, multiple: true, max: 20 }, ], } const step = (params) => ({ actionId: LEND.id, paramsText: JSON.stringify(params) }) test('a new step holds the example as a list of one', () => { const fresh = JSON.parse(blankStep(LEND).paramsText) assert.deepEqual(fresh.perms, ['nightvision.allowed']) assert.deepEqual(fresh.ids, [7]) assert.equal(fresh.server, 'main') }) test('a list is a value the form can hold; a structure in it is not', () => { assert.equal(paramsMode(step({ perms: ['a', 'b'] }), LEND).mode, PARAM_FORM) assert.equal(paramsMode(step({ perms: 'a' }), LEND).mode, PARAM_FORM) const nested = paramsMode(step({ perms: [{ a: 1 }] }), LEND) assert.equal(nested.mode, PARAM_JSON) assert.match(nested.reason, /"perms" holds a structure/) // A list against a single-value param still forces JSON, as before 1.14.0. assert.equal(paramsMode(step({ server: ['main'] }), LEND).mode, PARAM_JSON) }) test('listValue reads a bare value as a list of one, and nothing as none', () => { assert.deepEqual(listValue(['a', 'b']), ['a', 'b']) assert.deepEqual(listValue('a'), ['a']) for (const empty of [undefined, null, '']) assert.deepEqual(listValue(empty), []) }) test('setParamList coerces, drops duplicates in order, and removes an empty list', () => { const s = step({ server: 'main' }) assert.deepEqual(JSON.parse(setParamList(s, 'ids', ['3', '1', '3'], 'int')).ids, [3, 1]) assert.deepEqual(JSON.parse(setParamList(s, 'perms', ['b', 'a', 'b', ''], 'string')).perms, ['b', 'a']) const cleared = JSON.parse(setParamList(step({ server: 'main', perms: ['a'] }), 'perms', [], 'string')) assert.equal('perms' in cleared, false) // A box that does not parse is handed back untouched. assert.equal(setParamList({ paramsText: '{nope' }, 'perms', ['a'], 'string'), '{nope') }) test('addToList ignores blanks and repeats, and refuses past max in words', () => { assert.deepEqual(addToList(['a'], ' b ', 3).list, ['a', 'b']) assert.deepEqual(addToList(['a'], 'a', 3).list, ['a']) assert.deepEqual(addToList(['a'], ' ', 3).list, ['a']) assert.match(addToList(['a', 'b', 'c'], 'd', 3).refused, /at most 3/) }) test('filterOptions matches value, label or group and keeps the source order', () => { const options = [ { value: 'nightvision.allowed', label: 'nightvision.allowed', group: 'NightVision' }, { value: 'betterchat.color', label: 'betterchat.color', group: 'BetterChat' }, { value: 'kits.vip', label: 'kits.vip', group: 'Kits' }, ] assert.equal(filterOptions(options, '').length, 3) assert.deepEqual(filterOptions(options, 'NIGHT').map((o) => o.value), ['nightvision.allowed']) assert.deepEqual(filterOptions(options, 'chat').map((o) => o.value), ['betterchat.color']) assert.deepEqual(filterOptions(options, 'i').map((o) => o.value), ['nightvision.allowed', 'kits.vip']) }) test('an empty required list is missing', () => { assert.deepEqual(missingParams(step({ server: 'main', perms: [] }), LEND), ['perms']) assert.deepEqual(missingParams(step({ server: 'main' }), LEND), ['perms']) assert.deepEqual(missingParams(step({ server: 'main', perms: ['a'] }), LEND), []) }) test('the run console shows a list as its values joined by commas', () => { assert.equal( describeParams({ server: 'main', perms: ['nightvision.allowed', 'betterchat.color'], confirm: true }), 'server: main · perms: nightvision.allowed, betterchat.color · confirm: true', ) assert.equal(describeParams({ where: { a: 1 } }), 'where: {"a":1}') assert.equal(describeParams(null), '') })