diff --git a/package.json b/package.json index 6c0a2e0..db9462a 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dev": "tsc -w", "format": "prettier --write \"src/**/*.ts\"", "lint": "prettier --check \"src/**/*.ts\"", + "test": "node --test tests/rest-async-video.test.mjs", "prepare": "pnpm run build", "pretest": "pnpm run build", "inspector": "npx @modelcontextprotocol/inspector build/index.js" diff --git a/src/services/media-service.ts b/src/services/media-service.ts index 2ff1947..771b07e 100644 --- a/src/services/media-service.ts +++ b/src/services/media-service.ts @@ -133,14 +133,17 @@ export class MediaService extends BaseService { public async generateVideo(params: any): Promise { this.checkInitialized(); try { + // REST tools expose the option as async_mode, while VideoAPI uses asyncMode. + const asyncMode = params.asyncMode ?? params.async_mode; + // Auto-generate output filename if not provided if (!params.outputFile) { const promptPrefix = params.prompt.substring(0, 20).replace(/[^\w]/g, '_'); params.outputFile = `video_${promptPrefix}_${Date.now()}`; } - const result = await this.videoApi.generateVideo(params); - if (params.async_mode) { + const result = await this.videoApi.generateVideo({ ...params, asyncMode }); + if (asyncMode) { return { content: [ { diff --git a/tests/rest-async-video.test.mjs b/tests/rest-async-video.test.mjs new file mode 100644 index 0000000..e12adb9 --- /dev/null +++ b/tests/rest-async-video.test.mjs @@ -0,0 +1,46 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { MediaService } from '../build/services/media-service.js'; + +class FakeVideoAPI { + constructor() { + this.postCalls = []; + this.getCalls = []; + } + + async post(endpoint, body) { + this.postCalls.push({ endpoint, body }); + return { task_id: 'task-123' }; + } + + async get(endpoint) { + this.getCalls.push(endpoint); + throw new Error('video polling should not start for async requests'); + } + + getResourceMode() { + return 'url'; + } +} + +test('REST async_mode returns the task id without polling', async () => { + const api = new FakeVideoAPI(); + const service = new MediaService(api); + await service.initialize({ apiKey: 'test-key', resourceMode: 'url' }); + + const result = await service.generateVideo({ + prompt: 'a test video', + async_mode: true, + }); + + assert.deepEqual(result, { + content: [ + { + type: 'text', + text: 'Success. Video generation task submitted: Task ID: task-123. Please use `query_video_generation` tool to check the status of the task and get the result.', + }, + ], + }); + assert.equal(api.postCalls.length, 1); + assert.equal(api.getCalls.length, 0); +});