Files
openmotion/tests/cloud/model-gateway.test.ts
open-design-crew[bot] 4e2a9ed88a feat(game,studio): sync 12.2 OpenGame main features and dogfooding fixes (#1228)
* fix(studio,auth): surgical dogfooding fixes for P1-21, P0-07, P1-13, and P1-17

* feat(game): restore OpenGame Studio landing hero, composer controls, and strategy genre

* feat(game): restore template market search, leaderboards, and community strategy games

* chore(game): sync 262 updated game template covers from opengame

* feat(game): add 27 missing community games for Fable 5.2 prerequisite

* feat(game): add Claude Fable 5.2 game hub and spotlight banner for Stage 1 cloud

* feat(studio,cloud): add Plus upgrade badge in media model pickers with server entitlement check

* feat(studio): adapt Black Friday launch modal to OpenGame flagship image offer

* fix(game,studio): consume requested game navigation, label official templates on leaderboards, and gate Fable hero video on viewport visibility

---------

Co-authored-by: heggria <bshengtao@gmail.com>
2026-12-02 20:00:07 +08:00

1328 lines
56 KiB
TypeScript

import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { describe, it } from 'node:test';
import { Hono } from 'hono';
import sharp from 'sharp';
import { mediaType } from '../../gateway/network.ts';
import {
InMemoryAuthStore,
SESSION_COOKIE_NAME,
createAuthMiddleware,
createBrowserSession,
createPat,
type AuthEnv,
} from '../../src/auth/index.ts';
import { isCloudError } from '../../src/contracts/errors.ts';
import { parseConfig, type AppConfig } from '../../src/infra/config.ts';
import { createAccountRoutes } from '../../src/routes/account.ts';
import { createModelRoutes } from '../../src/routes/models.ts';
import {
MockModelGateway,
generateDeterministicMp4,
generateDeterministicPng,
generateDeterministicWav,
} from '../../src/services/mock-model-adapter.ts';
import {
GatewayError,
createPlatformModelGateway,
} from '../../src/services/model-gateway.ts';
import { OpenDesignModelGateway } from '../../src/services/opendesign-adapter.ts';
async function streamToBuffer(stream: ReadableStream<Uint8Array>): Promise<Buffer> {
const reader = stream.getReader();
const chunks: Uint8Array[] = [];
for (;;) {
const { done, value } = await reader.read();
if (done) break;
if (value) chunks.push(value);
}
return Buffer.concat(chunks);
}
function makeGatewayConfig(overrides: Record<string, string | undefined> = {}): AppConfig {
return parseConfig({
NODE_ENV: 'test',
WORKER_ROLE: 'api',
APP_PUBLIC_ORIGIN: 'https://motion.example.test',
AUTH_MODE: 'oidc',
OIDC_ISSUER: 'https://idp.example.test',
OIDC_CLIENT_ID: 'openmotion-cloud',
SESSION_SECRET: '0'.repeat(32),
MODEL_GATEWAY_MODE: 'mock',
DATABASE_URL: 'postgres://postgres:postgres@127.0.0.1:5432/openmotion',
REDIS_URL: 'redis://127.0.0.1:6379',
...overrides,
});
}
describe('MockModelGateway Deterministic Valid PNG/MP4/WAV Generation', () => {
it('generates valid PNG (Sharp-decodable), WAV (RIFF/PCM), and MP4 (ISO BMFF) buffers deterministically', async () => {
const gateway = new MockModelGateway({ pollCountBeforeSuccess: 1 });
const ctx = { workspaceId: randomUUID(), idempotencyKey: 'idem-mock-1' };
const models = await gateway.listMediaModels(ctx);
assert.equal(models.length, 4);
assert.ok(models.every((m) => m.provider === 'opendesign'));
// Upload reference image
const refPng = generateDeterministicPng('ref-seed');
const uploaded = await gateway.uploadReference(ctx, {
bytes: refPng,
mimeType: 'image/png',
idempotencyKey: 'ref-1',
});
assert.ok(uploaded.remoteAssetId.startsWith('mock_ref_'));
// Image generation -> 1 running poll -> succeeded -> Sharp verifies 64x64 RGBA PNG
const imgSubmit = await gateway.submitTask(ctx, {
jobId: 'job-img-1',
modality: 'image',
mode: 'text-to-image',
modelId: 'mock-image-1',
prompt: 'neon city skyline',
parameters: {},
inputs: [{ remoteAssetId: uploaded.remoteAssetId }],
});
// Idempotent repeat returns same remoteTaskId
const imgSubmitRepeat = await gateway.submitTask(ctx, {
jobId: 'job-img-1',
modality: 'image',
mode: 'text-to-image',
modelId: 'mock-image-1',
prompt: 'neon city skyline',
parameters: {},
inputs: [{ remoteAssetId: uploaded.remoteAssetId }],
});
assert.equal(imgSubmitRepeat.remoteTaskId, imgSubmit.remoteTaskId);
const poll1 = await gateway.pollTask(ctx, { modality: 'image', remoteTaskId: imgSubmit.remoteTaskId });
assert.equal(poll1.status, 'running');
const poll2 = await gateway.pollTask(ctx, { modality: 'image', remoteTaskId: imgSubmit.remoteTaskId });
assert.equal(poll2.status, 'succeeded');
const imgBytes = await streamToBuffer(await gateway.downloadOutput(ctx, poll2.outputs![0]));
assert.deepEqual(mediaType(imgBytes, 'image'), { mimeType: 'image/png', extension: 'png' });
const meta = await sharp(imgBytes).metadata();
assert.equal(meta.format, 'png');
assert.equal(meta.width, 64);
assert.equal(meta.height, 64);
// Video generation -> valid MP4 container
const vidGateway = new MockModelGateway();
const vidSubmit = await vidGateway.submitTask(
{ workspaceId: ctx.workspaceId, idempotencyKey: 'idem-vid' },
{
jobId: 'job-vid-1',
modality: 'video',
mode: 'text-to-video',
modelId: 'mock-video-1',
prompt: 'camera dolly shot',
parameters: {},
inputs: [],
},
);
const vidPoll = await vidGateway.pollTask(ctx, { modality: 'video', remoteTaskId: vidSubmit.remoteTaskId });
assert.equal(vidPoll.status, 'succeeded');
const mp4Bytes = await streamToBuffer(await vidGateway.downloadOutput(ctx, vidPoll.outputs![0]));
assert.deepEqual(mediaType(mp4Bytes, 'video'), { mimeType: 'video/mp4', extension: 'mp4' });
assert.equal(mp4Bytes.subarray(4, 8).toString('ascii'), 'ftyp');
assert.ok(mp4Bytes.includes(Buffer.from('moov', 'ascii')));
assert.ok(mp4Bytes.includes(Buffer.from('mdat', 'ascii')));
// Speech generation -> valid WAV + sentence timing metadata
const audioSubmit = await vidGateway.submitTask(
{ workspaceId: ctx.workspaceId, idempotencyKey: 'idem-aud' },
{
jobId: 'job-aud-1',
modality: 'audio',
mode: 'text-to-speech',
modelId: 'mock-speech-1',
prompt: 'Welcome to OpenMotion',
parameters: { voice: 'default' },
inputs: [],
},
);
const audioPoll = await vidGateway.pollTask(ctx, { modality: 'audio', remoteTaskId: audioSubmit.remoteTaskId });
assert.equal(audioPoll.status, 'succeeded');
assert.ok(Array.isArray(audioPoll.metadata?.sentences));
const wavBytes = await streamToBuffer(await vidGateway.downloadOutput(ctx, audioPoll.outputs![0]));
assert.deepEqual(mediaType(wavBytes, 'audio'), { mimeType: 'audio/wav', extension: 'wav' });
assert.equal(wavBytes.subarray(0, 4).toString('ascii'), 'RIFF');
assert.equal(wavBytes.subarray(8, 12).toString('ascii'), 'WAVE');
// Error hooks: insufficient_balance on submit & audio_outcome_unknown on poll
await assert.rejects(
() =>
vidGateway.submitTask(ctx, {
jobId: 'job-fail-1',
modality: 'image',
mode: 'text-to-image',
modelId: 'mock-image-1',
prompt: '[mock:fail=insufficient_balance] test',
parameters: {},
inputs: [],
}),
(err: unknown) => err instanceof GatewayError && err.code === 'insufficient_balance',
);
const unknownSub = await vidGateway.submitTask(
{ workspaceId: ctx.workspaceId, idempotencyKey: 'idem-unk' },
{
jobId: 'job-unk-1',
modality: 'audio',
mode: 'text-to-audio',
modelId: 'mock-music-1',
prompt: '[mock:fail=audio_outcome_unknown] track',
parameters: {},
inputs: [],
},
);
const unknownPoll = await vidGateway.pollTask(ctx, { modality: 'audio', remoteTaskId: unknownSub.remoteTaskId });
assert.equal(unknownPoll.status, 'failed');
assert.equal(unknownPoll.error?.code, 'audio_outcome_unknown');
assert.equal(unknownPoll.error?.outcomeUnknown, true);
assert.equal(unknownPoll.error?.remoteTerminal, true);
const unknownVideoSub = await vidGateway.submitTask(
{ workspaceId: ctx.workspaceId, idempotencyKey: 'idem-unk-vid' },
{
jobId: 'job-unk-vid-1',
modality: 'video',
mode: 'text-to-video',
modelId: 'mock-video-1',
prompt: '[mock:fail=media_execution_unknown] clip',
parameters: {},
inputs: [],
},
);
const unknownVideoPoll = await vidGateway.pollTask(ctx, { modality: 'video', remoteTaskId: unknownVideoSub.remoteTaskId });
assert.equal(unknownVideoPoll.status, 'failed');
assert.equal(unknownVideoPoll.error?.code, 'media_execution_unknown');
assert.equal(unknownVideoPoll.error?.outcomeUnknown, true);
assert.equal(unknownVideoPoll.error?.remoteTerminal, undefined);
});
});
describe('OpenDesignModelGateway Direct S2S HTTP Contract (Zero vela-cli / Zero BYOK)', () => {
it('rejects Eleven Music durations beyond five minutes before submitting', async () => {
const sent: unknown[] = [];
const gateway = new OpenDesignModelGateway({ apiUrl: 'https://amr.example.test', linkUrl: 'https://link.example.test',
fetcher: async (_url, init) => { sent.push(JSON.parse(String(init?.body)).parameters); return Response.json({ task_id: 'music-task' }); } });
const submit = (music_length_ms: number) => gateway.submitTask({ workspaceId: 'workspace' }, { jobId: 'music-job', modality: 'audio',
mode: 'text-to-audio', modelId: 'elevenlabs-music-v2', prompt: 'Rain piano', parameters: { music_length_ms }, inputs: [] });
for (const ms of [599000, 600000]) await assert.rejects(() => submit(ms), /Duration \(seconds\)/);
assert.equal(sent.length, 0);
await submit(300000);
assert.deepEqual(sent, [{ music_length_ms: 300000, prompt: 'Rain piano' }]);
});
it('marks a failed Cloud audio task as remote-terminal, including an unknown outcome', async () => {
const statuses: Record<string, Record<string, unknown>> = {
'audio-unknown': { status: 'failed', error: { code: 'audio_outcome_unknown', retryable: false } },
'audio-rejected': { status: 'failed', error: { code: 'upstream_request_failed' } },
'video-unknown': { status: 'failed', error: { code: 'media_execution_unknown' } },
};
const gateway = new OpenDesignModelGateway({ apiUrl: 'https://amr.example.test', linkUrl: 'https://link.example.test',
fetcher: async (url) => Response.json(statuses[String(url).split('/').at(-1)!]) });
const ctx = { workspaceId: 'workspace' };
const unknown = await gateway.pollTask(ctx, { modality: 'audio', remoteTaskId: 'audio-unknown' });
assert.equal(unknown.status, 'failed');
assert.equal(unknown.error?.outcomeUnknown, true);
assert.equal(unknown.error?.remoteTerminal, true);
assert.doesNotMatch(unknown.error?.message ?? '', /Inspect the original task/);
const rejected = await gateway.pollTask(ctx, { modality: 'audio', remoteTaskId: 'audio-rejected' });
assert.equal(rejected.error?.outcomeUnknown, false);
assert.equal(rejected.error?.remoteTerminal, true);
// Video tasks keep the existing contract: an unknown outcome stays resumable.
const video = await gateway.pollTask(ctx, { modality: 'video', remoteTaskId: 'video-unknown' });
assert.equal(video.error?.outcomeUnknown, true);
assert.notEqual(video.error?.remoteTerminal, true);
});
it('sends speech speed in ElevenLabs voice settings and preserves other voice settings', async () => {
const gateway = new OpenDesignModelGateway({ apiUrl: 'https://amr.example.test', linkUrl: 'https://link.example.test',
fetcher: async (_url, init) => {
const payload = JSON.parse(String(init?.body));
assert.deepEqual(payload.parameters, { text: 'Hello', voice_settings: { stability: 0.5, speed: 1.2 } });
return Response.json({ task_id: 'speech-task' });
},
});
await gateway.submitTask({ workspaceId: 'workspace' }, { jobId: 'speech-job', modality: 'audio', mode: 'text-to-speech',
modelId: 'elevenlabs-v3', prompt: 'Hello', parameters: { speed: 1.2, voice_settings: { stability: 0.5 } }, inputs: [] });
});
it('sends S2S Bearer, x-vela-workspace-id, Idempotency-Key and strips Authorization on presigned PUT and video redirect', async () => {
const calls: Array<{ url: string; method: string; headers: Record<string, string>; body?: string }> = [];
const sampleMp4 = generateDeterministicMp4('s2s-video');
const mockFetcher: typeof fetch = async (input, init = {}) => {
const url = String(input);
const method = (init.method ?? 'GET').toUpperCase();
const hdrs = Object.fromEntries(new Headers(init.headers).entries());
calls.push({
url,
method,
headers: hdrs,
body: typeof init.body === 'string' ? init.body : undefined,
});
if (url === 'https://amr-api.open-design.ai/api/v1/media/models?include_audio=true') {
return new Response(
JSON.stringify({
models: [
{
model: 'wan-2.7',
kind: 'video',
enabled: true,
capabilities: { max_input_images: 1, ratios: ['16:9', '9:16'], durations: [5] },
},
{
model: 'flux-pro',
kind: 'image',
enabled: true,
capabilities: {
edits: true,
max_input_images: 4,
profiles: {
generations: {
profiles: [{ aspect_ratio: '16:9', resolution: '1K' }],
default: { aspect_ratio: '16:9', resolution: '1K' },
},
},
},
},
{
model: 'elevenlabs-v3',
label: 'Eleven v3',
kind: 'audio_speech',
enabled: true,
},
{
model: 'elevenlabs-music-v2',
label: 'ElevenLabs Music',
kind: 'audio_music',
enabled: true,
},
{
model: 'sound-fx-unsupported',
kind: 'audio_sound_effects',
enabled: true,
},
],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
if (url === 'https://amr-api.open-design.ai/api/v1/media/uploads' && method === 'POST') {
return new Response(
JSON.stringify({
asset_id: 'od_asset_123',
upload_url: 'https://storage.example.test/presigned/od_asset_123',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
if (url === 'https://storage.example.test/presigned/od_asset_123' && method === 'PUT') {
// CRITICAL security invariant: presigned PUT must NEVER include Authorization header!
assert.equal(hdrs.authorization, undefined);
return new Response(null, { status: 200 });
}
if (url === 'https://amr-api.open-design.ai/api/v1/media/uploads/od_asset_123/commit') {
return new Response(JSON.stringify({ status: 'ready', asset_id: 'od_asset_123' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
if (url === 'https://amr-api.open-design.ai/api/v1/media/images/edits') {
return new Response(JSON.stringify({ task_id: 'img_task_99' }), {
status: 202,
headers: { 'Content-Type': 'application/json' },
});
}
if (url === 'https://amr-api.open-design.ai/api/v1/media/videos/tasks') {
return new Response(JSON.stringify({ task_id: 'vid_task_77' }), {
status: 202,
headers: { 'Content-Type': 'application/json' },
});
}
if (url === 'https://amr-api.open-design.ai/api/v1/media/videos/tasks/vid_task_77') {
return new Response(JSON.stringify({ task_id: 'vid_task_77', status: 'succeeded' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
if (url === 'https://amr-api.open-design.ai/api/v1/media/videos/tasks/vid_task_77/content') {
return new Response(null, {
status: 302,
headers: { Location: 'https://cdn-signed.example.test/videos/vid_task_77.mp4' },
});
}
if (url === 'https://cdn-signed.example.test/videos/vid_task_77.mp4') {
// CRITICAL security invariant: redirected signed download MUST NOT receive Authorization header!
assert.equal(hdrs.authorization, undefined);
return new Response(new Uint8Array(sampleMp4), {
status: 200,
headers: { 'Content-Type': 'video/mp4' },
});
}
return new Response(JSON.stringify({ error: { code: 'not_found' } }), { status: 404 });
};
const gateway = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
s2sApiKey: 'test-s2s-service-key',
billingSyncEnabled: false,
fetcher: mockFetcher,
});
const ctx = {
workspaceId: randomUUID(),
externalWorkspaceId: 'vela-ws-88',
externalUserId: 'vela-user-55',
appUserId: 'app-user-55',
amrWorkspaceMemberId: 'mem-88',
amrWorkspaceRole: 'owner' as const,
amrWorkspaceLifecycleState: 'active',
idempotencyKey: 'idem-job-1',
};
// 1. Model catalog discovery (requests include_audio=true and maps live speech + music without unsupported kinds)
const models = await gateway.listMediaModels(ctx);
assert.ok(models.some((m) => m.id === 'opendesign:wan-2.7'));
assert.ok(models.some((m) => m.id === 'opendesign:flux-pro' && m.supportsImageReference === true));
assert.ok(models.some((m) => m.id === 'opendesign:elevenlabs-v3' && m.modality === 'audio' && m.mode === 'text-to-speech' && m.source === 'live'));
assert.ok(models.some((m) => m.id === 'opendesign:elevenlabs-music-v2' && m.modality === 'audio' && m.mode === 'text-to-audio' && m.source === 'live'));
assert.equal(models.some((m) => m.modelId === 'sound-fx-unsupported'), false);
assert.equal(calls[0].headers.authorization, undefined);
assert.equal(calls[0].headers['x-internal-token'], 'test-s2s-service-key');
assert.equal(calls[0].headers['x-workspace-team-id'], 'vela-ws-88');
assert.equal(calls[0].headers['x-workspace-member-id'], 'mem-88');
assert.equal(calls[0].headers['x-workspace-role'], 'owner');
assert.equal(calls[0].headers['x-workspace-lifecycle-state'], 'active');
assert.equal(calls[0].headers['x-vela-workspace-id'], 'vela-ws-88');
// 2. 3-step reference upload
const refBytes = generateDeterministicPng('ref-s2s');
const uploaded = await gateway.uploadReference(ctx, {
bytes: refBytes,
mimeType: 'image/png',
idempotencyKey: 'upload-1',
});
assert.equal(uploaded.remoteAssetId, 'od_asset_123');
assert.equal(calls[1].headers['idempotency-key'], 'upload-1-upload');
assert.equal(calls[3].headers['idempotency-key'], 'upload-1-commit');
// 3. Image edit with paired aspect_ratio + resolution -> wire output object
const imgSubmitted = await gateway.submitTask(ctx, {
jobId: 'job-edit-1',
modality: 'image',
mode: 'text-to-image',
modelId: 'flux-pro',
prompt: 'make it golden hour',
parameters: { aspect_ratio: '16:9', resolution: '1K' },
inputs: [{ remoteAssetId: 'od_asset_123' }],
});
assert.equal(imgSubmitted.remoteTaskId, 'img_task_99');
const editPayload = JSON.parse(calls[4].body!) as { output?: { aspect_ratio: string; resolution: string }; images?: string[] };
assert.deepEqual(editPayload.output, { aspect_ratio: '16:9', resolution: '1K' });
assert.deepEqual(editPayload.images, ['od_asset_123']);
// 4. Video submit -> poll -> /content 302 credential-free download
const vidSubmitted = await gateway.submitTask(ctx, {
jobId: 'job-vid-s2s',
modality: 'video',
mode: 'text-to-video',
modelId: 'wan-2.7',
prompt: 'cinematic glide',
parameters: {},
inputs: [{ remoteAssetId: 'od_asset_123', role: 'first_frame' }],
});
assert.equal(vidSubmitted.remoteTaskId, 'vid_task_77');
const polled = await gateway.pollTask(ctx, { modality: 'video', remoteTaskId: 'vid_task_77' });
assert.equal(polled.status, 'succeeded');
const downloaded = await streamToBuffer(await gateway.downloadOutput(ctx, polled.outputs![0]));
assert.deepEqual(downloaded, sampleMp4);
for (const call of calls) {
const jobId = call.method === 'POST' && call.url.endsWith('/images/edits') ? 'job-edit-1'
: call.method === 'POST' && call.url.endsWith('/videos/tasks') ? 'job-vid-s2s' : undefined;
assert.equal(call.headers['x-openmotion-job-id'], jobId);
assert.equal(call.headers['x-source-product'], jobId ? 'openmotion' : undefined);
if (jobId) assert.equal(call.headers['idempotency-key'], ctx.idempotencyKey);
}
// 5. Billing sync disabled by default (no upstream call)
const billing = await gateway.getBillingSummary(ctx);
assert.equal(billing.unavailable, true);
// 6. ASR without OPENDESIGN_ASR_URL returns explicit FEATURE_DEFERRED_P2 (501)
await assert.rejects(
() => gateway.transcribeAudio(ctx, { audioWav: generateDeterministicWav('asr') }),
(err: unknown) => isCloudError(err) && err.code === 'FEATURE_DEFERRED_P2' && err.status === 501,
);
});
it('keeps custom gateway task submissions on the existing idempotency protocol', async () => {
const gateway = new OpenDesignModelGateway({
apiUrl: 'https://custom.example.test', linkUrl: 'https://link.example.test',
fetcher: async (_url, init) => {
const headers = new Headers(init?.headers);
assert.equal(headers.get('idempotency-key'), 'retry-key');
assert.equal(headers.has('x-openmotion-job-id'), false);
assert.equal(headers.has('x-source-product'), false);
return Response.json({ task_id: 'custom-task' });
},
});
const result = await gateway.submitTask({ workspaceId: 'workspace', idempotencyKey: 'retry-key' }, {
jobId: 'internal-job', modality: 'video', mode: 'text-to-video', modelId: 'video-model',
prompt: 'Ocean waves', parameters: {}, inputs: [],
});
assert.equal(result.remoteTaskId, 'custom-task');
});
it('returns an honest empty catalog without fabricated fallback models, skips caching empty results, and scopes cache by actor', async () => {
const store = new Map<string, string>();
const deletedKeys: string[] = [];
const cache = {
async get(key: string) {
return store.get(key) ?? null;
},
async set(key: string, value: string) {
store.set(key, value);
},
async del(key: string) {
deletedKeys.push(key);
store.delete(key);
},
};
let memberARequests = 0;
let memberBRequests = 0;
let grantMemberA = false;
const mockFetcher: typeof fetch = async (input, init = {}) => {
const url = String(input);
assert.equal(url, 'https://amr-api.open-design.ai/api/v1/media/models?include_audio=true');
const hdrs = new Headers(init.headers);
const memberId = hdrs.get('x-workspace-member-id');
if (memberId === 'mem-a') {
memberARequests++;
const models = grantMemberA
? [{ model: 'flux-pro', kind: 'image', enabled: true }]
: [];
return new Response(JSON.stringify({ models }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
memberBRequests++;
return new Response(
JSON.stringify({ models: [{ model: 'wan-2.7', kind: 'video', enabled: true }] }),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
};
const gateway = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: false,
fetcher: mockFetcher,
cache,
});
const ctxA = {
workspaceId: randomUUID(),
externalWorkspaceId: 'ws-shared',
appUserId: 'app-user-a',
amrWorkspaceMemberId: 'mem-a',
};
const ctxB = {
workspaceId: ctxA.workspaceId,
externalWorkspaceId: 'ws-shared',
appUserId: 'app-user-b',
amrWorkspaceMemberId: 'mem-b',
};
// Member A initially receives 0 models: MUST be empty (no fabricated fallback) and NOT cached.
const emptyModels = await gateway.listMediaModels(ctxA);
assert.deepEqual(emptyModels, []);
assert.equal(memberARequests, 1);
assert.equal(store.size, 0);
assert.equal(deletedKeys.length, 1);
// Member B in the same external workspace has its own isolated cache entry.
const memberBModels = await gateway.listMediaModels(ctxB);
assert.deepEqual(
memberBModels.map((m) => m.id),
['opendesign:wan-2.7'],
);
assert.equal(memberBRequests, 1);
// Second call for Member B hits cache.
await gateway.listMediaModels(ctxB);
assert.equal(memberBRequests, 1);
// After Member A upgrades, the next call re-fetches immediately (empty catalog was never cached).
grantMemberA = true;
const upgradedModels = await gateway.listMediaModels(ctxA);
assert.deepEqual(
upgradedModels.map((m) => m.id),
['opendesign:flux-pro'],
);
assert.equal(memberARequests, 2);
});
});
describe('Hono Routes: /motion-api/models/* & /motion-api/account/*', () => {
it('enforces RBAC + strict scope checks on model defaults and workspace switch', async () => {
const config = makeGatewayConfig();
const authStore = new InMemoryAuthStore();
const ws1 = authStore.seedWorkspace({ role: 'owner', name: 'Studio Alpha' });
const ws2 = authStore.seedWorkspace({ role: 'owner', name: 'Studio Beta' });
// Grant ws1.user editor membership in ws2
authStore.setRole(ws2.workspace.id, ws1.user.id, 'editor');
const ws3 = authStore.seedWorkspace({ role: 'owner', name: 'Forbidden Studio' });
const session = await createBrowserSession(authStore, {
userId: ws1.user.id,
workspaceId: ws1.workspace.id,
});
const restrictedOwnerPat = await createPat(
authStore,
{
workspaceId: ws1.workspace.id,
userId: ws1.user.id,
role: 'owner',
scopes: ['assets:read'],
authMode: 'oidc',
authMethod: 'session_cookie',
},
{ name: 'Read Only Owner PAT' },
);
const gateway = createPlatformModelGateway(config);
const app = new Hono<AuthEnv>();
const authMw = createAuthMiddleware({ config, store: authStore });
app.use('/motion-api/*', authMw);
app.route('/motion-api/models', createModelRoutes({ gateway, authStore }));
app.route('/motion-api/account', createAccountRoutes({ config, authStore, gateway }));
// 1. Model catalog & status
const catalogRes = await app.request('/motion-api/models', {
headers: { Authorization: `Bearer ${restrictedOwnerPat.token}` },
});
assert.equal(catalogRes.status, 200);
const catalogBody = (await catalogRes.json()) as { provider: string; models: unknown[] };
assert.equal(catalogBody.provider, 'opendesign');
assert.equal(catalogBody.models.length, 4);
// 2. Removed BYOK providers return 404; opendesign reference returns guide
const falRef = await app.request('/motion-api/models/providers/fal/reference', {
headers: { Authorization: `Bearer ${restrictedOwnerPat.token}` },
});
assert.equal(falRef.status, 404);
const odRef = await app.request('/motion-api/models/providers/opendesign/reference', {
headers: { Authorization: `Bearer ${restrictedOwnerPat.token}` },
});
assert.equal(odRef.status, 200);
// 3. PUT /motion-api/models/defaults rejects restricted owner PAT (403) and succeeds for full browser admin session
const patPutRes = await app.request('/motion-api/models/defaults', {
method: 'PUT',
headers: {
Authorization: `Bearer ${restrictedOwnerPat.token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ image: 'opendesign:mock-image-1' }),
});
assert.equal(patPutRes.status, 403);
const browserPutRes = await app.request('/motion-api/models/defaults', {
method: 'PUT',
headers: {
Cookie: `${SESSION_COOKIE_NAME}=${session.token}`,
Origin: 'https://motion.example.test',
'Content-Type': 'application/json',
},
body: JSON.stringify({ image: 'opendesign:mock-image-1', video: 'opendesign:mock-video-1' }),
});
assert.equal(browserPutRes.status, 200);
const savedDefaults = (await browserPutRes.json()) as {
defaults: { image: string; video: string };
};
assert.equal(savedDefaults.defaults.image, 'opendesign:mock-image-1');
// 4. Account summary, integration discovery & workspace switch isolation
const summaryRes = await app.request('/motion-api/account/summary', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${session.token}` },
});
assert.equal(summaryRes.status, 200);
const summary = (await summaryRes.json()) as {
activeWorkspace: { id: string };
workspaces: Array<{ id: string }>;
};
assert.equal(summary.activeWorkspace.id, ws1.workspace.id);
assert.equal(summary.workspaces.length, 2);
const integrationRes = await app.request('/motion-api/account/integration', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${session.token}` },
});
assert.equal(integrationRes.status, 200);
const integration = (await integrationRes.json()) as {
transport: { type: string; url: string };
tools: unknown[];
};
assert.equal(integration.transport.type, 'streamable-http');
assert.equal(integration.transport.url, 'https://motion.example.test/mcp');
assert.equal(integration.tools.length, 23);
// Switching to non-member workspace ws3 is rejected with 403 WORKSPACE_FORBIDDEN
const forbiddenSwitch = await app.request('/motion-api/account/workspaces/switch', {
method: 'POST',
headers: {
Cookie: `${SESSION_COOKIE_NAME}=${session.token}`,
Origin: 'https://motion.example.test',
'Content-Type': 'application/json',
},
body: JSON.stringify({ workspaceId: ws3.workspace.id }),
});
assert.equal(forbiddenSwitch.status, 403);
// Switching to member workspace ws2 succeeds
const okSwitch = await app.request('/motion-api/account/workspaces/switch', {
method: 'POST',
headers: {
Cookie: `${SESSION_COOKIE_NAME}=${session.token}`,
Origin: 'https://motion.example.test',
'Content-Type': 'application/json',
},
body: JSON.stringify({ workspaceId: ws2.workspace.id }),
});
assert.equal(okSwitch.status, 200);
assert.equal(((await okSwitch.json()) as { activeWorkspace: { id: string } }).activeWorkspace.id, ws2.workspace.id);
});
it('reports unavailable status, honest reason, omits placeholder plan, and exposes availableModelCount=0 when upstream returns no models', async () => {
const config = makeGatewayConfig({
OPENDESIGN_CONSOLE_URL: 'https://console.example.test',
});
const authStore = new InMemoryAuthStore();
const ws = authStore.seedWorkspace({ role: 'owner', name: 'Free Studio' });
const session = await createBrowserSession(authStore, {
userId: ws.user.id,
workspaceId: ws.workspace.id,
});
const gateway = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: false,
fetcher: async () =>
new Response(JSON.stringify({ models: [] }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}),
});
const app = new Hono<AuthEnv>();
app.use('/motion-api/*', createAuthMiddleware({ config, store: authStore }));
app.route(
'/motion-api/models',
createModelRoutes({ gateway, authStore, consoleUrl: config.OPENDESIGN_CONSOLE_URL }),
);
app.route('/motion-api/account', createAccountRoutes({ config, authStore, gateway }));
const statusRes = await app.request('/motion-api/models/status', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${session.token}` },
});
assert.equal(statusRes.status, 200);
const statusBody = (await statusRes.json()) as {
consoleUrl?: string;
providers: Array<{ available: boolean; plan?: string; reason: string }>;
capabilities: Array<{ available: boolean; modelIds: string[] }>;
};
assert.equal(statusBody.consoleUrl, 'https://console.example.test');
assert.equal(statusBody.providers[0].available, false);
assert.equal(statusBody.providers[0].plan, undefined);
assert.match(statusBody.providers[0].reason, /No media models are currently available/i);
assert.ok(statusBody.capabilities.every((cap) => !cap.available && cap.modelIds.length === 0));
const summaryRes = await app.request('/motion-api/account/summary', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${session.token}` },
});
assert.equal(summaryRes.status, 200);
const summaryBody = (await summaryRes.json()) as {
billing: { unavailable?: boolean };
modelGateway: { provider: string; mode: string; availableModelCount?: number };
};
assert.equal(summaryBody.billing.unavailable, true);
assert.equal(summaryBody.modelGateway.availableModelCount, 0);
// When billing is unavailable (not confirmed Free/Go), /models/catalog preserves the honest empty list.
const unavailableCatalogRes = await app.request('/motion-api/models/catalog', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${session.token}` },
});
assert.equal(unavailableCatalogRes.status, 200);
const unavailableCatalogBody = (await unavailableCatalogRes.json()) as { models: unknown[] };
assert.equal(unavailableCatalogBody.models.length, 0);
});
it('serves built-in preview models on /models/catalog only for non-entitled workspaces and keeps IDs/caps aligned with shared tables', async () => {
const { openDesignModels, openDesignImageReferenceCap } = await import('../../shared/opendesign-models.ts');
const { modelReferenceLimit } = await import('../../shared/gateway.ts');
type SharedModel = import('../../shared/gateway.ts').Model;
const config = makeGatewayConfig({ OPENDESIGN_CONSOLE_URL: 'https://console.example.test' });
const authStore = new InMemoryAuthStore();
const freeWs = authStore.seedWorkspace({ role: 'owner', name: 'Free Workspace' });
const plusWs = authStore.seedWorkspace({ role: 'owner', name: 'Plus Workspace' });
const freeSession = await createBrowserSession(authStore, { userId: freeWs.user.id, workspaceId: freeWs.workspace.id });
const plusSession = await createBrowserSession(authStore, { userId: plusWs.user.id, workspaceId: plusWs.workspace.id });
const makeEmptyGateway = (plan: string, mediaEntitlement: 'included' | 'not-included') => {
const base = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: false,
fetcher: async () =>
new Response(JSON.stringify({ models: [] }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}),
});
base.getBillingSummary = async () => ({
plan,
mediaEntitlement,
hasBalance: mediaEntitlement === 'included',
});
return base;
};
const freeApp = new Hono<AuthEnv>();
freeApp.use('/motion-api/*', createAuthMiddleware({ config, store: authStore }));
freeApp.route('/motion-api/models', createModelRoutes({ gateway: makeEmptyGateway('free', 'not-included'), authStore }));
const freeCatalogRes = await freeApp.request('/motion-api/models/catalog', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${freeSession.token}` },
});
assert.equal(freeCatalogRes.status, 200);
const freeCatalogBody = (await freeCatalogRes.json()) as { models: SharedModel[] };
assert.deepEqual(
freeCatalogBody.models.map((m) => m.modelId),
openDesignModels.map((m) => m.modelId),
);
assert.ok(freeCatalogBody.models.every((m) => m.source === 'builtin'));
for (const model of freeCatalogBody.models) {
const expectedCap = openDesignImageReferenceCap(model.modelId);
if (expectedCap !== undefined) {
assert.equal(modelReferenceLimit(model), expectedCap);
}
if (model.modality === 'video') {
assert.ok(Array.isArray(model.nativeOptions?.ratios));
assert.ok(model.generationOptions?.fields.some((f) => f.key === 'ratio'));
}
}
const plusApp = new Hono<AuthEnv>();
plusApp.use('/motion-api/*', createAuthMiddleware({ config, store: authStore }));
plusApp.route('/motion-api/models', createModelRoutes({ gateway: makeEmptyGateway('plus', 'included'), authStore }));
const plusCatalogRes = await plusApp.request('/motion-api/models/catalog', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${plusSession.token}` },
});
assert.equal(plusCatalogRes.status, 200);
const plusCatalogBody = (await plusCatalogRes.json()) as { models: unknown[] };
assert.equal(plusCatalogBody.models.length, 0);
});
it('skips billing snapshot by default, fetches Design Plan quota only when includeQuota is true, and degrades cleanly on snapshot failure or timeout', async () => {
const requestedUrls: string[] = [];
const gateway = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: true,
fetcher: async (url) => {
const urlStr = String(url);
requestedUrls.push(urlStr);
if (urlStr.includes('/api/internal/billing/summary/usr_1')) {
return new Response(
JSON.stringify({
membershipTier: 'plus',
totalAvailableCreditsUsd: '85.00',
subscriptionEntitlementStatus: 'active',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
if (urlStr.includes('/api/v1/workspaces/ws_ext_1/billing/snapshot')) {
return new Response(
JSON.stringify({
schemaVersion: 1,
workspaceId: 'ws_ext_1',
designPlanWindows: [
{ kind: '5h', remainingCredits: '800', limitCredits: '1000', windowStart: '2026-12-01T00:00:00Z', resetsAt: '2026-12-01T05:00:00Z' },
{ kind: '7d', remainingCredits: '620', limitCredits: '1000', windowStart: '2026-11-25T00:00:00Z', resetsAt: '2026-12-02T00:00:00Z' },
],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
return new Response('not found', { status: 404 });
},
});
const ctx = {
workspaceId: 'ws-local',
appUserId: 'usr_1',
externalWorkspaceId: 'ws_ext_1',
};
// Default (includeQuota omitted/false) skips /billing/snapshot completely.
const defaultSummary = await gateway.getBillingSummary(ctx);
assert.equal(defaultSummary.plan, 'plus');
assert.equal(defaultSummary.balanceUsd, '85.00');
assert.equal(defaultSummary.mediaEntitlement, 'included');
assert.equal(defaultSummary.quota, undefined);
assert.equal(
requestedUrls.some((u) => u.includes('/billing/snapshot')),
false,
);
// Explicit includeQuota: true fetches /billing/snapshot and populates quota.
const summaryWithQuota = await gateway.getBillingSummary(ctx, { includeQuota: true });
assert.equal(summaryWithQuota.plan, 'plus');
assert.equal(summaryWithQuota.balanceUsd, '85.00');
assert.equal(summaryWithQuota.mediaEntitlement, 'included');
assert.deepEqual(summaryWithQuota.quota, {
kind: '7d',
remainingPercent: 62,
resetsAt: '2026-12-02T00:00:00Z',
});
assert.equal(
requestedUrls.filter((u) => u.includes('/billing/snapshot')).length,
1,
);
// When the snapshot endpoint fails, plan and balance still succeed and quota is simply omitted.
const degraded = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: true,
fetcher: async (url) => {
if (String(url).includes('/api/internal/billing/summary/usr_1')) {
return new Response(
JSON.stringify({
membershipTier: 'pro',
totalAvailableCreditsUsd: '42.50',
subscriptionEntitlementStatus: 'active',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
return new Response('upstream unavailable', { status: 503 });
},
});
const fallback = await degraded.getBillingSummary(ctx, { includeQuota: true });
assert.equal(fallback.plan, 'pro');
assert.equal(fallback.balanceUsd, '42.50');
assert.equal(fallback.quota, undefined);
assert.equal(fallback.unavailable, undefined);
// When /billing/snapshot hangs past the bounded timeout, it aborts and falls back to null without stalling summary.
let snapshotAborted = false;
const slowSnapshot = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: true,
billingSnapshotTimeoutMs: 40,
fetcher: async (url, init) => {
if (String(url).includes('/api/internal/billing/summary/usr_1')) {
return new Response(
JSON.stringify({
membershipTier: 'plus',
totalAvailableCreditsUsd: '19.00',
subscriptionEntitlementStatus: 'active',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
return await new Promise<Response>((_resolve, reject) => {
const onAbort = () => {
snapshotAborted = true;
reject(init?.signal?.reason ?? new DOMException('Aborted', 'AbortError'));
};
if (init?.signal?.aborted) {
onAbort();
return;
}
init?.signal?.addEventListener('abort', onAbort, { once: true });
});
},
});
const startedAt = Date.now();
const timedOut = await slowSnapshot.getBillingSummary(ctx, { includeQuota: true });
const elapsedMs = Date.now() - startedAt;
assert.equal(snapshotAborted, true);
assert.ok(elapsedMs < 1_000, `expected bounded snapshot timeout, took ${elapsedMs}ms`);
assert.equal(timedOut.plan, 'plus');
assert.equal(timedOut.balanceUsd, '19.00');
assert.equal(timedOut.quota, undefined);
assert.equal(timedOut.unavailable, undefined);
});
it('evicts session, AMR identity, and model catalog caches on Free -> Plus workspace switch so media models immediately reflect Plus entitlement', async () => {
const { AmrIdentityClient, amrIdentityCacheKey } = await import('../../src/auth/amr-identity.ts');
const { InMemoryAuthCache } = await import('../../src/auth/store.ts');
const { modelCatalogCacheKey } = await import('../../src/services/opendesign-adapter.ts');
const config = makeGatewayConfig({
AUTH_MODE: 'oidc',
OPENDESIGN_CONSOLE_URL: 'https://console.example.test',
});
const authStore = new InMemoryAuthStore();
const cache = new InMemoryAuthCache();
const freeMembership = await authStore.projectAmrUserAndWorkspace({
oidcSub: 'auth-user-switch',
appUserId: 'app-user-switch',
email: 'mason@example.test',
displayName: 'Mason',
externalWorkspaceId: 'ext-ws-free',
workspaceName: 'Free Space',
role: 'owner',
});
const plusMembership = await authStore.projectAmrUserAndWorkspace({
oidcSub: 'auth-user-switch',
appUserId: 'app-user-switch',
email: 'mason@example.test',
displayName: 'Mason',
externalWorkspaceId: 'ext-ws-plus',
workspaceName: 'Plus Space',
role: 'owner',
});
const session = await createBrowserSession(authStore, {
userId: freeMembership.user.id,
workspaceId: freeMembership.workspace.id,
});
// Seed stale cached entries for the target Plus workspace (e.g. from before an upgrade).
await cache.set(
amrIdentityCacheKey('auth-user-switch', 'ext-ws-plus'),
JSON.stringify({
active: true,
status: 'active',
authUserId: 'auth-user-switch',
appUserId: 'app-user-switch',
email: 'mason@example.test',
name: 'Mason',
image: null,
workspace: {
workspaceId: 'ext-ws-plus',
workspaceName: 'Plus Space',
workspaceType: 'team',
workspaceMemberId: 'stale-mem-plus',
role: 'member',
lifecycleState: 'active',
effectiveWriteAccess: 'writable',
},
}),
60,
);
await cache.set(
modelCatalogCacheKey({
externalWorkspaceId: 'ext-ws-plus',
appUserId: 'app-user-switch',
externalUserId: 'auth-user-switch',
}),
JSON.stringify([
{
id: 'opendesign:stale-go-only',
provider: 'opendesign',
modelId: 'stale-go-only',
label: 'Stale Go Only',
modality: 'image',
mode: 'text-to-image',
source: 'live',
},
]),
300,
);
const seenIdentityLookups: string[] = [];
const amrIdentity = new AmrIdentityClient({
apiUrl: 'https://amr-api.open-design.ai',
internalToken: 'test-token',
cache,
fetcher: async (input) => {
const url = new URL(typeof input === 'string' ? input : input.toString());
const requestedWs = url.searchParams.get('workspaceId') ?? 'ext-ws-free';
seenIdentityLookups.push(requestedWs);
const isPlus = requestedWs === 'ext-ws-plus';
return new Response(
JSON.stringify({
active: true,
status: 'active',
authUserId: 'auth-user-switch',
appUserId: 'app-user-switch',
email: 'mason@example.test',
name: 'Mason',
image: null,
workspace: {
workspaceId: isPlus ? 'ext-ws-plus' : 'ext-ws-free',
workspaceName: isPlus ? 'Plus Space' : 'Free Space',
workspaceType: isPlus ? 'team' : 'personal',
workspaceMemberId: isPlus ? 'fresh-mem-plus' : 'mem-free',
role: 'owner',
lifecycleState: 'active',
effectiveWriteAccess: 'writable',
},
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
},
});
const seenMediaRequests: Array<{ workspaceId: string | null; memberId: string | null }> = [];
const seenBillingUrls: string[] = [];
const seenSnapshotUrls: string[] = [];
const gateway = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: true,
cache,
fetcher: async (url, init) => {
const urlStr = typeof url === 'string' ? url : url instanceof URL ? url.href : url.url;
const parsed = new URL(urlStr);
const hdrs = new Headers(init?.headers);
if (parsed.pathname === '/api/v1/media/models') {
const wsId = hdrs.get('x-workspace-team-id');
const memId = hdrs.get('x-workspace-member-id');
seenMediaRequests.push({ workspaceId: wsId, memberId: memId });
const models =
wsId === 'ext-ws-plus' && memId === 'fresh-mem-plus'
? [
{ model: 'gpt-image-2', kind: 'image', label: 'GPT Image 2.0' },
{ model: 'wan-2.7', kind: 'video', label: 'Wan 2.7' },
]
: [];
return new Response(JSON.stringify({ models }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
if (parsed.pathname.startsWith('/api/internal/billing/summary/')) {
seenBillingUrls.push(urlStr);
const wsId = parsed.searchParams.get('workspaceId') ?? hdrs.get('x-vela-workspace-id');
const isPlus = wsId === 'ext-ws-plus';
return new Response(
JSON.stringify({
membershipTier: isPlus ? 'plus' : null,
subscriptionEntitlementStatus: isPlus ? 'active' : null,
totalAvailableCreditsUsd: isPlus ? '223.41' : '0.00',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
if (parsed.pathname.endsWith('/billing/snapshot')) {
seenSnapshotUrls.push(urlStr);
return new Response(
JSON.stringify({
schemaVersion: 1,
workspaceId: parsed.pathname.split('/')[4],
designPlanWindows: [],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
return new Response(JSON.stringify({ error: 'unexpected_url' }), { status: 404 });
},
});
const app = new Hono<AuthEnv>();
app.use('/motion-api/*', createAuthMiddleware({ config, store: authStore, cache, amrIdentity }));
app.route(
'/motion-api/models',
createModelRoutes({ gateway, authStore, consoleUrl: config.OPENDESIGN_CONSOLE_URL }),
);
app.route('/motion-api/account', createAccountRoutes({ config, authStore, gateway, cache }));
const cookieHeaders = { Cookie: `${SESSION_COOKIE_NAME}=${session.token}` };
// 1. Before switch: Free workspace shows free plan and 0 live models.
const freeSummaryRes = await app.request('/motion-api/account/summary', { headers: cookieHeaders });
assert.equal(freeSummaryRes.status, 200);
const freeSummary = (await freeSummaryRes.json()) as {
billing: { plan: string; mediaEntitlement: string };
modelGateway: { availableModelCount?: number };
};
assert.equal(freeSummary.billing.plan, 'free');
assert.equal(freeSummary.billing.mediaEntitlement, 'not-included');
assert.equal(freeSummary.modelGateway.availableModelCount, 0);
// 2. Switch workspace from Free -> Plus.
const switchRes = await app.request('/motion-api/account/workspaces/switch', {
method: 'POST',
headers: {
...cookieHeaders,
'Content-Type': 'application/json',
Origin: config.APP_PUBLIC_ORIGIN,
},
body: JSON.stringify({ workspaceId: plusMembership.workspace.id }),
});
assert.equal(switchRes.status, 200);
// Target workspace's stale AMR identity and model catalog cache entries must be evicted.
assert.equal(await cache.get(amrIdentityCacheKey('auth-user-switch', 'ext-ws-plus')), null);
assert.equal(
await cache.get(
modelCatalogCacheKey({
externalWorkspaceId: 'ext-ws-plus',
appUserId: 'app-user-switch',
externalUserId: 'auth-user-switch',
}),
),
null,
);
// 3. Immediately after switch: summary, catalog, and status?refresh=true all reflect fresh Plus workspace identity & models.
const plusSummaryRes = await app.request('/motion-api/account/summary?refresh=true', {
headers: cookieHeaders,
});
assert.equal(plusSummaryRes.status, 200);
const plusSummary = (await plusSummaryRes.json()) as {
activeWorkspace: { id: string; externalWorkspaceId: string | null };
billing: { plan: string; mediaEntitlement: string; balanceUsd?: string | null };
modelGateway: { availableModelCount?: number };
};
assert.equal(plusSummary.activeWorkspace.id, plusMembership.workspace.id);
assert.equal(plusSummary.activeWorkspace.externalWorkspaceId, 'ext-ws-plus');
assert.equal(plusSummary.billing.plan, 'plus');
assert.equal(plusSummary.billing.mediaEntitlement, 'included');
assert.equal(plusSummary.billing.balanceUsd, '223.41');
assert.equal(plusSummary.modelGateway.availableModelCount, 2);
const plusCatalogRes = await app.request('/motion-api/models/catalog?refresh=true', {
headers: cookieHeaders,
});
assert.equal(plusCatalogRes.status, 200);
const plusCatalog = (await plusCatalogRes.json()) as { models: Array<{ id: string }> };
assert.deepEqual(
plusCatalog.models.map((m) => m.id),
['opendesign:gpt-image-2', 'opendesign:wan-2.7'],
);
const snapshotCountBeforeStatus = seenSnapshotUrls.length;
assert.equal(snapshotCountBeforeStatus, 2);
const plusStatusRes = await app.request('/motion-api/models/status?refresh=true', {
headers: cookieHeaders,
});
assert.equal(plusStatusRes.status, 200);
// /motion-api/models/status must skip /billing/snapshot completely.
assert.equal(seenSnapshotUrls.length, snapshotCountBeforeStatus);
const plusStatus = (await plusStatusRes.json()) as {
providers: Array<{ available: boolean; plan?: string; mediaEntitlement?: string }>;
};
assert.equal(plusStatus.providers[0].available, true);
assert.equal(plusStatus.providers[0].plan, 'plus');
assert.equal(plusStatus.providers[0].mediaEntitlement, 'included');
assert.ok(seenIdentityLookups.includes('ext-ws-plus'));
assert.ok(
seenMediaRequests.some(
(req) => req.workspaceId === 'ext-ws-plus' && req.memberId === 'fresh-mem-plus',
),
);
assert.ok(seenBillingUrls.some((u) => u.includes('workspaceId=ext-ws-plus')));
});
it('exposes entitledModelIds on /models/status and merges live Go-entitled models with locked previews on /models/catalog', async () => {
const { isModelRunnableOnProvider } = await import('../../shared/gateway.ts');
type SharedModel = import('../../shared/gateway.ts').Model;
type SharedProviderState = import('../../shared/gateway.ts').ProviderState;
const config = makeGatewayConfig({ OPENDESIGN_CONSOLE_URL: 'https://console.example.test' });
const authStore = new InMemoryAuthStore();
const goWs = authStore.seedWorkspace({ role: 'owner', name: 'Go Workspace' });
const goSession = await createBrowserSession(authStore, { userId: goWs.user.id, workspaceId: goWs.workspace.id });
const goGateway = new OpenDesignModelGateway({
apiUrl: 'https://amr-api.open-design.ai',
linkUrl: 'https://amr-link.open-design.ai',
internalToken: 'test-token',
billingSyncEnabled: false,
fetcher: async () =>
new Response(
JSON.stringify({
models: [
{
model: 'magi-2',
label: 'Magi-2',
kind: 'video',
enabled: true,
max_input_images: 1,
},
],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
),
});
goGateway.getBillingSummary = async () => ({
plan: 'go',
mediaEntitlement: 'not-included',
hasBalance: true,
});
const app = new Hono<AuthEnv>();
app.use('/motion-api/*', createAuthMiddleware({ config, store: authStore }));
app.route('/motion-api/models', createModelRoutes({ gateway: goGateway, authStore }));
const [statusRes, catalogRes] = await Promise.all([
app.request('/motion-api/models/status', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${goSession.token}` },
}),
app.request('/motion-api/models/catalog', {
headers: { Cookie: `${SESSION_COOKIE_NAME}=${goSession.token}` },
}),
]);
assert.equal(statusRes.status, 200);
assert.equal(catalogRes.status, 200);
const statusBody = (await statusRes.json()) as { providers: SharedProviderState[] };
const catalogBody = (await catalogRes.json()) as { models: SharedModel[] };
assert.equal(statusBody.providers[0].id, 'opendesign');
assert.equal(statusBody.providers[0].available, true);
assert.equal(statusBody.providers[0].mediaEntitlement, 'not-included');
assert.deepEqual(statusBody.providers[0].entitledModelIds, ['opendesign:magi-2']);
const liveMagi = catalogBody.models.find((m) => m.id === 'opendesign:magi-2');
const previewSeedance = catalogBody.models.find((m) => m.id === 'opendesign:doubao-seedance-2-5-260628');
const previewImage = catalogBody.models.find((m) => m.modality === 'image');
assert.ok(liveMagi && liveMagi.source === 'live');
assert.ok(previewSeedance && previewSeedance.source === 'builtin');
assert.ok(previewImage && previewImage.source === 'builtin');
assert.equal(isModelRunnableOnProvider(liveMagi, statusBody.providers), true);
assert.equal(isModelRunnableOnProvider(previewSeedance, statusBody.providers), false);
assert.equal(isModelRunnableOnProvider(previewImage, statusBody.providers), false);
});
});