55 lines
2.7 KiB
TypeScript
55 lines
2.7 KiB
TypeScript
import { test } from 'node:test';
|
|
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 { Store } from '../src/core/db/db.ts';
|
|
import { Timeline } from '../src/core/timeline.ts';
|
|
import { Bus } from '../src/core/util/events.ts';
|
|
import { SttPipeline } from '../src/core/stt/pipeline.ts';
|
|
import { WorkerError } from '../src/core/stt/worker.ts';
|
|
|
|
test('앱 종료: 변환 중이던 작업이 워커 종료로 실패해도 닫힌 DB 를 건드리지 않고, 남은 작업을 꺼내지 않는다', async () => {
|
|
const dir = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-stop-'));
|
|
const store = new Store(path.join(dir, 'db.sqlite'));
|
|
store.upsertUser({ id: 'u1', name: '하늘', avatar: null });
|
|
for (const id of ['s1', 's2']) {
|
|
const f = path.join(dir, `${id}.ogg`);
|
|
fs.writeFileSync(f, Buffer.concat([Buffer.from('OggS'), Buffer.alloc(50)]));
|
|
store.insertUtterance({ id, session_id: null, user_id: 'u1', started_at: Date.now(), ended_at: Date.now() + 1000, audio_path: f, match_id: null });
|
|
}
|
|
const transcribing: string[] = [];
|
|
let fail: ((e: Error) => void) | null = null;
|
|
const worker: any = Object.assign(new EventEmitter(), {
|
|
ready: true,
|
|
request: async (op: string, req: any) => {
|
|
if (op === 'models') return { models: [{ name: 'm', installed: true }] };
|
|
if (op !== 'transcribe') return {};
|
|
transcribing.push(path.basename(req.path, '.ogg'));
|
|
return new Promise((_, rej) => (fail = rej));
|
|
},
|
|
});
|
|
const settings: any = () => ({ stt: { realtimeModel: 'm', preciseModel: 'm' }, audio: { targetLufs: -18, noiseSensitivity: 0.5, denoise: false } });
|
|
const dict: any = { hotwords: () => '', corrections: () => [], candidates: () => [] };
|
|
const stt = new SttPipeline(worker, store, dict, new Timeline(store), new Bus(), settings);
|
|
await stt.load('realtime', 'm');
|
|
const done: string[] = [];
|
|
stt.onDone = (id) => {
|
|
done.push(id);
|
|
store.getUtterance(id); // 실제 앱의 녹음 파일 이름 맞추기도 DB 를 읽는다
|
|
};
|
|
stt.enqueue('s1', 'realtime');
|
|
stt.enqueue('s2', 'realtime');
|
|
assert.deepEqual(transcribing, ['s1']);
|
|
|
|
// core.stop 순서: stt.stop → 워커 종료 → DB 닫기 → (워커 프로세스 종료 이벤트로) 변환 요청 실패
|
|
stt.stop();
|
|
store.close();
|
|
fail!(new WorkerError('worker_exit', '워커 종료'));
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
assert.deepEqual(done, [], '종료 중에는 변환 뒤 처리를 하지 않는다');
|
|
assert.deepEqual(transcribing, ['s1']);
|
|
assert.equal(stt.status.queue, 1, '종료 중에는 다음 작업을 꺼내지 않는다 (다음 시작 때 다시 처리)');
|
|
});
|