|
| 1 | +import fetchMock from "jest-fetch-mock"; |
| 2 | +import { |
| 3 | + SpeakerIdentificationEffort, |
| 4 | + SpeakerIdentificationRequest, |
| 5 | +} from "../../src"; |
| 6 | +import { createClient, requestMatches } from "./utils"; |
| 7 | + |
| 8 | +fetchMock.enableMocks(); |
| 9 | + |
| 10 | +const assembly = createClient(); |
| 11 | +const transcriptId = "transcript_123"; |
| 12 | +const remoteAudioURL = "https://assembly.ai/espn.m4a"; |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + jest.clearAllMocks(); |
| 16 | + fetchMock.resetMocks(); |
| 17 | + fetchMock.doMock(); |
| 18 | +}); |
| 19 | + |
| 20 | +describe("speaker identification", () => { |
| 21 | + it("should create transcript with speaker_identification effort", async () => { |
| 22 | + const speakerIdentification: SpeakerIdentificationRequest = { |
| 23 | + speaker_type: "name", |
| 24 | + effort: "medium", |
| 25 | + }; |
| 26 | + |
| 27 | + fetchMock.doMockOnceIf( |
| 28 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 29 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 30 | + ); |
| 31 | + |
| 32 | + const transcript = await assembly.transcripts.submit({ |
| 33 | + audio_url: remoteAudioURL, |
| 34 | + speaker_labels: true, |
| 35 | + speech_understanding: { |
| 36 | + request: { speaker_identification: speakerIdentification }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + expect(transcript.id).toBe(transcriptId); |
| 41 | + expect(transcript.status).toBe("queued"); |
| 42 | + |
| 43 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 44 | + expect( |
| 45 | + requestBody.speech_understanding.request.speaker_identification, |
| 46 | + ).toEqual(speakerIdentification); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should create transcript with speaker_identification without effort", async () => { |
| 50 | + fetchMock.doMockOnceIf( |
| 51 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 52 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 53 | + ); |
| 54 | + |
| 55 | + const transcript = await assembly.transcripts.submit({ |
| 56 | + audio_url: remoteAudioURL, |
| 57 | + speaker_labels: true, |
| 58 | + speech_understanding: { |
| 59 | + request: { |
| 60 | + speaker_identification: { |
| 61 | + speaker_type: "role", |
| 62 | + known_values: ["Agent", "Customer"], |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(transcript.id).toBe(transcriptId); |
| 69 | + |
| 70 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 71 | + const speakerIdentification = |
| 72 | + requestBody.speech_understanding.request.speaker_identification; |
| 73 | + expect(speakerIdentification.speaker_type).toBe("role"); |
| 74 | + expect(speakerIdentification.known_values).toEqual(["Agent", "Customer"]); |
| 75 | + expect(speakerIdentification.effort).toBeUndefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should accept every documented effort value", () => { |
| 79 | + const efforts: SpeakerIdentificationEffort[] = ["low", "medium"]; |
| 80 | + |
| 81 | + for (const effort of efforts) { |
| 82 | + const speakerIdentification: SpeakerIdentificationRequest = { |
| 83 | + speaker_type: "name", |
| 84 | + effort, |
| 85 | + }; |
| 86 | + expect(speakerIdentification.effort).toBe(effort); |
| 87 | + } |
| 88 | + }); |
| 89 | +}); |
0 commit comments