test: 覆盖实时语音中间结果回传与 EBML 头定位
- DashScope 实时会话:断言 sentence_end=false 的中间结果逐条经 onPartial 回传、task-finished 定稿 - findEbmlHeader:前导字节后的魔数定位、偏移 0、纯编码数据不误判
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
import {
|
||||
createStreamingWebmDecoder,
|
||||
downsamplePcm16,
|
||||
findEbmlHeader,
|
||||
sniffAudioFormat,
|
||||
webmToPcm,
|
||||
} from "../agent/infra/media/audio.js";
|
||||
@@ -51,6 +52,15 @@ test("按文件头识别音频容器,开发者工具 WebM 不会被当成 mp3"
|
||||
assert.equal(sniffAudioFormat(Buffer.alloc(0)), "unknown");
|
||||
});
|
||||
|
||||
test("首片带前导字节时能在头部窗口内定位 EBML 头", () => {
|
||||
const magic = Buffer.from([0x1a, 0x45, 0xdf, 0xa3]);
|
||||
const padded = Buffer.concat([Buffer.from([0, 0, 0]), magic, Buffer.alloc(8)]);
|
||||
assert.equal(findEbmlHeader(padded), 3);
|
||||
assert.equal(findEbmlHeader(Buffer.concat([magic, Buffer.alloc(8)]), 4096), 0);
|
||||
// 纯编码数据里不该出现魔数,扫描结果必须是 -1
|
||||
assert.equal(findEbmlHeader(Buffer.from([0xff, 0xf3, 0x84, 0xc0, 0x00, 0x11]), 4096), -1);
|
||||
});
|
||||
|
||||
test("WebM 解码结果下混为单声道 16bit PCM", async () => {
|
||||
const decodeImpl = async () => ({
|
||||
channelData: [new Float32Array([0.5, -0.5, 1.5, -1.5])],
|
||||
@@ -202,6 +212,80 @@ test("DashScope 会话缺密钥时在本地失败且不建连", async () => {
|
||||
await assert.rejects(() => session.finish(), /api key missing/);
|
||||
});
|
||||
|
||||
test("DashScope 实时会话把中间结果经 onPartial 回传,末帧定稿", async () => {
|
||||
/** 假 WebSocket:只回放本用例关心的事件 */
|
||||
class FakeWs {
|
||||
constructor(url, options) {
|
||||
this.url = url;
|
||||
this.options = options;
|
||||
this.readyState = 1;
|
||||
this.bufferedAmount = 0;
|
||||
this.sent = [];
|
||||
this.listeners = {};
|
||||
FakeWs.instances.push(this);
|
||||
}
|
||||
on(event, callback) {
|
||||
this.listeners[event] = callback;
|
||||
}
|
||||
emit(event, data) {
|
||||
this.listeners[event]?.(data);
|
||||
}
|
||||
send(data) {
|
||||
this.sent.push(data);
|
||||
}
|
||||
close() {
|
||||
this.readyState = 3;
|
||||
}
|
||||
}
|
||||
FakeWs.instances = [];
|
||||
|
||||
const partials = [];
|
||||
const session = createRecognizer({
|
||||
provider: "dashscope",
|
||||
endpoint: "wss://example.invalid/asr",
|
||||
apiKey: "test-key",
|
||||
logger: silentLogger,
|
||||
// 关掉节流,断言每一个中间结果都回调
|
||||
partialIntervalMs: 0,
|
||||
WebSocketImpl: FakeWs,
|
||||
onPartial: (text) => partials.push(text),
|
||||
});
|
||||
|
||||
session.push(Buffer.from([1, 2, 3, 4]));
|
||||
const ws = FakeWs.instances[0];
|
||||
assert.ok(ws, "首帧应触发建连");
|
||||
|
||||
// 建连后先发 run-task,再在 task-started 时把缓存的音频冲出去
|
||||
ws.emit("open");
|
||||
const runTask = JSON.parse(ws.sent[0]);
|
||||
assert.equal(runTask.header.action, "run-task");
|
||||
assert.equal(runTask.header.task_id.length > 0, true);
|
||||
|
||||
ws.emit("message", JSON.stringify({ header: { event: "task-started" }, payload: {} }));
|
||||
assert.equal(Buffer.isBuffer(ws.sent[1]), true);
|
||||
|
||||
// sentence_end=false 是中间结果:必须实时回显(不再等整句说完)
|
||||
ws.emit("message", JSON.stringify({
|
||||
header: { event: "result-generated" },
|
||||
payload: { output: { sentence: { text: "你好", sentence_end: false } } },
|
||||
}));
|
||||
assert.deepEqual(partials, ["你好"]);
|
||||
|
||||
ws.emit("message", JSON.stringify({
|
||||
header: { event: "result-generated" },
|
||||
payload: { output: { sentence: { text: "你好,我是小橙", sentence_end: false } } },
|
||||
}));
|
||||
assert.deepEqual(partials, ["你好", "你好,我是小橙"]);
|
||||
|
||||
// 定稿:句子结束 + finish-task 后的 task-finished 取全量
|
||||
const finished = session.finish();
|
||||
ws.emit("message", JSON.stringify({
|
||||
header: { event: "task-finished" },
|
||||
payload: { output: { text: "你好,我是小橙。" } },
|
||||
}));
|
||||
assert.match(await finished, /你好,我是小橙。/);
|
||||
});
|
||||
|
||||
test("批式 OCR 会话累积分片并传递元信息", async () => {
|
||||
let captured = null;
|
||||
const session = createOcrSession({
|
||||
|
||||
Reference in New Issue
Block a user