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.
Files changed (66) hide show
  1. package/README.md +202 -10
  2. package/dist/helpers/download.d.ts +5 -0
  3. package/dist/helpers/download.js +88 -0
  4. package/dist/helpers/generate-type.d.ts +51 -0
  5. package/dist/helpers/generate-type.js +2 -0
  6. package/dist/helpers/sleep.d.ts +4 -0
  7. package/dist/helpers/sleep.js +9 -0
  8. package/dist/logger.d.ts +48 -0
  9. package/dist/logger.js +97 -0
  10. package/dist/resources/v1/ai-clothes-changer/resource-client.d.ts +50 -0
  11. package/dist/resources/v1/ai-clothes-changer/resource-client.js +75 -0
  12. package/dist/resources/v1/ai-face-editor/resource-client.d.ts +55 -0
  13. package/dist/resources/v1/ai-face-editor/resource-client.js +85 -0
  14. package/dist/resources/v1/ai-gif-generator/resource-client.d.ts +29 -0
  15. package/dist/resources/v1/ai-gif-generator/resource-client.js +60 -0
  16. package/dist/resources/v1/ai-headshot-generator/resource-client.d.ts +38 -0
  17. package/dist/resources/v1/ai-headshot-generator/resource-client.js +68 -0
  18. package/dist/resources/v1/ai-image-editor/resource-client.d.ts +39 -0
  19. package/dist/resources/v1/ai-image-editor/resource-client.js +69 -0
  20. package/dist/resources/v1/ai-image-generator/resource-client.d.ts +31 -0
  21. package/dist/resources/v1/ai-image-generator/resource-client.js +62 -0
  22. package/dist/resources/v1/ai-image-upscaler/resource-client.d.ts +40 -0
  23. package/dist/resources/v1/ai-image-upscaler/resource-client.js +70 -0
  24. package/dist/resources/v1/ai-meme-generator/resource-client.d.ts +33 -0
  25. package/dist/resources/v1/ai-meme-generator/resource-client.js +64 -0
  26. package/dist/resources/v1/ai-photo-editor/resource-client.d.ts +49 -0
  27. package/dist/resources/v1/ai-photo-editor/resource-client.js +79 -0
  28. package/dist/resources/v1/ai-qr-code-generator/resource-client.d.ts +30 -0
  29. package/dist/resources/v1/ai-qr-code-generator/resource-client.js +61 -0
  30. package/dist/resources/v1/ai-talking-photo/resource-client.d.ts +51 -0
  31. package/dist/resources/v1/ai-talking-photo/resource-client.js +76 -0
  32. package/dist/resources/v1/animation/resource-client.d.ts +61 -0
  33. package/dist/resources/v1/animation/resource-client.js +98 -0
  34. package/dist/resources/v1/auto-subtitle-generator/resource-client.d.ts +41 -0
  35. package/dist/resources/v1/auto-subtitle-generator/resource-client.js +71 -0
  36. package/dist/resources/v1/face-detection/resource-client.d.ts +30 -0
  37. package/dist/resources/v1/face-detection/resource-client.js +75 -0
  38. package/dist/resources/v1/face-swap/resource-client.d.ts +77 -0
  39. package/dist/resources/v1/face-swap/resource-client.js +125 -0
  40. package/dist/resources/v1/face-swap-photo/resource-client.d.ts +73 -0
  41. package/dist/resources/v1/face-swap-photo/resource-client.js +117 -0
  42. package/dist/resources/v1/files/resource-client.d.ts +40 -0
  43. package/dist/resources/v1/files/resource-client.js +236 -0
  44. package/dist/resources/v1/files/resource-client.test.d.ts +1 -0
  45. package/dist/resources/v1/files/resource-client.test.js +422 -0
  46. package/dist/resources/v1/image-background-remover/resource-client.d.ts +49 -0
  47. package/dist/resources/v1/image-background-remover/resource-client.js +82 -0
  48. package/dist/resources/v1/image-projects/resource-client.d.ts +22 -0
  49. package/dist/resources/v1/image-projects/resource-client.js +68 -0
  50. package/dist/resources/v1/image-projects/resource-client.test.d.ts +1 -0
  51. package/dist/resources/v1/image-projects/resource-client.test.js +538 -0
  52. package/dist/resources/v1/image-to-video/resource-client.d.ts +40 -0
  53. package/dist/resources/v1/image-to-video/resource-client.js +70 -0
  54. package/dist/resources/v1/lip-sync/resource-client.d.ts +53 -0
  55. package/dist/resources/v1/lip-sync/resource-client.js +84 -0
  56. package/dist/resources/v1/photo-colorizer/resource-client.d.ts +38 -0
  57. package/dist/resources/v1/photo-colorizer/resource-client.js +68 -0
  58. package/dist/resources/v1/text-to-video/resource-client.d.ts +32 -0
  59. package/dist/resources/v1/text-to-video/resource-client.js +63 -0
  60. package/dist/resources/v1/video-projects/resource-client.d.ts +22 -0
  61. package/dist/resources/v1/video-projects/resource-client.js +68 -0
  62. package/dist/resources/v1/video-projects/resource-client.test.d.ts +1 -0
  63. package/dist/resources/v1/video-projects/resource-client.test.js +713 -0
  64. package/dist/resources/v1/video-to-video/resource-client.d.ts +48 -0
  65. package/dist/resources/v1/video-to-video/resource-client.js +84 -0
  66. package/package.json +15 -11
