From 911b8953123ed6d15216d3d8fa5767795180b007 Mon Sep 17 00:00:00 2001 From: Timothy Koech Date: Fri, 10 Jul 2026 13:16:32 +0300 Subject: [PATCH] refactor: update path normalization for language switching and add tests for story creation redirects --- src/frontend/shared/helpers.ts | 5 ++++- tests/unit/replaceLocaleInPath.spec.ts | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/frontend/shared/helpers.ts b/src/frontend/shared/helpers.ts index aabe7672..7df68df4 100644 --- a/src/frontend/shared/helpers.ts +++ b/src/frontend/shared/helpers.ts @@ -404,7 +404,7 @@ export function parentPathForBack(pathname: string): string | null { return null; } -/** Redirect chapter preview paths to the story index before switching locale. */ +/** Redirect unsafe paths to a locale-switchable parent before swapping locale. */ export function normalizePathForLanguageSwitch(pathname: string): string { const segments = pathname.split('/').filter(Boolean); if ( @@ -414,6 +414,9 @@ export function normalizePathForLanguageSwitch(pathname: string): string { ) { return `/${segments.slice(0, 3).join('/')}`; } + if (segments.length === 3 && segments[1] === 'story' && segments[2] === 'create') { + return `/${segments.slice(0, 2).join('/')}`; + } return pathname; } diff --git a/tests/unit/replaceLocaleInPath.spec.ts b/tests/unit/replaceLocaleInPath.spec.ts index ad6cf494..5f3fe1e9 100644 --- a/tests/unit/replaceLocaleInPath.spec.ts +++ b/tests/unit/replaceLocaleInPath.spec.ts @@ -105,11 +105,21 @@ test.describe('normalizePathForLanguageSwitch', () => { expect(normalizePathForLanguageSwitch('/en/dashboard')).toBe('/en/dashboard'); }); + test('redirects story create to story gallery', () => { + expect(normalizePathForLanguageSwitch('/en/story/create')).toBe('/en/story'); + }); + test('combined with replaceLocaleInPath redirects chapter to translated story index', () => { const pathname = '/en/story/1/chapter/3'; const normalized = normalizePathForLanguageSwitch(pathname); expect(replaceLocaleInPath(normalized, 'fr', locales)).toBe('/fr/story/1'); }); + + test('combined with replaceLocaleInPath redirects story create to translated story gallery', () => { + const pathname = '/en/story/create'; + const normalized = normalizePathForLanguageSwitch(pathname); + expect(replaceLocaleInPath(normalized, 'fr', locales)).toBe('/fr/story'); + }); }); test.describe('parentPathForBack', () => {