import assert from 'node:assert/strict'; import { after, before, beforeEach, describe, it } from 'node:test'; import { ChannelType, Collection } from 'discord.js'; /** * Ticket channel creation (M6). * * The channels are declared before the modules that read them are loaded, hence the * dynamic imports: `createTicket` refuses to work without a ticket category, and the * journal goes to the technical logs salon. */ process.env.CATEGORY_TICKETS_ID = '1463259435970461925'; process.env.CHANNEL_LOGS_ID = '1237076782453358745'; process.env.ROLE_STAFF_ID = 'role-staff'; const { closeDatabase, getDatabase, initDatabase } = await import('../src/database/index.js'); const { UserFacingError } = await import('../src/core/errors.js'); const { getTicketPanel } = await import('../src/database/repositories/ticket-repository.js'); const { createTicket, handleTicketComponent, removePanel, setupPanel } = await import('../src/services/tickets/ticket-service.js'); const CATEGORY_ID = process.env.CATEGORY_TICKETS_ID; const LOGS_CHANNEL = process.env.CHANNEL_LOGS_ID; const TICKET_CATEGORY = { id: CATEGORY_ID, type: ChannelType.GuildCategory }; const member = (roleIds = []) => ({ id: 'author-1', guild: { ownerId: 'owner-1' }, roles: { cache: new Collection(roleIds.map((id) => [id, { id }])) }, }); /** Discord reduced to what opening a ticket touches: the category and the new channel. */ function harness({ parent = TICKET_CATEGORY } = {}) { const state = { channelMessages: [], created: [], deleted: [], fetched: [], journal: [], pinned: [], replies: [], }; const journalChannel = { isSendable: () => true, send: async (payload) => state.journal.push(payload), }; const panelChannel = { id: 'panel-1', isTextBased: () => true, isThread: () => false, send: async (payload) => { const message = { id: `panel-message-${state.channelMessages.length + 1}`, pin: async () => { state.pinned.push('panel'); }, delete: async () => { state.deleted.push(message.id); }, }; state.channelMessages.push({ ...payload, id: message.id }); return message; }, messages: { fetch: async (id) => ({ id, delete: async () => state.deleted.push(id) }), }, }; const channel = { id: 'channel-1', send: async (payload) => state.channelMessages.push(payload), delete: async () => {}, }; const guild = { id: 'guild-1', roles: { everyone: { id: 'everyone' } }, channels: { fetch: async (id) => { state.fetched.push(id); if (id === CATEGORY_ID) return parent; if (id === LOGS_CHANNEL) return journalChannel; return null; }, create: async (options) => { state.created.push(options); return channel; }, }, }; const interaction = { guild, guildId: 'guild-1', client: { user: { id: 'bot-1' }, channels: guild.channels }, user: { id: 'author-1', username: 'Léa', tag: 'Léa#0001' }, member: member([process.env.ROLE_STAFF_ID]), reply: async (payload) => state.replies.push(payload), deferReply: async () => {}, editReply: async (payload) => state.replies.push(payload), }; return { state, interaction, journalChannel, panelChannel }; } const open = (harness) => createTicket(harness.interaction, 'bug'); const embedsOf = (payload) => payload.embeds.map((embed) => embed.toJSON()); describe('ticket creation', () => { before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(() => getDatabase().exec('DELETE FROM tickets')); it('creates the channel inside the category declared in the environment', async () => { const test = harness(); await open(test); assert.equal(test.state.fetched.includes(CATEGORY_ID), true); assert.equal(test.state.created.length, 1); assert.equal(test.state.created[0].parent, CATEGORY_ID); assert.equal(test.state.created[0].type, ChannelType.GuildText); }); it('posts the ticket header and a guidance embed', async () => { const test = harness(); await open(test); assert.equal(test.state.channelMessages.length, 1); const [header, guidance] = embedsOf(test.state.channelMessages[0]); assert.match(header.title, /^🎫 Ticket Bug #/); assert.match(guidance.description, /Détaille au maximum/); assert.match(guidance.fields[0].value, /étapes pour reproduire/); assert.match(guidance.fields[1].name, /un peu de temps/); }); it('writes the journal in the technical logs salon', async () => { const test = harness(); await open(test); assert.equal(test.state.journal.length, 1); assert.equal(test.state.journal[0].embeds[0].toJSON().title, '🎫 Ticket ouvert'); }); it('explains how to fix the environment when the category is missing', async () => { const test = harness({ parent: null }); await assert.rejects( () => open(test), (error) => { assert.ok(error instanceof UserFacingError); assert.match(error.message, /CATEGORY_TICKETS_ID/); return true; }, ); assert.equal(test.state.created.length, 0); }); it('refuses a category id that points at a plain channel', async () => { const test = harness({ parent: { id: CATEGORY_ID, type: ChannelType.GuildText } }); await assert.rejects( () => open(test), (error) => { assert.ok(error instanceof UserFacingError); assert.match(error.message, /catégorie Discord/); return true; }, ); }); }); describe('ticket panel', () => { before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(() => getDatabase().exec('DELETE FROM tickets; DELETE FROM ticket_panels;')); const panelInteraction = (test) => ({ ...test.interaction, channel: test.panelChannel }); const panelMessage = (test) => test.state.channelMessages.at(-1); it('publishes and pins the panel, then remembers it', async () => { const test = harness(); await setupPanel(panelInteraction(test)); const message = panelMessage(test); assert.match(message.embeds[0].toJSON().title, /Besoin d’aide/); assert.deepEqual(test.state.pinned, ['panel']); const panel = getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }); assert.equal(panel.message_id, message.id); assert.equal(panel.created_by, 'author-1'); assert.match(test.state.replies.at(-1).embeds[0].toJSON().description, /Panneau publié et épinglé/); }); it('replaces the previous panel instead of stacking a second one', async () => { const test = harness(); await setupPanel(panelInteraction(test)); await setupPanel(panelInteraction(test)); assert.deepEqual(test.state.deleted, ['panel-message-1']); assert.equal(test.state.channelMessages.length, 2); assert.equal(getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }).message_id, 'panel-message-2'); }); it('keeps the panel when the previous message was deleted by hand', async () => { const test = harness(); await setupPanel(panelInteraction(test)); await setupPanel(panelInteraction(test)); assert.equal(getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }).message_id, 'panel-message-2'); }); it('refuses to install a panel when the ticket category is unusable', async () => { const test = harness(); test.interaction.guild.channels.fetch = async () => null; await assert.rejects( () => setupPanel(panelInteraction(test)), (error) => { assert.ok(error instanceof UserFacingError); assert.match(error.message, /CATEGORY_TICKETS_ID/); return true; }, ); assert.equal(test.state.channelMessages.length, 0); }); it('removes the panel and its message', async () => { const test = harness(); await setupPanel(panelInteraction(test)); await removePanel(panelInteraction(test)); assert.deepEqual(test.state.deleted, ['panel-message-1']); assert.equal(getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }), null); }); it('says so when the channel has no panel', async () => { const test = harness(); await assert.rejects( () => removePanel(panelInteraction(test)), (error) => { assert.ok(error instanceof UserFacingError); assert.match(error.message, /\/ticket setup/); return true; }, ); }); it('offers only the categories the member may open', async () => { const test = harness(); await handleTicketComponent({ ...panelInteraction(test), customId: 'ticket:panel:open', member: member([process.env.ROLE_STAFF_ID]), }); const [row] = test.state.replies.at(-1).components; const { options } = row.toJSON().components[0]; assert.deepEqual( options.map((option) => option.value), ['support', 'bug', 'report'], ); }); it('tells a member with no category to ask in the salon', async () => { const test = harness(); await handleTicketComponent({ ...panelInteraction(test), customId: 'ticket:panel:open', member: member([]), }); const reply = test.state.replies.at(-1); assert.equal(reply.components, undefined); assert.match(reply.embeds[0].toJSON().description, /Aucune catégorie/); }); it('opens the ticket from the chosen category', async () => { const test = harness(); await handleTicketComponent({ ...panelInteraction(test), customId: 'ticket:panel:pick', values: ['bug'], }); assert.equal(test.state.created[0].parent, CATEGORY_ID); assert.match(test.state.replies.at(-1).embeds[0].toJSON().description, /Ton ticket est ouvert/); }); });