Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "@toolctrl/i18n",
|
||||
"name": "@hivegrid/i18n",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { anredeVariants, defaultAnrede, effectiveLng, formalLngs, isAnrede, supportedLocales } from "./index.js";
|
||||
|
||||
// Coverage gap: the formality (Anrede) axis — `isAnrede` and `effectiveLng`
|
||||
// (index.ts) had no direct test. This is the du/sie switch that maps a tenant's
|
||||
// formality choice onto an i18next "<locale>-formal" language variant.
|
||||
|
||||
describe("anrede axis constants", () => {
|
||||
test("exposes du/sie as the variant set with du as the additive default", () => {
|
||||
expect(anredeVariants).toEqual(["du", "sie"]);
|
||||
// "du" must stay the default so existing (informal) keys render byte-identical
|
||||
// without any tenant config — see the additive-design note in index.ts.
|
||||
expect(defaultAnrede).toBe("du");
|
||||
});
|
||||
|
||||
test("derives a formal language variant for every supported locale", () => {
|
||||
expect(formalLngs).toEqual(supportedLocales.map((locale) => `${locale}-formal`));
|
||||
expect(formalLngs).toEqual(["de-formal", "en-formal"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isAnrede", () => {
|
||||
test("accepts the known formality values", () => {
|
||||
expect(isAnrede("du")).toBe(true);
|
||||
expect(isAnrede("sie")).toBe(true);
|
||||
});
|
||||
|
||||
test("rejects unknown, empty and undefined values", () => {
|
||||
expect(isAnrede("Sie")).toBe(false); // case-sensitive, not normalized
|
||||
expect(isAnrede("formal")).toBe(false);
|
||||
expect(isAnrede("")).toBe(false);
|
||||
expect(isAnrede(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("effectiveLng", () => {
|
||||
test("returns the plain locale for the informal default (du)", () => {
|
||||
// No second arg → defaultAnrede ("du") → effective language is the bare locale.
|
||||
expect(effectiveLng("de")).toBe("de");
|
||||
expect(effectiveLng("en", "du")).toBe("en");
|
||||
});
|
||||
|
||||
test("maps the formal axis (sie) onto the <locale>-formal variant", () => {
|
||||
expect(effectiveLng("de", "sie")).toBe("de-formal");
|
||||
expect(effectiveLng("en", "sie")).toBe("en-formal");
|
||||
});
|
||||
|
||||
test("does not validate the locale string — it only suffixes it", () => {
|
||||
// effectiveLng is a pure string mapping; an unknown locale still gets the
|
||||
// "-formal" suffix under "sie" (validation is the caller's job).
|
||||
expect(effectiveLng("xx", "sie")).toBe("xx-formal");
|
||||
expect(effectiveLng("xx")).toBe("xx");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { createIcuFormatter, formatIcu } from "./plural.js";
|
||||
|
||||
// Coverage gap: `intl-messageformat`'s `format()` does NOT always return a
|
||||
// string. When a message interleaves literals with a non-string argument value
|
||||
// it returns an array of parts, and for a single non-string argument it returns
|
||||
// that raw value. Both `formatIcu` and `createIcuFormatter` normalize those
|
||||
// shapes back into a string (the `Array.isArray` join branch and the
|
||||
// `String(...)` fallback). These were the only uncovered branches in plural.ts.
|
||||
|
||||
describe("formatIcu — non-string format() output normalization", () => {
|
||||
test("joins multi-part (array) output into a single string", () => {
|
||||
// A boolean argument between two literals forces `format()` to emit an array
|
||||
// `["A ", true, " B"]` instead of a collapsed string.
|
||||
expect(formatIcu("A {flag} B", { flag: true }, { locale: "en-US" })).toBe("A true B");
|
||||
});
|
||||
|
||||
test("coerces a single non-string argument result to a string", () => {
|
||||
// A lone `{flag}` arg makes `format()` return the raw boolean — the helper
|
||||
// must stringify it rather than leak a non-string back to callers.
|
||||
expect(formatIcu("{flag}", { flag: true }, { locale: "en-US" })).toBe("true");
|
||||
});
|
||||
|
||||
test("throws on a structurally invalid plural message (missing other clause)", () => {
|
||||
// This is the contract `useTranslate` depends on for its catch-and-fallback:
|
||||
// a malformed plural must throw rather than silently mis-render.
|
||||
expect(() => formatIcu("{count, plural, one {# item}}", { count: 1 }, { locale: "en-US" })).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("createIcuFormatter — non-string format() output normalization", () => {
|
||||
test("joins multi-part (array) output into a single string", () => {
|
||||
const fmt = createIcuFormatter("A {flag} B", { locale: "en-US" });
|
||||
expect(fmt({ flag: true })).toBe("A true B");
|
||||
});
|
||||
|
||||
test("coerces a single non-string argument result to a string", () => {
|
||||
const fmt = createIcuFormatter("{flag}", { locale: "en-US" });
|
||||
expect(fmt({ flag: true })).toBe("true");
|
||||
});
|
||||
});
|
||||
@@ -24,7 +24,7 @@ function collectLeafKeys(value: JsonValue, prefix = ""): string[] {
|
||||
.sort();
|
||||
}
|
||||
|
||||
describe("@toolctrl/i18n", () => {
|
||||
describe("@hivegrid/i18n", () => {
|
||||
test("keeps German as the default and fallback locale", () => {
|
||||
expect(defaultLocale).toBe("de");
|
||||
expect(i18nConfig.fallbackLng).toBe("de");
|
||||
|
||||
@@ -29,6 +29,30 @@ export function isSupportedLocale(locale: string | undefined): locale is Support
|
||||
return Boolean(locale && supportedLocales.includes(locale));
|
||||
}
|
||||
|
||||
// Formality (Anrede) axis — orthogonal to locale. "du" is the base language
|
||||
// (existing keys), "sie" resolves to the "<locale>-formal" i18next variant
|
||||
// which holds only the formal overrides and falls back to the base locale via
|
||||
// the existing `fallbackLng` ("de"). This keeps the change purely additive:
|
||||
// with the default "du" the effective language is the plain locale, so existing
|
||||
// behaviour is byte-identical. Toggling a tenant to "sie" switches the
|
||||
// effective language to "de-formal"/"en-formal"; any key without a formal
|
||||
// override transparently falls back to the informal text.
|
||||
export const anredeVariants = ["du", "sie"] as const;
|
||||
export type Anrede = (typeof anredeVariants)[number];
|
||||
export const defaultAnrede: Anrede = "du";
|
||||
|
||||
/** Formal language variants for every supported locale ("de-formal", "en-formal"). */
|
||||
export const formalLngs = supportedLocales.map((locale) => `${locale}-formal`);
|
||||
|
||||
export function isAnrede(value: string | undefined): value is Anrede {
|
||||
return Boolean(value && (anredeVariants as readonly string[]).includes(value));
|
||||
}
|
||||
|
||||
/** Maps (locale, anrede) → effective i18next language. "sie" → "<locale>-formal", "du" → locale. */
|
||||
export function effectiveLng(locale: string, anrede: Anrede = defaultAnrede): string {
|
||||
return anrede === "sie" ? `${locale}-formal` : locale;
|
||||
}
|
||||
|
||||
const ltrLocales = new Set<string>(["de", "en"]);
|
||||
const rtlLocales = new Set<string>(["ar", "he", "fa", "ur"]);
|
||||
|
||||
@@ -48,7 +72,7 @@ export function isRTL(locale: string | undefined): boolean {
|
||||
// Foundation extensions (#72 i18n-foundation). Exports are namespaced via
|
||||
// `./intl` and `./icu` subpaths in `package.json#exports`, but we also
|
||||
// re-export the helpers here so callers can `import { formatCurrency } from
|
||||
// "@toolctrl/i18n"` without juggling subpaths.
|
||||
// "@hivegrid/i18n"` without juggling subpaths.
|
||||
|
||||
export {
|
||||
formatNumber,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { formatRelativeTime } from "./date.js";
|
||||
|
||||
// Coverage gap: the unit-selection cascade in `formatRelativeTime` was only
|
||||
// exercised for days/hours. These tests walk the larger units (year/month),
|
||||
// the sub-minute fallback to "second", the zero-delta case, and the
|
||||
// `relativeTo` overload variants (Date vs number vs default-now).
|
||||
|
||||
describe("formatRelativeTime — unit cascade", () => {
|
||||
test("selects years for multi-year deltas", () => {
|
||||
const now = new Date("2026-05-17T12:00:00Z");
|
||||
const earlier = new Date("2024-05-17T12:00:00Z");
|
||||
expect(formatRelativeTime(earlier, { locale: "en-US", relativeTo: now, numeric: "always" })).toBe("2 years ago");
|
||||
expect(formatRelativeTime(earlier, { locale: "de-DE", relativeTo: now, numeric: "always" })).toBe("vor 2 Jahren");
|
||||
});
|
||||
|
||||
test("selects months for deltas under a year but over a month", () => {
|
||||
const now = new Date("2026-05-17T12:00:00Z");
|
||||
const earlier = new Date("2026-03-17T12:00:00Z");
|
||||
expect(formatRelativeTime(earlier, { locale: "en-US", relativeTo: now, numeric: "always" })).toBe("2 months ago");
|
||||
});
|
||||
|
||||
test("falls through to seconds for sub-minute deltas", () => {
|
||||
const now = new Date("2026-05-17T12:00:30Z");
|
||||
const earlier = new Date("2026-05-17T12:00:00Z");
|
||||
expect(formatRelativeTime(earlier, { locale: "en-US", relativeTo: now, numeric: "always" })).toBe("30 seconds ago");
|
||||
});
|
||||
|
||||
test("renders a zero delta as the seconds floor", () => {
|
||||
const now = new Date("2026-05-17T12:00:00Z");
|
||||
// Identical instants → delta 0 → no unit threshold met until the trailing
|
||||
// `unit === "second"` short-circuit fires with a rounded value of 0.
|
||||
expect(formatRelativeTime(now, { locale: "en-US", relativeTo: now, numeric: "always" })).toBe("in 0 seconds");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatRelativeTime — relativeTo overloads", () => {
|
||||
test("accepts a numeric (epoch ms) anchor", () => {
|
||||
const now = new Date("2026-05-17T12:00:00Z");
|
||||
const earlier = new Date("2026-05-14T12:00:00Z");
|
||||
expect(formatRelativeTime(earlier, { locale: "en-US", relativeTo: now.getTime(), numeric: "always" })).toBe("3 days ago");
|
||||
});
|
||||
|
||||
test("accepts a numeric (epoch ms) target value", () => {
|
||||
const now = new Date("2026-05-17T12:00:00Z");
|
||||
const earlier = new Date("2026-05-14T12:00:00Z");
|
||||
expect(formatRelativeTime(earlier.getTime(), { locale: "en-US", relativeTo: now, numeric: "always" })).toBe("3 days ago");
|
||||
});
|
||||
|
||||
test("anchors to the current time when relativeTo is omitted", () => {
|
||||
// No `relativeTo` → anchor falls back to `Date.now()`. A target three hours
|
||||
// in the future stays well clear of the rounding boundary, so the assertion
|
||||
// is stable despite the few-ms gap between target build and now().
|
||||
const future = new Date(Date.now() + 3 * 60 * 60 * 1000);
|
||||
expect(formatRelativeTime(future, { locale: "en-US", numeric: "always" })).toBe("in 3 hours");
|
||||
});
|
||||
});
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { createUseTranslate, type ReactI18nextLike } from "./useTranslate.js";
|
||||
|
||||
// Coverage gap: the resilience paths of the translator returned by
|
||||
// `createUseTranslate` — the ICU catch-and-fallback (lines 58-59), the
|
||||
// `icu: false` opt-out, and the non-string coercion of a raw `t()` result.
|
||||
|
||||
function makeStubDeps(translations: Record<string, unknown>, language: string): ReactI18nextLike {
|
||||
return {
|
||||
useTranslation: () => ({
|
||||
// `t` is typed to return string; we deliberately allow non-string values
|
||||
// through the stub to exercise the defensive coercion branch.
|
||||
t: ((key: string) => translations[key] ?? key) as (key: string, options?: Record<string, unknown>) => string,
|
||||
i18n: { language },
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
describe("createUseTranslate — ICU fallback", () => {
|
||||
test("ships the raw message instead of crashing when ICU parsing fails", () => {
|
||||
// The message is detected as ICU (matches the plural pattern) but is
|
||||
// structurally invalid (no `other` clause), so `formatIcu` throws. The
|
||||
// translator must fall through and return the source string.
|
||||
const malformed = "{count, plural, one {# item}}";
|
||||
const deps = makeStubDeps({ broken: malformed }, "en-US");
|
||||
const t = createUseTranslate(deps)();
|
||||
expect(t("broken", { count: 1 })).toBe(malformed);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createUseTranslate — icu opt-out", () => {
|
||||
test("bypasses the ICU formatter entirely when icu:false", () => {
|
||||
// With ICU disabled, even a well-formed ICU message is returned verbatim
|
||||
// from i18next rather than being rendered.
|
||||
const icuMessage = "{count, plural, one {# item} other {# items}}";
|
||||
const deps = makeStubDeps({ items: icuMessage }, "en-US");
|
||||
const t = createUseTranslate(deps, { icu: false })();
|
||||
expect(t("items", { count: 5 })).toBe(icuMessage);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createUseTranslate — non-string coercion", () => {
|
||||
test("coerces a non-string t() result to a string", () => {
|
||||
// i18next can return non-strings (e.g. with returnObjects); the translator
|
||||
// contract is `=> string`, so the value must be stringified.
|
||||
const deps = makeStubDeps({ count: 42 }, "en-US");
|
||||
const t = createUseTranslate(deps)();
|
||||
expect(t("count")).toBe("42");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user