Files
homaroma/apps/backend/src/routes/pages.localization.test.ts
T
2026-07-24 21:45:44 -04:00

73 lines
2.2 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
mergeSectionTranslations,
transformLocalizedPaths,
} from "./pages";
describe("Page section localization", () => {
const paths = ["title", "items[].title", "items[].note"];
const payload = {
title: { th: "เอกสารอ้างอิง", en: "References" },
items: [{
title: { th: "เทคโนโลยีไฟน์แบลงก์", en: "Fine blanking technology" },
note: { th: "ข้อมูลทางเทคนิค" },
url: "https://example.com/reference",
}],
variant: "numbered",
};
test("resolves only the requested language and reports missing nested fields", () => {
const result = transformLocalizedPaths(payload, paths, "zh", false);
expect(result.payload).toEqual({
title: null,
items: [{
title: null,
note: null,
url: "https://example.com/reference",
}],
variant: "numbered",
});
expect(result.fallbackPaths).toEqual([
"title",
"items[0].title",
"items[0].note",
]);
});
test("merges one language while preserving other languages and configuration", () => {
const result = mergeSectionTranslations(payload, {
title: "参考资料",
items: [{
title: "精密冲裁技术",
note: "技术资料",
}],
}, paths, "zh");
expect(result.payload).toEqual({
title: { th: "เอกสารอ้างอิง", en: "References", zh: "参考资料" },
items: [{
title: {
th: "เทคโนโลยีไฟน์แบลงก์",
en: "Fine blanking technology",
zh: "精密冲裁技术",
},
note: { th: "ข้อมูลทางเทคนิค", zh: "技术资料" },
url: "https://example.com/reference",
}],
variant: "numbered",
});
expect(result.translatedPaths).toEqual([
"title",
"items[0].title",
"items[0].note",
]);
});
test("rejects an array shape that cannot be merged safely", () => {
expect(() => mergeSectionTranslations(payload, {
items: [],
}, paths, "zh")).toThrow("Translation array shape mismatch at items");
});
});