10 changed files with 146 additions and 14 deletions
@ -0,0 +1,28 @@ |
|||
import { describe, expect, it } from "vitest"; |
|||
import { normalizeReferenceSubjectListIds } from "../referenceSubjectList"; |
|||
|
|||
describe("normalizeReferenceSubjectListIds", () => { |
|||
it("maps reference subject ids to numeric sequence values", () => { |
|||
const parameters = normalizeReferenceSubjectListIds({ |
|||
referenceSubjectList: [ |
|||
{ id: "node-a", type: "image", url: "https://example.com/a.png", name: "图1" }, |
|||
{ id: "node-b", type: "video", url: "https://example.com/b.mp4", name: "视频2", duration: 3 }, |
|||
], |
|||
resolution: "720p", |
|||
}); |
|||
|
|||
expect(parameters).toEqual({ |
|||
referenceSubjectList: [ |
|||
{ id: 1, type: "image", url: "https://example.com/a.png", name: "图1" }, |
|||
{ id: 2, type: "video", url: "https://example.com/b.mp4", name: "视频2", duration: 3 }, |
|||
], |
|||
resolution: "720p", |
|||
}); |
|||
}); |
|||
|
|||
it("leaves parameters without reference subjects unchanged", () => { |
|||
const parameters = { resolution: "720p" }; |
|||
|
|||
expect(normalizeReferenceSubjectListIds(parameters)).toBe(parameters); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,17 @@ |
|||
export function normalizeReferenceSubjectListIds( |
|||
parameters: Record<string, unknown> | undefined |
|||
): Record<string, unknown> | undefined { |
|||
const referenceSubjectList = parameters?.referenceSubjectList; |
|||
if (!Array.isArray(referenceSubjectList)) return parameters; |
|||
|
|||
return { |
|||
...parameters, |
|||
referenceSubjectList: referenceSubjectList.map((item, index) => { |
|||
if (!item || typeof item !== "object" || Array.isArray(item)) return item; |
|||
return { |
|||
...item, |
|||
id: index + 1, |
|||
}; |
|||
}), |
|||
}; |
|||
} |
|||
Loading…
Reference in new issue