|
| 1 | +import fetchMock from "jest-fetch-mock"; |
| 2 | +import { LanguageDetectionOptions } 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("language detection options", () => { |
| 18 | + it("should create transcript with all language_detection_options", async () => { |
| 19 | + const languageDetectionOptions: LanguageDetectionOptions = { |
| 20 | + expected_languages: ["en", "es"], |
| 21 | + fallback_language: "en", |
| 22 | + code_switching: true, |
| 23 | + code_switching_confidence_threshold: 0.8, |
| 24 | + }; |
| 25 | + |
| 26 | + fetchMock.doMockOnceIf( |
| 27 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 28 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 29 | + ); |
| 30 | + |
| 31 | + const transcript = await assembly.transcripts.submit({ |
| 32 | + audio_url: remoteAudioURL, |
| 33 | + language_detection: true, |
| 34 | + language_detection_options: languageDetectionOptions, |
| 35 | + }); |
| 36 | + |
| 37 | + expect(transcript.id).toBe(transcriptId); |
| 38 | + expect(transcript.status).toBe("queued"); |
| 39 | + |
| 40 | + // Verify the request body included language_detection_options |
| 41 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 42 | + expect(requestBody.language_detection).toBe(true); |
| 43 | + expect(requestBody.language_detection_options).toEqual( |
| 44 | + languageDetectionOptions, |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should create transcript with only code_switching enabled", async () => { |
| 49 | + const languageDetectionOptions: LanguageDetectionOptions = { |
| 50 | + code_switching: true, |
| 51 | + }; |
| 52 | + |
| 53 | + fetchMock.doMockOnceIf( |
| 54 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 55 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 56 | + ); |
| 57 | + |
| 58 | + const transcript = await assembly.transcripts.submit({ |
| 59 | + audio_url: remoteAudioURL, |
| 60 | + language_detection: true, |
| 61 | + language_detection_options: languageDetectionOptions, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(transcript.id).toBe(transcriptId); |
| 65 | + |
| 66 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 67 | + expect(requestBody.language_detection_options.code_switching).toBe(true); |
| 68 | + expect( |
| 69 | + requestBody.language_detection_options |
| 70 | + .code_switching_confidence_threshold, |
| 71 | + ).toBeUndefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should create transcript with code_switching and confidence threshold", async () => { |
| 75 | + const languageDetectionOptions: LanguageDetectionOptions = { |
| 76 | + code_switching: true, |
| 77 | + code_switching_confidence_threshold: 0.75, |
| 78 | + }; |
| 79 | + |
| 80 | + fetchMock.doMockOnceIf( |
| 81 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 82 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 83 | + ); |
| 84 | + |
| 85 | + const transcript = await assembly.transcripts.submit({ |
| 86 | + audio_url: remoteAudioURL, |
| 87 | + language_detection: true, |
| 88 | + language_detection_options: languageDetectionOptions, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(transcript.id).toBe(transcriptId); |
| 92 | + |
| 93 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 94 | + expect(requestBody.language_detection_options.code_switching).toBe(true); |
| 95 | + expect( |
| 96 | + requestBody.language_detection_options |
| 97 | + .code_switching_confidence_threshold, |
| 98 | + ).toBe(0.75); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should create transcript with only confidence threshold", async () => { |
| 102 | + const languageDetectionOptions: LanguageDetectionOptions = { |
| 103 | + code_switching_confidence_threshold: 0.9, |
| 104 | + }; |
| 105 | + |
| 106 | + fetchMock.doMockOnceIf( |
| 107 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 108 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 109 | + ); |
| 110 | + |
| 111 | + const transcript = await assembly.transcripts.submit({ |
| 112 | + audio_url: remoteAudioURL, |
| 113 | + language_detection: true, |
| 114 | + language_detection_options: languageDetectionOptions, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(transcript.id).toBe(transcriptId); |
| 118 | + |
| 119 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 120 | + expect( |
| 121 | + requestBody.language_detection_options.code_switching, |
| 122 | + ).toBeUndefined(); |
| 123 | + expect( |
| 124 | + requestBody.language_detection_options |
| 125 | + .code_switching_confidence_threshold, |
| 126 | + ).toBe(0.9); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should handle null language_detection_options", async () => { |
| 130 | + fetchMock.doMockOnceIf( |
| 131 | + requestMatches({ url: "/v2/transcript", method: "POST" }), |
| 132 | + JSON.stringify({ id: transcriptId, status: "queued" }), |
| 133 | + ); |
| 134 | + |
| 135 | + const transcript = await assembly.transcripts.submit({ |
| 136 | + audio_url: remoteAudioURL, |
| 137 | + language_detection: true, |
| 138 | + language_detection_options: null, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(transcript.id).toBe(transcriptId); |
| 142 | + |
| 143 | + const requestBody = JSON.parse(fetchMock.mock.calls[0][1]?.body as string); |
| 144 | + expect(requestBody.language_detection_options).toBe(null); |
| 145 | + }); |
| 146 | +}); |
0 commit comments