import assert from 'node:assert/strict'; import { after, before, beforeEach, describe, it } from 'node:test'; import { UserFacingError } from '../src/core/errors.js'; import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js'; import { addBotStatus as addRow, countBotStatuses, getBotStatus, listBotStatuses, listEnabledBotStatuses, removeBotStatus, setBotStatusEnabled, } from '../src/database/repositories/bot-status-repository.js'; import { setSetting } from '../src/database/repositories/settings-repository.js'; import { addBotStatus, applyStatusIfAlone, clampRotation, clearBotStatusList, FALLBACK_STATUS, formatSeconds, getRotationSeconds, normalizeStatusType, ROTATION_KEY, ROTATION_MAX_SECONDS, ROTATION_MIN_SECONDS, rotateBotStatus, sanitizeStatusText, saveRotationSeconds, selectNextStatus, STATUS_MAX, STATUS_TEXT_MAX, statusListEmbed, toggleBotStatus, validateStatusUrl, } from '../src/services/status/bot-status-service.js'; const GUILD = 'g'; describe('bot status repository', () => { before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(() => getDatabase().exec('DELETE FROM bot_statuses')); it('stores a status and reads it back', () => { const status = addRow({ text: 'les commandes avec /help', type: 'watching', createdBy: 'u1' }); assert.equal(status.text, 'les commandes avec /help'); assert.equal(status.type, 'watching'); assert.equal(status.enabled, 1); assert.equal(status.created_by, 'u1'); assert.equal(getBotStatus(status.id).text, status.text); assert.equal(countBotStatuses(), 1); }); it('keeps the insertion order and separates the paused ones', () => { const first = addRow({ text: 'un' }); addRow({ text: 'deux' }); const third = addRow({ text: 'trois' }); setBotStatusEnabled(third.id, false); assert.deepEqual( listBotStatuses().map((status) => status.text), ['un', 'deux', 'trois'], ); assert.deepEqual( listEnabledBotStatuses().map((status) => status.text), ['un', 'deux'], ); assert.equal(first.id < third.id, true); }); it('refuses the same text twice and forgets a removed status', () => { const status = addRow({ text: 'déjà là' }); assert.throws(() => addRow({ text: 'déjà là' }), /UNIQUE constraint/i); assert.equal(removeBotStatus(status.id), 1); assert.equal(getBotStatus(status.id), null); assert.equal(removeBotStatus(status.id), 0); }); }); describe('bot status helpers', () => { it('normalises the text and refuses what Discord would reject', () => { assert.equal(sanitizeStatusText(' les tickets '), 'les tickets'); assert.equal(sanitizeStatusText('a\nb'), 'a b'); assert.equal(sanitizeStatusText('x'.repeat(STATUS_TEXT_MAX)).length, STATUS_TEXT_MAX); assert.throws(() => sanitizeStatusText(' '), UserFacingError); assert.throws(() => sanitizeStatusText('x'.repeat(STATUS_TEXT_MAX + 1)), UserFacingError); }); it('accepts the known types and explains the others', () => { assert.equal(normalizeStatusType(), 'watching'); assert.equal(normalizeStatusType('PLAYING'), 'playing'); assert.throws(() => normalizeStatusType('danse'), UserFacingError); }); it('requires a link for a stream and validates the ones it gets', () => { assert.throws(() => validateStatusUrl(null, 'streaming'), UserFacingError); assert.throws(() => validateStatusUrl('twitch.tv/fws', 'streaming'), UserFacingError); assert.equal(validateStatusUrl('https://twitch.tv/fws', 'streaming'), 'https://twitch.tv/fws'); assert.equal(validateStatusUrl(null, 'watching'), null); assert.equal(validateStatusUrl('https://exemple.fr', 'playing'), 'https://exemple.fr'); }); it('formats a delay the way the embeds show it', () => { assert.equal(formatSeconds(30), '30 s'); assert.equal(formatSeconds(60), '1 min'); assert.equal(formatSeconds(3600), '1 h'); assert.equal(formatSeconds(9000), '2 h 30 min'); }); }); describe('bot status rotation', () => { before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(() => { getDatabase().exec('DELETE FROM bot_statuses'); getDatabase().exec(`DELETE FROM guild_settings WHERE key = '${ROTATION_KEY}'`); }); const client = () => { const presences = []; return { presences, user: { setPresence: (payload) => presences.push(payload) } }; }; it('walks the enabled statuses one after the other, then starts over', () => { const first = addBotStatus({ text: 'un' }); const second = addBotStatus({ text: 'deux' }); const third = addBotStatus({ text: 'trois', type: 'listening' }); setBotStatusEnabled(third.id, false); const statuses = listEnabledBotStatuses(); assert.equal(selectNextStatus(statuses).id, first.id); assert.equal(selectNextStatus(statuses, first.id).id, second.id); assert.equal(selectNextStatus(statuses, second.id).id, first.id); assert.equal(selectNextStatus([]), null); }); it('shows the statuses in Discord with the right activity type', () => { addBotStatus({ text: 'les tickets', type: 'listening' }); addBotStatus({ text: 'en direct', type: 'streaming', url: 'https://twitch.tv/fws' }); const fake = client(); const first = rotateBotStatus(fake); const second = rotateBotStatus(fake, first.id); assert.deepEqual(fake.presences[0].activities[0], { name: 'les tickets', type: 2 }); assert.equal(fake.presences[1].activities[0].url, 'https://twitch.tv/fws'); assert.equal(second.id !== first.id, true); }); it('falls back to the default line instead of showing nothing', () => { const fake = client(); assert.equal(rotateBotStatus(fake), FALLBACK_STATUS); assert.equal(fake.presences[0].activities[0].name, FALLBACK_STATUS.text); }); it('shows a lone status right away', () => { const status = addBotStatus({ text: 'seul statut' }); const fake = client(); assert.equal(applyStatusIfAlone(fake).id, status.id); addBotStatus({ text: 'un autre' }); assert.equal(applyStatusIfAlone(fake), null, 'with several statuses the rotation decides'); }); it('refuses the same text twice and stops at the maximum', () => { addBotStatus({ text: 'unique' }); assert.throws(() => addBotStatus({ text: 'unique' }), UserFacingError); for (let index = countBotStatuses(); index < STATUS_MAX; index += 1) { addBotStatus({ text: `statut ${index}` }); } assert.equal(countBotStatuses(), STATUS_MAX); assert.throws(() => addBotStatus({ text: 'un de trop' }), UserFacingError); }); it('pauses and resumes a status without losing it', () => { const status = addBotStatus({ text: 'en pause' }); const paused = toggleBotStatus(status.id, false); assert.equal(paused.enabled, 0); assert.equal(listEnabledBotStatuses().length, 0); assert.throws(() => toggleBotStatus(status.id, false), UserFacingError); toggleBotStatus(status.id, true); assert.equal(listEnabledBotStatuses()[0].id, status.id); assert.throws(() => toggleBotStatus(999, true), UserFacingError); }); it('empties the list and forgets every status', () => { addBotStatus({ text: 'un' }); addBotStatus({ text: 'deux' }); assert.equal(clearBotStatusList(), 2); assert.equal(countBotStatuses(), 0); assert.equal(clearBotStatusList(), 0); }); it('keeps the rotation delay between the allowed bounds, database first', () => { assert.equal(clampRotation(30), 30); assert.equal(clampRotation(ROTATION_MIN_SECONDS - 1), 60, 'an unusable value falls back to the default'); assert.equal(clampRotation(ROTATION_MAX_SECONDS + 1), 60); assert.throws(() => clampRotation(1, { strict: true }), UserFacingError); saveRotationSeconds(45, GUILD, 'u1'); assert.equal(getRotationSeconds(GUILD), 45); setSetting(GUILD, ROTATION_KEY, 'pas un nombre', 'u1'); assert.equal(getRotationSeconds(GUILD), 60, 'a corrupted setting never breaks the rotation'); assert.equal(getRotationSeconds('autre-serveur'), 60); }); it('lists the statuses with their state and the current rhythm', () => { const empty = statusListEmbed({ guildId: GUILD, statuses: [] }); assert.match(empty.data.description, /Aucun statut personnalisé/); const status = addBotStatus({ text: 'les commandes avec /help' }); const embed = statusListEmbed({ guildId: GUILD, statuses: listBotStatuses() }); assert.match(embed.data.description, /\*\*1\*\* statut\(s\), dont \*\*1\*\* en rotation/); assert.equal(embed.data.fields[0].name, `#${status.id} · ✅ en rotation`); assert.match(embed.data.fields[0].value, /les commandes avec \/help/); }); });