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 { createTicket } = 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 = { created: [], channelMessages: [], journal: [], fetched: [] }; const journalChannel = { isSendable: () => true, send: async (payload) => state.journal.push(payload), }; 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, 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]), }; return { state, interaction, journalChannel }; } 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; }, ); }); });