CI / quality (push) Failing after 58s
A refusal without a reason leaves the member guessing why, which makes the recruitment feel arbitrary. The reject button now opens a modal, the reason is stored with the application, added to the review message and sent verbatim in the DM; when the DM cannot be delivered, the team gets it back to pass along.
283 lines
9.1 KiB
JavaScript
283 lines
9.1 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { Collection } from 'discord.js';
|
|
import { after, before, beforeEach, describe, it } from 'node:test';
|
|
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
|
|
import {
|
|
claimApplication,
|
|
createApplication,
|
|
getApplication,
|
|
getPendingApplicationByMember,
|
|
getPendingApplicationByUsername,
|
|
getRecruitmentPanel,
|
|
rejectApplication,
|
|
releaseApplication,
|
|
} from '../src/database/repositories/gitea-recruitment-repository.js';
|
|
import {
|
|
CATEGORY_NAME,
|
|
CHANNEL_NAME,
|
|
ensureRecruitmentPanel,
|
|
generatePassword,
|
|
openApplicationModal,
|
|
openRejectionModal,
|
|
validateEmail,
|
|
validateGiteaUsername,
|
|
} from '../src/services/gitea/team-recruitment.js';
|
|
|
|
/** Gitea team recruitment (M20): panel installation and stored applications. */
|
|
|
|
const GUILD_ID = 'guild-1';
|
|
|
|
describe('application answers', () => {
|
|
it('generates a long password from an unambiguous alphabet', () => {
|
|
const password = generatePassword(24);
|
|
|
|
assert.equal(password.length, 24);
|
|
assert.match(password, /^[A-Za-z0-9!@#$%*-_]+$/);
|
|
assert.notEqual(generatePassword(24), generatePassword(24));
|
|
});
|
|
|
|
it('accepts the names Gitea allows', () => {
|
|
assert.equal(validateGiteaUsername('damien'), null);
|
|
assert.equal(validateGiteaUsername('fws-bot_2.0'), null);
|
|
});
|
|
|
|
it('refuses a name Gitea would reject', () => {
|
|
assert.match(validateGiteaUsername(''), /Indique/);
|
|
assert.match(validateGiteaUsername('-leading'), /commence/);
|
|
assert.match(validateGiteaUsername('has space'), /commence/);
|
|
assert.match(validateGiteaUsername('a'.repeat(40)), /39 caractères/);
|
|
});
|
|
|
|
it('checks the e-mail before calling Gitea', () => {
|
|
assert.equal(validateEmail('moi@exemple.org'), null);
|
|
assert.match(validateEmail('pas-un-email'), /valide/);
|
|
assert.match(validateEmail(''), /Indique/);
|
|
});
|
|
|
|
/** Discord rejects more than five inputs, and a modal cannot answer another modal. */
|
|
it('asks everything in a single modal of five fields', async () => {
|
|
let shown;
|
|
await openApplicationModal({
|
|
showModal: async (modal) => {
|
|
shown = modal.toJSON();
|
|
},
|
|
});
|
|
|
|
assert.equal(shown.custom_id, 'gitea-team:form');
|
|
assert.equal(shown.components.length, 5);
|
|
assert.deepEqual(
|
|
shown.components.map((row) => row.components[0].custom_id),
|
|
['languages', 'level', 'experience', 'username', 'email'],
|
|
);
|
|
});
|
|
|
|
/** A refusal must carry a written reason, not a silent click. */
|
|
it('asks the team for a reason when it refuses an application', async () => {
|
|
let shown;
|
|
await openRejectionModal(
|
|
{
|
|
showModal: async (modal) => {
|
|
shown = modal.toJSON();
|
|
},
|
|
},
|
|
{ id: 7 },
|
|
);
|
|
|
|
assert.equal(shown.custom_id, 'gitea-team:reject:7');
|
|
assert.equal(shown.components.length, 1);
|
|
assert.equal(shown.components[0].components[0].custom_id, 'reason');
|
|
assert.equal(shown.components[0].components[0].style, 2, 'the reason is a paragraph');
|
|
assert.equal(shown.components[0].components[0].required, true);
|
|
});
|
|
});
|
|
|
|
describe('recruitment repository', () => {
|
|
before(() => initDatabase(':memory:'));
|
|
after(() => closeDatabase());
|
|
beforeEach(() =>
|
|
getDatabase().exec('DELETE FROM gitea_applications; DELETE FROM gitea_recruitment_panels;'),
|
|
);
|
|
|
|
const apply = (overrides = {}) =>
|
|
createApplication({
|
|
guildId: GUILD_ID,
|
|
userId: 'member-1',
|
|
languages: 'JavaScript, Rust',
|
|
level: 'Intermédiaire',
|
|
experience: 'Je veux aider sur la doc.',
|
|
giteaUsername: 'membre-1',
|
|
email: 'membre@exemple.org',
|
|
...overrides,
|
|
});
|
|
|
|
it('stores a pending application and finds it by member and username', () => {
|
|
const id = apply();
|
|
|
|
assert.equal(getApplication(id).status, 'pending');
|
|
assert.equal(getPendingApplicationByMember({ guildId: GUILD_ID, userId: 'member-1' }).id, id);
|
|
assert.equal(
|
|
getPendingApplicationByUsername({ guildId: GUILD_ID, username: 'MEMBRE-1' }).id,
|
|
id,
|
|
'Gitea compares usernames without case',
|
|
);
|
|
});
|
|
|
|
it('refuses a second pending application from the same member', () => {
|
|
apply();
|
|
assert.throws(() => apply({ giteaUsername: 'autre' }), /UNIQUE/);
|
|
});
|
|
|
|
it('claims only once, so two clicks cannot create two accounts', () => {
|
|
const id = apply();
|
|
|
|
assert.equal(claimApplication(id, 'staff'), 1);
|
|
assert.equal(claimApplication(id, 'other-staff'), 0);
|
|
assert.equal(getApplication(id).status, 'accepted');
|
|
assert.equal(getApplication(id).reviewed_by, 'staff');
|
|
});
|
|
|
|
it('puts a claimed application back in the queue when Gitea fails', () => {
|
|
const id = apply();
|
|
claimApplication(id, 'staff');
|
|
|
|
assert.equal(releaseApplication(id), 1);
|
|
assert.equal(getApplication(id).status, 'pending');
|
|
assert.equal(getApplication(id).reviewed_by, null);
|
|
assert.equal(releaseApplication(id), 0);
|
|
});
|
|
|
|
it('rejects only a pending application', () => {
|
|
const id = apply();
|
|
|
|
assert.equal(rejectApplication(id, 'staff'), 1);
|
|
assert.equal(getApplication(id).status, 'rejected');
|
|
assert.equal(rejectApplication(id, 'staff'), 0);
|
|
});
|
|
|
|
it('keeps the reason of the refusal for the member', () => {
|
|
const id = apply();
|
|
|
|
rejectApplication(id, 'staff', 'Il manque une présentation sur le salon.');
|
|
|
|
assert.equal(getApplication(id).rejection_reason, 'Il manque une présentation sur le salon.');
|
|
});
|
|
|
|
it('keeps one panel row per guild and updates it', () => {
|
|
assert.equal(getRecruitmentPanel(GUILD_ID), null);
|
|
});
|
|
});
|
|
|
|
/** A guild reduced to what installing the panel touches. */
|
|
function fakeGuild() {
|
|
const state = {
|
|
messages: new Map(),
|
|
sent: 0,
|
|
edits: 0,
|
|
nextId: 0,
|
|
};
|
|
const cache = new Collection();
|
|
|
|
const channel = ({ id, name, type, parent = null }) => {
|
|
const parentId = parent?.id ?? null;
|
|
const created = {
|
|
id,
|
|
name,
|
|
type,
|
|
parentId,
|
|
children: { cache: new Collection() },
|
|
isSendable: () => true,
|
|
messages: {
|
|
fetch: async (messageId) => state.messages.get(messageId) ?? null,
|
|
},
|
|
send: async (payload) => {
|
|
state.sent += 1;
|
|
const messageId = `message-${(state.nextId += 1)}`;
|
|
const message = {
|
|
id: messageId,
|
|
payload,
|
|
pin: async () => {},
|
|
edit: async (next) => {
|
|
state.edits += 1;
|
|
message.payload = next;
|
|
return message;
|
|
},
|
|
};
|
|
state.messages.set(messageId, message);
|
|
return message;
|
|
},
|
|
};
|
|
if (parent) parent.children.cache.set(id, created);
|
|
cache.set(id, created);
|
|
return created;
|
|
};
|
|
|
|
const guild = {
|
|
id: GUILD_ID,
|
|
channels: {
|
|
cache,
|
|
fetch: async (channelId) => cache.get(channelId) ?? null,
|
|
create: async (options) => {
|
|
if (options.type === 4) {
|
|
return channel({ id: `category-${(state.nextId += 1)}`, name: options.name, type: options.type });
|
|
}
|
|
return channel({
|
|
id: `channel-${(state.nextId += 1)}`,
|
|
name: options.name,
|
|
type: options.type,
|
|
parent: cache.get(options.parent),
|
|
});
|
|
},
|
|
},
|
|
};
|
|
|
|
return { guild, state, client: { guilds: { fetch: async () => guild } } };
|
|
}
|
|
|
|
describe('recruitment panel', () => {
|
|
before(() => initDatabase(':memory:'));
|
|
after(() => closeDatabase());
|
|
beforeEach(() =>
|
|
getDatabase().exec('DELETE FROM gitea_applications; DELETE FROM gitea_recruitment_panels;'),
|
|
);
|
|
|
|
it('creates the category, the salon and the panel on the first start', async () => {
|
|
const { guild, state, client } = fakeGuild();
|
|
|
|
const panel = await ensureRecruitmentPanel(client, { guildId: GUILD_ID, text: 'Viens aider !' });
|
|
|
|
assert.equal(panel.edited, false);
|
|
assert.equal(state.sent, 1);
|
|
assert.equal(guild.channels.cache.get(panel.categoryId).name, CATEGORY_NAME);
|
|
assert.equal(guild.channels.cache.get(panel.channelId).name, CHANNEL_NAME);
|
|
assert.equal(guild.channels.cache.get(panel.channelId).parentId, panel.categoryId);
|
|
assert.equal(getRecruitmentPanel(GUILD_ID).message_id, panel.messageId);
|
|
});
|
|
|
|
it('edits the existing panel instead of posting a second one', async () => {
|
|
const { state, client } = fakeGuild();
|
|
|
|
const first = await ensureRecruitmentPanel(client, { guildId: GUILD_ID, text: 'Version 1' });
|
|
const second = await ensureRecruitmentPanel(client, { guildId: GUILD_ID, text: 'Version 2' });
|
|
|
|
assert.equal(state.sent, 1, 'a second panel message was sent');
|
|
assert.equal(state.edits, 1);
|
|
assert.equal(second.messageId, first.messageId);
|
|
});
|
|
|
|
it('reuses a category and a salon that already exist by name', async () => {
|
|
const { guild, state, client } = fakeGuild();
|
|
const category = await guild.channels.create({ name: CATEGORY_NAME, type: 4 });
|
|
const salon = await guild.channels.create({
|
|
name: CHANNEL_NAME,
|
|
type: 0,
|
|
parent: category.id,
|
|
});
|
|
|
|
const panel = await ensureRecruitmentPanel(client, { guildId: GUILD_ID, text: 'Salut' });
|
|
|
|
assert.equal(panel.categoryId, category.id);
|
|
assert.equal(panel.channelId, salon.id);
|
|
assert.equal(state.sent, 1);
|
|
});
|
|
});
|