Add consent site + consent-only follow recorder bot
Discord OAuth login -> explicit consent (3 checkboxes, versioned text) -> bot follows consenting users into voice channels and records only their per-user streams. Revoke stops recording immediately and optionally deletes files; consent log kept; export to training wav only for active consents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
64
consent-recorder/test/recorder.test.ts
Normal file
64
consent-recorder/test/recorder.test.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
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 });
|
||||
});
|
||||
Reference in New Issue
Block a user