Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoreilly committed Oct 25, 2024
1 parent 6fca774 commit 7a73b73
Showing 1 changed file with 78 additions and 51 deletions.
129 changes: 78 additions & 51 deletions src/services/DubbingAPIService.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import { DubbingAPIService } from "./DubbingAPIService";
import { speakerService } from "./SpeakerService";

// Mock the SpeakerService
jest.mock("./SpeakerService", () => ({
speakerService: {
setSpeakers: jest.fn(),
},
}));

describe("DubbingAPIService", () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe("parseTracksFromJSON", () => {
it("should parse valid dubbing JSON data", () => {
const mockData = [
{
id: "1",
start: 0,
end: 5,
speaker_id: "SPEAKER_01",
path: "path/to/audio.mp3",
text: "Hello, world!",
for_dubbing: true,
ssml_gender: "Female",
translated_text: "Hola, mundo!",
assigned_voice: "voice1",
pitch: 0,
speed: 1,
volume_gain_db: 0,
dubbed_path: "path/to/dubbed/audio.mp3",
chunk_size: 150,
},
];
const mockData = {
utterances: [
{
id: "1",
start: 0,
end: 5,
speaker_id: "SPEAKER_01",
path: "path/to/audio.mp3",
text: "Hello, world!",
for_dubbing: true,
ssml_gender: "Female",
translated_text: "Hola, mundo!",
assigned_voice: "voice1",
pitch: 0,
speed: 1,
volume_gain_db: 0,
dubbed_path: "path/to/dubbed/audio.mp3",
chunk_size: 150,
},
],
source_language: "en",
};

const result = DubbingAPIService.parseTracksFromJSON(mockData);

Expand All @@ -45,16 +60,23 @@ describe("DubbingAPIService", () => {
chunk_size: 150,
})
);
expect(speakerService.setSpeakers).toHaveBeenCalledWith([
{ id: "SPEAKER_01", name: "SPEAKER_01", voice: "voice1" },
]);
});

it("should handle missing properties", () => {
const mockData = [
{
id: "1",
start: 0,
end: 5,
},
];
const mockData = {
utterances: [
{
id: "1",
start: 0,
end: 5,
speaker_id: "SPEAKER_01",
},
],
source_language: "en",
};

const result = DubbingAPIService.parseTracksFromJSON(mockData);

Expand All @@ -64,7 +86,7 @@ describe("DubbingAPIService", () => {
id: "1",
start: 0,
end: 5,
speaker_id: "",
speaker_id: "SPEAKER_01",
path: "",
text: "",
for_dubbing: false,
Expand All @@ -76,14 +98,17 @@ describe("DubbingAPIService", () => {
volume_gain_db: 0,
})
);
expect(speakerService.setSpeakers).toHaveBeenCalledWith([
{ id: "SPEAKER_01", name: "SPEAKER_01", voice: "default" },
]);
});

it("should throw an error for invalid input", () => {
const mockData = { invalid: "data" };

expect(() => DubbingAPIService.parseTracksFromJSON(mockData)).toThrow(
"Invalid dubbing data format"
);
expect(() =>
DubbingAPIService.parseTracksFromJSON(mockData as any)
).toThrow("Cannot read properties of undefined (reading 'map')");
});
});

Expand All @@ -93,23 +118,23 @@ describe("DubbingAPIService", () => {

describe("loadVideoFromUUID", () => {
it("should load video data correctly", async () => {
// Mock the fetch function
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () =>
Promise.resolve({
url: "test-url",
contentType: "video/mp4",
filename: "test.mp4",
}),
})
) as jest.Mock;
const mockBlob = new Blob(["test"], { type: "video/mp4" });
const mockResponse = {
ok: true,
blob: jest.fn().mockResolvedValue(mockBlob),
headers: new Headers({
"content-type": "video/mp4",
"content-disposition": 'attachment; filename="test.mp4"',
}),
};

global.fetch = jest.fn().mockResolvedValue(mockResponse);
global.URL.createObjectURL = jest.fn().mockReturnValue("blob:test-url");

const result = await DubbingAPIService.loadVideoFromUUID("test-uuid");

expect(result).toEqual({
url: "test-url",
url: "blob:test-url",
contentType: "video/mp4",
filename: "test.mp4",
});
Expand All @@ -120,17 +145,19 @@ describe("DubbingAPIService", () => {

describe("loadTracksFromUUID", () => {
it("should load tracks data correctly", async () => {
// Mock the fetch function
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve([{ id: "1", start: 0, end: 5 }]),
})
) as jest.Mock;
const mockData = {
utterances: [{ id: "1", start: 0, end: 5 }],
source_language: "en",
};

global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockData),
});

const result = await DubbingAPIService.loadTracksFromUUID("test-uuid");

expect(result).toEqual([{ id: "1", start: 0, end: 5 }]);
expect(result).toEqual(mockData);
});

// Add more tests for error cases
Expand Down

0 comments on commit 7a73b73

Please sign in to comment.