import { beforeEach, describe, expect, it, vi } from "vitest"; import type { SiteTypeConfig } from "../site-types"; const getSiteTypeMock = vi.hoisted(() => vi.fn()); vi.mock("../site-types", () => ({ getSiteType: getSiteTypeMock, })); import { buildCopyWithFallbacks, getSiteCopyFallbackKey } from "./site-settings-utils"; const siteTypeConfig: SiteTypeConfig = { enabled: true, name: "Portal", description: "Customer-facing portal", copyKeys: [ { section: "Hero", name: "Title", key: "heroTitle", fallback: "Welcome", }, ], }; describe("site settings i18n fallbacks", () => { beforeEach(() => { getSiteTypeMock.mockReset(); getSiteTypeMock.mockReturnValue(siteTypeConfig); }); it("derives stable copy fallback translation keys from the site type and copy key", () => { expect(getSiteCopyFallbackKey("customer-portal", "heroTitle")).toBe("models.site.types.customer-portal.copy.heroTitle.fallback"); }); it("preserves saved tenant copy before resolving fallback translations", () => { const t = vi.fn((key: string) => `translated:${key}`); expect(buildCopyWithFallbacks("portal", { heroTitle: "Tenant welcome" }, t)).toEqual({ heroTitle: "Tenant welcome", }); expect(t).not.toHaveBeenCalled(); }); it("resolves missing copy through the i18n fallback key", () => { const t = vi.fn((key: string) => (key === "models.site.types.portal.copy.heroTitle.fallback" ? "Willkommen" : key)); expect(buildCopyWithFallbacks("portal", {}, t)).toEqual({ heroTitle: "Willkommen", }); expect(t).toHaveBeenCalledWith("models.site.types.portal.copy.heroTitle.fallback"); }); it("keeps the config fallback as the last resort when an i18n key is missing", () => { const t = vi.fn((key: string) => key); expect(buildCopyWithFallbacks("portal", {}, t)).toEqual({ heroTitle: "Welcome", }); }); });