- /delete discards the in-progress utterance before deleting files - keep the current channel even when the bot itself fills it (joinable=false) - bind OAuth state to the browser via a short-lived cookie - reject consent submitted from a page showing an older consent version - regression tests for all four Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { EventEmitter } from 'node:events';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { PassThrough } from 'node:stream';
|
|
import { test } from 'node:test';
|
|
import { Store } from '../src/db.ts';
|
|
import { Recorder } from '../src/recorder.ts';
|
|
|
|
/** @discordjs/voice 수신기 흉내: speaking 'start' 이벤트 + 사람별 스트림 */
|
|
function fakeConnection() {
|
|
const subscriptions = new Map<string, PassThrough>();
|
|
const receiver = {
|
|
speaking: new EventEmitter(),
|
|
subscriptions,
|
|
subscribe(userId: string) {
|
|
const s = new PassThrough({ objectMode: true });
|
|
s.on('close', () => subscriptions.delete(userId));
|
|
subscriptions.set(userId, s);
|
|
return s;
|
|
},
|
|
};
|
|
return { receiver } as any;
|
|
}
|
|
|
|
const OPUS_20MS = Buffer.from([0x78, 0x01, 0x02, 0x03]); // TOC config 15 = 20ms 프레임 1개 (960 샘플)
|
|
|
|
test('동의한 사람만 구독해서 저장하고, 철회하면 쓰던 파일을 버린다', async () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.homedir(), '.tmp-rec-'));
|
|
const store = new Store(':memory:');
|
|
store.consent({ id: 'yes', username: 'y' });
|
|
const conn = fakeConnection();
|
|
const rec = new Recorder(store, tmp, conn, 'g', 'c', (uid) => store.isConsented(uid));
|
|
|
|
conn.receiver.speaking.emit('start', 'no'); // 동의 안 한 사람
|
|
assert.equal(conn.receiver.subscriptions.has('no'), false);
|
|
|
|
conn.receiver.speaking.emit('start', 'yes');
|
|
const s = conn.receiver.subscriptions.get('yes')!;
|
|
for (let i = 0; i < 50; i++) s.write(OPUS_20MS); // 1초
|
|
await new Promise((r) => setImmediate(r));
|
|
s.end();
|
|
await new Promise((r) => setImmediate(r));
|
|
const rows = store.recordings('yes');
|
|
assert.equal(rows.length, 1);
|
|
assert.equal(rows[0].duration_ms, 1000);
|
|
assert.equal(fs.readFileSync(rows[0].path).subarray(0, 4).toString(), 'OggS');
|
|
|
|
// 말하는 도중 철회 → 저장 안 함
|
|
conn.receiver.speaking.emit('start', 'yes');
|
|
const s2 = conn.receiver.subscriptions.get('yes')!;
|
|
for (let i = 0; i < 50; i++) s2.write(OPUS_20MS);
|
|
await new Promise((r) => setImmediate(r));
|
|
store.revoke('yes');
|
|
s2.write(OPUS_20MS);
|
|
await new Promise((r) => setImmediate(r));
|
|
assert.equal(store.recordings('yes').length, 1);
|
|
assert.equal(fs.readdirSync(path.dirname(rows[0].path)).length, 1);
|
|
|
|
rec.close();
|
|
store.close();
|
|
fs.rmSync(tmp, { recursive: true, force: true });
|
|
});
|
|
|
|
test('파일 삭제 직전 진행 중인 녹음을 버리면 삭제 뒤에 남지 않는다', async () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.homedir(), '.tmp-rec-'));
|
|
const store = new Store(':memory:');
|
|
store.consent({ id: 'yes', username: 'y' });
|
|
const conn = fakeConnection();
|
|
const rec = new Recorder(store, tmp, conn, 'g', 'c', (uid) => store.isConsented(uid));
|
|
conn.receiver.speaking.emit('start', 'yes');
|
|
const s = conn.receiver.subscriptions.get('yes')!;
|
|
for (let i = 0; i < 30; i++) s.write(OPUS_20MS); // 600ms, 아직 진행 중
|
|
await new Promise((r) => setImmediate(r));
|
|
rec.stopUser('yes'); // 사이트 /delete → discardActive
|
|
store.deleteRecordings('yes');
|
|
s.end();
|
|
await new Promise((r) => setImmediate(r));
|
|
assert.equal(store.recordings('yes').length, 0);
|
|
const left = fs.readdirSync(tmp, { recursive: true }).filter((f) => String(f).endsWith('.ogg'));
|
|
assert.deepEqual(left, []);
|
|
// 동의는 그대로라 다시 말하면 새로 녹음한다
|
|
conn.receiver.speaking.emit('start', 'yes');
|
|
assert.equal(conn.receiver.subscriptions.has('yes'), true);
|
|
rec.close();
|
|
store.close();
|
|
fs.rmSync(tmp, { recursive: true, force: true });
|
|
});
|