|
| 1 | +import fetchMock from "jest-fetch-mock"; |
| 2 | +import { SpeakerOptions } from "../../src"; |
| 3 | +import { createClient, requestMatches } from "./utils"; |
| 4 | + |
| 5 | +fetchMock.enableMocks(); |
| 6 | + |
| 7 | +const assembly = createClient(); |
| 8 | +const transcriptId = "transcript_123"; |
| 9 | +const remoteAudioURL = "https://assembly.ai/espn.m4a"; |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + fetchMock.resetMocks(); |
| 14 | + fetchMock.doMock(); |
| 15 | +}); |
| 16 | + |
| 17 | +describe("speaker options", () => { |
| 18 | + it("should create transcript with speaker_options", async () => { |
| 19 | + const speakerOptions: SpeakerOptions = { |
| 20 | + min_speakers_expected: 2, |
| 21 | + max_speakers_expected: 4, |
| 22 | + }; |
| 23 | + |
| 24 | + fetchMock.doMockOnceIf( |
| 25 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 26 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 27 | + ); |
| 28 | + |
| 29 | + const transcript = await assembly.transcripts.submit({ |
| 30 | + audio_url: remoteAudioURL, |
| 31 | + speaker_labels: true, |
| 32 | + speaker_options: speakerOptions, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(transcript.id).toBe(transcriptId); |
| 36 | + expect(transcript.status).toBe("queued"); |
| 37 | + |
| 38 | + // Verify the request body included speaker_options |
| 39 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 40 | + expect(requestBody.speaker_labels).toBe(true); |
| 41 | + expect(requestBody.speaker_options).toEqual(speakerOptions); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should create transcript with only min_speakers_expected", async () => { |
| 45 | + const speakerOptions: SpeakerOptions = { |
| 46 | + min_speakers_expected: 3, |
| 47 | + }; |
| 48 | + |
| 49 | + fetchMock.doMockOnceIf( |
| 50 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 51 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 52 | + ); |
| 53 | + |
| 54 | + const transcript = await assembly.transcripts.submit({ |
| 55 | + audio_url: remoteAudioURL, |
| 56 | + speaker_labels: true, |
| 57 | + speaker_options: speakerOptions, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(transcript.id).toBe(transcriptId); |
| 61 | + |
| 62 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 63 | + expect(requestBody.speaker_options.min_speakers_expected).toBe(3); |
| 64 | + expect(requestBody.speaker_options.max_speakers_expected).toBeUndefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should create transcript with only max_speakers_expected", async () => { |
| 68 | + const speakerOptions: SpeakerOptions = { |
| 69 | + max_speakers_expected: 5, |
| 70 | + }; |
| 71 | + |
| 72 | + fetchMock.doMockOnceIf( |
| 73 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 74 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 75 | + ); |
| 76 | + |
| 77 | + const transcript = await assembly.transcripts.submit({ |
| 78 | + audio_url: remoteAudioURL, |
| 79 | + speaker_labels: true, |
| 80 | + speaker_options: speakerOptions, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(transcript.id).toBe(transcriptId); |
| 84 | + |
| 85 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 86 | + expect(requestBody.speaker_options.min_speakers_expected).toBeUndefined(); |
| 87 | + expect(requestBody.speaker_options.max_speakers_expected).toBe(5); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should create transcript with speakers_expected (without speaker_options)", async () => { |
| 91 | + fetchMock.doMockOnceIf( |
| 92 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 93 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 94 | + ); |
| 95 | + |
| 96 | + const transcript = await assembly.transcripts.submit({ |
| 97 | + audio_url: remoteAudioURL, |
| 98 | + speaker_labels: true, |
| 99 | + speakers_expected: 3, |
| 100 | + }); |
| 101 | + |
| 102 | + expect(transcript.id).toBe(transcriptId); |
| 103 | + |
| 104 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 105 | + expect(requestBody.speaker_labels).toBe(true); |
| 106 | + expect(requestBody.speakers_expected).toBe(3); |
| 107 | + expect(requestBody.speaker_options).toBeUndefined(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should handle null speaker_options", async () => { |
| 111 | + fetchMock.doMockOnceIf( |
| 112 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 113 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 114 | + ); |
| 115 | + |
| 116 | + const transcript = await assembly.transcripts.submit({ |
| 117 | + audio_url: remoteAudioURL, |
| 118 | + speaker_labels: true, |
| 119 | + speaker_options: null, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(transcript.id).toBe(transcriptId); |
| 123 | + |
| 124 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 125 | + expect(requestBody.speaker_options).toBe(null); |
| 126 | + }); |
| 127 | +}); |
0 commit comments