magic-hour 0.41.0 → 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/resources/v1/ai-voice-generator/resource-client.d.ts +29 -0
- package/dist/resources/v1/ai-voice-generator/resource-client.js +60 -0
- package/dist/resources/v1/audio-projects/resource-client.d.ts +23 -1
- package/dist/resources/v1/audio-projects/resource-client.js +68 -0
- package/dist/resources/v1/audio-projects/resource-client.test.d.ts +1 -0
- package/dist/resources/v1/audio-projects/resource-client.test.js +552 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -260,6 +260,7 @@ Valid values are: `none`, `error`, `warn`, `info`, `debug` (case insensitive). I
|
|
|
260
260
|
### [v1.aiVoiceGenerator](src/resources/v1/ai-voice-generator/README.md)
|
|
261
261
|
|
|
262
262
|
* [create](src/resources/v1/ai-voice-generator/README.md#create) - AI Voice Generator
|
|
263
|
+
* [generate](src/resources/v1/ai-voice-generator/README.md#generate) - AI Voice Generator Generate Workflow
|
|
263
264
|
|
|
264
265
|
### [v1.animation](src/resources/v1/animation/README.md)
|
|
265
266
|
|
|
@@ -1,8 +1,36 @@
|
|
|
1
1
|
import { ApiPromise, CoreClient, CoreResourceClient, RequestOptions, ResourceClientOptions } from "make-api-request-js";
|
|
2
|
+
import { GenerateOptions, GenerateRequestType } from "../../../helpers/generate-type";
|
|
2
3
|
import * as requests from "../../../resources/v1/ai-voice-generator/request-types";
|
|
3
4
|
import * as types from "../../../types";
|
|
5
|
+
type GenerateRequest = GenerateRequestType<requests.CreateRequest, {}>;
|
|
4
6
|
export declare class AiVoiceGeneratorClient extends CoreResourceClient {
|
|
5
7
|
constructor(coreClient: CoreClient, opts: ResourceClientOptions);
|
|
8
|
+
/**
|
|
9
|
+
* AI Voice Generator
|
|
10
|
+
*
|
|
11
|
+
* Generate speech from text
|
|
12
|
+
*
|
|
13
|
+
* This method provides a convenient way to create a request and automatically wait for completion and download outputs.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import Client from "../../../index.js";
|
|
18
|
+
*
|
|
19
|
+
* const client = new Client({ token: process.env["API_TOKEN"]!! });
|
|
20
|
+
* const res = await client.v1.aiVoiceGenerator.generate(
|
|
21
|
+
* {
|
|
22
|
+
* name: "Voice Generator audio",
|
|
23
|
+
* style: { prompt: "Hello, how are you?", voiceName: "Elon Musk" },
|
|
24
|
+
* },
|
|
25
|
+
* {
|
|
26
|
+
* waitForCompletion: true,
|
|
27
|
+
* downloadOutputs: true,
|
|
28
|
+
* downloadDirectory: "outputs",
|
|
29
|
+
* },
|
|
30
|
+
* );
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
generate(request: GenerateRequest, opts?: GenerateOptions): Promise<import("../image-projects/resource-client").V1ImageProjectsGetResponseWithDownloads>;
|
|
6
34
|
/**
|
|
7
35
|
* AI Voice Generator
|
|
8
36
|
*
|
|
@@ -12,3 +40,4 @@ export declare class AiVoiceGeneratorClient extends CoreResourceClient {
|
|
|
12
40
|
*/
|
|
13
41
|
create(request: requests.CreateRequest, opts?: RequestOptions): ApiPromise<types.V1AiVoiceGeneratorCreateResponse>;
|
|
14
42
|
}
|
|
43
|
+
export {};
|
|
@@ -1,13 +1,73 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
2
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
23
|
exports.AiVoiceGeneratorClient = void 0;
|
|
4
24
|
const make_api_request_js_1 = require("make-api-request-js");
|
|
25
|
+
const logger_1 = require("../../../logger");
|
|
26
|
+
const image_projects_1 = require("../../../resources/v1/image-projects");
|
|
5
27
|
const v1_ai_voice_generator_create_body_1 = require("../../../types/v1-ai-voice-generator-create-body");
|
|
6
28
|
const v1_ai_voice_generator_create_response_1 = require("../../../types/v1-ai-voice-generator-create-response");
|
|
7
29
|
class AiVoiceGeneratorClient extends make_api_request_js_1.CoreResourceClient {
|
|
8
30
|
constructor(coreClient, opts) {
|
|
9
31
|
super(coreClient, opts);
|
|
10
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* AI Voice Generator
|
|
35
|
+
*
|
|
36
|
+
* Generate speech from text
|
|
37
|
+
*
|
|
38
|
+
* This method provides a convenient way to create a request and automatically wait for completion and download outputs.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import Client from "../../../index.js";
|
|
43
|
+
*
|
|
44
|
+
* const client = new Client({ token: process.env["API_TOKEN"]!! });
|
|
45
|
+
* const res = await client.v1.aiVoiceGenerator.generate(
|
|
46
|
+
* {
|
|
47
|
+
* name: "Voice Generator audio",
|
|
48
|
+
* style: { prompt: "Hello, how are you?", voiceName: "Elon Musk" },
|
|
49
|
+
* },
|
|
50
|
+
* {
|
|
51
|
+
* waitForCompletion: true,
|
|
52
|
+
* downloadOutputs: true,
|
|
53
|
+
* downloadDirectory: "outputs",
|
|
54
|
+
* },
|
|
55
|
+
* );
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
generate(request_1) {
|
|
59
|
+
return __awaiter(this, arguments, void 0, function* (request, opts = {}) {
|
|
60
|
+
const { waitForCompletion = true, downloadOutputs = true, downloadDirectory = undefined } = opts, createOpts = __rest(opts, ["waitForCompletion", "downloadOutputs", "downloadDirectory"]);
|
|
61
|
+
const createResponse = yield this.create(request, createOpts);
|
|
62
|
+
(0, logger_1.getLogger)().info(`Created AiVoiceGeneratorClient project ${createResponse.id}`);
|
|
63
|
+
const projectsClient = new image_projects_1.ImageProjectsClient(this._client, this._opts);
|
|
64
|
+
(0, logger_1.getLogger)().debug(`Checking result for AiVoiceGeneratorClient project ${createResponse.id}`);
|
|
65
|
+
const result = yield projectsClient.checkResult({ id: createResponse.id }, Object.assign({ waitForCompletion,
|
|
66
|
+
downloadOutputs,
|
|
67
|
+
downloadDirectory }, createOpts));
|
|
68
|
+
return result;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
11
71
|
/**
|
|
12
72
|
* AI Voice Generator
|
|
13
73
|
*
|
|
@@ -1,8 +1,30 @@
|
|
|
1
1
|
import { ApiPromise, CoreClient, CoreResourceClient, RequestOptions, ResourceClientOptions } from "make-api-request-js";
|
|
2
|
+
import { types } from "../../../index.js";
|
|
3
|
+
import { GenerateOptions } from "../../../helpers/generate-type";
|
|
2
4
|
import * as requests from "../../../resources/v1/audio-projects/request-types";
|
|
3
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Extended response interface that includes downloaded paths
|
|
7
|
+
*/
|
|
8
|
+
export interface V1AudioProjectsGetResponseWithDownloads extends types.V1AudioProjectsGetResponse {
|
|
9
|
+
/**
|
|
10
|
+
* The paths to the downloaded files.
|
|
11
|
+
* This field is only populated if `download_outputs` is True and the audio project is complete.
|
|
12
|
+
*/
|
|
13
|
+
downloadedPaths?: string[];
|
|
14
|
+
}
|
|
4
15
|
export declare class AudioProjectsClient extends CoreResourceClient {
|
|
5
16
|
constructor(coreClient: CoreClient, opts: ResourceClientOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Check the result of an audio project with optional waiting and downloading.
|
|
19
|
+
*
|
|
20
|
+
* This method retrieves the status of an audio project and optionally waits for completion
|
|
21
|
+
* and downloads the output files.
|
|
22
|
+
*
|
|
23
|
+
* @param request - Request object containing the audio project ID
|
|
24
|
+
* @param opts - Additional request options
|
|
25
|
+
* @returns The audio project response with optional downloaded file paths included
|
|
26
|
+
*/
|
|
27
|
+
checkResult(request: requests.GetRequest, opts?: GenerateOptions): Promise<V1AudioProjectsGetResponseWithDownloads>;
|
|
6
28
|
/**
|
|
7
29
|
* Delete audio
|
|
8
30
|
*
|
|
@@ -1,12 +1,80 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
2
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
23
|
exports.AudioProjectsClient = void 0;
|
|
4
24
|
const make_api_request_js_1 = require("make-api-request-js");
|
|
25
|
+
const download_1 = require("../../../helpers/download");
|
|
26
|
+
const sleep_1 = require("../../../helpers/sleep");
|
|
27
|
+
const logger_1 = require("../../../logger");
|
|
5
28
|
const v1_audio_projects_get_response_1 = require("../../../types/v1-audio-projects-get-response");
|
|
6
29
|
class AudioProjectsClient extends make_api_request_js_1.CoreResourceClient {
|
|
7
30
|
constructor(coreClient, opts) {
|
|
8
31
|
super(coreClient, opts);
|
|
9
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Check the result of an audio project with optional waiting and downloading.
|
|
35
|
+
*
|
|
36
|
+
* This method retrieves the status of an audio project and optionally waits for completion
|
|
37
|
+
* and downloads the output files.
|
|
38
|
+
*
|
|
39
|
+
* @param request - Request object containing the audio project ID
|
|
40
|
+
* @param opts - Additional request options
|
|
41
|
+
* @returns The audio project response with optional downloaded file paths included
|
|
42
|
+
*/
|
|
43
|
+
checkResult(request, opts) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
const _c = opts || {}, { waitForCompletion = true, downloadOutputs = true, downloadDirectory = undefined } = _c, requestOpts = __rest(_c, ["waitForCompletion", "downloadOutputs", "downloadDirectory"]);
|
|
47
|
+
let apiResponse = yield this.get({ id: request.id }, requestOpts);
|
|
48
|
+
if (!waitForCompletion) {
|
|
49
|
+
return Object.assign({}, apiResponse);
|
|
50
|
+
}
|
|
51
|
+
const pollInterval = parseFloat(process.env["MAGIC_HOUR_POLL_INTERVAL"] || "0.5");
|
|
52
|
+
(0, logger_1.getLogger)().debug(`Polling audio project ${request.id} every ${pollInterval} seconds`);
|
|
53
|
+
while (!["complete", "error", "canceled"].includes(apiResponse.status)) {
|
|
54
|
+
yield (0, sleep_1.sleep)(pollInterval * 1000); // Convert seconds to milliseconds
|
|
55
|
+
(0, logger_1.getLogger)().info(`Audio project ${request.id} status: ${apiResponse.status}, waiting for ${pollInterval} seconds and checking again`);
|
|
56
|
+
apiResponse = yield this.get({ id: request.id }, requestOpts);
|
|
57
|
+
}
|
|
58
|
+
if (apiResponse.status !== "complete") {
|
|
59
|
+
if (apiResponse.status === "error") {
|
|
60
|
+
const message = `Audio project ${request.id} has status ${apiResponse.status}: [${(_a = apiResponse.error) === null || _a === void 0 ? void 0 : _a.code}] ${(_b = apiResponse.error) === null || _b === void 0 ? void 0 : _b.message}`;
|
|
61
|
+
(0, logger_1.getLogger)().error(message);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
(0, logger_1.getLogger)().info(`Audio project ${request.id} has status ${apiResponse.status}. Stopping polling.`);
|
|
65
|
+
}
|
|
66
|
+
return Object.assign({}, apiResponse);
|
|
67
|
+
}
|
|
68
|
+
if (!downloadOutputs) {
|
|
69
|
+
(0, logger_1.getLogger)().info(`Download outputs is disabled. Returning audio project ${request.id} with status ${apiResponse.status}`);
|
|
70
|
+
return Object.assign({}, apiResponse);
|
|
71
|
+
}
|
|
72
|
+
(0, logger_1.getLogger)().debug(`Downloading outputs for audio project ${request.id} to ${downloadDirectory !== null && downloadDirectory !== void 0 ? downloadDirectory : "current directory"}`);
|
|
73
|
+
const downloadedPaths = yield (0, download_1.downloadFiles)(apiResponse.downloads, downloadDirectory);
|
|
74
|
+
(0, logger_1.getLogger)().info(`Downloaded outputs for audio project ${request.id} to ${downloadedPaths}`);
|
|
75
|
+
return Object.assign(Object.assign({}, apiResponse), { downloadedPaths });
|
|
76
|
+
});
|
|
77
|
+
}
|
|
10
78
|
/**
|
|
11
79
|
* Delete audio
|
|
12
80
|
*
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const magic_hour_1 = __importDefault(require("../../../index.js"));
|
|
16
|
+
const node_1 = require("msw/node");
|
|
17
|
+
const msw_1 = require("msw");
|
|
18
|
+
// Create MSW server
|
|
19
|
+
const server = (0, node_1.setupServer)();
|
|
20
|
+
// Mock the sleep helper
|
|
21
|
+
jest.mock("magic-hour/helpers/sleep", () => ({
|
|
22
|
+
sleep: jest.fn().mockResolvedValue(undefined),
|
|
23
|
+
}));
|
|
24
|
+
// Mock the download helper
|
|
25
|
+
jest.mock("magic-hour/helpers/download", () => ({
|
|
26
|
+
downloadFiles: jest.fn(),
|
|
27
|
+
}));
|
|
28
|
+
const { sleep } = require("../../../helpers/sleep");
|
|
29
|
+
const { downloadFiles } = require("../../../helpers/download");
|
|
30
|
+
describe("AudioProjectsClient.checkResult", () => {
|
|
31
|
+
let client;
|
|
32
|
+
beforeAll(() => {
|
|
33
|
+
// Start MSW server
|
|
34
|
+
server.listen();
|
|
35
|
+
});
|
|
36
|
+
afterAll(() => {
|
|
37
|
+
// Stop MSW server
|
|
38
|
+
server.close();
|
|
39
|
+
});
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
client = new magic_hour_1.default({
|
|
42
|
+
token: "API_TOKEN",
|
|
43
|
+
});
|
|
44
|
+
// Reset all mocks
|
|
45
|
+
jest.clearAllMocks();
|
|
46
|
+
// Reset MSW handlers
|
|
47
|
+
server.resetHandlers();
|
|
48
|
+
// Clean up environment variables
|
|
49
|
+
delete process.env["MAGIC_HOUR_POLL_INTERVAL"];
|
|
50
|
+
});
|
|
51
|
+
describe("Basic functionality", () => {
|
|
52
|
+
test("should return response without waiting when waitForCompletion is false", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const mockResponse = {
|
|
54
|
+
id: "audio-123",
|
|
55
|
+
status: "rendering",
|
|
56
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
57
|
+
credits_charged: 2,
|
|
58
|
+
downloads: [],
|
|
59
|
+
enabled: true,
|
|
60
|
+
error: null,
|
|
61
|
+
name: "Test Audio Project",
|
|
62
|
+
type: "VOICE_GENERATOR",
|
|
63
|
+
};
|
|
64
|
+
// Set up MSW handler for the API call
|
|
65
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
66
|
+
return msw_1.HttpResponse.json(mockResponse);
|
|
67
|
+
}));
|
|
68
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, { waitForCompletion: false });
|
|
69
|
+
expect(result).toEqual({
|
|
70
|
+
id: "audio-123",
|
|
71
|
+
status: "rendering",
|
|
72
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
73
|
+
creditsCharged: 2,
|
|
74
|
+
downloads: [],
|
|
75
|
+
enabled: true,
|
|
76
|
+
error: null,
|
|
77
|
+
name: "Test Audio Project",
|
|
78
|
+
type: "VOICE_GENERATOR",
|
|
79
|
+
});
|
|
80
|
+
expect(sleep).not.toHaveBeenCalled();
|
|
81
|
+
}));
|
|
82
|
+
test("should wait for completion and return successful result", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
+
const renderingResponse = {
|
|
84
|
+
id: "audio-123",
|
|
85
|
+
status: "rendering",
|
|
86
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
87
|
+
credits_charged: 2,
|
|
88
|
+
downloads: [],
|
|
89
|
+
enabled: true,
|
|
90
|
+
error: null,
|
|
91
|
+
name: "Test Audio Project",
|
|
92
|
+
type: "VOICE_GENERATOR",
|
|
93
|
+
};
|
|
94
|
+
const completeResponse = {
|
|
95
|
+
id: "audio-123",
|
|
96
|
+
status: "complete",
|
|
97
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
98
|
+
credits_charged: 2,
|
|
99
|
+
downloads: [
|
|
100
|
+
{
|
|
101
|
+
url: "https://example.com/download/audio1.wav",
|
|
102
|
+
expires_at: "2024-01-01T01:00:00Z",
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
enabled: true,
|
|
106
|
+
error: null,
|
|
107
|
+
name: "Test Audio Project",
|
|
108
|
+
type: "VOICE_GENERATOR",
|
|
109
|
+
};
|
|
110
|
+
let callCount = 0;
|
|
111
|
+
// Set up MSW handler that returns different responses on each call
|
|
112
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
113
|
+
callCount++;
|
|
114
|
+
if (callCount === 1) {
|
|
115
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
119
|
+
}
|
|
120
|
+
}));
|
|
121
|
+
downloadFiles.mockResolvedValue(["/tmp/audio1.wav"]);
|
|
122
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, {
|
|
123
|
+
waitForCompletion: true,
|
|
124
|
+
downloadOutputs: true,
|
|
125
|
+
downloadDirectory: "/tmp",
|
|
126
|
+
});
|
|
127
|
+
expect(result).toEqual({
|
|
128
|
+
id: "audio-123",
|
|
129
|
+
status: "complete",
|
|
130
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
131
|
+
creditsCharged: 2,
|
|
132
|
+
downloads: [
|
|
133
|
+
{
|
|
134
|
+
url: "https://example.com/download/audio1.wav",
|
|
135
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
enabled: true,
|
|
139
|
+
error: null,
|
|
140
|
+
name: "Test Audio Project",
|
|
141
|
+
type: "VOICE_GENERATOR",
|
|
142
|
+
downloadedPaths: ["/tmp/audio1.wav"],
|
|
143
|
+
});
|
|
144
|
+
expect(sleep).toHaveBeenCalledWith(500); // Default poll interval
|
|
145
|
+
expect(downloadFiles).toHaveBeenCalledWith([
|
|
146
|
+
{
|
|
147
|
+
url: "https://example.com/download/audio1.wav",
|
|
148
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
149
|
+
},
|
|
150
|
+
], "/tmp");
|
|
151
|
+
}));
|
|
152
|
+
test("should handle error status during polling", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
+
const renderingResponse = {
|
|
154
|
+
id: "audio-123",
|
|
155
|
+
status: "rendering",
|
|
156
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
157
|
+
credits_charged: 2,
|
|
158
|
+
downloads: [],
|
|
159
|
+
enabled: true,
|
|
160
|
+
error: null,
|
|
161
|
+
name: "Test Audio Project",
|
|
162
|
+
type: "VOICE_GENERATOR",
|
|
163
|
+
};
|
|
164
|
+
const errorResponse = {
|
|
165
|
+
id: "audio-123",
|
|
166
|
+
status: "error",
|
|
167
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
168
|
+
credits_charged: 2,
|
|
169
|
+
downloads: [],
|
|
170
|
+
enabled: true,
|
|
171
|
+
error: { message: "Rendering failed", code: "ERROR_001" },
|
|
172
|
+
name: "Test Audio Project",
|
|
173
|
+
type: "VOICE_GENERATOR",
|
|
174
|
+
};
|
|
175
|
+
let callCount = 0;
|
|
176
|
+
// Set up MSW handler that returns different responses on each call
|
|
177
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
178
|
+
callCount++;
|
|
179
|
+
if (callCount === 1) {
|
|
180
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
return msw_1.HttpResponse.json(errorResponse);
|
|
184
|
+
}
|
|
185
|
+
}));
|
|
186
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, { waitForCompletion: true });
|
|
187
|
+
expect(result).toEqual({
|
|
188
|
+
id: "audio-123",
|
|
189
|
+
status: "error",
|
|
190
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
191
|
+
creditsCharged: 2,
|
|
192
|
+
downloads: [],
|
|
193
|
+
enabled: true,
|
|
194
|
+
error: { message: "Rendering failed", code: "ERROR_001" },
|
|
195
|
+
name: "Test Audio Project",
|
|
196
|
+
type: "VOICE_GENERATOR",
|
|
197
|
+
});
|
|
198
|
+
}));
|
|
199
|
+
test("should handle canceled status during polling", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
200
|
+
const renderingResponse = {
|
|
201
|
+
id: "audio-123",
|
|
202
|
+
status: "rendering",
|
|
203
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
204
|
+
credits_charged: 2,
|
|
205
|
+
downloads: [],
|
|
206
|
+
enabled: true,
|
|
207
|
+
error: null,
|
|
208
|
+
name: "Test Audio Project",
|
|
209
|
+
type: "VOICE_GENERATOR",
|
|
210
|
+
};
|
|
211
|
+
const canceledResponse = {
|
|
212
|
+
id: "audio-123",
|
|
213
|
+
status: "canceled",
|
|
214
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
215
|
+
credits_charged: 2,
|
|
216
|
+
downloads: [],
|
|
217
|
+
enabled: true,
|
|
218
|
+
error: null,
|
|
219
|
+
name: "Test Audio Project",
|
|
220
|
+
type: "VOICE_GENERATOR",
|
|
221
|
+
};
|
|
222
|
+
let callCount = 0;
|
|
223
|
+
// Set up MSW handler that returns different responses on each call
|
|
224
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
225
|
+
callCount++;
|
|
226
|
+
if (callCount === 1) {
|
|
227
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return msw_1.HttpResponse.json(canceledResponse);
|
|
231
|
+
}
|
|
232
|
+
}));
|
|
233
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, { waitForCompletion: true });
|
|
234
|
+
expect(result).toEqual({
|
|
235
|
+
id: "audio-123",
|
|
236
|
+
status: "canceled",
|
|
237
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
238
|
+
creditsCharged: 2,
|
|
239
|
+
downloads: [],
|
|
240
|
+
enabled: true,
|
|
241
|
+
error: null,
|
|
242
|
+
name: "Test Audio Project",
|
|
243
|
+
type: "VOICE_GENERATOR",
|
|
244
|
+
});
|
|
245
|
+
}));
|
|
246
|
+
});
|
|
247
|
+
describe("Polling behavior", () => {
|
|
248
|
+
test("should use custom poll interval from environment", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
249
|
+
const renderingResponse = {
|
|
250
|
+
id: "audio-123",
|
|
251
|
+
status: "rendering",
|
|
252
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
253
|
+
credits_charged: 2,
|
|
254
|
+
downloads: [],
|
|
255
|
+
enabled: true,
|
|
256
|
+
error: null,
|
|
257
|
+
name: "Test Audio Project",
|
|
258
|
+
type: "VOICE_GENERATOR",
|
|
259
|
+
};
|
|
260
|
+
const completeResponse = {
|
|
261
|
+
id: "audio-123",
|
|
262
|
+
status: "complete",
|
|
263
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
264
|
+
credits_charged: 2,
|
|
265
|
+
downloads: [],
|
|
266
|
+
enabled: true,
|
|
267
|
+
error: null,
|
|
268
|
+
name: "Test Audio Project",
|
|
269
|
+
type: "VOICE_GENERATOR",
|
|
270
|
+
};
|
|
271
|
+
let callCount = 0;
|
|
272
|
+
// Set up MSW handler that returns different responses on each call
|
|
273
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
274
|
+
callCount++;
|
|
275
|
+
if (callCount === 1) {
|
|
276
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
280
|
+
}
|
|
281
|
+
}));
|
|
282
|
+
// Set environment variable before the checkResult call
|
|
283
|
+
process.env["MAGIC_HOUR_POLL_INTERVAL"] = "1.5";
|
|
284
|
+
try {
|
|
285
|
+
yield client.v1.audioProjects.checkResult({ id: "audio-123" });
|
|
286
|
+
expect(sleep).toHaveBeenCalledWith(1500); // 1.5 seconds converted to milliseconds
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
// Clean up environment variable
|
|
290
|
+
delete process.env["MAGIC_HOUR_POLL_INTERVAL"];
|
|
291
|
+
}
|
|
292
|
+
}));
|
|
293
|
+
test("should poll multiple times until completion", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
294
|
+
const renderingResponse = {
|
|
295
|
+
id: "audio-123",
|
|
296
|
+
status: "rendering",
|
|
297
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
298
|
+
credits_charged: 2,
|
|
299
|
+
downloads: [],
|
|
300
|
+
enabled: true,
|
|
301
|
+
error: null,
|
|
302
|
+
name: "Test Audio Project",
|
|
303
|
+
type: "VOICE_GENERATOR",
|
|
304
|
+
};
|
|
305
|
+
const queuedResponse = Object.assign(Object.assign({}, renderingResponse), { status: "queued" });
|
|
306
|
+
const completeResponse = Object.assign(Object.assign({}, renderingResponse), { status: "complete" });
|
|
307
|
+
let callCount = 0;
|
|
308
|
+
// Set up MSW handler that returns different responses on each call
|
|
309
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
310
|
+
callCount++;
|
|
311
|
+
if (callCount === 1) {
|
|
312
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
313
|
+
}
|
|
314
|
+
else if (callCount === 2) {
|
|
315
|
+
return msw_1.HttpResponse.json(queuedResponse);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
319
|
+
}
|
|
320
|
+
}));
|
|
321
|
+
yield client.v1.audioProjects.checkResult({ id: "audio-123" });
|
|
322
|
+
expect(sleep).toHaveBeenCalledTimes(2);
|
|
323
|
+
}));
|
|
324
|
+
});
|
|
325
|
+
describe("Download functionality", () => {
|
|
326
|
+
test("should skip download when downloadOutputs is false", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
327
|
+
const completeResponse = {
|
|
328
|
+
id: "audio-123",
|
|
329
|
+
status: "complete",
|
|
330
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
331
|
+
credits_charged: 2,
|
|
332
|
+
downloads: [
|
|
333
|
+
{
|
|
334
|
+
url: "https://example.com/download/audio1.wav",
|
|
335
|
+
expires_at: "2024-01-01T01:00:00Z",
|
|
336
|
+
},
|
|
337
|
+
],
|
|
338
|
+
enabled: true,
|
|
339
|
+
error: null,
|
|
340
|
+
name: "Test Audio Project",
|
|
341
|
+
type: "VOICE_GENERATOR",
|
|
342
|
+
};
|
|
343
|
+
// Set up MSW handler
|
|
344
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
345
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
346
|
+
}));
|
|
347
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, {
|
|
348
|
+
waitForCompletion: true,
|
|
349
|
+
downloadOutputs: false,
|
|
350
|
+
});
|
|
351
|
+
expect(result).toEqual({
|
|
352
|
+
id: "audio-123",
|
|
353
|
+
status: "complete",
|
|
354
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
355
|
+
creditsCharged: 2,
|
|
356
|
+
downloads: [
|
|
357
|
+
{
|
|
358
|
+
url: "https://example.com/download/audio1.wav",
|
|
359
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
360
|
+
},
|
|
361
|
+
],
|
|
362
|
+
enabled: true,
|
|
363
|
+
error: null,
|
|
364
|
+
name: "Test Audio Project",
|
|
365
|
+
type: "VOICE_GENERATOR",
|
|
366
|
+
});
|
|
367
|
+
expect(downloadFiles).not.toHaveBeenCalled();
|
|
368
|
+
}));
|
|
369
|
+
test("should download files to current directory when no downloadDirectory specified", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
370
|
+
const completeResponse = {
|
|
371
|
+
id: "audio-123",
|
|
372
|
+
status: "complete",
|
|
373
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
374
|
+
credits_charged: 2,
|
|
375
|
+
downloads: [
|
|
376
|
+
{
|
|
377
|
+
url: "https://example.com/download/audio1.wav",
|
|
378
|
+
expires_at: "2024-01-01T01:00:00Z",
|
|
379
|
+
},
|
|
380
|
+
],
|
|
381
|
+
enabled: true,
|
|
382
|
+
error: null,
|
|
383
|
+
name: "Test Audio Project",
|
|
384
|
+
type: "VOICE_GENERATOR",
|
|
385
|
+
};
|
|
386
|
+
// Set up MSW handler
|
|
387
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
388
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
389
|
+
}));
|
|
390
|
+
downloadFiles.mockResolvedValue(["/current/dir/audio1.wav"]);
|
|
391
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, { downloadOutputs: true });
|
|
392
|
+
expect(downloadFiles).toHaveBeenCalledWith([
|
|
393
|
+
{
|
|
394
|
+
url: "https://example.com/download/audio1.wav",
|
|
395
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
396
|
+
},
|
|
397
|
+
], undefined);
|
|
398
|
+
expect(result.downloadedPaths).toEqual(["/current/dir/audio1.wav"]);
|
|
399
|
+
}));
|
|
400
|
+
test("should handle download errors gracefully", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
401
|
+
const completeResponse = {
|
|
402
|
+
id: "audio-123",
|
|
403
|
+
status: "complete",
|
|
404
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
405
|
+
credits_charged: 2,
|
|
406
|
+
downloads: [
|
|
407
|
+
{
|
|
408
|
+
url: "https://example.com/download/audio1.wav",
|
|
409
|
+
expires_at: "2024-01-01T01:00:00Z",
|
|
410
|
+
},
|
|
411
|
+
],
|
|
412
|
+
enabled: true,
|
|
413
|
+
error: null,
|
|
414
|
+
name: "Test Audio Project",
|
|
415
|
+
type: "VOICE_GENERATOR",
|
|
416
|
+
};
|
|
417
|
+
// Set up MSW handler
|
|
418
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
419
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
420
|
+
}));
|
|
421
|
+
downloadFiles.mockRejectedValue(new Error("Download failed"));
|
|
422
|
+
yield expect(client.v1.audioProjects.checkResult({ id: "audio-123" }, { downloadOutputs: true })).rejects.toThrow("Download failed");
|
|
423
|
+
}));
|
|
424
|
+
});
|
|
425
|
+
describe("Default behavior", () => {
|
|
426
|
+
test("should use default options when no options provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
427
|
+
const completeResponse = {
|
|
428
|
+
id: "audio-123",
|
|
429
|
+
status: "complete",
|
|
430
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
431
|
+
credits_charged: 2,
|
|
432
|
+
downloads: [
|
|
433
|
+
{
|
|
434
|
+
url: "https://example.com/download/audio1.wav",
|
|
435
|
+
expires_at: "2024-01-01T01:00:00Z",
|
|
436
|
+
},
|
|
437
|
+
],
|
|
438
|
+
enabled: true,
|
|
439
|
+
error: null,
|
|
440
|
+
name: "Test Audio Project",
|
|
441
|
+
type: "VOICE_GENERATOR",
|
|
442
|
+
};
|
|
443
|
+
const renderingResponse = Object.assign(Object.assign({}, completeResponse), { status: "rendering" });
|
|
444
|
+
let callCount = 0;
|
|
445
|
+
// Set up MSW handler that returns different responses on each call
|
|
446
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
447
|
+
callCount++;
|
|
448
|
+
if (callCount === 1) {
|
|
449
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
453
|
+
}
|
|
454
|
+
}));
|
|
455
|
+
downloadFiles.mockResolvedValue(["/tmp/audio1.wav"]);
|
|
456
|
+
const result = yield client.v1.audioProjects.checkResult({
|
|
457
|
+
id: "audio-123",
|
|
458
|
+
});
|
|
459
|
+
expect(result.downloadedPaths).toEqual(["/tmp/audio1.wav"]);
|
|
460
|
+
expect(sleep).toHaveBeenCalledWith(500); // Default poll interval
|
|
461
|
+
}));
|
|
462
|
+
});
|
|
463
|
+
describe("Request options", () => {
|
|
464
|
+
test("should pass additional request options to get method", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
465
|
+
const mockResponse = {
|
|
466
|
+
id: "audio-123",
|
|
467
|
+
status: "complete",
|
|
468
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
469
|
+
credits_charged: 2,
|
|
470
|
+
downloads: [],
|
|
471
|
+
enabled: true,
|
|
472
|
+
error: null,
|
|
473
|
+
name: "Test Audio Project",
|
|
474
|
+
type: "VOICE_GENERATOR",
|
|
475
|
+
};
|
|
476
|
+
// Set up MSW handler
|
|
477
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
478
|
+
return msw_1.HttpResponse.json(mockResponse);
|
|
479
|
+
}));
|
|
480
|
+
const getMock = jest.spyOn(client.v1.audioProjects, "get");
|
|
481
|
+
yield client.v1.audioProjects.checkResult({ id: "audio-123" }, {
|
|
482
|
+
waitForCompletion: false,
|
|
483
|
+
timeout: 30000,
|
|
484
|
+
});
|
|
485
|
+
expect(getMock).toHaveBeenCalledWith({ id: "audio-123" }, {
|
|
486
|
+
timeout: 30000,
|
|
487
|
+
});
|
|
488
|
+
}));
|
|
489
|
+
});
|
|
490
|
+
describe("Edge cases", () => {
|
|
491
|
+
test("should handle immediate completion without polling", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
492
|
+
const completeResponse = {
|
|
493
|
+
id: "audio-123",
|
|
494
|
+
status: "complete",
|
|
495
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
496
|
+
credits_charged: 2,
|
|
497
|
+
downloads: [],
|
|
498
|
+
enabled: true,
|
|
499
|
+
error: null,
|
|
500
|
+
name: "Test Audio Project",
|
|
501
|
+
type: "VOICE_GENERATOR",
|
|
502
|
+
};
|
|
503
|
+
// Set up MSW handler
|
|
504
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
505
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
506
|
+
}));
|
|
507
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, { waitForCompletion: false });
|
|
508
|
+
expect(result).toEqual({
|
|
509
|
+
id: "audio-123",
|
|
510
|
+
status: "complete",
|
|
511
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
512
|
+
creditsCharged: 2,
|
|
513
|
+
downloads: [],
|
|
514
|
+
enabled: true,
|
|
515
|
+
error: null,
|
|
516
|
+
name: "Test Audio Project",
|
|
517
|
+
type: "VOICE_GENERATOR",
|
|
518
|
+
});
|
|
519
|
+
expect(sleep).not.toHaveBeenCalled();
|
|
520
|
+
expect(downloadFiles).not.toHaveBeenCalled();
|
|
521
|
+
}));
|
|
522
|
+
test("should handle empty downloads array", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
523
|
+
const completeResponse = {
|
|
524
|
+
id: "audio-123",
|
|
525
|
+
status: "complete",
|
|
526
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
527
|
+
credits_charged: 2,
|
|
528
|
+
downloads: [],
|
|
529
|
+
enabled: true,
|
|
530
|
+
error: null,
|
|
531
|
+
name: "Test Audio Project",
|
|
532
|
+
type: "VOICE_GENERATOR",
|
|
533
|
+
};
|
|
534
|
+
// Set up MSW handler
|
|
535
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
536
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
537
|
+
}));
|
|
538
|
+
downloadFiles.mockResolvedValue([]);
|
|
539
|
+
const result = yield client.v1.audioProjects.checkResult({ id: "audio-123" }, { downloadOutputs: true });
|
|
540
|
+
expect(result.downloadedPaths).toEqual([]);
|
|
541
|
+
expect(downloadFiles).toHaveBeenCalledWith([], undefined);
|
|
542
|
+
}));
|
|
543
|
+
test("should handle get method throwing error", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
544
|
+
const error = new Error("API Error");
|
|
545
|
+
// Set up MSW handler that returns an error
|
|
546
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/audio-projects/audio-123", () => {
|
|
547
|
+
return msw_1.HttpResponse.json({ error: "API Error" }, { status: 500 });
|
|
548
|
+
}));
|
|
549
|
+
yield expect(client.v1.audioProjects.checkResult({ id: "audio-123" })).rejects.toThrow();
|
|
550
|
+
}));
|
|
551
|
+
});
|
|
552
|
+
});
|