Files
omniai-ds-code-package/src/features/ecommerce/utils/platformRules.test.ts
T

96 lines
3.7 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import {
defaultCloneOutput,
defaultEcommercePlatform,
defaultProductSetOutput,
formatUploadedImageRatio,
getPlatformDefaultLanguage,
getPlatformDefaultRatio,
getPlatformLanguageOptions,
getPlatformRatioOptions,
getUniqueRatioOptions,
normalizeLanguageForPlatform,
normalizeMarket,
normalizePlatform,
normalizeRatioForPlatform,
platformOptions,
} from "./platformRules";
describe("platform defaults", () => {
it("exposes the default ecommerce platform and outputs", () => {
expect(defaultEcommercePlatform).toBe("淘宝/天猫");
expect(defaultProductSetOutput).toBe("set");
expect(defaultCloneOutput).toBe("set");
});
it("lists platform labels for UI selectors", () => {
expect(platformOptions).toContain("淘宝/天猫");
expect(platformOptions).toContain("亚马逊 Amazon");
expect(platformOptions).toContain("TikTok Shop");
});
});
describe("normalizePlatform", () => {
it("normalizes legacy labels", () => {
expect(normalizePlatform("亚马逊Amazon")).toBe("亚马逊 Amazon");
expect(normalizePlatform("亚马逊")).toBe("亚马逊 Amazon");
});
it("falls back to the default platform for unknown labels", () => {
expect(normalizePlatform("unknown")).toBe("淘宝/天猫");
});
});
describe("platform ratios", () => {
it("returns mode-specific ratios", () => {
expect(getPlatformRatioOptions("淘宝/天猫", "set")).toContain("1000×1000px\u00a0\u00a0\u00a01:1");
expect(getPlatformDefaultRatio("淘宝/天猫", "video")).toBe("1080×1920px\u00a0\u00a0\u00a09:16");
});
it("normalizes an existing or partially matching ratio for a platform", () => {
expect(normalizeRatioForPlatform("淘宝/天猫", "1000×1000px\u00a0\u00a0\u00a01:1", "set")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
expect(normalizeRatioForPlatform("淘宝/天猫", "1000×1000px", "set")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
});
it("falls back to the mode default when no ratio matches", () => {
expect(normalizeRatioForPlatform("淘宝/天猫", "nope", "set")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
});
it("deduplicates ratio lists without changing order", () => {
expect(getUniqueRatioOptions(["1:1", "3:4", "1:1"])).toEqual(["1:1", "3:4"]);
});
});
describe("market and language rules", () => {
it("normalizes unknown markets to the default country", () => {
expect(normalizeMarket("火星")).toBe("中国");
});
it("uses Chinese by default for domestic platforms", () => {
expect(getPlatformDefaultLanguage("淘宝/天猫", "美国")).toBe("中文");
});
it("includes English for domestic platforms while preserving local languages", () => {
expect(getPlatformLanguageOptions("淘宝/天猫", "美国")).toEqual(["中文", "英文"]);
});
it("uses market languages for cross-border platforms", () => {
expect(getPlatformDefaultLanguage("亚马逊 Amazon", "日本")).toBe("日文");
});
it("normalizes language aliases and falls back when not available", () => {
expect(normalizeLanguageForPlatform("亚马逊 Amazon", "日本", "日语")).toBe("日文");
expect(normalizeLanguageForPlatform("亚马逊 Amazon", "日本", "德语")).toBe("日文");
});
});
describe("formatUploadedImageRatio", () => {
it("formats dimensions and aspect ratio", () => {
expect(formatUploadedImageRatio({ width: 750, height: 1000, format: "PNG" })).toBe("上传图片 750×1000px\u00a0\u00a0\u00a03:4\u00a0\u00a0\u00a0PNG");
});
it("falls back to original ratio when dimensions are missing", () => {
expect(formatUploadedImageRatio({ format: "JPG" })).toBe("上传图片\u00a0\u00a0\u00a0原图比例\u00a0\u00a0\u00a0JPG");
});
});