268 lines
19 KiB
TypeScript
268 lines
19 KiB
TypeScript
import { test, expect } from "@playwright/test";
|
|
import { createVip98kChatModel } from "@/lib/ai/providers/vip98k-chat";
|
|
import { buildVip98kModelPresets } from "@/lib/ai/vip98k-models";
|
|
import { buildTaskBody } from "@/lib/ai/providers/vip98k";
|
|
import { encodeVip98kTaskBody, getVip98kImageBindingKeys, selectVip98kTaskInvocation } from "@/lib/ai/vip98k-task-invocation";
|
|
import { getAudioCheckpoint } from "@/lib/video-canvas/task-recovery";
|
|
import { audioRequestSchema } from "@/lib/video-canvas/audio-contract";
|
|
import { buildVip98kAudioBody, vip98kAudioContracts } from "@/lib/video-canvas/vip98k-audio-contract";
|
|
import { vip98kVideoDurationBounds } from "@/lib/ai/vip98k-video-parameters";
|
|
import { readVip98kAudioFile } from "@/lib/video-canvas/vip98k-audio-file";
|
|
import { getCanvasVideoParameters } from "@/lib/video-canvas/model-matching";
|
|
|
|
test("Anthropic-only routes preserve tool calls and relay routing", async () => {
|
|
let sent: any;
|
|
const model = createVip98kChatModel({ apiKey:"fixture", baseURL:"https://relay.example", modelId:"claude-haiku-5-5",
|
|
metadata:{invocation:{kind:"chat",path:"/v1/messages",headers:{"X-Relay-Route-Option":"native_route"}}},
|
|
fetch:async (url, init) => {
|
|
expect(String(url)).toBe("https://relay.example/v1/messages");
|
|
const headers = new Headers(init?.headers);
|
|
expect(headers.get("x-relay-route-option")).toBe("native_route");
|
|
expect(headers.get("authorization")).toBe("Bearer fixture");
|
|
sent=JSON.parse(String(init?.body));
|
|
return Response.json({id:"message_test",type:"message",role:"assistant",model:"claude-haiku-5-5",
|
|
content:[{type:"tool_use",id:"call_test",name:"record_result",input:{message:"ok"}}],
|
|
stop_reason:"tool_use",stop_sequence:null,usage:{input_tokens:10,output_tokens:8}});
|
|
},
|
|
});
|
|
const result = await model.doGenerate({ prompt:[{role:"user",content:[{type:"text",text:"Run the test tool"}]}],
|
|
tools:[{type:"function",name:"record_result",inputSchema:{type:"object",properties:{message:{type:"string"}},required:["message"]}}],toolChoice:{type:"auto"},maxOutputTokens:128 });
|
|
expect(sent.messages[0].content[0].text).toBe("Run the test tool");
|
|
expect(result.content).toContainEqual(expect.objectContaining({type:"tool-call",toolName:"record_result"}));
|
|
});
|
|
|
|
test("zero image slots and forbidden fields do not become reference support", () => {
|
|
const invocation = {kind:"tool",path:"/v1/images/generations",request_example:{model:"text_image",prompt:"a cup"},
|
|
input_schema:{properties:{file_urls:{type:"array",maxItems:0},image:false,partial_images:{type:"integer"}}}};
|
|
const [model] = buildVip98kModelPresets([{id:"text_image",type:"image",capabilities:["text_to_image"],invocation}]);
|
|
expect(model.capabilities).toMatchObject({maxReferenceImages:0,supportedModes:["text-to-image"]});
|
|
expect(model.displayName).toContain("仅文生图");
|
|
expect(getVip98kImageBindingKeys({requestExample:invocation.request_example,inputSchema:invocation.input_schema} as any)).toEqual([]);
|
|
});
|
|
|
|
const params = {apiKey:"fixture",modelId:"image",kind:"image",prompt:"a blue cup",imageUrls:[],videoUrls:[],modelConfig:{},imageParams:{aspectRatio:"16:9",imageSize:"2K"}};
|
|
const invocation = (properties: Record<string, unknown>, requestExample: Record<string, unknown> = {}) => ({kind:"tool",method:"POST",path:"/v1/images/generations",contentType:"application/json",headers:{},requestExample:{model:"image",prompt:"example",...requestExample},inputSchema:{properties},resultMethod:"GET",resultPath:""});
|
|
|
|
test("ratio-sized image contracts bind size and resolution without stale square examples", () => {
|
|
const result = buildTaskBody(params,invocation({size:{enum:["1:1","16:9"]},resolution:{enum:["1K","2K"]}},{size:"1:1",resolution:"1K"}));
|
|
expect(result.body).toMatchObject({size:"16:9",resolution:"2K",prompt:"a blue cup"});
|
|
});
|
|
test("pixel-only image contracts use a supported size in the requested tier", () => {
|
|
const result = buildTaskBody(params,invocation({size:{enum:["1024x1024","1280x720","2048x1152","3840x2160"]}},{size:"1024x1024"}));
|
|
expect(result.body.size).toBe("2048x1152");
|
|
});
|
|
|
|
test("square image dimensions agree with the requested resolution tier", () => {
|
|
const contract=invocation({size:{enum:["auto","1024x1024","2048x2048","4096x4096"]},resolution:{enum:["1K","2K","4K"]},aspect_ratio:{enum:["1:1"]}},{size:"1024x1024",resolution:"1K"});
|
|
for(const [imageSize,size] of [["1K","1024x1024"],["2K","2048x2048"],["4K","4096x4096"]]) {
|
|
const result=buildTaskBody({...params,modelId:"nano-banana-2",imageParams:{imageSize,aspectRatio:"1:1"}},contract);
|
|
expect(result.body).toMatchObject({size,resolution:imageSize,aspect_ratio:"1:1"});
|
|
}
|
|
});
|
|
|
|
test("4K uses the largest published square size for area-based image contracts", () => {
|
|
const result=buildTaskBody({...params,imageParams:{imageSize:"4K",aspectRatio:"1:1"}},invocation({size:{enum:["1024x1024","2048x2048","2880x2880"]},resolution:{enum:["1K","2K","4K"]}}));
|
|
expect(result.body).toMatchObject({size:"2880x2880",resolution:"4K"});
|
|
});
|
|
test("OpenAI image size fields receive pixel dimensions instead of a resolution label", () => {
|
|
const result = buildTaskBody({...params,modelId:"gpt-image-2"},invocation({size:{type:["string","null"]}}));
|
|
expect(result.body.size).toMatch(/^\d+x\d+$/);
|
|
});
|
|
test("task size patterns take precedence over ratio aliases in a catalog enum", () => {
|
|
const result = buildTaskBody({...params,imageParams:{imageSize:"1K",aspectRatio:"1:1"}},invocation({size:{enum:["1:1","1024x1024"],pattern:"^(auto|[1-9][0-9]*x[1-9][0-9]*)$"},resolution:{enum:["1K","2K"]}}));
|
|
expect(result.body.size).toBe("1024x1024");
|
|
});
|
|
test("image reference limits reject excess inputs before submission", () => {
|
|
expect(() => buildTaskBody({...params,imageUrls:["https://example.com/1.png","https://example.com/2.png"]},invocation({image:{type:"array",maxItems:1}},{image:[]}))).toThrow("最多支持 1 张");
|
|
});
|
|
|
|
test("standard JSON edits bind reference objects and take precedence over multipart examples", () => {
|
|
const json = { ...invocation({ images: { type: "array", items: { properties: { image_url: { type: "string" } } } } }, {
|
|
images: [{ image_url: "https://example.com/catalog.png" }],
|
|
}), path: "/v1/images/edits" };
|
|
const multipart = { ...json, contentType: "multipart/form-data", requestExample: { model: "image", prompt: "example", "image[]": "@./input.png" } };
|
|
expect(selectVip98kTaskInvocation({ modelId: "image", kind: "image", configuredInvocation: multipart, configuredInvocations: [multipart, json], imageCount: 2 })).toEqual(json);
|
|
const urls = ["https://example.com/one.png", "https://example.com/two.png"];
|
|
const { body } = buildTaskBody({ ...params, imageUrls: urls }, json);
|
|
expect(body.images).toEqual(urls.map(image_url => ({ image_url })));
|
|
expect(body).not.toHaveProperty("file_urls");
|
|
expect(body).not.toHaveProperty("image[]");
|
|
const encoded = encodeVip98kTaskBody(body, json.contentType);
|
|
expect(encoded.headers).toEqual({ "Content-Type": "application/json" });
|
|
expect(JSON.parse(String(encoded.body)).images).toEqual(body.images);
|
|
});
|
|
|
|
test("multipart edits replace file placeholders and let fetch provide the boundary", async () => {
|
|
const contract = { ...invocation({ "image[]": {} }, { "image[]": "@./input.png", stream: false }), contentType: "multipart/form-data", path: "/v1/images/edits" };
|
|
const urls = ["https://example.com/one.png", "https://example.com/two.png"];
|
|
const { body } = buildTaskBody({ ...params, imageUrls: urls }, contract);
|
|
const encoded = encodeVip98kTaskBody(body, contract.contentType);
|
|
const request = new Request("https://relay.example/v1/images/edits", { method: "POST", ...encoded });
|
|
expect(request.headers.get("content-type")).toContain("multipart/form-data; boundary=");
|
|
const form = await request.formData();
|
|
expect(form.getAll("image[]")).toEqual(urls);
|
|
expect(form.get("stream")).toBe("false");
|
|
expect(form.get("prompt")).toContain("a blue cup");
|
|
expect(form.has("file_urls")).toBe(false);
|
|
});
|
|
|
|
test("Seedance content binds prompts and reference roles", () => {
|
|
const i=invocation({content:{items:{oneOf:[{properties:{type:{const:"text"}}},{properties:{type:{const:"image_url"}}}]}},duration:{anyOf:[{const:-1},{minimum:4,maximum:30}]},resolution:{enum:["480P","720P"]},ratio:{enum:["16:9","9:16"]}});
|
|
const result=buildTaskBody({...params,kind:"video",imageUrls:["https://example.com/one.png","https://example.com/two.png"],videoParams:{mode:"first-last-frame",resolution:"480p",duration:4,ratio:"16:9"}},i);
|
|
expect(result.body).toMatchObject({resolution:"480P",duration:4,content:[{type:"text",text:"a blue cup"},{type:"image_url",role:"first_frame"},{type:"image_url",role:"last_frame"}]});
|
|
expect(result.imageBindingKeys).toContain("content");
|
|
expect(vip98kVideoDurationBounds(i.inputSchema)).toMatchObject({min:4,max:30});
|
|
});
|
|
|
|
test("published first-frame ratio constraints reach the canvas and submission", () => {
|
|
const schema = {
|
|
properties: {
|
|
content: { items: { oneOf: [
|
|
{ properties: { type: { const: "text" } } },
|
|
{ properties: { type: { const: "image_url" }, role: { enum: ["first_frame", "last_frame", "reference_image"] } } },
|
|
] } },
|
|
ratio: { enum: ["16:9", "1:1", "adaptive"] },
|
|
},
|
|
allOf: [{ allOf: [{
|
|
if: { properties: { content: { contains: { properties: { type: { const: "image_url" }, role: { const: "first_frame" } }, required: ["type"] } } } },
|
|
// biome-ignore lint/suspicious/noThenProperty: JSON Schema uses then for conditional constraints.
|
|
then: { properties: { ratio: { const: "adaptive" } } },
|
|
}] }],
|
|
};
|
|
const [model] = buildVip98kModelPresets([{ id: "video-fixture", type: "video", input_modes: ["image_url"], invocation: {
|
|
kind: "task", path: "/extensions/v1/tasks", input_schema: schema, request_example: { model: "video-fixture", ratio: "adaptive" },
|
|
} }]);
|
|
expect(getCanvasVideoParameters(model, "first-frame").ratios).toEqual(["adaptive"]);
|
|
expect(getCanvasVideoParameters(model, "first-last-frame").ratios).toEqual(["adaptive"]);
|
|
expect(getCanvasVideoParameters(model, "reference-images").ratios).toContain("16:9");
|
|
const config = { ...invocation(schema.properties), inputSchema: schema };
|
|
const request = { ...params, kind: "video", imageParams: {}, imageUrls: ["https://example.com/qa.png"], videoParams: { mode: "first-frame", ratio: "adaptive" } };
|
|
expect(buildTaskBody(request, config).body.ratio).toBe("adaptive");
|
|
expect(() => buildTaskBody({ ...request, videoParams: { mode: "first-frame", ratio: "16:9" } }, config)).toThrow("沿用原图比例");
|
|
const reference = buildTaskBody({ ...request, videoParams: { mode: "reference-images", ratio: "16:9" } }, config).body;
|
|
expect(reference.ratio).toBe("16:9");
|
|
expect(reference.content).toContainEqual({ type: "image_url", image_url: { url: "https://example.com/qa.png" }, role: "reference_image" });
|
|
});
|
|
|
|
test("all fourteen audio entries expose distinct supported modes", () => {
|
|
expect(Object.keys(vip98kAudioContracts)).toHaveLength(14);
|
|
expect(vip98kAudioContracts["mimo-v2.5-asr"].kind).toBe("asr");
|
|
expect(vip98kAudioContracts["mimo-v2.5-tts-voiceclone"].kind).toBe("voiceclone");
|
|
expect(vip98kAudioContracts["suno-v6"].kind).toBe("music");
|
|
});
|
|
test("reference-only video routes reject an empty image list before submission", () => {
|
|
expect(()=>buildTaskBody({...params,kind:"video"},invocation({file_urls:{type:"array",minItems:1,maxItems:1}}))).toThrow("参考图");
|
|
});
|
|
test("Speech archives return the audio member instead of the TAR bytes", () => {
|
|
const audio=Buffer.from("RIFF1234WAVEfmt fixture audio");
|
|
const header=Buffer.alloc(512);header.write("output.wav");header.write(audio.length.toString(8).padStart(11,"0"),124);header[156]=48;header.write("ustar",257);
|
|
const file=readVip98kAudioFile(Buffer.concat([header,audio,Buffer.alloc(512-audio.length),Buffer.alloc(1024)]));
|
|
expect(file.format).toBe("wav");expect(file.bytes).toEqual(audio);
|
|
expect(()=>readVip98kAudioFile(Buffer.from('{"error":"unavailable"}'))).toThrow("音频文件");
|
|
});
|
|
test("audio input requires a reference for cloning and maps the user's audio", () => {
|
|
const input=audioRequestSchema.parse({modelRef:"test",mode:"voiceclone",text:"你好"});
|
|
expect(()=>buildVip98kAudioBody(input,"mimo-v2.5-tts-voiceclone",{})).toThrow("参考音频");
|
|
const body=buildVip98kAudioBody({...input,referenceAudioUrl:"https://example.com/audio.wav"},"mimo-v2.5-tts-voiceclone",{},"data:audio/wav;base64,fixture");
|
|
expect(body.audio.voice).toBe("data:audio/wav;base64,fixture");
|
|
expect(body.messages[1]).toEqual({role:"assistant",content:"你好"});
|
|
});
|
|
test("Suno automatic music omits the gender parameter that requires custom lyrics mode", () => {
|
|
const input=audioRequestSchema.parse({modelRef:"test",mode:"music",text:"gentle piano",format:"mp3",instrumental:true});
|
|
const body=buildVip98kAudioBody(input,"suno-v5",{custom_mode:false,metadata:{vocal_gender:"m"}});
|
|
expect(body.custom_mode).toBe(false);expect(body.metadata.vocal_gender).toBeUndefined();
|
|
const song=buildVip98kAudioBody({...input,instrumental:false,lyrics:"阳光照进窗台"},"suno-v5",{custom_mode:false,metadata:{vocal_gender:"m"}});
|
|
expect(song).toMatchObject({custom_mode:true,prompt:"阳光照进窗台",style:"gentle piano",metadata:{vocal_gender:"f"}});
|
|
});
|
|
test("completed audio checkpoints retain the original provider and resumable task", () => {
|
|
const checkpoint={operation:"audio",provider:"vip98k",modelRef:"test",modelId:"gem-2.5-tts",providerId:"source",baseUrl:"https://relay.example",taskId:"original",url:"https://example.com/audio.wav",format:"wav"};
|
|
expect(getAudioCheckpoint({request:{kind:"audio",params:{modelRef:"test"}},result:{upstreamAudio:checkpoint}})).toEqual(checkpoint);
|
|
});
|
|
|
|
test("route-scoped image contracts take precedence over generic task examples", () => {
|
|
const selected = {...invocation({size:{type:'string'},image:{type:'array',maxItems:8}}),headers:{'X-Relay-Route-Option':'selected'}};
|
|
const generic = {...invocation({resolution:{enum:['1K','2K']},aspect_ratio:{enum:['1:1','16:9']},file_urls:{type:'array'}},{file_urls:[]}),kind:'task',path:'/extensions/v1/tasks'};
|
|
for(const imageCount of [0,1]) {
|
|
const resolved=selectVip98kTaskInvocation({modelId:'image',kind:'image',configuredInvocation:selected,configuredInvocations:[generic,selected],imageCount});
|
|
expect(resolved?.headers).toEqual({'X-Relay-Route-Option':'selected'});
|
|
}
|
|
});
|
|
|
|
test("native ratio and resolution contracts can use automatic pixels for non-square images", () => {
|
|
const contract=invocation({size:{enum:['auto','1024x1024','2048x2048','4096x4096']},resolution:{enum:['1K','2K','4K']},aspect_ratio:{enum:['1:1','16:9','9:16']}},{size:'1024x1024'});
|
|
for(const aspectRatio of ['16:9','9:16'])for(const imageSize of ['1K','2K','4K']) {
|
|
const result=buildTaskBody({...params,imageParams:{imageSize,aspectRatio}},contract);
|
|
expect(result.body).toMatchObject({size:'auto',resolution:imageSize,aspect_ratio:aspectRatio});
|
|
}
|
|
});
|
|
|
|
test("reference limits inside allOf are enforced before submission and during import", () => {
|
|
const schema={properties:{model:{type:'string'},prompt:{type:'string'},images:{allOf:[{type:'array',maxItems:16},{maxItems:14}]}}};
|
|
const contract={...invocation({},{images:[]}),inputSchema:schema};
|
|
const images=Array.from({length:15},(_,i)=>`https://fixture.example/${i}.png`);
|
|
expect(()=>buildTaskBody({...params,imageUrls:images},contract)).toThrow('14');
|
|
const [model]=buildVip98kModelPresets([{id:'image',type:'image',invocation:{kind:'tool',path:contract.path,input_schema:schema,request_example:contract.requestExample}}]);
|
|
expect(model.imageGenConfig?.maxReferenceImages).toBe(14);
|
|
});
|
|
|
|
test("quality and negative prompt have a single effective location", () => {
|
|
const metadata={quality:'high'};
|
|
const contract=invocation({quality:{const:'high'},negative_prompt:{type:'string'},negativePrompt:{type:'string'}},{metadata,negativePrompt:'example'});
|
|
const result=buildTaskBody({...params,imageParams:{quality:'auto',negativePrompt:'blurry'}},contract);
|
|
expect(result.body.metadata).toEqual({quality:'high'});
|
|
expect(result.body).not.toHaveProperty('quality');
|
|
expect(result.body.negative_prompt).toBe('blurry');
|
|
expect(result.body).not.toHaveProperty('negativePrompt');
|
|
expect(metadata).toEqual({quality:'high'});
|
|
});
|
|
|
|
test("fixed resolution edit contracts determine the imported default", () => {
|
|
for(const resolution of ['2K','4K']){
|
|
const [model] = buildVip98kModelPresets([{
|
|
id: 'fixed-image', type: 'image',
|
|
invocation: {
|
|
kind: 'tool', path: '/v1/images/generations',
|
|
request_example: { model: 'fixed-image', prompt: 'test' },
|
|
input_schema: { properties: { size: { type: 'string' } } },
|
|
},
|
|
invocations: [{
|
|
kind: 'tool', path: '/v1/images/edits',
|
|
request_example: { model: 'fixed-image', prompt: 'test', file_urls: [] },
|
|
input_schema: { properties: {
|
|
file_urls: { type: 'array', maxItems: 16 }, size: { enum: ['auto'] },
|
|
resolution: { enum: [resolution] }, aspect_ratio: { enum: ['1:1', '16:9'] },
|
|
} },
|
|
}],
|
|
}]);
|
|
expect(model.defaultParams.imageSize).toBe(resolution);
|
|
expect(model.imageGenConfig?.supportedImageSizes).toEqual([resolution]);
|
|
}
|
|
});
|
|
|
|
test("video duration choices follow the selected resolution", () => {
|
|
const schema={properties:{duration:{minimum:1,maximum:15}},allOf:[{if:{properties:{resolution:{const:'1080p'}}},then:{properties:{duration:{maximum:10}}}}]};
|
|
const model={providerType:'vip98k',capabilities:{kind:'video',durationOptions:[5,10,15],vip98k:{invocation:{inputSchema:schema}}}};
|
|
expect(getCanvasVideoParameters(model,'text-to-video','1080P').durations).toEqual([5,10]);
|
|
expect(getCanvasVideoParameters(model,'text-to-video','720p').durations).toEqual([5,10,15]);
|
|
});
|
|
|
|
test("generation controls preserve false and zero in the declared wire fields", () => {
|
|
const contract=invocation({seed:{type:'integer'},watermark:{type:'boolean'},prompt_extend:{type:'boolean'},return_last_frame:{type:'boolean'},camera_fixed:{type:'boolean'}},{metadata:{watermark:true}});
|
|
const image=buildTaskBody({...params,imageParams:{seed:0,watermark:false,promptExtend:false}},contract).body;
|
|
expect(image).toMatchObject({seed:0,prompt_extend:false,metadata:{watermark:false}});
|
|
expect(image).not.toHaveProperty('watermark');
|
|
const video=buildTaskBody({...params,kind:'video',videoParams:{seed:0,watermark:false,returnLastFrame:true,cameraFixed:false}},contract).body;
|
|
expect(video).toMatchObject({seed:0,return_last_frame:true,camera_fixed:false,metadata:{watermark:false}});
|
|
const unsupported=buildTaskBody({...params,imageParams:{seed:0,watermark:false}},invocation({seed:false,watermark:false})).body;
|
|
expect(unsupported).not.toHaveProperty('seed');
|
|
expect(unsupported).not.toHaveProperty('watermark');
|
|
});
|
|
|
|
test("fixed quality routes replace stale quality choices before submission", () => {
|
|
const contract=invocation({quality:{enum:['auto']}},{quality:'auto'});
|
|
for(const quality of ['auto','low','medium','high']) {
|
|
expect(buildTaskBody({...params,imageParams:{quality}},contract).body.quality).toBe('auto');
|
|
}
|
|
expect(()=>buildTaskBody({...params,imageParams:{quality:'invalid'}},invocation({quality:{enum:['low','high']}}))).toThrow('图片质量');
|
|
});
|