@@ -0,0 +1,713 @@
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("VideoProjectsClient.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: "video-123",
55
+ status: "rendering",
56
+ created_at: "2024-01-01T00:00:00Z",
57
+ credits_charged: 15,
58
+ download: null,
59
+ downloads: [],
60
+ enabled: true,
61
+ end_seconds: 10.5,
62
+ error: null,
63
+ fps: 30,
64
+ height: 720,
65
+ name: "Test Video Project",
66
+ start_seconds: 0.0,
67
+ total_frame_cost: 15,
68
+ type: "VIDEO_TO_VIDEO",
69
+ width: 1280,
70
+ };
71
+ // Set up MSW handler for the API call
72
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
73
+ return msw_1.HttpResponse.json(mockResponse);
74
+ }));
75
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, { waitForCompletion: false });
76
+ expect(result).toEqual({
77
+ id: "video-123",
78
+ status: "rendering",
79
+ createdAt: "2024-01-01T00:00:00Z",
80
+ creditsCharged: 15,
81
+ download: null,
82
+ downloads: [],
83
+ enabled: true,
84
+ endSeconds: 10.5,
85
+ error: null,
86
+ fps: 30,
87
+ height: 720,
88
+ name: "Test Video Project",
89
+ startSeconds: 0.0,
90
+ totalFrameCost: 15,
91
+ type: "VIDEO_TO_VIDEO",
92
+ width: 1280,
93
+ });
94
+ expect(sleep).not.toHaveBeenCalled();
95
+ }));
96
+ test("should wait for completion and return successful result", () => __awaiter(void 0, void 0, void 0, function* () {
97
+ const renderingResponse = {
98
+ id: "video-123",
99
+ status: "rendering",
100
+ created_at: "2024-01-01T00:00:00Z",
101
+ credits_charged: 15,
102
+ download: null,
103
+ downloads: [],
104
+ enabled: true,
105
+ end_seconds: 10.5,
106
+ error: null,
107
+ fps: 30,
108
+ height: 720,
109
+ name: "Test Video Project",
110
+ start_seconds: 0.0,
111
+ total_frame_cost: 15,
112
+ type: "VIDEO_TO_VIDEO",
113
+ width: 1280,
114
+ };
115
+ const completeResponse = {
116
+ id: "video-123",
117
+ status: "complete",
118
+ created_at: "2024-01-01T00:00:00Z",
119
+ credits_charged: 15,
120
+ download: null,
121
+ downloads: [
122
+ {
123
+ url: "https://example.com/download/video1.mp4",
124
+ expires_at: "2024-01-01T01:00:00Z",
125
+ },
126
+ ],
127
+ enabled: true,
128
+ end_seconds: 10.5,
129
+ error: null,
130
+ fps: 30,
131
+ height: 720,
132
+ name: "Test Video Project",
133
+ start_seconds: 0.0,
134
+ total_frame_cost: 15,
135
+ type: "VIDEO_TO_VIDEO",
136
+ width: 1280,
137
+ };
138
+ let callCount = 0;
139
+ // Set up MSW handler that returns different responses on each call
140
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
141
+ callCount++;
142
+ if (callCount === 1) {
143
+ return msw_1.HttpResponse.json(renderingResponse);
144
+ }
145
+ else {
146
+ return msw_1.HttpResponse.json(completeResponse);
147
+ }
148
+ }));
149
+ downloadFiles.mockResolvedValue(["/tmp/video1.mp4"]);
150
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, {
151
+ waitForCompletion: true,
152
+ downloadOutputs: true,
153
+ downloadDirectory: "/tmp",
154
+ });
155
+ expect(result).toEqual({
156
+ id: "video-123",
157
+ status: "complete",
158
+ createdAt: "2024-01-01T00:00:00Z",
159
+ creditsCharged: 15,
160
+ download: null,
161
+ downloads: [
162
+ {
163
+ url: "https://example.com/download/video1.mp4",
164
+ expiresAt: "2024-01-01T01:00:00Z",
165
+ },
166
+ ],
167
+ enabled: true,
168
+ endSeconds: 10.5,
169
+ error: null,
170
+ fps: 30,
171
+ height: 720,
172
+ name: "Test Video Project",
173
+ startSeconds: 0.0,
174
+ totalFrameCost: 15,
175
+ type: "VIDEO_TO_VIDEO",
176
+ width: 1280,
177
+ downloadedPaths: ["/tmp/video1.mp4"],
178
+ });
179
+ expect(sleep).toHaveBeenCalledWith(500); // Default poll interval
180
+ expect(downloadFiles).toHaveBeenCalledWith([
181
+ {
182
+ url: "https://example.com/download/video1.mp4",
183
+ expiresAt: "2024-01-01T01:00:00Z",
184
+ },
185
+ ], "/tmp");
186
+ }));
187
+ test("should handle error status during polling", () => __awaiter(void 0, void 0, void 0, function* () {
188
+ const renderingResponse = {
189
+ id: "video-123",
190
+ status: "rendering",
191
+ created_at: "2024-01-01T00:00:00Z",
192
+ credits_charged: 15,
193
+ download: null,
194
+ downloads: [],
195
+ enabled: true,
196
+ end_seconds: 10.5,
197
+ error: null,
198
+ fps: 30,
199
+ height: 720,
200
+ name: "Test Video Project",
201
+ start_seconds: 0.0,
202
+ total_frame_cost: 15,
203
+ type: "VIDEO_TO_VIDEO",
204
+ width: 1280,
205
+ };
206
+ const errorResponse = {
207
+ id: "video-123",
208
+ status: "error",
209
+ created_at: "2024-01-01T00:00:00Z",
210
+ credits_charged: 15,
211
+ download: null,
212
+ downloads: [],
213
+ enabled: true,
214
+ end_seconds: 10.5,
215
+ error: { message: "Rendering failed", code: "ERROR_001" },
216
+ fps: 30,
217
+ height: 720,
218
+ name: "Test Video Project",
219
+ start_seconds: 0.0,
220
+ total_frame_cost: 15,
221
+ type: "VIDEO_TO_VIDEO",
222
+ width: 1280,
223
+ };
224
+ let callCount = 0;
225
+ // Set up MSW handler that returns different responses on each call
226
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
227
+ callCount++;
228
+ if (callCount === 1) {
229
+ return msw_1.HttpResponse.json(renderingResponse);
230
+ }
231
+ else {
232
+ return msw_1.HttpResponse.json(errorResponse);
233
+ }
234
+ }));
235
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, { waitForCompletion: true });
236
+ expect(result).toEqual({
237
+ id: "video-123",
238
+ status: "error",
239
+ createdAt: "2024-01-01T00:00:00Z",
240
+ creditsCharged: 15,
241
+ download: null,
242
+ downloads: [],
243
+ enabled: true,
244
+ endSeconds: 10.5,
245
+ error: { message: "Rendering failed", code: "ERROR_001" },
246
+ fps: 30,
247
+ height: 720,
248
+ name: "Test Video Project",
249
+ startSeconds: 0.0,
250
+ totalFrameCost: 15,
251
+ type: "VIDEO_TO_VIDEO",
252
+ width: 1280,
253
+ });
254
+ }));
255
+ test("should handle canceled status during polling", () => __awaiter(void 0, void 0, void 0, function* () {
256
+ const renderingResponse = {
257
+ id: "video-123",
258
+ status: "rendering",
259
+ created_at: "2024-01-01T00:00:00Z",
260
+ credits_charged: 15,
261
+ download: null,
262
+ downloads: [],
263
+ enabled: true,
264
+ end_seconds: 10.5,
265
+ error: null,
266
+ fps: 30,
267
+ height: 720,
268
+ name: "Test Video Project",
269
+ start_seconds: 0.0,
270
+ total_frame_cost: 15,
271
+ type: "VIDEO_TO_VIDEO",
272
+ width: 1280,
273
+ };
274
+ const canceledResponse = {
275
+ id: "video-123",
276
+ status: "canceled",
277
+ created_at: "2024-01-01T00:00:00Z",
278
+ credits_charged: 15,
279
+ download: null,
280
+ downloads: [],
281
+ enabled: true,
282
+ end_seconds: 10.5,
283
+ error: null,
284
+ fps: 30,
285
+ height: 720,
286
+ name: "Test Video Project",
287
+ start_seconds: 0.0,
288
+ total_frame_cost: 15,
289
+ type: "VIDEO_TO_VIDEO",
290
+ width: 1280,
291
+ };
292
+ let callCount = 0;
293
+ // Set up MSW handler that returns different responses on each call
294
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
295
+ callCount++;
296
+ if (callCount === 1) {
297
+ return msw_1.HttpResponse.json(renderingResponse);
298
+ }
299
+ else {
300
+ return msw_1.HttpResponse.json(canceledResponse);
301
+ }
302
+ }));
303
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, { waitForCompletion: true });
304
+ expect(result).toEqual({
305
+ id: "video-123",
306
+ status: "canceled",
307
+ createdAt: "2024-01-01T00:00:00Z",
308
+ creditsCharged: 15,
309
+ download: null,
310
+ downloads: [],
311
+ enabled: true,
312
+ endSeconds: 10.5,
313
+ error: null,
314
+ fps: 30,
315
+ height: 720,
316
+ name: "Test Video Project",
317
+ startSeconds: 0.0,
318
+ totalFrameCost: 15,
319
+ type: "VIDEO_TO_VIDEO",
320
+ width: 1280,
321
+ });
322
+ }));
323
+ });
324
+ describe("Polling behavior", () => {
325
+ test("should use custom poll interval from environment", () => __awaiter(void 0, void 0, void 0, function* () {
326
+ const renderingResponse = {
327
+ id: "video-123",
328
+ status: "rendering",
329
+ created_at: "2024-01-01T00:00:00Z",
330
+ credits_charged: 15,
331
+ download: null,
332
+ downloads: [],
333
+ enabled: true,
334
+ end_seconds: 10.5,
335
+ error: null,
336
+ fps: 30,
337
+ height: 720,
338
+ name: "Test Video Project",
339
+ start_seconds: 0.0,
340
+ total_frame_cost: 15,
341
+ type: "VIDEO_TO_VIDEO",
342
+ width: 1280,
343
+ };
344
+ const completeResponse = {
345
+ id: "video-123",
346
+ status: "complete",
347
+ created_at: "2024-01-01T00:00:00Z",
348
+ credits_charged: 15,
349
+ download: null,
350
+ downloads: [],
351
+ enabled: true,
352
+ end_seconds: 10.5,
353
+ error: null,
354
+ fps: 30,
355
+ height: 720,
356
+ name: "Test Video Project",
357
+ start_seconds: 0.0,
358
+ total_frame_cost: 15,
359
+ type: "VIDEO_TO_VIDEO",
360
+ width: 1280,
361
+ };
362
+ let callCount = 0;
363
+ // Set up MSW handler that returns different responses on each call
364
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
365
+ callCount++;
366
+ if (callCount === 1) {
367
+ return msw_1.HttpResponse.json(renderingResponse);
368
+ }
369
+ else {
370
+ return msw_1.HttpResponse.json(completeResponse);
371
+ }
372
+ }));
373
+ // Set environment variable before the checkResult call
374
+ process.env["MAGIC_HOUR_POLL_INTERVAL"] = "1.5";
375
+ try {
376
+ yield client.v1.videoProjects.checkResult({ id: "video-123" });
377
+ expect(sleep).toHaveBeenCalledWith(1500); // 1.5 seconds converted to milliseconds
378
+ }
379
+ finally {
380
+ // Clean up environment variable
381
+ delete process.env["MAGIC_HOUR_POLL_INTERVAL"];
382
+ }
383
+ }));
384
+ test("should poll multiple times until completion", () => __awaiter(void 0, void 0, void 0, function* () {
385
+ const renderingResponse = {
386
+ id: "video-123",
387
+ status: "rendering",
388
+ created_at: "2024-01-01T00:00:00Z",
389
+ credits_charged: 15,
390
+ download: null,
391
+ downloads: [],
392
+ enabled: true,
393
+ end_seconds: 10.5,
394
+ error: null,
395
+ fps: 30,
396
+ height: 720,
397
+ name: "Test Video Project",
398
+ start_seconds: 0.0,
399
+ total_frame_cost: 15,
400
+ type: "VIDEO_TO_VIDEO",
401
+ width: 1280,
402
+ };
403
+ const queuedResponse = Object.assign(Object.assign({}, renderingResponse), { status: "queued" });
404
+ const completeResponse = Object.assign(Object.assign({}, renderingResponse), { status: "complete" });
405
+ let callCount = 0;
406
+ // Set up MSW handler that returns different responses on each call
407
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
408
+ callCount++;
409
+ if (callCount === 1) {
410
+ return msw_1.HttpResponse.json(renderingResponse);
411
+ }
412
+ else if (callCount === 2) {
413
+ return msw_1.HttpResponse.json(queuedResponse);
414
+ }
415
+ else {
416
+ return msw_1.HttpResponse.json(completeResponse);
417
+ }
418
+ }));
419
+ yield client.v1.videoProjects.checkResult({ id: "video-123" });
420
+ expect(sleep).toHaveBeenCalledTimes(2);
421
+ }));
422
+ });
423
+ describe("Download functionality", () => {
424
+ test("should skip download when downloadOutputs is false", () => __awaiter(void 0, void 0, void 0, function* () {
425
+ const completeResponse = {
426
+ id: "video-123",
427
+ status: "complete",
428
+ created_at: "2024-01-01T00:00:00Z",
429
+ credits_charged: 15,
430
+ download: null,
431
+ downloads: [
432
+ {
433
+ url: "https://example.com/download/video1.mp4",
434
+ expires_at: "2024-01-01T01:00:00Z",
435
+ },
436
+ ],
437
+ enabled: true,
438
+ end_seconds: 10.5,
439
+ error: null,
440
+ fps: 30,
441
+ height: 720,
442
+ name: "Test Video Project",
443
+ start_seconds: 0.0,
444
+ total_frame_cost: 15,
445
+ type: "VIDEO_TO_VIDEO",
446
+ width: 1280,
447
+ };
448
+ // Set up MSW handler
449
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
450
+ return msw_1.HttpResponse.json(completeResponse);
451
+ }));
452
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, {
453
+ waitForCompletion: true,
454
+ downloadOutputs: false,
455
+ });
456
+ expect(result).toEqual({
457
+ id: "video-123",
458
+ status: "complete",
459
+ createdAt: "2024-01-01T00:00:00Z",
460
+ creditsCharged: 15,
461
+ download: null,
462
+ downloads: [
463
+ {
464
+ url: "https://example.com/download/video1.mp4",
465
+ expiresAt: "2024-01-01T01:00:00Z",
466
+ },
467
+ ],
468
+ enabled: true,
469
+ endSeconds: 10.5,
470
+ error: null,
471
+ fps: 30,
472
+ height: 720,
473
+ name: "Test Video Project",
474
+ startSeconds: 0.0,
475
+ totalFrameCost: 15,
476
+ type: "VIDEO_TO_VIDEO",
477
+ width: 1280,
478
+ });
479
+ expect(downloadFiles).not.toHaveBeenCalled();
480
+ }));
481
+ test("should download files to current directory when no downloadDirectory specified", () => __awaiter(void 0, void 0, void 0, function* () {
482
+ const completeResponse = {
483
+ id: "video-123",
484
+ status: "complete",
485
+ created_at: "2024-01-01T00:00:00Z",
486
+ credits_charged: 15,
487
+ download: null,
488
+ downloads: [
489
+ {
490
+ url: "https://example.com/download/video1.mp4",
491
+ expires_at: "2024-01-01T01:00:00Z",
492
+ },
493
+ ],
494
+ enabled: true,
495
+ end_seconds: 10.5,
496
+ error: null,
497
+ fps: 30,
498
+ height: 720,
499
+ name: "Test Video Project",
500
+ start_seconds: 0.0,
501
+ total_frame_cost: 15,
502
+ type: "VIDEO_TO_VIDEO",
503
+ width: 1280,
504
+ };
505
+ // Set up MSW handler
506
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
507
+ return msw_1.HttpResponse.json(completeResponse);
508
+ }));
509
+ downloadFiles.mockResolvedValue(["/current/dir/video1.mp4"]);
510
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, { downloadOutputs: true });
511
+ expect(downloadFiles).toHaveBeenCalledWith([
512
+ {
513
+ url: "https://example.com/download/video1.mp4",
514
+ expiresAt: "2024-01-01T01:00:00Z",
515
+ },
516
+ ], undefined);
517
+ expect(result.downloadedPaths).toEqual(["/current/dir/video1.mp4"]);
518
+ }));
519
+ test("should handle download errors gracefully", () => __awaiter(void 0, void 0, void 0, function* () {
520
+ const completeResponse = {
521
+ id: "video-123",
522
+ status: "complete",
523
+ created_at: "2024-01-01T00:00:00Z",
524
+ credits_charged: 15,
525
+ download: null,
526
+ downloads: [
527
+ {
528
+ url: "https://example.com/download/video1.mp4",
529
+ expires_at: "2024-01-01T01:00:00Z",
530
+ },
531
+ ],
532
+ enabled: true,
533
+ end_seconds: 10.5,
534
+ error: null,
535
+ fps: 30,
536
+ height: 720,
537
+ name: "Test Video Project",
538
+ start_seconds: 0.0,
539
+ total_frame_cost: 15,
540
+ type: "VIDEO_TO_VIDEO",
541
+ width: 1280,
542
+ };
543
+ // Set up MSW handler
544
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
545
+ return msw_1.HttpResponse.json(completeResponse);
546
+ }));
547
+ downloadFiles.mockRejectedValue(new Error("Download failed"));
548
+ yield expect(client.v1.videoProjects.checkResult({ id: "video-123" }, { downloadOutputs: true })).rejects.toThrow("Download failed");
549
+ }));
550
+ });
551
+ describe("Default behavior", () => {
552
+ test("should use default options when no options provided", () => __awaiter(void 0, void 0, void 0, function* () {
553
+ const completeResponse = {
554
+ id: "video-123",
555
+ status: "complete",
556
+ created_at: "2024-01-01T00:00:00Z",
557
+ credits_charged: 15,
558
+ download: null,
559
+ downloads: [
560
+ {
561
+ url: "https://example.com/download/video1.mp4",
562
+ expires_at: "2024-01-01T01:00:00Z",
563
+ },
564
+ ],
565
+ enabled: true,
566
+ end_seconds: 10.5,
567
+ error: null,
568
+ fps: 30,
569
+ height: 720,
570
+ name: "Test Video Project",
571
+ start_seconds: 0.0,
572
+ total_frame_cost: 15,
573
+ type: "VIDEO_TO_VIDEO",
574
+ width: 1280,
575
+ };
576
+ const renderingResponse = Object.assign(Object.assign({}, completeResponse), { status: "rendering" });
577
+ let callCount = 0;
578
+ // Set up MSW handler that returns different responses on each call
579
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
580
+ callCount++;
581
+ if (callCount === 1) {
582
+ return msw_1.HttpResponse.json(renderingResponse);
583
+ }
584
+ else {
585
+ return msw_1.HttpResponse.json(completeResponse);
586
+ }
587
+ }));
588
+ downloadFiles.mockResolvedValue(["/tmp/video1.mp4"]);
589
+ const result = yield client.v1.videoProjects.checkResult({
590
+ id: "video-123",
591
+ });
592
+ expect(result.downloadedPaths).toEqual(["/tmp/video1.mp4"]);
593
+ expect(sleep).toHaveBeenCalledWith(500); // Default poll interval
594
+ }));
595
+ });
596
+ describe("Request options", () => {
597
+ test("should pass additional request options to get method", () => __awaiter(void 0, void 0, void 0, function* () {
598
+ const mockResponse = {
599
+ id: "video-123",
600
+ status: "complete",
601
+ created_at: "2024-01-01T00:00:00Z",
602
+ credits_charged: 15,
603
+ download: null,
604
+ downloads: [],
605
+ enabled: true,
606
+ end_seconds: 10.5,
607
+ error: null,
608
+ fps: 30,
609
+ height: 720,
610
+ name: "Test Video Project",
611
+ start_seconds: 0.0,
612
+ total_frame_cost: 15,
613
+ type: "VIDEO_TO_VIDEO",
614
+ width: 1280,
615
+ };
616
+ // Set up MSW handler
617
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
618
+ return msw_1.HttpResponse.json(mockResponse);
619
+ }));
620
+ const getMock = jest.spyOn(client.v1.videoProjects, "get");
621
+ yield client.v1.videoProjects.checkResult({ id: "video-123" }, {
622
+ waitForCompletion: false,
623
+ timeout: 30000,
624
+ });
625
+ expect(getMock).toHaveBeenCalledWith({ id: "video-123" }, {
626
+ timeout: 30000,
627
+ });
628
+ }));
629
+ });
630
+ describe("Edge cases", () => {
631
+ test("should handle immediate completion without polling", () => __awaiter(void 0, void 0, void 0, function* () {
632
+ const completeResponse = {
633
+ id: "video-123",
634
+ status: "complete",
635
+ created_at: "2024-01-01T00:00:00Z",
636
+ credits_charged: 15,
637
+ download: null,
638
+ downloads: [],
639
+ enabled: true,
640
+ end_seconds: 10.5,
641
+ error: null,
642
+ fps: 30,
643
+ height: 720,
644
+ name: "Test Video Project",
645
+ start_seconds: 0.0,
646
+ total_frame_cost: 15,
647
+ type: "VIDEO_TO_VIDEO",
648
+ width: 1280,
649
+ };
650
+ // Set up MSW handler
651
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
652
+ return msw_1.HttpResponse.json(completeResponse);
653
+ }));
654
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, { waitForCompletion: false });
655
+ expect(result).toEqual({
656
+ id: "video-123",
657
+ status: "complete",
658
+ createdAt: "2024-01-01T00:00:00Z",
659
+ creditsCharged: 15,
660
+ download: null,
661
+ downloads: [],
662
+ enabled: true,
663
+ endSeconds: 10.5,
664
+ error: null,
665
+ fps: 30,
666
+ height: 720,
667
+ name: "Test Video Project",
668
+ startSeconds: 0.0,
669
+ totalFrameCost: 15,
670
+ type: "VIDEO_TO_VIDEO",
671
+ width: 1280,
672
+ });
673
+ expect(sleep).not.toHaveBeenCalled();
674
+ expect(downloadFiles).not.toHaveBeenCalled();
675
+ }));
676
+ test("should handle empty downloads array", () => __awaiter(void 0, void 0, void 0, function* () {
677
+ const completeResponse = {
678
+ id: "video-123",
679
+ status: "complete",
680
+ created_at: "2024-01-01T00:00:00Z",
681
+ credits_charged: 15,
682
+ download: null,
683
+ downloads: [],
684
+ enabled: true,
685
+ end_seconds: 10.5,
686
+ error: null,
687
+ fps: 30,
688
+ height: 720,
689
+ name: "Test Video Project",
690
+ start_seconds: 0.0,
691
+ total_frame_cost: 15,
692
+ type: "VIDEO_TO_VIDEO",
693
+ width: 1280,
694
+ };
695
+ // Set up MSW handler
696
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
697
+ return msw_1.HttpResponse.json(completeResponse);
698
+ }));
699
+ downloadFiles.mockResolvedValue([]);
700
+ const result = yield client.v1.videoProjects.checkResult({ id: "video-123" }, { downloadOutputs: true });
701
+ expect(result.downloadedPaths).toEqual([]);
702
+ expect(downloadFiles).toHaveBeenCalledWith([], undefined);
703
+ }));
704
+ test("should handle get method throwing error", () => __awaiter(void 0, void 0, void 0, function* () {
705
+ const error = new Error("API Error");
706
+ // Set up MSW handler that returns an error
707
+ server.use(msw_1.http.get("https://api.magichour.ai/v1/video-projects/video-123", () => {
708
+ return msw_1.HttpResponse.json({ error: "API Error" }, { status: 500 });
709
+ }));
710
+ yield expect(client.v1.videoProjects.checkResult({ id: "video-123" })).rejects.toThrow();
711
+ }));
712
+ });
713
+ });