diff --git a/src/app/pages/pathUtils.test.ts b/src/app/pages/pathUtils.test.ts index 141ee990e..3928d7750 100644 --- a/src/app/pages/pathUtils.test.ts +++ b/src/app/pages/pathUtils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { getSettingsPath } from './pathUtils'; +import { getAppPathFromHref, getSettingsPath } from './pathUtils'; describe('getSettingsPath', () => { it('returns the settings root path', () => { @@ -13,3 +13,33 @@ describe('getSettingsPath', () => { ); }); }); + +describe('getAppPathFromHref', () => { + it('extracts the app path for a matching browser-router origin', () => { + expect(getAppPathFromHref('https://app.sable.moe/', 'https://app.sable.moe/')).toBe('/'); + expect(getAppPathFromHref('https://app.sable.moe/', 'https://app.sable.moe/login')).toBe( + '/login' + ); + expect( + getAppPathFromHref('https://app.sable.moe/', 'https://app.sable.moe/home/room/%21abc') + ).toBe('/home/room/%21abc'); + }); + + it('extracts the app path for a matching hash-router origin', () => { + expect(getAppPathFromHref('https://app.sable.moe/#/', 'https://app.sable.moe/#/')).toBe('/'); + expect( + getAppPathFromHref('https://app.sable.moe/#/', 'https://app.sable.moe/#/login?code=c&state=s') + ).toBe('/login?code=c&state=s'); + }); + + it('returns empty when the href origin does not match the base', () => { + expect(getAppPathFromHref('https://app.sable.moe/', 'https://tauri.localhost/')).toBe(''); + expect( + getAppPathFromHref('https://app.sable.moe/', 'https://tauri.localhost/login?code=c&state=s') + ).toBe(''); + }); + + it('returns empty when a hash-router base is paired with a hashless href', () => { + expect(getAppPathFromHref('https://app.sable.moe/#/', 'https://tauri.localhost/')).toBe(''); + }); +}); diff --git a/src/app/pages/pathUtils.ts b/src/app/pages/pathUtils.ts index f3f9aef21..37cfc0606 100644 --- a/src/app/pages/pathUtils.ts +++ b/src/app/pages/pathUtils.ts @@ -64,16 +64,25 @@ export const getAppPathFromHref = (baseUrl: string, href: string): string => { const baseHashIndex = baseUrl.indexOf('#'); if (baseHashIndex > -1) { const hrefHashIndex = href.indexOf('#'); + if (hrefHashIndex === -1) return ''; + // href may/not have "/" around "#" // we need to take care of this when extracting app path const trimmedBaseUrl = trimLeadingSlash(baseUrl.slice(baseHashIndex + 1)); const trimmedHref = trimLeadingSlash(href.slice(hrefHashIndex + 1)); - const appPath = trimmedHref.slice(trimmedBaseUrl.length); + // On Tauri the base origin is synthetic and never matches the href origin; guard + // against slicing a basename the href does not start with. + const appPath = trimmedHref.startsWith(trimmedBaseUrl) + ? trimmedHref.slice(trimmedBaseUrl.length) + : ''; return `/${trimLeadingSlash(appPath)}`; } - return href.slice(trimTrailingSlash(baseUrl).length); + // On Tauri the base origin is synthetic and never matches the href origin; return + // empty instead of slicing a garbage path that would be stored as a redirect. + const trimmedBaseUrl = trimTrailingSlash(baseUrl); + return href.startsWith(trimmedBaseUrl) ? href.slice(trimmedBaseUrl.length) : ''; }; export const getRootPath = (): string => ROOT_PATH;