Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

test(window): Bug D recovery unit tests (reset data-safety + XSS escape)

purple5adb7a4a73fc
diff --git a/src/lib/__tests__/fatal-error.test.ts b/src/lib/__tests__/fatal-error.test.ts
new file mode 100644
index 00000000..2fda0824
--- /dev/null
+++ b/src/lib/__tests__/fatal-error.test.ts
@@ -0,0 +1,41 @@
+/**
+ * Bug D (surfingbird1010) — boot-failure recovery (fatal-error.ts).
+ *
+ * Critical safety properties (env: node — no DOM): a "Reset settings" recovery
+ * must clear app settings but NEVER user DATA (chats, knowledge, curated
+ * memory), and the error text shown in the recovery screen must be escaped (no
+ * HTML injection). The DOM render itself is verified live (app launch) — here we
+ * lock the data-safety + escaping logic.
+ *
+ * Run: npx vitest run src/lib/__tests__/fatal-error.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { SETTINGS_STORAGE_KEYS, escapeHtml } from '../fatal-error'
+
+describe('SETTINGS_STORAGE_KEYS (Bug D reset safety)', () => {
+ it('includes the settings/state stores', () => {
+ for (const k of ['chat-settings', 'lu-providers', 'locally-uncensored-voice'])
+ expect(SETTINGS_STORAGE_KEYS).toContain(k)
+ })
+
+ it('NEVER includes user-DATA stores (chats / knowledge / memory)', () => {
+ for (const dataKey of ['chat-conversations', 'rag-store', 'locally-uncensored-memory']) {
+ expect(SETTINGS_STORAGE_KEYS).not.toContain(dataKey)
+ }
+ })
+})
+
+describe('escapeHtml (Bug D recovery-screen XSS safety)', () => {
+ it('escapes angle brackets, quotes and ampersands', () => {
+ expect(escapeHtml('<img src=x onerror=alert(1)>')).toBe(
+ '&lt;img src=x onerror=alert(1)&gt;',
+ )
+ expect(escapeHtml(`a & "b" 'c'`)).toBe('a &amp; &quot;b&quot; &#39;c&#39;')
+ })
+
+ it('leaves plain text unchanged', () => {
+ expect(escapeHtml('hydration boom: cannot read x of undefined')).toBe(
+ 'hydration boom: cannot read x of undefined',
+ )
+ })
+})
diff --git a/src/lib/fatal-error.ts b/src/lib/fatal-error.ts
index ba0f644b..5bacd89b 100644
--- a/src/lib/fatal-error.ts
+++ b/src/lib/fatal-error.ts
@@ -58,7 +58,7 @@ export function resetSettingsAndReload(): void {
}
}
-function escapeHtml(s: string): string {
+export function escapeHtml(s: string): string {
return s.replace(/[&<>"']/g, (c) =>
({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c] as string),
)