Files
tv-discord/test/bot.test.js
T

100 lines
3.8 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { createRooms } = require('../src/room');
const { createBotContext } = require('../src/bot');
const { handleCommand } = require('../src/bot/commands');
const { handleComponent } = require('../src/bot/controls');
const { fakeBugtv, stream } = require('./helpers');
function fakeDiscord() {
const edits = [];
const voice = new Map([['u1', { id: 'u1', channelId: 'vc', member: { user: { bot: false } } }]]);
const guild = { id: 'g', voiceStates: { cache: voice } };
const text = { id: 't', guild, messages: { edit: async (id, p) => edits.push({ id, p }), delete: async () => {} } };
const vc = { id: 'vc', guild };
const channels = new Map([['vc', vc], ['t', text]]);
const client = { channels: { cache: channels, fetch: async (id) => channels.get(id) }, users: { cache: new Map() } };
return { client, guild, edits, voice };
}
function interaction(guild, extra) {
const calls = [];
const rec = (name, ret) => async (arg) => {
calls.push([name, arg]);
return ret;
};
return {
calls,
guild,
guildId: 'g',
channelId: 't',
user: { id: 'u1' },
deferReply: rec('deferReply'),
deferUpdate: rec('deferUpdate'),
editReply: rec('editReply', { id: 'm1' }),
reply: rec('reply'),
update: rec('update'),
followUp: rec('followUp'),
showModal: rec('showModal'),
isAnySelectMenu: () => false,
...extra,
};
}
test('/watch creates the room, posts the control message; buttons drive the room', async () => {
const bugtv = fakeBugtv();
const rooms = createRooms({}, { bugtv });
const d = fakeDiscord();
const ctx = createBotContext({}, rooms, d.client);
ctx.updater.stop();
const w = interaction(d.guild, {
commandName: 'watch',
options: { getString: () => 'movie:550', getInteger: () => null },
});
await handleCommand(w, ctx);
const room = rooms.get('vc');
assert.ok(room);
assert.equal(room.state.messageId, 'm1');
assert.equal(room.voiceCount, 1);
const payload = w.calls.find((c) => c[0] === 'editReply')[1];
assert.equal(payload.embeds[0].title, 'Fight Club (1999)');
assert.equal(payload.components.length, 5);
bugtv.last().h.onStreams({ provider: 'a', streams: [stream('a1'), stream('a2', { pixels: 720 })] });
assert.equal(room.state.sourceId, 'a1');
const click = interaction(d.guild, { customId: 'tv:toggle:vc', message: { id: 'm1' } });
await handleComponent(click, ctx);
assert.equal(click.calls[0][0], 'deferUpdate');
assert.equal(room.state.playing, true);
const sel = interaction(d.guild, { customId: 'tv:src:vc', message: { id: 'm1' }, values: ['a2'], isAnySelectMenu: () => true });
await handleComponent(sel, ctx);
assert.equal(room.state.sourceId, 'a2');
// user not in the VC → ephemeral error, no change
d.voice.delete('u1');
const out = interaction(d.guild, { customId: 'tv:toggle:vc', message: { id: 'm1' } });
await handleComponent(out, ctx);
assert.equal(out.calls[0][0], 'reply');
assert.match(out.calls[0][1].content, /Join <#vc>/);
assert.equal(room.state.playing, true);
d.voice.set('u1', { id: 'u1', channelId: 'vc', member: { user: { bot: false } } });
// seek modal submit
room.report({ duration: 1000 }, null);
const form = interaction(d.guild, { customId: 'tv:seekForm:vc', fields: { getTextInputValue: () => '1:00' } });
await handleComponent(form, ctx);
assert.ok(Math.abs(room.clock() - 60) < 1);
// updater edits the message, then the final "Stopped" edit drops components
await ctx.updater.flush(room);
assert.ok(d.edits.length >= 1);
await room.command('stop', {}, 'u1');
await new Promise((r) => setTimeout(r, 10));
const last = d.edits[d.edits.length - 1];
assert.deepEqual(last.p.components, []);
assert.match(last.p.embeds[0].description, /Stopped/);
});