126 lines
8.3 KiB
TypeScript
126 lines
8.3 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { Store } from '../src/core/db/db.ts';
|
|
import { AudioNamer, audioFileName, safePart } from '../src/core/audio-names.ts';
|
|
import { Bundles } from '../src/core/bundles.ts';
|
|
import { readZip, writeZip } from '../src/core/util/zip.ts';
|
|
|
|
const tmp = () => fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-names-'));
|
|
const at = (h: number, m: number, s: number) => new Date(2026, 9, 16, h, m, s).getTime();
|
|
|
|
test('녹음 파일 이름: 날짜 · 시각 · 말한 사람 · 게임 · 모드 · 맵 · 라운드 · 발화 ID', () => {
|
|
assert.equal(
|
|
audioFileName({ id: 'u1', startedAt: at(4, 3, 16), speaker: '사금향', game: 'valorant', mode: '경쟁전', map: '어센트', round: 7 }),
|
|
'2026-10-16_04-03-16_사금향_발로란트_경쟁전_어센트_R7_u1.ogg',
|
|
);
|
|
assert.equal(audioFileName({ id: 'u2', startedAt: at(21, 10, 5), speaker: '하늘', game: 'lol', mode: '솔로 랭크', gameTimeMs: 750_400 }), '2026-10-16_21-10-05_하늘_롤_솔로랭크_12분30초_u2.ogg');
|
|
assert.equal(audioFileName({ id: 'u3', startedAt: at(3, 58, 56), speaker: '사금향', game: null }), '2026-10-16_03-58-56_사금향_경기밖_u3.ogg');
|
|
// Windows 에서 못 쓰는 글자는 뺀다
|
|
assert.equal(safePart('a/b:c*d?"e<f>g|h i_j.'), 'abcdefghij');
|
|
assert.equal(audioFileName({ id: 'u4', startedAt: at(1, 2, 3), speaker: '', game: null }), '2026-10-16_01-02-03_알수없음_경기밖_u4.ogg');
|
|
});
|
|
|
|
test('녹음 파일 이름: 실제 파일을 바꾸고, 경기 뒤 라운드 · 표시 이름이 바뀌면 다시 맞추고, zip 에도 그 이름으로 넣는다', () => {
|
|
const dir = tmp();
|
|
const audio = path.join(dir, 'audio');
|
|
const store = new Store(path.join(dir, 'db.sqlite'));
|
|
store.upsertUser({ id: 'u1', name: '하늘', avatar: null });
|
|
const games: any = { summary: (m: any) => ({ mode: m.game === 'lol' ? '솔로 랭크' : '경쟁전', map: { name: m.game === 'lol' ? '소환사의 협곡' : '어센트' } }), liveRound: () => 3 };
|
|
const busy = new Set<string>();
|
|
const namer = new AudioNamer(store, games, (id) => busy.has(id));
|
|
const S = at(4, 0, 0);
|
|
store.upsertMatch({ id: 'val-1', game: 'valorant', started_at: S, ended_at: S + 40 * 60000, map: 'Ascent', queue: 'competitive', status: 'live' });
|
|
const mk = (id: string, ts: number, match: string | null) => {
|
|
const f = path.join(audio, 'day', `${id}.ogg`);
|
|
fs.mkdirSync(path.dirname(f), { recursive: true });
|
|
fs.writeFileSync(f, Buffer.concat([Buffer.from('OggS'), Buffer.alloc(50)]));
|
|
store.insertUtterance({ id, session_id: null, user_id: 'u1', started_at: ts, ended_at: ts + 1000, audio_path: f, match_id: match });
|
|
store.updateUtteranceResult(id, { text: `말 ${id}`, raw_text: null, words: [], noise: false, noise_reason: null, confidence: 1, lufs: -20, gain_db: 0, model: 'm', status: 'precise' });
|
|
};
|
|
mk('a1', S + 5 * 60000, 'val-1');
|
|
mk('a2', S - 60000, null);
|
|
// 경기 중: 라운드 표가 없어서 지금 라운드(3)를 쓴다
|
|
assert.equal(namer.refresh('a1'), true);
|
|
assert.equal(path.basename(store.getUtterance('a1')!.audio_path!), '2026-10-16_04-05-00_하늘_발로란트_경쟁전_어센트_R3_a1.ogg');
|
|
assert.ok(fs.existsSync(store.getUtterance('a1')!.audio_path!));
|
|
assert.ok(!fs.existsSync(path.join(audio, 'day', 'a1.ogg')));
|
|
// 경기 뒤 라운드 표가 생기면 정확한 라운드로 다시 맞춘다 (음성 인식 중인 발화는 건드리지 않음)
|
|
store.replaceRounds('val-1', [
|
|
{ match_id: 'val-1', num: 1, started_at: S, combat_at: S + 30000, ended_at: S + 2 * 60000, win: 1, result: null, ally_score: 1, enemy_score: 0, economy: '{}' },
|
|
{ match_id: 'val-1', num: 2, started_at: S + 2 * 60000, combat_at: S + 150000, ended_at: S + 6 * 60000, win: 1, result: null, ally_score: 2, enemy_score: 0, economy: '{}' },
|
|
]);
|
|
busy.add('a1');
|
|
assert.equal(namer.refreshMatch('val-1'), 0);
|
|
busy.delete('a1');
|
|
assert.equal(namer.refreshMatch('val-1'), 1);
|
|
assert.match(store.getUtterance('a1')!.audio_path!, /_R2_a1\.ogg$/);
|
|
// 표시 이름이 바뀌면 전체를 다시 맞춘다
|
|
store.setDisplayNames({ u1: '사금향' });
|
|
namer.refreshAll();
|
|
assert.match(path.basename(store.getUtterance('a2')!.audio_path!), /^2026-10-16_03-59-00_사금향_경기밖_a2\.ogg$/);
|
|
assert.match(path.basename(store.getUtterance('a1')!.audio_path!), /_사금향_발로란트_/);
|
|
// zip 안에도 같은 이름, 받아오면 받은 PC 에서도 같은 규칙으로
|
|
const b = new Bundles(store, { summary: (m: any) => ({ ...games.summary(m), score: null, win: null }) } as any, () => audio, 'test', (ids) => ids.forEach((id) => namer.refresh(id)));
|
|
const id = b.list().find((x) => x.kind === 'match')!.id;
|
|
store.db.prepare(`UPDATE matches SET status='complete' WHERE id='val-1'`).run();
|
|
const { zip } = b.export([id]);
|
|
assert.ok([...readZip(zip).keys()].includes('audio/2026-10-16_04-05-00_사금향_발로란트_경쟁전_어센트_R2_a1.ogg'));
|
|
b.delete([id]);
|
|
assert.equal(b.import(zip).added.utterances, 1);
|
|
assert.equal(path.basename(store.getUtterance('a1')!.audio_path!), '2026-10-16_04-05-00_사금향_발로란트_경쟁전_어센트_R2_a1.ogg');
|
|
assert.ok(fs.existsSync(store.getUtterance('a1')!.audio_path!));
|
|
store.close();
|
|
});
|
|
|
|
test('녹음 파일 이름: 표시 이름에 ".." 이 있어도 zip 내보내기 → 지우기 → 받아오기로 되살린다', () => {
|
|
const dir = tmp();
|
|
const audio = path.join(dir, 'audio');
|
|
const store = new Store(path.join(dir, 'db.sqlite'));
|
|
store.upsertUser({ id: 'u1', name: '하늘..별', avatar: null });
|
|
const namer = new AudioNamer(store, { summary: () => ({ mode: '', map: { name: '' } }), liveRound: () => null } as any, () => false);
|
|
const f = path.join(audio, 'day', 'x1.ogg');
|
|
fs.mkdirSync(path.dirname(f), { recursive: true });
|
|
fs.writeFileSync(f, Buffer.concat([Buffer.from('OggS'), Buffer.alloc(50)]));
|
|
const ts = at(5, 0, 0);
|
|
store.insertUtterance({ id: 'x1', session_id: null, user_id: 'u1', started_at: ts, ended_at: ts + 1000, audio_path: f, match_id: null });
|
|
store.updateUtteranceResult('x1', { text: '안녕', raw_text: null, words: [], noise: false, noise_reason: null, confidence: 1, lufs: -20, gain_db: 0, model: 'm', status: 'precise' });
|
|
assert.equal(namer.refresh('x1'), true);
|
|
assert.equal(path.basename(store.getUtterance('x1')!.audio_path!), '2026-10-16_05-00-00_하늘..별_경기밖_x1.ogg');
|
|
const b = new Bundles(store, { summary: () => ({ mode: '', map: { name: '' }, score: null, win: null }) } as any, () => audio, 'test', (ids) => ids.forEach((id) => namer.refresh(id)));
|
|
const id = b.list()[0].id;
|
|
const { zip } = b.export([id]);
|
|
assert.ok(readZip(zip).has('audio/2026-10-16_05-00-00_하늘..별_경기밖_x1.ogg'));
|
|
b.delete([id]);
|
|
assert.equal(store.getUtterance('x1'), undefined);
|
|
const r = b.import(zip);
|
|
assert.equal(r.added.utterances, 1);
|
|
assert.equal(r.added.audio, 1);
|
|
const back = store.getUtterance('x1')!;
|
|
assert.equal(back.text, '안녕');
|
|
assert.ok(fs.existsSync(back.audio_path!));
|
|
assert.ok(back.audio_path!.startsWith(audio), '받은 파일은 이 PC 의 audio 폴더 안에만 쓴다');
|
|
store.close();
|
|
});
|
|
|
|
test('받아오기: 폴더를 넘나드는 zip 안 이름은 막는다', () => {
|
|
const dir = tmp();
|
|
const store = new Store(path.join(dir, 'db.sqlite'));
|
|
store.upsertUser({ id: 'u1', name: '하늘', avatar: null });
|
|
const b = new Bundles(store, { summary: () => ({ mode: '', map: { name: '' }, score: null, win: null }) } as any, () => path.join(dir, 'audio'), 'test');
|
|
for (const bad of ['audio/../x1.ogg', 'audio/..\\x1.ogg', 'audio/sub/x1.ogg', '../x1.ogg']) {
|
|
const data = { users: [{ id: 'u1', name: '하늘' }], utterances: [{ id: 'x1', user_id: 'u1', started_at: 1, ended_at: 2, audio: bad }] };
|
|
const manifest = { format: 'gvr-bundle', version: 1, bundles: [{ kind: 'talk', id: 'talk-x1' }], counts: { utterances: 1 } };
|
|
const zip = writeZip([
|
|
{ name: 'manifest.json', data: Buffer.from(JSON.stringify(manifest)) },
|
|
{ name: 'data.json', data: Buffer.from(JSON.stringify(data)) },
|
|
{ name: bad, data: Buffer.from('OggS....') },
|
|
]);
|
|
assert.throws(() => b.import(zip), /음성 파일 이름이 잘못되었습니다/, bad);
|
|
}
|
|
assert.equal(store.getUtterance('x1'), undefined);
|
|
store.close();
|
|
});
|