chatvoice-persistence.test.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { buildCanonicalTranscript } from '../src/canonical-transcript.mjs';
  4. import { ChatVoicePersistence } from '../src/chatvoice-persistence.mjs';
  5. test('canonical 字数和哈希规则与故事素材校验一致', () => {
  6. const canonical = buildCanonicalTranscript(
  7. '第一段。 \n\n\n第二段。',
  8. [{ text: '不完整分段', speakerId: '1', startMs: 0, endMs: 1000 }],
  9. '2026-08-26T00:00:00.000Z',
  10. );
  11. assert.equal(canonical.transcription, '第一段。\n\n第二段。');
  12. assert.equal(canonical.transcriptionCharCount, 8);
  13. assert.equal(canonical.transcriptionSegments.length, 1);
  14. assert.equal(canonical.transcriptionSegments[0].synthetic, true);
  15. assert.match(canonical.transcriptionHash, /^[0-9a-f]{64}$/);
  16. });
  17. test('服务端写入 ChatVoice 后必须通过 hash 和字数回读', async () => {
  18. let stored = null;
  19. const fetchImpl = async (_url, options = {}) => {
  20. if (options.method === 'PUT') {
  21. stored = JSON.parse(options.body);
  22. return { ok: true, status: 200, async json() { return { updatedAt: 'now' }; } };
  23. }
  24. return {
  25. ok: true,
  26. status: 200,
  27. async json() { return stored || { objectId: 'voice-1' }; },
  28. };
  29. };
  30. const persistence = new ChatVoicePersistence({
  31. canonicalPersistenceEnabled: true,
  32. parseMasterKey: 'test-master-key',
  33. parseServerUrl: 'https://parse.example.test/parse',
  34. parseAppId: 'test-app',
  35. }, fetchImpl);
  36. await persistence.validateAccess('voice-1', 'Bearer session-token');
  37. const result = await persistence.commit({
  38. request: { chatVoiceId: 'voice-1' },
  39. }, {
  40. text: '完整逐字稿',
  41. segments: [{ text: '完整逐字稿', speakerId: '1', startMs: 0, endMs: 1000 }],
  42. });
  43. assert.equal(result.persisted, true);
  44. assert.equal(stored.transcriptionStatus, 'completed');
  45. assert.equal(stored.transcriptionCompleteness, 'long_complete');
  46. assert.equal(stored.transcriptionRevalidationStatus, 'completed');
  47. });