Files
node_server/tests/plugin_states_info.test.mjs
T
2026-10-22 17:07:08 +08:00

103 lines
4.7 KiB
JavaScript

import assert from 'node:assert/strict';
import {EventEmitter} from 'node:events';
import test from 'node:test';
import {tsImport} from 'tsx/esm/api';
const {init: PluginStates} = await tsImport('../bot/info/bot/plugin_states.ts', import.meta.url);
const {PluginManager} = await tsImport('../bot/plugins/init.ts', import.meta.url);
const {create_info_manager} = await tsImport('../bot/info/init.ts', import.meta.url);
const item = {name: 'auto_reconnect', path: 'runtime', config: {}};
function setup() {
const context = new EventEmitter();
context.cancel_reconnect = () => {};
const manager = new PluginManager(context);
let subscriptions = 0;
const source = {
get_all_plugins: () => manager.get_all_plugins(),
on_update(callback) {
subscriptions++;
const unsubscribe = manager.on_update(callback);
let active = true;
return () => {
if (!active) return;
active = false; subscriptions--; unsubscribe();
};
},
};
return {context, manager, source, subscriptions: () => subscriptions};
}
test('plugin info initially reads source, streams states without config and owns only its subscription', async () => {
const {context, manager, source, subscriptions} = setup();
await manager.inject(item);
const bot = new EventEmitter();
const info = new PluginStates(bot, {plugin_source: source});
assert.equal(bot.listenerCount('end'), 0);
assert.equal(subscriptions(), 1);
assert.deepEqual(Object.keys(info.get_info().plugins[0]).sort(), ['enabled', 'lifecycle', 'name', 'path', 'states']);
const updates = [];
info.on_update(() => { throw new Error('listener failed'); });
info.on_update(value => { value.plugins[0].states.attempts = 99; });
const unsubscribe = info.on_update(value => updates.push(value));
context.emit('reconnect_attempt');
assert.equal(updates[0].plugins[0].states.attempts, 1);
assert.equal(info.get_info().plugins[0].states.attempts, 1);
const detached = info.get_info(); detached.plugins.length = 0;
assert.equal(info.get_info().plugins.length, 1);
context.emit('status_update', 'connecting');
assert.equal(updates.length, 1);
await manager.uninject(item.name);
assert.equal(updates.at(-1).plugins[0].lifecycle, 'disabled');
assert.equal(updates.at(-1).plugins[0].states.attempts, 1);
unsubscribe(); unsubscribe();
const before = updates.length;
await manager.inject(item);
assert.equal(updates.length, before);
info.dispose(); info.dispose();
assert.equal(subscriptions(), 0);
context.emit('reconnect_attempt');
assert.equal(info.get_info().plugins[0].states.attempts, 0);
assert.throws(() => info.on_update(() => {}), /disposed/);
await manager.dispose();
});
test('missing source is explicitly unavailable and empty manager is available', async () => {
const missing = new PluginStates(new EventEmitter());
assert.deepEqual(missing.get_info(), {available: false, plugins: []});
const {manager, source} = setup();
const empty = new PluginStates(new EventEmitter(), {plugin_source: source});
assert.deepEqual(empty.get_info(), {available: true, plugins: []});
missing.dispose(); empty.dispose(); await manager.dispose();
});
test('InfoManager owns end cleanup and reconnect info uses the same plugin manager', async () => {
const {context, manager, source, subscriptions} = setup();
await manager.inject(item);
const bot = new EventEmitter();
bot._client = new EventEmitter();
const infos = await create_info_manager(bot, {plugin_source: source});
assert.equal(subscriptions(), 1);
const updates = [];
infos.on_update(event => {
if (event.name === 'plugin_states') event.value.plugins[0].states.attempts = 999;
});
infos.on_update(event => { if (event.name === 'plugin_states') updates.push(event.value); });
context.emit('reconnect_attempt');
assert.equal(updates.at(-1).plugins[0].states.attempts, 1);
assert.equal('config' in infos.get_info('plugin_states').value.plugins[0], false);
bot.emit('end');
assert.equal(subscriptions(), 0);
assert.equal(bot.listenerCount('end'), 0);
const before = updates.length;
context.emit('reconnect_attempt');
assert.equal(updates.length, before);
const next = new EventEmitter(); next._client = new EventEmitter();
const reconnected = await create_info_manager(next, {plugin_source: source});
assert.equal(reconnected.get_info('plugin_states').value.plugins[0].states.attempts, 2);
assert.equal(subscriptions(), 1);
reconnected.dispose(); reconnected.dispose();
assert.equal(subscriptions(), 0);
await manager.dispose();
});