Files
FWS-DiscordBot/tests/notifications.test.js

89 lines
3.2 KiB
JavaScript

import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { buildGiteaNotification } from '../src/services/notifications/gitea-notifications.js';
import { releaseEmbed, truncate } from '../src/services/gitea/gitea-embeds.js';
const issue = {
number: 12,
title: 'Le lecteur ne démarre pas',
body: 'Depuis la mise à jour.',
state: 'open',
comments: 3,
user: { login: 'alice' },
html_url: 'https://gitea.example/FWS/FWS-Arch/issues/12',
labels: [{ name: 'bug' }],
};
describe('buildGiteaNotification', () => {
it('maps an issue event to the issues channel', () => {
const notification = buildGiteaNotification('issues', { issue });
assert.equal(notification.type, 'issues');
assert.equal(notification.embed.data.title, '#12 Le lecteur ne démarre pas');
assert.equal(notification.embed.data.url, issue.html_url);
});
it('maps a pull request to its own type', () => {
const notification = buildGiteaNotification('pull_request', {
pull_request: {
number: 4,
title: 'Corrige le démarrage',
state: 'open',
user: { login: 'bob' },
head: { ref: 'fix/boot' },
base: { ref: 'main' },
},
});
assert.equal(notification.type, 'pull_request');
assert.match(notification.embed.data.fields[2].value, /fix\/boot → main/);
});
it('reports a release with its tag', () => {
const notification = buildGiteaNotification('release', {
release: { tag_name: 'v1.2.0', name: 'Version 1.2.0', body: 'Nouveautés' },
});
assert.equal(notification.type, 'release');
assert.equal(notification.embed.data.title, 'Version 1.2.0');
});
it('ignores a push without commits', () => {
assert.equal(buildGiteaNotification('push', { commits: [], ref: 'refs/heads/main' }), null);
});
it('summarises the commits of a push', () => {
const notification = buildGiteaNotification('push', {
ref: 'refs/heads/main',
sender: { login: 'carol' },
commits: [{ id: 'abcdef1234567', message: 'fix: boot\n\nDétails' }],
});
assert.equal(notification.type, 'push');
assert.equal(notification.embed.data.title, 'Push sur main');
assert.match(notification.embed.data.description, /`abcdef1` fix: boot/);
});
it('ignores events without a notification type', () => {
assert.equal(buildGiteaNotification('repository', { repository: {} }), null);
});
});
describe('releaseEmbed', () => {
it('keeps the notes by default and can drop them', () => {
const release = {
tag_name: 'v2.0.0',
name: 'v2.0.0',
body: 'Changements',
published_at: '2026-01-01T10:00:00Z',
};
assert.equal(releaseEmbed(release).data.description, 'Changements');
assert.equal(releaseEmbed(release, { withNotes: false }).data.description, undefined);
});
});
describe('truncate', () => {
it('keeps short text and cuts long text', () => {
assert.equal(truncate('court'), 'court');
assert.equal(truncate('', 10), '');
assert.equal(truncate('ab'.repeat(20), 10).length, 10);
assert.match(truncate('ab'.repeat(20), 10), /…$/);
});
});