fix: validate byteplus video duration metadata

This commit is contained in:
Peter Steinberger
2026-05-28 17:54:35 -04:00
parent 04c2982535
commit 938b2a84dd
2 changed files with 44 additions and 1 deletions

View File

@@ -177,6 +177,42 @@ describe("byteplus video generation provider", () => {
expect(requireBytePlusPostBody()).not.toHaveProperty("duration");
});
it("drops malformed response duration metadata", async () => {
postJsonRequestMock.mockResolvedValue({
response: {
json: async () => ({
id: "task_123",
}),
},
release: vi.fn(async () => {}),
});
fetchWithTimeoutMock
.mockResolvedValueOnce({
json: async () => ({
id: "task_123",
status: "succeeded",
content: {
video_url: "https://example.com/byteplus.mp4",
},
duration: 1.5,
}),
})
.mockResolvedValueOnce({
headers: new Headers({ "content-type": "video/mp4" }),
arrayBuffer: async () => Buffer.from("mp4-bytes"),
});
const provider = buildBytePlusVideoGenerationProvider();
const result = await provider.generateVideo({
provider: "byteplus",
model: "seedance-1-0-lite-t2v-250428",
prompt: "A lantern floats upward into the night sky",
cfg: {},
});
expect(result.metadata).toMatchObject({ duration: undefined });
});
it("reports malformed create JSON with a provider-owned error", async () => {
const release = vi.fn(async () => {});
postJsonRequestMock.mockResolvedValue({

View File

@@ -137,6 +137,13 @@ function resolveBytePlusDurationSeconds(value: unknown): number | undefined {
});
}
function readBytePlusDurationSeconds(value: unknown): number | undefined {
return asSafeIntegerInRange(value, {
min: BYTEPLUS_MIN_DURATION_SECONDS,
max: BYTEPLUS_MAX_DURATION_SECONDS,
});
}
async function pollBytePlusTask(params: {
taskId: string;
headers: Headers;
@@ -396,7 +403,7 @@ export function buildBytePlusVideoGenerationProvider(): VideoGenerationProvider
videoUrl,
ratio: normalizeOptionalString(completed.ratio),
resolution: normalizeOptionalString(completed.resolution),
duration: typeof completed.duration === "number" ? completed.duration : undefined,
duration: readBytePlusDurationSeconds(completed.duration),
},
};
} finally {