feat(voice): add temporary voice channel manager
Joining the trigger channel must feel instant and never leave orphan channels behind, so the ownership is stored in SQLite, empty channels are deleted after a short grace period and a startup sweep cleans up whatever a restart left behind.
This commit is contained in:
12 files changed
+1277
-1
No files matched your search
@@ -0,0 +1,139 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { after, before, beforeEach, describe, it } from 'node:test';
|
||||
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
|
||||
import {
|
||||
addVoiceChannel,
|
||||
countVoiceChannels,
|
||||
getVoiceChannel,
|
||||
listOwnedVoiceChannels,
|
||||
listVoiceChannels,
|
||||
removeVoiceChannel,
|
||||
setVoiceLimit,
|
||||
setVoiceLocked,
|
||||
setVoiceName,
|
||||
setVoiceOwner,
|
||||
} from '../src/database/repositories/voice-repository.js';
|
||||
import { PermissionDeniedError, UserFacingError } from '../src/core/errors.js';
|
||||
import {
|
||||
assertVoiceOwner,
|
||||
buildChannelName,
|
||||
clampLimit,
|
||||
isStaff,
|
||||
sanitizeChannelName,
|
||||
} from '../src/services/voice/voice-service.js';
|
||||
|
||||
const member = (roleIds = [], { id = 'u1', guildOwnerId = 'someone-else' } = {}) => ({
|
||||
id,
|
||||
guild: { ownerId: guildOwnerId },
|
||||
roles: { cache: new Set(roleIds) },
|
||||
});
|
||||
|
||||
describe('voice repository', () => {
|
||||
before(() => initDatabase(':memory:'));
|
||||
after(() => closeDatabase());
|
||||
beforeEach(() => getDatabase().exec('DELETE FROM voice_channels'));
|
||||
|
||||
const add = (channelId, ownerId = 'u1') =>
|
||||
addVoiceChannel({
|
||||
channelId,
|
||||
guildId: 'g',
|
||||
ownerId,
|
||||
triggerId: 'trigger',
|
||||
name: 'Salon de u1',
|
||||
userLimit: 0,
|
||||
});
|
||||
|
||||
it('stores a temporary channel and reads it back', () => {
|
||||
add('c1');
|
||||
const row = getVoiceChannel('c1');
|
||||
assert.equal(row.owner_id, 'u1');
|
||||
assert.equal(row.trigger_id, 'trigger');
|
||||
assert.equal(row.locked, 0);
|
||||
assert.equal(row.user_limit, 0);
|
||||
assert.ok(row.created_at);
|
||||
});
|
||||
|
||||
it('keeps several channels per guild and per owner, without any limit', () => {
|
||||
add('c1');
|
||||
add('c2');
|
||||
add('c3', 'u2');
|
||||
assert.equal(countVoiceChannels('g'), 3);
|
||||
assert.equal(listVoiceChannels('g').length, 3);
|
||||
assert.deepEqual(
|
||||
listOwnedVoiceChannels('g', 'u1').map((row) => row.channel_id),
|
||||
['c1', 'c2'],
|
||||
);
|
||||
});
|
||||
|
||||
it('updates the owner, the name, the lock and the limit', () => {
|
||||
add('c1');
|
||||
setVoiceOwner('c1', 'u2');
|
||||
setVoiceName('c1', 'Nouveau nom');
|
||||
setVoiceLocked('c1', true);
|
||||
setVoiceLimit('c1', 5);
|
||||
|
||||
const row = getVoiceChannel('c1');
|
||||
assert.equal(row.owner_id, 'u2');
|
||||
assert.equal(row.name, 'Nouveau nom');
|
||||
assert.equal(row.locked, 1);
|
||||
assert.equal(row.user_limit, 5);
|
||||
});
|
||||
|
||||
it('forgets a channel once deleted', () => {
|
||||
add('c1');
|
||||
assert.equal(removeVoiceChannel('c1'), 1);
|
||||
assert.equal(getVoiceChannel('c1'), null);
|
||||
assert.equal(removeVoiceChannel('c1'), 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('voice helpers', () => {
|
||||
it('normalises a channel name', () => {
|
||||
assert.equal(sanitizeChannelName(' Salon de Léa '), 'Salon de Léa');
|
||||
assert.equal(sanitizeChannelName('a\nb'), 'a b');
|
||||
assert.equal(sanitizeChannelName('x'.repeat(150)).length, 100);
|
||||
assert.throws(() => sanitizeChannelName(' '), UserFacingError);
|
||||
});
|
||||
|
||||
it('builds the channel name from the template', () => {
|
||||
const member = { displayName: 'Léa', user: { username: 'lea' } };
|
||||
assert.equal(buildChannelName('🔊 Salon de {user}', member), '🔊 Salon de Léa');
|
||||
assert.equal(buildChannelName('', member), '🔊 Salon de Léa');
|
||||
assert.equal(buildChannelName('{user} {user}', member), 'Léa Léa');
|
||||
assert.equal(buildChannelName(' ', member), '🔊 Salon vocal');
|
||||
});
|
||||
|
||||
it('accepts 0..99 as a user limit and rejects anything else', () => {
|
||||
assert.equal(clampLimit(0), 0);
|
||||
assert.equal(clampLimit(99), 99);
|
||||
assert.equal(clampLimit('12'), 12);
|
||||
assert.equal(clampLimit(150), 0);
|
||||
assert.equal(clampLimit('abc'), 0);
|
||||
assert.throws(() => clampLimit(150, { strict: true }), UserFacingError);
|
||||
assert.throws(() => clampLimit(-1, { strict: true }), UserFacingError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('voice ownership', () => {
|
||||
const roles = { owner: 'r-owner', dev: 'r-dev', staff: 'r-staff' };
|
||||
const row = { owner_id: 'u1' };
|
||||
const context = (id, roleIds = [], overrides = {}) => ({
|
||||
user: { id },
|
||||
member: member(roleIds, { id, ...overrides }),
|
||||
});
|
||||
|
||||
it('lets the owner manage their channel', () => {
|
||||
assert.doesNotThrow(() => assertVoiceOwner(context('u1'), row));
|
||||
});
|
||||
|
||||
it('refuses a plain member', () => {
|
||||
assert.throws(() => assertVoiceOwner(context('u2'), row), PermissionDeniedError);
|
||||
});
|
||||
|
||||
it('recognises staff from the configured roles', () => {
|
||||
assert.equal(isStaff(member(['r-staff']), roles), true);
|
||||
assert.equal(isStaff(member(['r-dev']), roles), true);
|
||||
assert.equal(isStaff(member(['r-member']), roles), false);
|
||||
assert.equal(isStaff(member([], { id: 'u9', guildOwnerId: 'u9' }), roles), true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user