Files
yingxiangli-duanju/tests/normalize.test.mjs
2026-08-05 17:59:38 +08:00

57 lines
2.7 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import {
extractVideoTaskId,
extractVideoUrl,
normalizeAssetReference,
normalizeVideoStatus,
} from "../lib/normalize.mjs";
test("素材审核返回值统一为 asset 协议并保留大小写", () => {
assert.equal(normalizeAssetReference({ asset_url: "Asset://asset-1" }), "asset://asset-1");
assert.equal(normalizeAssetReference({ downstream_asset_id: "asset-2" }), "asset://asset-2");
assert.equal(normalizeAssetReference({ Id: "asset-3" }), "asset://asset-3");
assert.equal(normalizeAssetReference({ asset_id: "mat_ADyx4iQv" }), "asset://mat_ADyx4iQv");
});
test("兼容视频任务 id 和状态字段", () => {
assert.equal(extractVideoTaskId({ task_id: "task-1" }), "task-1");
assert.equal(extractVideoTaskId({ data: { id: "task-2" } }), "task-2");
assert.equal(normalizeVideoStatus({ status: "succeeded" }), "completed");
assert.equal(normalizeVideoStatus({ data: { status: "failed" } }), "failed");
});
test("从常见响应位置提取视频地址", () => {
assert.equal(extractVideoUrl({ output: { video_url: "https://example.com/demo.mp4" } }), "https://example.com/demo.mp4");
});
test("生成时长提供 5 到 14 秒全部整数选项", () => {
const html = readFileSync(new URL("../public/index.html", import.meta.url), "utf8");
const durationSelect = html.match(/<select id="duration">([\s\S]*?)<\/select>/)?.[1] || "";
const values = [...durationSelect.matchAll(/<option value="(\d+)">/g)].map((match) => Number(match[1]));
assert.deepEqual(values, [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
});
test("人物素材支持任选 1 到 3 张,且提供独立通用素材库", () => {
const html = readFileSync(new URL("../public/index.html", import.meta.url), "utf8");
assert.match(html, /data-module="generic-library"/);
assert.match(html, /id="genericAssetForm"/);
assert.match(html, /id="genericAssetType"/);
for (const id of ["assetFrontUrl", "assetSideUrl", "assetBackUrl"]) {
const input = html.match(new RegExp(`<input id="${id}"[^>]*>`))?.[0] || "";
assert.ok(input, `缺少 ${id}`);
assert.doesNotMatch(input, /\srequired(?:\s|>)/);
}
});
test("提供独立通用视频生成页签并允许不选择人物素材", () => {
const html = readFileSync(new URL("../public/index.html", import.meta.url), "utf8");
const server = readFileSync(new URL("../server.mjs", import.meta.url), "utf8");
assert.match(html, /data-module="general-video"/);
assert.match(html, /id="generalVideoForm"/);
assert.match(html, /id="generalAssetPicker"/);
assert.match(html, /id="generalResultList"/);
assert.doesNotMatch(server, /assetReferences\.length\s*<\s*1/);
});