Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

154 lines
4.0 KiB
TypeScript

/**
* Unit tests for the URL helpers in `src/i18n/resolve.ts`.
*
* `localizePath` reads the resolved Astro i18n config from
* `astro:config/server` at runtime. In this unit-test environment
* the virtual module isn't resolvable, so these tests exercise the
* fallback path that reads from EmDash's runtime `setI18nConfig`.
* End-to-end coverage of the Astro virtual-module path lives in
* `tests/integration/seo/sitemap-route.test.ts` plus manual demo
* testing.
*/
import { afterEach, describe, expect, it } from "vitest";
import { setI18nConfig } from "../../../src/i18n/config.js";
import {
_resetAstroI18nCacheForTests,
interpolateUrlPattern,
localizePath,
} from "../../../src/i18n/resolve.js";
describe("interpolateUrlPattern", () => {
it("substitutes {slug} and {id}", () => {
expect(
interpolateUrlPattern({
pattern: "/blog/{slug}",
collection: "post",
slug: "hello",
id: "abc",
}),
).toBe("/blog/hello");
expect(
interpolateUrlPattern({
pattern: "/p/{id}",
collection: "post",
slug: "hello",
id: "abc",
}),
).toBe("/p/abc");
});
it("falls back to /{collection}/{slug} when no pattern is configured", () => {
expect(
interpolateUrlPattern({
pattern: null,
collection: "post",
slug: "hello",
id: "abc",
}),
).toBe("/post/hello");
});
it("URL-encodes slug and id segments", () => {
expect(
interpolateUrlPattern({
pattern: "/blog/{slug}",
collection: "post",
slug: "hello world",
id: "abc",
}),
).toBe("/blog/hello%20world");
});
it("collapses repeated slashes and trims trailing slash", () => {
expect(
interpolateUrlPattern({
pattern: "//blog//{slug}/",
collection: "post",
slug: "hello",
id: "abc",
}),
).toBe("/blog/hello");
});
it("ensures a leading slash", () => {
expect(
interpolateUrlPattern({
pattern: "blog/{slug}",
collection: "post",
slug: "hello",
id: "abc",
}),
).toBe("/blog/hello");
});
});
describe("localizePath", () => {
afterEach(() => {
setI18nConfig(null);
_resetAstroI18nCacheForTests();
});
it("returns path unchanged when i18n is disabled", async () => {
setI18nConfig(null);
expect(await localizePath("/blog/hello", "en")).toBe("/blog/hello");
});
it("returns path unchanged when only one locale is configured", async () => {
// isI18nEnabled() requires >1 locale.
setI18nConfig({ defaultLocale: "en", locales: ["en"] });
expect(await localizePath("/blog/hello", "en")).toBe("/blog/hello");
});
it("does not prefix the default locale when prefixDefaultLocale is false", async () => {
setI18nConfig({
defaultLocale: "en",
locales: ["en", "fr"],
prefixDefaultLocale: false,
});
expect(await localizePath("/blog/hello", "en")).toBe("/blog/hello");
});
it("prefixes non-default locales when prefixDefaultLocale is false", async () => {
setI18nConfig({
defaultLocale: "en",
locales: ["en", "fr"],
prefixDefaultLocale: false,
});
expect(await localizePath("/blog/hello", "fr")).toBe("/fr/blog/hello");
});
it("prefixes every locale when prefixDefaultLocale is true", async () => {
setI18nConfig({
defaultLocale: "en",
locales: ["en", "fr"],
prefixDefaultLocale: true,
});
expect(await localizePath("/blog/hello", "en")).toBe("/en/blog/hello");
expect(await localizePath("/blog/hello", "fr")).toBe("/fr/blog/hello");
});
it("normalises adjacent slashes and trailing slash from the result", async () => {
setI18nConfig({
defaultLocale: "en",
locales: ["en", "fr"],
prefixDefaultLocale: false,
});
expect(await localizePath("//blog/hello/", "fr")).toBe("/fr/blog/hello");
});
it("returns null for drifted/legacy locales not in the configured list", async () => {
setI18nConfig({
defaultLocale: "en",
locales: ["en", "fr"],
prefixDefaultLocale: false,
});
// `de` isn't configured. The sitemap route would emit a link
// to `/de/blog/hello`, but the site has no route there -- the
// caller should drop the entry instead.
expect(await localizePath("/blog/hello", "de")).toBeNull();
});
});