Commit 55070e612 for llama.cpp
commit 55070e612cd90901de4be909e4020b8e3f634f17
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date: Wed Dec 2 15:41:27 2026 +0100
ui: keep a single history entry in the installed app, so the edge swipe opens the sidebar (#33535)
* ui: keep a single history entry in the installed app
In standalone PWA mode every navigation replaces the current history
entry (goto through #lib/utils/navigation.ts, links through
data-sveltekit-replacestate on <body>), so iOS offers no edge swipe to
go back and the left edge stays free for dragging the sidebar open.
Assisted-by: Claude Code (Claude Opus 5.5)
* ui: return to the page search came from in the installed app, and fix lint
The installed app keeps one history entry, so the search page's back
button can no longer rely on history.length: it now goes back to the
page search was opened from. Also sorts the swapped imports and wraps
the lint rule's message for Prettier.
Assisted-by: Claude Code (Claude Opus 5.5)
* ui: test the installed app's goto, keep its default for an undefined replace, catch dynamic imports
- goto keeps the installed-app default when a caller passes replace as
undefined, so a forwarded options object can't turn it off
- a client test covers goto's replace in and out of the installed app,
a caller's own replace, and the link option on <body>
- the lint rule also catches import('$app/navigation'), and both rules
share one message
Assisted-by: Claude Code (Claude Opus 5.5)
diff --git a/ui/apps/chat/AGENTS.md b/ui/apps/chat/AGENTS.md
index 762b1f325..79e9f5ed8 100644
--- a/ui/apps/chat/AGENTS.md
+++ b/ui/apps/chat/AGENTS.md
@@ -6,6 +6,7 @@ These add to the repository's [AGENTS.md](../../../AGENTS.md) for work in `ui/ap
- **Demos, checks and docs media:** [skills/ui-demo](../../../skills/ui-demo/SKILL.md) records a PR's demo video, turns its flow into an e2e check and captures docs videos and screenshots, for Llama Chat and Llama UI alike.
- **Stories:** a story lives with the code it shows. Llama UI's (its foundations, base components, app components and Model UI) are in `ui/lib/ui/tests/stories`, titled `Llama UI/...`, and run as the library's tests; this app's Storybook shows them too. Llama Chat's are in `tests/stories/<area>`, one folder per `src/lib/components/app/<area>` (`chat/` has one per Chat folder), titled `Llama Chat/<Area>/<Component>`. How the app works on a phone (the sidebar's keys, the sheets and the pages each pushes) is written up in `tests/stories/mobile`, titled `Llama Chat/Mobile/Flows`: update it when a sheet gains or loses a page. An accessibility story sits next to its component's, as `<Component>.a11y.stories.svelte`, titled `.../<Component>/Accessibility`. The story helpers both Storybooks use (the Editorial the prose pages are set in, `StateGallery`) live with the library's stories and are imported here as `$llama-ui-stories/...`; reuse them rather than copying them here.
- **Releases:** `@ggml-org/llama-chat` and `@ggml-org/llama-ui` are versioned by hand and published by CI when a new version reaches `master` (see [Releases](../../README.md#releases)). Bump a version only when asked to release, never as a side effect of a change; Chat's version and its alias's (`alias/package.json`) always move together.
+- **Navigation:** navigate with `goto` from `#lib/utils/navigation.ts`, never `$app/navigation`'s directly (a `no-restricted-imports` lint rule enforces it). In an installed app (standalone PWA) every navigation replaces the current history entry, and the root layout sets `data-sveltekit-replacestate` on `<body>` for links, so history never grows: iOS then offers no edge swipe to go back and the left edge stays free for dragging the sidebar open. A browser tab keeps normal history.
- **Components:** the `SKILL.md` next to a component folder (such as [src/lib/components/app/SKILL.md](src/lib/components/app/SKILL.md)) holds that folder's conventions. A component whose machinery is hard to read keeps its own `AGENTS.md` beside it, such as [the Markdown editor's](src/lib/components/app/content/MarkdownEditor/AGENTS.md).
## Content is Markdown, custom pieces are Svelte: .svx
diff --git a/ui/apps/chat/eslint.config.js b/ui/apps/chat/eslint.config.js
index 330551d9d..c34d898c1 100644
--- a/ui/apps/chat/eslint.config.js
+++ b/ui/apps/chat/eslint.config.js
@@ -11,6 +11,9 @@ import globals from 'globals';
import { fileURLToPath } from 'node:url';
import ts from 'typescript-eslint';
+// An installed app keeps a single history entry, which only the app's own goto takes care of
+const GOTO_IMPORT_MESSAGE =
+ "Import goto from '#lib/utils/navigation.ts', which keeps the installed app's history to one entry.";
const svelteConfig = (await loadConfig('./', { traverse: false }))?.config;
const gitignorePath = fileURLToPath(new URL('./.gitignore', import.meta.url));
// Require a blank line between sibling element-like nodes in a Svelte template
@@ -191,6 +194,26 @@ export default ts.config(
'local/blank-line-between-accessors': 'error',
// Require a blank line between sibling elements in a Svelte template
'local/padding-line-between-elements': 'error',
+ 'no-restricted-imports': [
+ 'error',
+ {
+ paths: [
+ {
+ importNames: ['goto'],
+ message: GOTO_IMPORT_MESSAGE,
+ name: '$app/navigation'
+ }
+ ]
+ }
+ ],
+ // no-restricted-imports only sees static imports; this catches `import('$app/navigation')`
+ 'no-restricted-syntax': [
+ 'error',
+ {
+ message: GOTO_IMPORT_MESSAGE,
+ selector: "ImportExpression[source.value='$app/navigation']"
+ }
+ ],
// typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.
// see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors
'no-undef': 'off',
@@ -321,6 +344,11 @@ export default ts.config(
}
}
},
+ {
+ // the one place that wraps SvelteKit's goto, and its test
+ files: ['src/lib/utils/navigation.ts', 'tests/client/navigation.svelte.test.ts'],
+ rules: { 'no-restricted-imports': 'off', 'no-restricted-syntax': 'off' }
+ },
{
// Exclude generated build output and Storybook files from ESLint
ignores: [
diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte
index a66c07d5e..3720c3dfd 100644
--- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte
+++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte
@@ -30,7 +30,7 @@
} from '#lib/types/index.ts';
import { parseFilesToMessageExtras } from '#lib/utils/browser-only.ts';
import { deriveAgenticSections } from '#lib/utils/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
interface Props {
class?: string;
diff --git a/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte b/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte
index 5a7a6ba4d..9c8720c68 100644
--- a/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte
+++ b/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte
@@ -2,7 +2,7 @@
import * as AlertDialog from '@ggml-org/llama-ui/ui/alert-dialog';
import { AlertTriangle, ArrowRight } from '@lucide/svelte';
import { ICON_CLASS_DEFAULT, URL_PARAMS } from '#lib/constants/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { page } from '$app/state';
interface Props {
diff --git a/ui/apps/chat/src/lib/components/app/dialogs/DialogSearch.svelte b/ui/apps/chat/src/lib/components/app/dialogs/DialogSearch.svelte
index 4bc6fa8f5..a952faeb5 100644
--- a/ui/apps/chat/src/lib/components/app/dialogs/DialogSearch.svelte
+++ b/ui/apps/chat/src/lib/components/app/dialogs/DialogSearch.svelte
@@ -43,7 +43,7 @@
import type { HfModelInfo } from '#lib/types/huggingface.js';
import type { ConversationSearchSnippet } from '#lib/utils/conversation-search.ts';
import { formatRelativeTime, isIMEComposing, orgOf } from '#lib/utils/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { type Component, untrack } from 'svelte';
interface Props {
diff --git a/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte b/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte
index 2193aef89..cb1bf33fb 100644
--- a/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte
+++ b/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte
@@ -44,7 +44,7 @@
uiStore
} from '#lib/stores/index.ts';
import { buildConversationTree, hapticTap, remToPx } from '#lib/utils/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { page } from '$app/state';
import { tick, untrack } from 'svelte';
import { SvelteSet } from 'svelte/reactivity';
diff --git a/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte b/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte
index 820eb29cb..6463e0acf 100644
--- a/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte
+++ b/ui/apps/chat/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte
@@ -11,7 +11,7 @@
import { deviceStore, uiStore } from '#lib/stores/index.ts';
import type { DesktopIconStripItem } from '#lib/types/index.ts';
import { hapticTap } from '#lib/utils/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { page } from '$app/state';
import type { Component } from 'svelte';
import { onMount } from 'svelte';
diff --git a/ui/apps/chat/src/lib/components/app/servers/ServerErrorSplash.svelte b/ui/apps/chat/src/lib/components/app/servers/ServerErrorSplash.svelte
index 620c2bff8..1c458a426 100644
--- a/ui/apps/chat/src/lib/components/app/servers/ServerErrorSplash.svelte
+++ b/ui/apps/chat/src/lib/components/app/servers/ServerErrorSplash.svelte
@@ -6,8 +6,8 @@
import { HEADERS, ICON_CLASS_DEFAULT, ROUTES, SETTINGS_KEYS } from '#lib/constants/index.ts';
import { KeyboardKey } from '#lib/enums/index.ts';
import { serverStore, settingsStore } from '#lib/stores/index.ts';
+ import { goto } from '#lib/utils/navigation.ts';
import { serverUrl } from '#lib/utils/server-config.ts';
- import { goto } from '$app/navigation';
import { fade, fly, scale } from 'svelte/transition';
interface Props {
diff --git a/ui/apps/chat/src/lib/components/app/settings/SettingsMcpServers.svelte b/ui/apps/chat/src/lib/components/app/settings/SettingsMcpServers.svelte
index 316939d80..800ae74c1 100644
--- a/ui/apps/chat/src/lib/components/app/settings/SettingsMcpServers.svelte
+++ b/ui/apps/chat/src/lib/components/app/settings/SettingsMcpServers.svelte
@@ -9,7 +9,7 @@
import { McpServerCard, McpServerCardSkeleton } from '#lib/components/app/index.ts';
import { HealthCheckStatus } from '#lib/enums/index.ts';
import { mcpStore, toolsStore } from '#lib/stores/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { page } from '$app/state';
import { onMount } from 'svelte';
import { fade } from 'svelte/transition';
diff --git a/ui/apps/chat/src/lib/constants/pwa.constants.ts b/ui/apps/chat/src/lib/constants/pwa.constants.ts
index dc2b6d4fe..c61a5c041 100644
--- a/ui/apps/chat/src/lib/constants/pwa.constants.ts
+++ b/ui/apps/chat/src/lib/constants/pwa.constants.ts
@@ -12,6 +12,12 @@ export const MEDIA_QUERIES = {
PREFERS_LIGHT: '(prefers-color-scheme: light)'
} as const;
+/**
+ * SvelteKit's link option that replaces the current history entry instead of adding one. It is
+ * read from the clicked link and its ancestors, so setting it on <body> covers every link.
+ */
+export const REPLACE_HISTORY_LINK_ATTRIBUTE = 'data-sveltekit-replacestate';
+
export const THEME_COLORS = {
ACCENT_BLUE: '#2563eb',
ACCENT_BLUE_HOVER: '#1d4ed8',
diff --git a/ui/apps/chat/src/lib/hooks/use-drop-import.svelte.ts b/ui/apps/chat/src/lib/hooks/use-drop-import.svelte.ts
index 1f037c55e..ef804df47 100644
--- a/ui/apps/chat/src/lib/hooks/use-drop-import.svelte.ts
+++ b/ui/apps/chat/src/lib/hooks/use-drop-import.svelte.ts
@@ -20,7 +20,7 @@ import { groupsStore } from '#lib/stores/groups.svelte.ts';
import { settingsStore } from '#lib/stores/settings/index.svelte.ts';
import type { SettingsConfigType, SettingsExportType } from '#lib/types/index.ts';
import { createMessageCountMap } from '#lib/utils/index.ts';
-import { goto } from '$app/navigation';
+import { goto } from '#lib/utils/navigation.ts';
import { strFromU8 } from 'fflate';
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
import { toast } from 'svelte-sonner';
diff --git a/ui/apps/chat/src/lib/stores/conversations/index.svelte.ts b/ui/apps/chat/src/lib/stores/conversations/index.svelte.ts
index 4aace719c..6162caf5f 100644
--- a/ui/apps/chat/src/lib/stores/conversations/index.svelte.ts
+++ b/ui/apps/chat/src/lib/stores/conversations/index.svelte.ts
@@ -27,9 +27,9 @@ import {
findLeafNode,
generateConversationTitle
} from '#lib/utils/index.ts';
+import { goto } from '#lib/utils/navigation.ts';
import { withSearchChatsDisabled } from '#lib/utils/search-chats.ts';
import { browser } from '$app/env';
-import { goto } from '$app/navigation';
import { SvelteSet } from 'svelte/reactivity';
import { toast } from 'svelte-sonner';
diff --git a/ui/apps/chat/src/lib/stores/tabs.svelte.ts b/ui/apps/chat/src/lib/stores/tabs.svelte.ts
index d326d0fed..25ab1400e 100644
--- a/ui/apps/chat/src/lib/stores/tabs.svelte.ts
+++ b/ui/apps/chat/src/lib/stores/tabs.svelte.ts
@@ -24,8 +24,8 @@ import {
} from '#lib/constants/index.ts';
import { RouterService } from '#lib/services/router.service.ts';
import type { TabView } from '#lib/utils/chat-tabs.ts';
+import { goto } from '#lib/utils/navigation.ts';
import { browser } from '$app/env';
-import { goto } from '$app/navigation';
import { untrack } from 'svelte';
export class TabsStore {
diff --git a/ui/apps/chat/src/lib/stores/ui.svelte.ts b/ui/apps/chat/src/lib/stores/ui.svelte.ts
index da9eeea05..aaec13bca 100644
--- a/ui/apps/chat/src/lib/stores/ui.svelte.ts
+++ b/ui/apps/chat/src/lib/stores/ui.svelte.ts
@@ -22,8 +22,8 @@ import { deviceStore } from '#lib/stores/device.svelte.ts';
import { settingsStore } from '#lib/stores/settings/index.svelte.ts';
import type { Group } from '#lib/types/groups.js';
import { remToPx } from '#lib/utils/css.ts';
+import { goto } from '#lib/utils/navigation.ts';
import { browser } from '$app/env';
-import { goto } from '$app/navigation';
import { page } from '$app/state';
import { SvelteSet } from 'svelte/reactivity';
diff --git a/ui/apps/chat/src/lib/utils/navigation.ts b/ui/apps/chat/src/lib/utils/navigation.ts
new file mode 100644
index 000000000..d3b9362b5
--- /dev/null
+++ b/ui/apps/chat/src/lib/utils/navigation.ts
@@ -0,0 +1,26 @@
+import { REPLACE_HISTORY_LINK_ATTRIBUTE } from '#lib/constants/index.ts';
+import { deviceStore } from '#lib/stores/device.svelte.ts';
+import { goto as kitGoto } from '$app/navigation';
+
+/**
+ * An installed app (standalone PWA) keeps a single history entry: every navigation replaces the
+ * current one. With nothing to go back to, iOS offers no edge swipe for history, so the left edge
+ * stays free for dragging the sidebar open; on Android the system back gesture leaves the app.
+ * A browser tab keeps normal history, with back and forward.
+ */
+export function shouldReplaceHistory(): boolean {
+ return deviceStore.isStandalone;
+}
+
+/**
+ * SvelteKit's `goto`, replacing the current history entry in an installed app unless the caller
+ * says otherwise. App code navigates through this one, never `$app/navigation`'s directly.
+ */
+export function goto(...[url, options]: Parameters<typeof kitGoto>): ReturnType<typeof kitGoto> {
+ return kitGoto(url, { ...options, replace: options?.replace ?? shouldReplaceHistory() });
+}
+
+/** Does the same for links: SvelteKit reads the option from <body> when no closer element sets it. */
+export function syncLinkHistoryMode(): void {
+ document.body.toggleAttribute(REPLACE_HISTORY_LINK_ATTRIBUTE, shouldReplaceHistory());
+}
diff --git a/ui/apps/chat/src/routes/(chat)/+page.svelte b/ui/apps/chat/src/routes/(chat)/+page.svelte
index d3d799d4d..426402c6e 100644
--- a/ui/apps/chat/src/routes/(chat)/+page.svelte
+++ b/ui/apps/chat/src/routes/(chat)/+page.svelte
@@ -2,7 +2,7 @@
import { DialogModelNotAvailable } from '#lib/components/app/index.ts';
import { APP_NAME, URL_PARAMS } from '#lib/constants/index.ts';
import { conversationsStore, modelsStore, serverStore } from '#lib/stores/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { page } from '$app/state';
import { onMount } from 'svelte';
diff --git a/ui/apps/chat/src/routes/(chat)/chat/[id]/+page.svelte b/ui/apps/chat/src/routes/(chat)/chat/[id]/+page.svelte
index 4e39b2985..bb588ce53 100644
--- a/ui/apps/chat/src/routes/(chat)/chat/[id]/+page.svelte
+++ b/ui/apps/chat/src/routes/(chat)/chat/[id]/+page.svelte
@@ -2,7 +2,7 @@
import { DialogModelNotAvailable } from '#lib/components/app/index.ts';
import { APP_NAME, ROUTES, URL_PARAMS } from '#lib/constants/index.ts';
import { chatStore, conversationsStore, modelsStore } from '#lib/stores/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { afterNavigate } from '$app/navigation';
import { page } from '$app/state';
diff --git a/ui/apps/chat/src/routes/+error.svelte b/ui/apps/chat/src/routes/+error.svelte
index af6c5f023..2ed4bc20d 100644
--- a/ui/apps/chat/src/routes/+error.svelte
+++ b/ui/apps/chat/src/routes/+error.svelte
@@ -1,7 +1,7 @@
<script lang="ts">
import { ServerErrorSplash } from '#lib/components/app/index.ts';
import { APP_NAME, ROUTES } from '#lib/constants/index.ts';
- import { goto } from '$app/navigation';
+ import { goto } from '#lib/utils/navigation.ts';
import { page } from '$app/state';
let error = $derived(page.error);
diff --git a/ui/apps/chat/src/routes/+layout.svelte b/ui/apps/chat/src/routes/+layout.svelte
index b8fb56ab6..3674b3eec 100644
--- a/ui/apps/chat/src/routes/+layout.svelte
+++ b/ui/apps/chat/src/routes/+layout.svelte
@@ -45,9 +45,10 @@
} from '#lib/stores/index.ts';
import { initStores } from '#lib/stores/init.ts';
import { hapticTap } from '#lib/utils/index.ts';
+ import { goto, syncLinkHistoryMode } from '#lib/utils/navigation.ts';
import { serverUrl } from '#lib/utils/server-config.ts';
import { browser } from '$app/env';
- import { afterNavigate, goto } from '$app/navigation';
+ import { afterNavigate } from '$app/navigation';
import { page } from '$app/state';
import { mode, ModeWatcher } from 'mode-watcher';
import { untrack } from 'svelte';
@@ -236,6 +237,13 @@
updateFavicon();
});
+ // an installed app replaces history on link clicks too, see #lib/utils/navigation.ts
+ $effect(() => {
+ void deviceStore.isStandalone;
+
+ syncLinkHistoryMode();
+ });
+
// Initialize server properties on app load (run once)
$effect(() => {
// Only fetch if we don't already have props
diff --git a/ui/apps/chat/src/routes/search/+page.svelte b/ui/apps/chat/src/routes/search/+page.svelte
index ad9fe10c2..6a180e07c 100644
--- a/ui/apps/chat/src/routes/search/+page.svelte
+++ b/ui/apps/chat/src/routes/search/+page.svelte
@@ -3,12 +3,15 @@
import { ROUTES } from '#lib/constants/index.ts';
import { RouterService } from '#lib/services/router.service.ts';
import { chatStore, conversationsStore, deviceStore } from '#lib/stores/index.ts';
+ import { goto, shouldReplaceHistory } from '#lib/utils/navigation.ts';
import { browser } from '$app/env';
- import { goto } from '$app/navigation';
+ import { afterNavigate } from '$app/navigation';
import { page } from '$app/state';
let searchQuery = $state('');
let searchInputRef = $state<HTMLInputElement | null>(null);
+ /** The page search was opened from, for the installed app, which keeps no history to go back through. */
+ let returnRoute: string | null = null;
let currentChatId = $derived(page.params.id);
@@ -28,6 +31,10 @@
}
});
+ afterNavigate(({ from }) => {
+ returnRoute = from?.url.hash || null;
+ });
+
async function selectConversation(id: string) {
await goto(RouterService.chat(id));
}
@@ -37,8 +44,10 @@
}
function handleBack() {
- if (history.length > 1) {
+ if (!shouldReplaceHistory() && history.length > 1) {
history.back();
+ } else if (returnRoute) {
+ void goto(returnRoute);
} else {
conversationsStore.openNewChat();
}
diff --git a/ui/apps/chat/tests/client/chat-tabs-store.svelte.test.ts b/ui/apps/chat/tests/client/chat-tabs-store.svelte.test.ts
index ae9ce514b..31e36c853 100644
--- a/ui/apps/chat/tests/client/chat-tabs-store.svelte.test.ts
+++ b/ui/apps/chat/tests/client/chat-tabs-store.svelte.test.ts
@@ -1,10 +1,10 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
-vi.mock('$app/navigation', () => ({ goto: vi.fn() }));
+vi.mock('#lib/utils/navigation.ts', () => ({ goto: vi.fn() }));
import { CONVERSATION_TABS_LOCALSTORAGE_KEY, NEW_CHAT_TAB_ID } from '#lib/constants/index.ts';
import { TabsStore } from '#lib/stores/tabs.svelte.ts';
-import { goto } from '$app/navigation';
+import { goto } from '#lib/utils/navigation.ts';
const gotoMock = vi.mocked(goto);
diff --git a/ui/apps/chat/tests/client/navigation.svelte.test.ts b/ui/apps/chat/tests/client/navigation.svelte.test.ts
new file mode 100644
index 000000000..354a5218c
--- /dev/null
+++ b/ui/apps/chat/tests/client/navigation.svelte.test.ts
@@ -0,0 +1,65 @@
+// Guards the installed app's single history entry: goto replaces the current entry when the app
+// runs standalone and pushes in a browser tab, a caller's own `replace` still decides, and the
+// <body> attribute SvelteKit reads for links follows the same mode.
+
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+vi.mock('$app/navigation', () => ({ goto: vi.fn() }));
+
+import { REPLACE_HISTORY_LINK_ATTRIBUTE } from '#lib/constants/index.ts';
+import { deviceStore } from '#lib/stores/device.svelte.ts';
+import { goto, syncLinkHistoryMode } from '#lib/utils/navigation.ts';
+import { goto as kitGoto } from '$app/navigation';
+
+const kitGotoMock = vi.mocked(kitGoto);
+const initialStandalone = deviceStore.isStandalone;
+
+afterEach(() => {
+ deviceStore.isStandalone = initialStandalone;
+ document.body.removeAttribute(REPLACE_HISTORY_LINK_ATTRIBUTE);
+ kitGotoMock.mockClear();
+});
+
+describe('goto', () => {
+ it('replaces the current entry in an installed app', async () => {
+ deviceStore.isStandalone = true;
+ await goto('#/chat/a');
+
+ expect(kitGotoMock).toHaveBeenLastCalledWith('#/chat/a', { replace: true });
+ });
+
+ it('adds an entry in a browser tab', async () => {
+ deviceStore.isStandalone = false;
+ await goto('#/chat/a');
+
+ expect(kitGotoMock).toHaveBeenLastCalledWith('#/chat/a', { replace: false });
+ });
+
+ it("keeps the caller's own replace and other options", async () => {
+ deviceStore.isStandalone = true;
+ await goto('#/chat/a', { replace: false, reset: true });
+
+ expect(kitGotoMock).toHaveBeenLastCalledWith('#/chat/a', { replace: false, reset: true });
+ });
+
+ it('keeps the installed-app default when replace is passed as undefined', async () => {
+ deviceStore.isStandalone = true;
+ await goto('#/chat/a', { replace: undefined });
+
+ expect(kitGotoMock).toHaveBeenLastCalledWith('#/chat/a', { replace: true });
+ });
+});
+
+describe('syncLinkHistoryMode', () => {
+ it('sets the link option on <body> in an installed app and removes it in a browser tab', () => {
+ deviceStore.isStandalone = true;
+ syncLinkHistoryMode();
+
+ expect(document.body.hasAttribute(REPLACE_HISTORY_LINK_ATTRIBUTE)).toBe(true);
+
+ deviceStore.isStandalone = false;
+ syncLinkHistoryMode();
+
+ expect(document.body.hasAttribute(REPLACE_HISTORY_LINK_ATTRIBUTE)).toBe(false);
+ });
+});