168 lines
13 KiB
TypeScript
168 lines
13 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("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);
|
|
});
|