magic-hour 0.36.0 → 0.37.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 +202 -10
- package/dist/helpers/download.d.ts +5 -0
- package/dist/helpers/download.js +88 -0
- package/dist/helpers/generate-type.d.ts +51 -0
- package/dist/helpers/generate-type.js +2 -0
- package/dist/helpers/sleep.d.ts +4 -0
- package/dist/helpers/sleep.js +9 -0
- package/dist/logger.d.ts +48 -0
- package/dist/logger.js +97 -0
- package/dist/resources/v1/ai-clothes-changer/resource-client.d.ts +50 -0
- package/dist/resources/v1/ai-clothes-changer/resource-client.js +75 -0
- package/dist/resources/v1/ai-face-editor/resource-client.d.ts +55 -0
- package/dist/resources/v1/ai-face-editor/resource-client.js +85 -0
- package/dist/resources/v1/ai-gif-generator/resource-client.d.ts +29 -0
- package/dist/resources/v1/ai-gif-generator/resource-client.js +60 -0
- package/dist/resources/v1/ai-headshot-generator/resource-client.d.ts +38 -0
- package/dist/resources/v1/ai-headshot-generator/resource-client.js +68 -0
- package/dist/resources/v1/ai-image-editor/resource-client.d.ts +39 -0
- package/dist/resources/v1/ai-image-editor/resource-client.js +69 -0
- package/dist/resources/v1/ai-image-generator/resource-client.d.ts +31 -0
- package/dist/resources/v1/ai-image-generator/resource-client.js +62 -0
- package/dist/resources/v1/ai-image-upscaler/resource-client.d.ts +40 -0
- package/dist/resources/v1/ai-image-upscaler/resource-client.js +70 -0
- package/dist/resources/v1/ai-meme-generator/resource-client.d.ts +33 -0
- package/dist/resources/v1/ai-meme-generator/resource-client.js +64 -0
- package/dist/resources/v1/ai-photo-editor/resource-client.d.ts +49 -0
- package/dist/resources/v1/ai-photo-editor/resource-client.js +79 -0
- package/dist/resources/v1/ai-qr-code-generator/resource-client.d.ts +30 -0
- package/dist/resources/v1/ai-qr-code-generator/resource-client.js +61 -0
- package/dist/resources/v1/ai-talking-photo/resource-client.d.ts +51 -0
- package/dist/resources/v1/ai-talking-photo/resource-client.js +76 -0
- package/dist/resources/v1/animation/resource-client.d.ts +61 -0
- package/dist/resources/v1/animation/resource-client.js +98 -0
- package/dist/resources/v1/auto-subtitle-generator/resource-client.d.ts +41 -0
- package/dist/resources/v1/auto-subtitle-generator/resource-client.js +71 -0
- package/dist/resources/v1/face-detection/resource-client.d.ts +30 -0
- package/dist/resources/v1/face-detection/resource-client.js +75 -0
- package/dist/resources/v1/face-swap/resource-client.d.ts +77 -0
- package/dist/resources/v1/face-swap/resource-client.js +125 -0
- package/dist/resources/v1/face-swap-photo/resource-client.d.ts +73 -0
- package/dist/resources/v1/face-swap-photo/resource-client.js +117 -0
- package/dist/resources/v1/files/resource-client.d.ts +40 -0
- package/dist/resources/v1/files/resource-client.js +236 -0
- package/dist/resources/v1/files/resource-client.test.d.ts +1 -0
- package/dist/resources/v1/files/resource-client.test.js +422 -0
- package/dist/resources/v1/image-background-remover/resource-client.d.ts +49 -0
- package/dist/resources/v1/image-background-remover/resource-client.js +82 -0
- package/dist/resources/v1/image-projects/resource-client.d.ts +22 -0
- package/dist/resources/v1/image-projects/resource-client.js +68 -0
- package/dist/resources/v1/image-projects/resource-client.test.d.ts +1 -0
- package/dist/resources/v1/image-projects/resource-client.test.js +538 -0
- package/dist/resources/v1/image-to-video/resource-client.d.ts +40 -0
- package/dist/resources/v1/image-to-video/resource-client.js +70 -0
- package/dist/resources/v1/lip-sync/resource-client.d.ts +53 -0
- package/dist/resources/v1/lip-sync/resource-client.js +84 -0
- package/dist/resources/v1/photo-colorizer/resource-client.d.ts +38 -0
- package/dist/resources/v1/photo-colorizer/resource-client.js +68 -0
- package/dist/resources/v1/text-to-video/resource-client.d.ts +32 -0
- package/dist/resources/v1/text-to-video/resource-client.js +63 -0
- package/dist/resources/v1/video-projects/resource-client.d.ts +22 -0
- package/dist/resources/v1/video-projects/resource-client.js +68 -0
- package/dist/resources/v1/video-projects/resource-client.test.d.ts +1 -0
- package/dist/resources/v1/video-projects/resource-client.test.js +713 -0
- package/dist/resources/v1/video-to-video/resource-client.d.ts +48 -0
- package/dist/resources/v1/video-to-video/resource-client.js +84 -0
- package/package.json +15 -11
|
@@ -0,0 +1,538 @@
|
|
|
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("ImageProjectsClient.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
|
+
});
|
|
49
|
+
describe("Basic functionality", () => {
|
|
50
|
+
test("should return response without waiting when waitForCompletion is false", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
+
const mockResponse = {
|
|
52
|
+
id: "project-123",
|
|
53
|
+
status: "rendering",
|
|
54
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
55
|
+
credits_charged: 10,
|
|
56
|
+
downloads: [],
|
|
57
|
+
enabled: true,
|
|
58
|
+
error: null,
|
|
59
|
+
image_count: 1,
|
|
60
|
+
name: "Test Project",
|
|
61
|
+
total_frame_cost: 10,
|
|
62
|
+
type: "AI_IMAGE",
|
|
63
|
+
};
|
|
64
|
+
// Set up MSW handler for the API call
|
|
65
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/image-projects/project-123", () => {
|
|
66
|
+
return msw_1.HttpResponse.json(mockResponse);
|
|
67
|
+
}));
|
|
68
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, { waitForCompletion: false });
|
|
69
|
+
expect(result).toEqual({
|
|
70
|
+
id: "project-123",
|
|
71
|
+
status: "rendering",
|
|
72
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
73
|
+
creditsCharged: 10,
|
|
74
|
+
downloads: [],
|
|
75
|
+
enabled: true,
|
|
76
|
+
error: null,
|
|
77
|
+
imageCount: 1,
|
|
78
|
+
name: "Test Project",
|
|
79
|
+
totalFrameCost: 10,
|
|
80
|
+
type: "AI_IMAGE",
|
|
81
|
+
});
|
|
82
|
+
expect(sleep).not.toHaveBeenCalled();
|
|
83
|
+
}));
|
|
84
|
+
test("should wait for completion and return successful result", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
|
+
const renderingResponse = {
|
|
86
|
+
id: "project-123",
|
|
87
|
+
status: "rendering",
|
|
88
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
89
|
+
credits_charged: 10,
|
|
90
|
+
downloads: [],
|
|
91
|
+
enabled: true,
|
|
92
|
+
error: null,
|
|
93
|
+
image_count: 1,
|
|
94
|
+
name: "Test Project",
|
|
95
|
+
total_frame_cost: 10,
|
|
96
|
+
type: "AI_IMAGE",
|
|
97
|
+
};
|
|
98
|
+
const completeResponse = {
|
|
99
|
+
id: "project-123",
|
|
100
|
+
status: "complete",
|
|
101
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
102
|
+
credits_charged: 10,
|
|
103
|
+
downloads: [
|
|
104
|
+
{
|
|
105
|
+
url: "https://example.com/download/image1.jpg",
|
|
106
|
+
expires_at: "2024-01-01T01:00:00Z",
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
enabled: true,
|
|
110
|
+
error: null,
|
|
111
|
+
image_count: 1,
|
|
112
|
+
name: "Test Project",
|
|
113
|
+
total_frame_cost: 10,
|
|
114
|
+
type: "AI_IMAGE",
|
|
115
|
+
};
|
|
116
|
+
let callCount = 0;
|
|
117
|
+
// Set up MSW handler that returns different responses on each call
|
|
118
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/image-projects/project-123", () => {
|
|
119
|
+
callCount++;
|
|
120
|
+
if (callCount === 1) {
|
|
121
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
125
|
+
}
|
|
126
|
+
}));
|
|
127
|
+
downloadFiles.mockResolvedValue(["/tmp/image1.jpg"]);
|
|
128
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, {
|
|
129
|
+
waitForCompletion: true,
|
|
130
|
+
downloadOutputs: true,
|
|
131
|
+
downloadDirectory: "/tmp",
|
|
132
|
+
});
|
|
133
|
+
expect(result).toEqual({
|
|
134
|
+
id: "project-123",
|
|
135
|
+
status: "complete",
|
|
136
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
137
|
+
creditsCharged: 10,
|
|
138
|
+
downloads: [
|
|
139
|
+
{
|
|
140
|
+
url: "https://example.com/download/image1.jpg",
|
|
141
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
enabled: true,
|
|
145
|
+
error: null,
|
|
146
|
+
imageCount: 1,
|
|
147
|
+
name: "Test Project",
|
|
148
|
+
totalFrameCost: 10,
|
|
149
|
+
type: "AI_IMAGE",
|
|
150
|
+
downloadedPaths: ["/tmp/image1.jpg"],
|
|
151
|
+
});
|
|
152
|
+
expect(sleep).toHaveBeenCalledWith(500); // Default poll interval
|
|
153
|
+
expect(downloadFiles).toHaveBeenCalledWith([
|
|
154
|
+
{
|
|
155
|
+
url: "https://example.com/download/image1.jpg",
|
|
156
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
157
|
+
},
|
|
158
|
+
], "/tmp");
|
|
159
|
+
}));
|
|
160
|
+
test("should handle error status during polling", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
const renderingResponse = {
|
|
162
|
+
id: "project-123",
|
|
163
|
+
status: "rendering",
|
|
164
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
165
|
+
credits_charged: 10,
|
|
166
|
+
downloads: [],
|
|
167
|
+
enabled: true,
|
|
168
|
+
error: null,
|
|
169
|
+
image_count: 1,
|
|
170
|
+
name: "Test Project",
|
|
171
|
+
total_frame_cost: 10,
|
|
172
|
+
type: "AI_IMAGE",
|
|
173
|
+
};
|
|
174
|
+
const errorResponse = {
|
|
175
|
+
id: "project-123",
|
|
176
|
+
status: "error",
|
|
177
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
178
|
+
credits_charged: 10,
|
|
179
|
+
downloads: [],
|
|
180
|
+
enabled: true,
|
|
181
|
+
error: { message: "Rendering failed", code: "ERROR_001" },
|
|
182
|
+
image_count: 1,
|
|
183
|
+
name: "Test Project",
|
|
184
|
+
total_frame_cost: 10,
|
|
185
|
+
type: "AI_IMAGE",
|
|
186
|
+
};
|
|
187
|
+
let callCount = 0;
|
|
188
|
+
// Set up MSW handler that returns different responses on each call
|
|
189
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/image-projects/project-123", () => {
|
|
190
|
+
callCount++;
|
|
191
|
+
if (callCount === 1) {
|
|
192
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
return msw_1.HttpResponse.json(errorResponse);
|
|
196
|
+
}
|
|
197
|
+
}));
|
|
198
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, { waitForCompletion: true });
|
|
199
|
+
expect(result).toEqual({
|
|
200
|
+
id: "project-123",
|
|
201
|
+
status: "error",
|
|
202
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
203
|
+
creditsCharged: 10,
|
|
204
|
+
downloads: [],
|
|
205
|
+
enabled: true,
|
|
206
|
+
error: { message: "Rendering failed", code: "ERROR_001" },
|
|
207
|
+
imageCount: 1,
|
|
208
|
+
name: "Test Project",
|
|
209
|
+
totalFrameCost: 10,
|
|
210
|
+
type: "AI_IMAGE",
|
|
211
|
+
});
|
|
212
|
+
}));
|
|
213
|
+
test("should handle canceled status during polling", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
214
|
+
const renderingResponse = {
|
|
215
|
+
id: "project-123",
|
|
216
|
+
status: "rendering",
|
|
217
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
218
|
+
credits_charged: 10,
|
|
219
|
+
downloads: [],
|
|
220
|
+
enabled: true,
|
|
221
|
+
error: null,
|
|
222
|
+
image_count: 1,
|
|
223
|
+
name: "Test Project",
|
|
224
|
+
total_frame_cost: 10,
|
|
225
|
+
type: "AI_IMAGE",
|
|
226
|
+
};
|
|
227
|
+
const canceledResponse = {
|
|
228
|
+
id: "project-123",
|
|
229
|
+
status: "canceled",
|
|
230
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
231
|
+
credits_charged: 10,
|
|
232
|
+
downloads: [],
|
|
233
|
+
enabled: true,
|
|
234
|
+
error: null,
|
|
235
|
+
image_count: 1,
|
|
236
|
+
name: "Test Project",
|
|
237
|
+
total_frame_cost: 10,
|
|
238
|
+
type: "AI_IMAGE",
|
|
239
|
+
};
|
|
240
|
+
let callCount = 0;
|
|
241
|
+
// Set up MSW handler that returns different responses on each call
|
|
242
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/image-projects/project-123", () => {
|
|
243
|
+
callCount++;
|
|
244
|
+
if (callCount === 1) {
|
|
245
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
return msw_1.HttpResponse.json(canceledResponse);
|
|
249
|
+
}
|
|
250
|
+
}));
|
|
251
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, { waitForCompletion: true });
|
|
252
|
+
expect(result).toEqual({
|
|
253
|
+
id: "project-123",
|
|
254
|
+
status: "canceled",
|
|
255
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
256
|
+
creditsCharged: 10,
|
|
257
|
+
downloads: [],
|
|
258
|
+
enabled: true,
|
|
259
|
+
error: null,
|
|
260
|
+
imageCount: 1,
|
|
261
|
+
name: "Test Project",
|
|
262
|
+
totalFrameCost: 10,
|
|
263
|
+
type: "AI_IMAGE",
|
|
264
|
+
});
|
|
265
|
+
}));
|
|
266
|
+
});
|
|
267
|
+
describe("Polling behavior", () => {
|
|
268
|
+
test("should use custom poll interval from environment", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
269
|
+
const originalEnv = process.env["MAGIC_HOUR_POLL_INTERVAL"];
|
|
270
|
+
const renderingResponse = {
|
|
271
|
+
id: "project-123",
|
|
272
|
+
status: "rendering",
|
|
273
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
274
|
+
credits_charged: 10,
|
|
275
|
+
downloads: [],
|
|
276
|
+
enabled: true,
|
|
277
|
+
error: null,
|
|
278
|
+
image_count: 1,
|
|
279
|
+
name: "Test Project",
|
|
280
|
+
total_frame_cost: 10,
|
|
281
|
+
type: "AI_IMAGE",
|
|
282
|
+
};
|
|
283
|
+
const completeResponse = {
|
|
284
|
+
id: "project-123",
|
|
285
|
+
status: "complete",
|
|
286
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
287
|
+
credits_charged: 10,
|
|
288
|
+
downloads: [],
|
|
289
|
+
enabled: true,
|
|
290
|
+
error: null,
|
|
291
|
+
image_count: 1,
|
|
292
|
+
name: "Test Project",
|
|
293
|
+
total_frame_cost: 10,
|
|
294
|
+
type: "AI_IMAGE",
|
|
295
|
+
};
|
|
296
|
+
let callCount = 0;
|
|
297
|
+
// Set up MSW handler that returns different responses on each call
|
|
298
|
+
server.use(msw_1.http.get("https://api.magichour.ai/v1/image-projects/project-123", () => {
|
|
299
|
+
callCount++;
|
|
300
|
+
if (callCount === 1) {
|
|
301
|
+
return msw_1.HttpResponse.json(renderingResponse);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
return msw_1.HttpResponse.json(completeResponse);
|
|
305
|
+
}
|
|
306
|
+
}));
|
|
307
|
+
// Set environment variable before the checkResult call
|
|
308
|
+
process.env["MAGIC_HOUR_POLL_INTERVAL"] = "1.5";
|
|
309
|
+
yield client.v1.imageProjects.checkResult({ id: "project-123" });
|
|
310
|
+
expect(sleep).toHaveBeenCalledWith(1500); // 1.5 seconds converted to milliseconds
|
|
311
|
+
// Restore environment variable
|
|
312
|
+
if (originalEnv !== undefined) {
|
|
313
|
+
process.env["MAGIC_HOUR_POLL_INTERVAL"] = originalEnv;
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
delete process.env["MAGIC_HOUR_POLL_INTERVAL"];
|
|
317
|
+
}
|
|
318
|
+
}));
|
|
319
|
+
test("should poll multiple times until completion", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
320
|
+
const renderingResponse = {
|
|
321
|
+
id: "project-123",
|
|
322
|
+
status: "rendering",
|
|
323
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
324
|
+
creditsCharged: 10,
|
|
325
|
+
downloads: [],
|
|
326
|
+
enabled: true,
|
|
327
|
+
error: null,
|
|
328
|
+
imageCount: 1,
|
|
329
|
+
name: "Test Project",
|
|
330
|
+
totalFrameCost: 10,
|
|
331
|
+
type: "AI_IMAGE",
|
|
332
|
+
};
|
|
333
|
+
const queuedResponse = Object.assign(Object.assign({}, renderingResponse), { status: "queued" });
|
|
334
|
+
const completeResponse = Object.assign(Object.assign({}, renderingResponse), { status: "complete" });
|
|
335
|
+
const getMock = jest.spyOn(client.v1.imageProjects, "get");
|
|
336
|
+
getMock
|
|
337
|
+
.mockResolvedValueOnce(renderingResponse)
|
|
338
|
+
.mockResolvedValueOnce(queuedResponse)
|
|
339
|
+
.mockResolvedValueOnce(completeResponse);
|
|
340
|
+
yield client.v1.imageProjects.checkResult({ id: "project-123" });
|
|
341
|
+
expect(sleep).toHaveBeenCalledTimes(2);
|
|
342
|
+
expect(client.v1.imageProjects.get).toHaveBeenCalledTimes(3);
|
|
343
|
+
}));
|
|
344
|
+
});
|
|
345
|
+
describe("Download functionality", () => {
|
|
346
|
+
test("should skip download when downloadOutputs is false", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
347
|
+
const completeResponse = {
|
|
348
|
+
id: "project-123",
|
|
349
|
+
status: "complete",
|
|
350
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
351
|
+
creditsCharged: 10,
|
|
352
|
+
downloads: [
|
|
353
|
+
{
|
|
354
|
+
url: "https://example.com/download/image1.jpg",
|
|
355
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
356
|
+
},
|
|
357
|
+
],
|
|
358
|
+
enabled: true,
|
|
359
|
+
error: null,
|
|
360
|
+
imageCount: 1,
|
|
361
|
+
name: "Test Project",
|
|
362
|
+
totalFrameCost: 10,
|
|
363
|
+
type: "AI_IMAGE",
|
|
364
|
+
};
|
|
365
|
+
jest
|
|
366
|
+
.spyOn(client.v1.imageProjects, "get")
|
|
367
|
+
.mockResolvedValue(completeResponse);
|
|
368
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, {
|
|
369
|
+
waitForCompletion: true,
|
|
370
|
+
downloadOutputs: false,
|
|
371
|
+
});
|
|
372
|
+
expect(result).toEqual(completeResponse);
|
|
373
|
+
expect(downloadFiles).not.toHaveBeenCalled();
|
|
374
|
+
}));
|
|
375
|
+
test("should download files to current directory when no downloadDirectory specified", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
376
|
+
const completeResponse = {
|
|
377
|
+
id: "project-123",
|
|
378
|
+
status: "complete",
|
|
379
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
380
|
+
creditsCharged: 10,
|
|
381
|
+
downloads: [
|
|
382
|
+
{
|
|
383
|
+
url: "https://example.com/download/image1.jpg",
|
|
384
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
385
|
+
},
|
|
386
|
+
],
|
|
387
|
+
enabled: true,
|
|
388
|
+
error: null,
|
|
389
|
+
imageCount: 1,
|
|
390
|
+
name: "Test Project",
|
|
391
|
+
totalFrameCost: 10,
|
|
392
|
+
type: "AI_IMAGE",
|
|
393
|
+
};
|
|
394
|
+
jest
|
|
395
|
+
.spyOn(client.v1.imageProjects, "get")
|
|
396
|
+
.mockResolvedValue(completeResponse);
|
|
397
|
+
downloadFiles.mockResolvedValue(["/current/dir/image1.jpg"]);
|
|
398
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, { downloadOutputs: true });
|
|
399
|
+
expect(downloadFiles).toHaveBeenCalledWith(completeResponse.downloads, undefined);
|
|
400
|
+
expect(result.downloadedPaths).toEqual(["/current/dir/image1.jpg"]);
|
|
401
|
+
}));
|
|
402
|
+
test("should handle download errors gracefully", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
403
|
+
const completeResponse = {
|
|
404
|
+
id: "project-123",
|
|
405
|
+
status: "complete",
|
|
406
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
407
|
+
creditsCharged: 10,
|
|
408
|
+
downloads: [
|
|
409
|
+
{
|
|
410
|
+
url: "https://example.com/download/image1.jpg",
|
|
411
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
412
|
+
},
|
|
413
|
+
],
|
|
414
|
+
enabled: true,
|
|
415
|
+
error: null,
|
|
416
|
+
imageCount: 1,
|
|
417
|
+
name: "Test Project",
|
|
418
|
+
totalFrameCost: 10,
|
|
419
|
+
type: "AI_IMAGE",
|
|
420
|
+
};
|
|
421
|
+
jest
|
|
422
|
+
.spyOn(client.v1.imageProjects, "get")
|
|
423
|
+
.mockResolvedValue(completeResponse);
|
|
424
|
+
downloadFiles.mockRejectedValue(new Error("Download failed"));
|
|
425
|
+
yield expect(client.v1.imageProjects.checkResult({ id: "project-123" }, { downloadOutputs: true })).rejects.toThrow("Download failed");
|
|
426
|
+
}));
|
|
427
|
+
});
|
|
428
|
+
describe("Default behavior", () => {
|
|
429
|
+
test("should use default options when no options provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
430
|
+
const completeResponse = {
|
|
431
|
+
id: "project-123",
|
|
432
|
+
status: "complete",
|
|
433
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
434
|
+
creditsCharged: 10,
|
|
435
|
+
downloads: [
|
|
436
|
+
{
|
|
437
|
+
url: "https://example.com/download/image1.jpg",
|
|
438
|
+
expiresAt: "2024-01-01T01:00:00Z",
|
|
439
|
+
},
|
|
440
|
+
],
|
|
441
|
+
enabled: true,
|
|
442
|
+
error: null,
|
|
443
|
+
imageCount: 1,
|
|
444
|
+
name: "Test Project",
|
|
445
|
+
totalFrameCost: 10,
|
|
446
|
+
type: "AI_IMAGE",
|
|
447
|
+
};
|
|
448
|
+
const renderingResponse = Object.assign(Object.assign({}, completeResponse), { status: "rendering" });
|
|
449
|
+
const getMock = jest.spyOn(client.v1.imageProjects, "get");
|
|
450
|
+
getMock
|
|
451
|
+
.mockResolvedValueOnce(renderingResponse)
|
|
452
|
+
.mockResolvedValueOnce(completeResponse);
|
|
453
|
+
downloadFiles.mockResolvedValue(["/tmp/image1.jpg"]);
|
|
454
|
+
const result = yield client.v1.imageProjects.checkResult({
|
|
455
|
+
id: "project-123",
|
|
456
|
+
});
|
|
457
|
+
expect(result.downloadedPaths).toEqual(["/tmp/image1.jpg"]);
|
|
458
|
+
expect(sleep).toHaveBeenCalledWith(500); // Default poll interval
|
|
459
|
+
}));
|
|
460
|
+
});
|
|
461
|
+
describe("Request options", () => {
|
|
462
|
+
test("should pass additional request options to get method", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
463
|
+
const mockResponse = {
|
|
464
|
+
id: "project-123",
|
|
465
|
+
status: "complete",
|
|
466
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
467
|
+
creditsCharged: 10,
|
|
468
|
+
downloads: [],
|
|
469
|
+
enabled: true,
|
|
470
|
+
error: null,
|
|
471
|
+
imageCount: 1,
|
|
472
|
+
name: "Test Project",
|
|
473
|
+
totalFrameCost: 10,
|
|
474
|
+
type: "AI_IMAGE",
|
|
475
|
+
};
|
|
476
|
+
const getMock = jest.spyOn(client.v1.imageProjects, "get");
|
|
477
|
+
getMock.mockResolvedValue(mockResponse);
|
|
478
|
+
yield client.v1.imageProjects.checkResult({ id: "project-123" }, {
|
|
479
|
+
waitForCompletion: false,
|
|
480
|
+
timeout: 30000,
|
|
481
|
+
});
|
|
482
|
+
expect(getMock).toHaveBeenCalledWith({ id: "project-123" }, {
|
|
483
|
+
timeout: 30000,
|
|
484
|
+
});
|
|
485
|
+
}));
|
|
486
|
+
});
|
|
487
|
+
describe("Edge cases", () => {
|
|
488
|
+
test("should handle immediate completion without polling", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
489
|
+
const completeResponse = {
|
|
490
|
+
id: "project-123",
|
|
491
|
+
status: "complete",
|
|
492
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
493
|
+
creditsCharged: 10,
|
|
494
|
+
downloads: [],
|
|
495
|
+
enabled: true,
|
|
496
|
+
error: null,
|
|
497
|
+
imageCount: 1,
|
|
498
|
+
name: "Test Project",
|
|
499
|
+
totalFrameCost: 10,
|
|
500
|
+
type: "AI_IMAGE",
|
|
501
|
+
};
|
|
502
|
+
jest
|
|
503
|
+
.spyOn(client.v1.imageProjects, "get")
|
|
504
|
+
.mockResolvedValue(completeResponse);
|
|
505
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, { waitForCompletion: false });
|
|
506
|
+
expect(result).toEqual(completeResponse);
|
|
507
|
+
expect(sleep).not.toHaveBeenCalled();
|
|
508
|
+
expect(downloadFiles).not.toHaveBeenCalled();
|
|
509
|
+
}));
|
|
510
|
+
test("should handle empty downloads array", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
511
|
+
const completeResponse = {
|
|
512
|
+
id: "project-123",
|
|
513
|
+
status: "complete",
|
|
514
|
+
createdAt: "2024-01-01T00:00:00Z",
|
|
515
|
+
creditsCharged: 10,
|
|
516
|
+
downloads: [],
|
|
517
|
+
enabled: true,
|
|
518
|
+
error: null,
|
|
519
|
+
imageCount: 1,
|
|
520
|
+
name: "Test Project",
|
|
521
|
+
totalFrameCost: 10,
|
|
522
|
+
type: "AI_IMAGE",
|
|
523
|
+
};
|
|
524
|
+
jest
|
|
525
|
+
.spyOn(client.v1.imageProjects, "get")
|
|
526
|
+
.mockResolvedValue(completeResponse);
|
|
527
|
+
downloadFiles.mockResolvedValue([]);
|
|
528
|
+
const result = yield client.v1.imageProjects.checkResult({ id: "project-123" }, { downloadOutputs: true });
|
|
529
|
+
expect(result.downloadedPaths).toEqual([]);
|
|
530
|
+
expect(downloadFiles).toHaveBeenCalledWith([], undefined);
|
|
531
|
+
}));
|
|
532
|
+
test("should handle get method throwing error", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
533
|
+
const error = new Error("API Error");
|
|
534
|
+
jest.spyOn(client.v1.imageProjects, "get").mockRejectedValue(error);
|
|
535
|
+
yield expect(client.v1.imageProjects.checkResult({ id: "project-123" })).rejects.toThrow("API Error");
|
|
536
|
+
}));
|
|
537
|
+
});
|
|
538
|
+
});
|
|
@@ -1,8 +1,47 @@
|
|
|
1
1
|
import { types } from "../../../index.js";
|
|
2
2
|
import { ApiPromise, CoreClient, CoreResourceClient, RequestOptions, ResourceClientOptions } from "../../../core";
|
|
3
|
+
import { GenerateOptions, GenerateRequestType } from "../../../helpers/generate-type";
|
|
3
4
|
import * as requests from "../../../resources/v1/image-to-video/request-types";
|
|
5
|
+
type GenerateRequest = GenerateRequestType<requests.CreateRequest, {
|
|
6
|
+
/**
|
|
7
|
+
* The path of the image file. This value is either
|
|
8
|
+
* - a direct URL to the image file
|
|
9
|
+
* - a path to a local file
|
|
10
|
+
*
|
|
11
|
+
* Note: if the path begins with `api-assets`, it will be assumed to already be uploaded to Magic Hour's storage, and will not be uploaded again.
|
|
12
|
+
*/
|
|
13
|
+
imageFilePath: string;
|
|
14
|
+
}>;
|
|
4
15
|
export declare class ImageToVideoClient extends CoreResourceClient {
|
|
5
16
|
constructor(coreClient: CoreClient, opts: ResourceClientOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Image-to-Video
|
|
19
|
+
*
|
|
20
|
+
* Create a Image To Video video
|
|
21
|
+
*
|
|
22
|
+
* This method provides a convenient way to create a request and automatically wait for completion and download outputs.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import Client from "../../../index.js";
|
|
27
|
+
*
|
|
28
|
+
* const client = new Client({ token: process.env["API_TOKEN"]!! });
|
|
29
|
+
* const res = await client.v1.imageToVideo.generate(
|
|
30
|
+
* {
|
|
31
|
+
* assets: { imageFilePath: "/path/to/1234.png" },
|
|
32
|
+
* endSeconds: 5.0,
|
|
33
|
+
* name: "Image To Video video",
|
|
34
|
+
* resolution: "720p",
|
|
35
|
+
* },
|
|
36
|
+
* {
|
|
37
|
+
* waitForCompletion: true,
|
|
38
|
+
* downloadOutputs: true,
|
|
39
|
+
* downloadDirectory: "outputs",
|
|
40
|
+
* },
|
|
41
|
+
* );
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
generate(request: GenerateRequest, opts?: GenerateOptions): Promise<import("../video-projects/resource-client").V1VideoProjectsGetResponseWithDownloads>;
|
|
6
45
|
/**
|
|
7
46
|
* Image-to-Video
|
|
8
47
|
*
|
|
@@ -15,3 +54,4 @@ export declare class ImageToVideoClient extends CoreResourceClient {
|
|
|
15
54
|
*/
|
|
16
55
|
create(request: requests.CreateRequest, opts?: RequestOptions): ApiPromise<types.V1ImageToVideoCreateResponse>;
|
|
17
56
|
}
|
|
57
|
+
export {};
|
|
@@ -1,13 +1,83 @@
|
|
|
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.ImageToVideoClient = void 0;
|
|
4
24
|
const core_1 = require("../../../core");
|
|
25
|
+
const files_1 = require("../../../resources/v1/files");
|
|
5
26
|
const v1_image_to_video_create_body_1 = require("../../../types/v1-image-to-video-create-body");
|
|
6
27
|
const v1_image_to_video_create_response_1 = require("../../../types/v1-image-to-video-create-response");
|
|
28
|
+
const logger_1 = require("../../../logger");
|
|
29
|
+
const video_projects_1 = require("../../../resources/v1/video-projects");
|
|
7
30
|
class ImageToVideoClient extends core_1.CoreResourceClient {
|
|
8
31
|
constructor(coreClient, opts) {
|
|
9
32
|
super(coreClient, opts);
|
|
10
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Image-to-Video
|
|
36
|
+
*
|
|
37
|
+
* Create a Image To Video video
|
|
38
|
+
*
|
|
39
|
+
* This method provides a convenient way to create a request and automatically wait for completion and download outputs.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* import Client from "../../../index.js";
|
|
44
|
+
*
|
|
45
|
+
* const client = new Client({ token: process.env["API_TOKEN"]!! });
|
|
46
|
+
* const res = await client.v1.imageToVideo.generate(
|
|
47
|
+
* {
|
|
48
|
+
* assets: { imageFilePath: "/path/to/1234.png" },
|
|
49
|
+
* endSeconds: 5.0,
|
|
50
|
+
* name: "Image To Video video",
|
|
51
|
+
* resolution: "720p",
|
|
52
|
+
* },
|
|
53
|
+
* {
|
|
54
|
+
* waitForCompletion: true,
|
|
55
|
+
* downloadOutputs: true,
|
|
56
|
+
* downloadDirectory: "outputs",
|
|
57
|
+
* },
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
generate(request_1) {
|
|
62
|
+
return __awaiter(this, arguments, void 0, function* (request, opts = {}) {
|
|
63
|
+
const { waitForCompletion = true, downloadOutputs = true, downloadDirectory = undefined } = opts, createOpts = __rest(opts, ["waitForCompletion", "downloadOutputs", "downloadDirectory"]);
|
|
64
|
+
const fileClient = new files_1.FilesClient(this._client, this._opts);
|
|
65
|
+
const _a = request.assets, { imageFilePath } = _a, restAssets = __rest(_a, ["imageFilePath"]);
|
|
66
|
+
(0, logger_1.getLogger)().debug(`Uploading file ${imageFilePath} to Magic Hour's storage`);
|
|
67
|
+
const [uploadedImageFilePath] = yield Promise.all([
|
|
68
|
+
fileClient.uploadFile(imageFilePath),
|
|
69
|
+
]);
|
|
70
|
+
(0, logger_1.getLogger)().info(`Uploaded file ${imageFilePath} to Magic Hour's storage as ${uploadedImageFilePath}`);
|
|
71
|
+
const createResponse = yield this.create(Object.assign(Object.assign({}, request), { assets: Object.assign(Object.assign({}, restAssets), { imageFilePath: uploadedImageFilePath }) }), createOpts);
|
|
72
|
+
(0, logger_1.getLogger)().info(`Created ImageToVideoClient project ${createResponse.id}`);
|
|
73
|
+
const projectsClient = new video_projects_1.VideoProjectsClient(this._client, this._opts);
|
|
74
|
+
(0, logger_1.getLogger)().debug(`Checking result for ImageToVideoClient project ${createResponse.id}`);
|
|
75
|
+
const result = yield projectsClient.checkResult({ id: createResponse.id }, Object.assign({ waitForCompletion,
|
|
76
|
+
downloadOutputs,
|
|
77
|
+
downloadDirectory }, createOpts));
|
|
78
|
+
return result;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
11
81
|
/**
|
|
12
82
|
* Image-to-Video
|
|
13
83
|
*
|