import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { formatDuration } from '../src/services/moderation/moderation-service.js'; import { buildPollEmbed, parseChoices, toSqliteDate } from '../src/services/polls/poll-service.js'; describe('parseChoices', () => { it('splits, trims and keeps the order', () => { assert.deepEqual(parseChoices('Oui ; Non; Peut-être '), ['Oui', 'Non', 'Peut-être']); }); it('rejects a single choice', () => { assert.throws(() => parseChoices('Oui'), /entre 2 et 10 choix/); }); it('rejects duplicated choices regardless of case', () => { assert.throws(() => parseChoices('Oui;oui'), /différents/); }); it('rejects more choices than allowed', () => { assert.throws(() => parseChoices(Array.from({ length: 11 }, (_v, i) => i).join(';')), /entre 2 et 10/); }); }); describe('toSqliteDate', () => { it('formats a UTC datetime the way SQLite stores it', () => { assert.equal(toSqliteDate(new Date('2026-03-04T05:06:07.890Z')), '2026-03-04 05:06:07'); }); }); describe('buildPollEmbed', () => { const poll = { id: 3, creator_id: 'u1', question: 'Quel thème ?', choices: JSON.stringify(['Clair', 'Sombre']), status: 'open', ends_at: null, }; it('reports the totals and the percentage of each choice', () => { const embed = buildPollEmbed(poll, [ { choice_index: 0, count: 3 }, { choice_index: 1, count: 1 }, ]); assert.equal(embed.data.title, 'Sondage #3 — Quel thème ?'); assert.match(embed.data.description, /Clair/); assert.match(embed.data.description, /3 \(75 %\)/); assert.equal(embed.data.fields[0].value, '4'); }); it('handles a poll without any vote', () => { const embed = buildPollEmbed(poll, []); assert.match(embed.data.description, /0 \(0 %\)/); assert.equal(embed.data.fields[0].value, '0'); }); it('announces the winning choice once closed', () => { const closed = { ...poll, status: 'closed' }; const embed = buildPollEmbed(closed, [ { choice_index: 0, count: 1 }, { choice_index: 1, count: 5 }, ]); assert.equal(embed.data.fields.at(-1).value, '**Sombre** (5 vote(s))'); }); it('says so when a closed poll has no vote', () => { const embed = buildPollEmbed({ ...poll, status: 'closed' }, []); assert.equal(embed.data.fields.at(-1).value, 'Aucun vote'); }); }); describe('formatDuration', () => { it('formats minutes, hours and days', () => { assert.equal(formatDuration(30 * 60_000), '30 min'); assert.equal(formatDuration(60 * 60_000), '1 h'); assert.equal(formatDuration(150 * 60_000), '2 h 30 min'); assert.equal(formatDuration(24 * 60 * 60_000), '1 j'); assert.equal(formatDuration(30 * 60 * 60_000), '1 j 6 h'); }); });