result-parser.test.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { parseIflytekResult } from '../src/result-parser.mjs';
  4. function lattice(text, { bg, ed, role }) {
  5. return {
  6. json_1best: JSON.stringify({
  7. st: {
  8. bg: String(bg),
  9. ed: String(ed),
  10. rl: role,
  11. rt: [{ ws: [...text].map(character => ({ cw: [{ w: character }] })) }],
  12. },
  13. }),
  14. };
  15. }
  16. test('解析正文、时间和说话人,并合并连续同一说话人', () => {
  17. const raw = JSON.stringify({
  18. lattice: [
  19. lattice('你好', { bg: 0, ed: 500, role: 1 }),
  20. lattice('世界', { bg: 500, ed: 900, role: 1 }),
  21. lattice('您好', { bg: 900, ed: 1300, role: 2 }),
  22. ],
  23. });
  24. const result = parseIflytekResult(raw);
  25. assert.equal(result.text, '你好 世界 您好');
  26. assert.deepEqual(result.segments, [
  27. { text: '你好世界', startMs: 0, endMs: 900, speakerId: '1' },
  28. { text: '您好', startMs: 900, endMs: 1300, speakerId: '2' },
  29. ]);
  30. });
  31. test('损坏结果安全返回空正文', () => {
  32. assert.deepEqual(parseIflytekResult('{bad json'), { text: '', segments: [] });
  33. });