import assert from "node:assert/strict"; import { mkdir } from "node:fs/promises"; // Production preview only; every API/socket request is a local fixture. // Uses PLAYWRIGHT_MODULE, CHROME_EXECUTABLE and WEB_TEST_URL like the push fixtures. const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || "playwright-core"); const target = new URL(process.env.WEB_TEST_URL || "http://127.0.0.1:5197/"); assert.ok(target.protocol === "http:" && ["127.0.0.1", "localhost", "[::1]"].includes(target.hostname)); const artifactDir = process.env.SETTINGS_ARTIFACT_DIR; if (artifactDir) await mkdir(artifactDir, { recursive: true }); const browser = await chromium.launch({ executablePath: process.env.CHROME_EXECUTABLE || undefined, headless: true, ...(process.env.BROWSER_NO_SANDBOX === "1" ? { args: ["--no-sandbox"] } : {}) }); const date = "2026-11-12T12:00:00.000Z"; const sourceId = "4599d348-e053-4937-987d-1392746fb2ce"; const chat = { id: "settings-chat", title: "Settings fixture conversation", parentChatId: null, titleGenerationPending: false, createdAt: date, updatedAt: date, starred: false, starredAt: null, initiatedProvider: "openai", initiatedModel: "fixture-model", lastUsedProvider: "openai", lastUsedModel: "fixture-model", additionalSystemPrompt: null, enabledTools: [], messages: [{ id: "reply", role: "assistant", content: "Keep this conversation open.", createdAt: date, name: null, metadata: null }] }; const search = { id: "settings-search", title: "Saved search fixture", query: "saved query", createdAt: date, updatedAt: date, starred: false, starredAt: null, results: [], answer: "Saved search answer", status: "completed" }; const idle = { state: "idle", isThinking: false, queued: 0, backgroundWorkers: 0, tools: [], queuedMessages: [] }; async function fixture(viewport, mode = "token", options = {}) { const context = await browser.newContext({ viewport, isMobile: viewport.width < 768, hasTouch: viewport.width < 768 }); const errors = [], writes = []; await context.addInitScript(({ mode, options }) => { if (mode === "token") localStorage.setItem("sybil_admin_token", "settings-fixture-token"); localStorage.setItem("sybil:ui-mode", "full"); if (options.seedModels && !localStorage.getItem("model-fixture-seeded")) { localStorage.setItem("model-fixture-seeded", "true"); localStorage.setItem("sybil:modelPreferencesByProvider", JSON.stringify({ openai: "remembered-initial", anthropic: "legacy-custom" })); localStorage.setItem("sybil:quickQuestionModelSelection", JSON.stringify({ provider: "xai", modelPreferences: { xai: "legacy-quick", openai: "quick-original" } })); } window.__denyPreferenceWrites = !!options.denyWrites; const setItem = Storage.prototype.setItem; Storage.prototype.setItem = function (key, value) { if (window.__denyPreferenceWrites && ["sybil:modelPreferencesByProvider", "sybil:quickQuestionModelSelection"].includes(key)) throw new DOMException("Full", "QuotaExceededError"); return setItem.call(this, key, value); }; if (options.blockStorage) Object.defineProperty(window, "localStorage", { get() { throw new Error("Storage unavailable"); } }); window.__permissionRequests = 0; Notification.requestPermission = () => { window.__permissionRequests++; return Promise.resolve("default"); }; }, { mode, options }); await context.route("**/*", async route => { const request = route.request(), url = new URL(request.url()), path = url.pathname; if (url.origin !== target.origin) return route.abort(); if (!path.startsWith("/api/")) return route.continue(); if (request.method() !== "GET") writes.push(path); let value = {}; if (path.endsWith("/auth/session")) value = { mode, authenticated: true }; else if (path.endsWith("/workspace-items")) value = { items: [{ ...chat, type: "chat" }, { ...search, type: "search" }] }; else if (path.endsWith(`/chats/${chat.id}`)) value = { chat }; else if (path.endsWith(`/searches/${search.id}`)) value = { search }; else if (path.endsWith("/models")) value = { providers: { openai: { models: ["fixture-model", "listed-next-model"], loadedAt: date, error: null } } }; else if (path.endsWith("/chat-tools") || path.endsWith("/agent/tools")) value = { tools: [] }; else if (path.endsWith("/active-runs")) value = { chats: [], searches: [] }; else if (path.endsWith("/agent/messages")) value = { conversationId: "1", messages: [], cursor: "0", beforeCursor: null, hasMore: false }; else if (path.endsWith("/agent")) value = { id: "1", title: "Sybil Agent", activity: idle, protocol: "pi-room.v1", websocket: "/v1/agent/ws" }; else if (path.endsWith("/read-state")) value = { sourceId, cursor: "0", updatedAt: null }; else if (path.endsWith("/push/config")) value = { version: 1, enabled: true, webPushAvailable: true, apnsAvailable: false, vapidPublicKey: "B".repeat(87), sourceId, quarantined: false, policy: { presenceHeartbeatMs: 15000, presenceLeaseMs: 60000 } }; else if (path.includes("/presence/")) value = { expiresAt: date }; await route.fulfill({ json: value }); }); await context.routeWebSocket("**/api/v1/agent/ws", socket => socket.onMessage(raw => { if (JSON.parse(String(raw)).type === "auth") socket.send(JSON.stringify({ type: "ready", conversationId: "1" })); })); const page = await context.newPage(); page.on("pageerror", error => errors.push(error.message)); await page.goto(new URL(`/?chat=${chat.id}`, target).href, { waitUntil: "networkidle" }); await page.locator("main h1").filter({ hasText: chat.title }).waitFor(); const openSidebar = async () => { if (await page.locator(".workspace-sidebar").evaluate(el => innerWidth < 768 && el.classList.contains("-translate-x-full"))) { await page.getByRole("button", { name: "Open sidebar", exact: true }).click(); } await page.waitForFunction(() => document.querySelector(".workspace-sidebar").getBoundingClientRect().left >= 0); }; const settings = page.getByRole("dialog", { name: "Settings", exact: true }); const trigger = page.getByRole("button", { name: "Open app settings", exact: true }); const selectSection = async name => { const back = settings.getByRole("button", { name: "Back to settings", exact: true }); if (await back.isVisible()) await back.click(); await settings.getByRole("button", { name, exact: true }).click(); await settings.getByRole("group", { name, exact: true }).waitFor(); }; const openSettings = async (section = "Notifications") => { await openSidebar(); await trigger.click(); await settings.waitFor(); assert.equal(await page.getByRole("button", { name: "Close settings", exact: true }).evaluate(el => el === document.activeElement), true); if (artifactDir && await page.evaluate(() => innerWidth < 768)) await page.screenshot({ path: `${artifactDir}/settings-sections-${viewport.width}.png` }); await selectSection(section); }; return { context, page, settings, trigger, openSidebar, openSettings, selectSection, errors, writes }; } async function assertFits(locator) { await locator.scrollIntoViewIfNeeded(); assert.ok(await locator.evaluate(el => { const rect = el.getBoundingClientRect(); return rect.left >= 0 && rect.right <= innerWidth && rect.top >= 0 && rect.bottom <= innerHeight; }), "control fits in the visible viewport"); } try { for (const viewport of [{ width: 1280, height: 900 }, { width: 390, height: 844 }]) { const { context, page, settings, trigger, openSidebar, openSettings, selectSection, errors, writes } = await fixture(viewport); const originalUrl = page.url(); await page.locator("#composer-input").fill("Keep my unfinished draft"); await openSidebar(); assert.equal(await page.getByRole("link", { name: /Lightweight view/i }).count(), 0); assert.equal(await page.locator('.workspace-sidebar a[href*="lite"]').count(), 0); assert.equal(await page.getByText("Agent notifications", { exact: true }).count(), 0); await assertFits(trigger); await openSettings(); assert.equal(await settings.evaluate(el => el.matches(":modal")), true); assert.equal(await page.getByRole("button", { name: "Refresh", exact: true }).isVisible(), false); assert.equal(await page.getByText("Browser permission", { exact: true }).isVisible(), false); assert.equal(await page.getByRole("link", { name: "GrapheneOS setup guide" }).isVisible(), false); assert.equal(await page.evaluate(() => window.__permissionRequests), 0, "opening Settings does not request consent"); // A modal must keep focus off background controls even when they request it. await page.locator("#composer-input").evaluate(el => el.focus()); assert.equal(await settings.evaluate(el => el.contains(document.activeElement)), true); for (let index = 0; index < 8; index++) { await page.keyboard.press("Tab"); assert.equal(await page.evaluate(() => !!document.activeElement?.closest(".workspace-shell")), false); } for (const shortcut of ["Control+j", "Control+Shift+j", "Control+i", "Control+ArrowDown"]) { await page.keyboard.press(shortcut); } assert.equal(page.url(), originalUrl, "workspace navigation shortcuts cannot change the selection under Settings"); assert.equal(await page.locator("#quick-question-input").count(), 0); await settings.getByText("Troubleshooting", { exact: true }).click(); await settings.getByText("Browser permission", { exact: true }).waitFor(); await settings.getByRole("button", { name: "Refresh", exact: true }).click(); await page.waitForFunction(() => Array.from(document.querySelectorAll("button")).some(el => el.textContent === "Enable notifications" && !el.disabled)); assert.equal(await page.evaluate(() => window.__permissionRequests), 0, "refresh does not request consent"); await page.keyboard.press("Escape"); await settings.waitFor({ state: "detached" }); assert.equal(await trigger.evaluate(el => el === document.activeElement), true, "dismissal restores focus to Settings"); assert.equal(await page.locator("#composer-input").inputValue(), "Keep my unfinished draft"); assert.equal(page.url(), originalUrl); await openSettings(); assert.equal(await settings.getByText("Browser permission", { exact: true }).isVisible(), false, "diagnostics starts collapsed on each visit"); if (artifactDir) await page.screenshot({ path: `${artifactDir}/settings-${viewport.width}.png` }); if (viewport.width >= 768) { await page.mouse.click(8, 8); await settings.waitFor({ state: "detached" }); assert.equal(await trigger.evaluate(el => el === document.activeElement), true); } else { await settings.getByRole("button", { name: "Close settings", exact: true }).click(); await settings.waitFor({ state: "detached" }); } if (artifactDir) await page.screenshot({ path: `${artifactDir}/sidebar-${viewport.width}.png` }); // The same entry remains available from chat, search, and Agent workspaces. for (const item of [page.getByRole("button", { name: new RegExp(search.title) }), page.locator('[data-testid="agent-sidebar-item"]')]) { await openSidebar(); await item.click(); await openSettings(); await settings.getByRole("heading", { name: "Notifications", exact: true }).waitFor(); await settings.getByRole("button", { name: "Close settings", exact: true }).click(); } await openSidebar(); await page.getByRole("button", { name: new RegExp(chat.title) }).click(); await page.getByRole("button", { name: "Open chat settings", exact: true }).click(); await page.getByRole("dialog", { name: "Chat settings", exact: true }).waitFor(); await page.keyboard.press("Escape"); await openSettings(); if (viewport.width < 768) { await settings.getByText("Troubleshooting", { exact: true }).click(); await settings.getByText("Android / GrapheneOS help", { exact: true }).click(); for (const smallViewport of [{ width: 320, height: 568 }, { width: 390, height: 450 }, { width: 767, height: 600 }]) { await page.setViewportSize(smallViewport); await assertFits(settings.getByRole("button", { name: "Refresh", exact: true })); await assertFits(settings.getByRole("button", { name: "Close settings", exact: true })); assert.ok(await settings.locator(".app-settings-content").evaluate(el => el.scrollWidth <= el.clientWidth), "no settings body overflow"); if (artifactDir) await page.screenshot({ path: `${artifactDir}/help-${smallViewport.width}x${smallViewport.height}.png` }); } } await selectSection("Connection & session"); await assertFits(settings.getByRole("button", { name: "Sign out", exact: true })); await settings.getByText(`${target.origin}/api`, { exact: true }).waitFor(); assert.doesNotMatch(await settings.textContent(), /settings-fixture-token/); await settings.getByText("Keyboard shortcuts", { exact: true }).click(); await settings.getByText("New chat", { exact: true }).waitFor(); if (artifactDir) await page.screenshot({ path: `${artifactDir}/connection-${viewport.width}.png` }); assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false); const viewportMeta = await page.locator('meta[name="viewport"]').getAttribute("content"); assert.match(viewportMeta, /user-scalable=no/); assert.match(viewportMeta, /maximum-scale=1\.0(?:,|$)/); assert.equal(writes.filter(path => !path.includes("/presence/") && !path.endsWith("/read-state")).length, 0, "opening settings never mutates chat or push preferences"); // The hidden route is still available despite a saved Full preference. await page.goto(new URL(`/lite?chat=${chat.id}`, target).href, { waitUntil: "networkidle" }); await page.locator(".lite-shell").waitFor(); assert.equal(new URL(page.url()).pathname, "/lite"); assert.equal(new URL(page.url()).searchParams.get("chat"), chat.id); const liteSidebarTrigger = page.getByRole("button", { name: "Open sidebar", exact: true }); if (await liteSidebarTrigger.isVisible()) await liteSidebarTrigger.click(); await page.getByRole("link", { name: "Full interface", exact: true }).click(); await page.locator("main h1").filter({ hasText: chat.title }).waitFor(); assert.match(await page.locator('meta[name="viewport"]').getAttribute("content"), /user-scalable=no/); assert.deepEqual(errors, []); await context.close(); } for (const viewport of [{ width: 1280, height: 900 }, { width: 320, height: 568 }]) { const { page, context, settings, openSidebar, openSettings, selectSection, errors, writes } = await fixture(viewport, "token", { seedModels: true }); await page.locator("#composer-input").fill("Preserve my draft while choosing future models"); await openSettings("Models"); const chatModel = settings.getByRole("combobox", { name: "OpenAI chat model", exact: true }); assert.equal(await chatModel.inputValue(), "remembered-initial"); assert.equal(await settings.getByRole("combobox", { name: "Anthropic chat model", exact: true }).inputValue(), "legacy-custom"); const quickProvider = settings.getByRole("combobox", { name: "Quick Questions provider", exact: true }); const quickModel = settings.getByRole("combobox", { name: "Quick Questions model", exact: true }); assert.equal(await quickProvider.inputValue(), "xai"); assert.equal(await quickModel.inputValue(), "legacy-quick"); // Reuse the catalog, accept custom values, commit with Tab, and cancel only the picker with Escape. await chatModel.fill("listed-next"); await settings.getByRole("option", { name: "listed-next-model", exact: true }).click(); assert.equal(await chatModel.inputValue(), "listed-next-model"); await chatModel.fill(""); await chatModel.press("ArrowUp"); await chatModel.press("Enter"); assert.equal(await chatModel.inputValue(), "listed-next-model", "ArrowUp starts at the last catalog choice"); await chatModel.fill(""); await chatModel.press("ArrowDown"); await chatModel.press("Enter"); assert.equal(await chatModel.inputValue(), "fixture-model", "ArrowDown starts at the first catalog choice"); await chatModel.fill("custom-chat-model"); await chatModel.press("Tab"); await page.waitForFunction(() => JSON.parse(localStorage.getItem("sybil:modelPreferencesByProvider")).openai === "custom-chat-model"); await chatModel.fill("discard-this-model"); await chatModel.press("Escape"); assert.equal(await settings.isVisible(), true, "first Escape dismisses the model picker"); assert.equal(await chatModel.inputValue(), "custom-chat-model"); await quickProvider.selectOption("openai"); assert.equal(await quickModel.inputValue(), "quick-original"); await quickModel.fill("quick-new-model"); await quickModel.press("Enter"); await quickProvider.selectOption("xai"); assert.equal(await quickModel.inputValue(), "legacy-quick", "Quick Questions retains a separate choice for every provider"); await quickProvider.selectOption("openai"); assert.equal(await quickModel.inputValue(), "quick-new-model"); await page.waitForFunction(() => { const selection = JSON.parse(localStorage.getItem("sybil:quickQuestionModelSelection")); return selection.provider === "openai" && selection.modelPreferences.openai === "quick-new-model"; }); const stored = await page.evaluate(() => ({ chats: JSON.parse(localStorage.getItem("sybil:modelPreferencesByProvider")), quick: JSON.parse(localStorage.getItem("sybil:quickQuestionModelSelection")) })); assert.equal(stored.chats.openai, "custom-chat-model"); assert.equal(stored.chats.anthropic, "legacy-custom"); assert.equal(stored.quick.provider, "openai"); assert.equal(stored.quick.modelPreferences.xai, "legacy-quick"); assert.equal(await settings.locator(".app-settings-content").evaluate(el => el.scrollWidth > el.clientWidth), false); await settings.getByRole("heading", { name: "Models", exact: true }).scrollIntoViewIfNeeded(); if (artifactDir) await page.screenshot({ path: `${artifactDir}/models-${viewport.width}.png` }); await settings.getByRole("button", { name: "Close settings", exact: true }).click(); assert.equal(await page.locator("#composer-input").inputValue(), "Preserve my draft while choosing future models"); // Global model changes never patch the selected chat or change its current model. if (viewport.width < 768) await page.getByRole("button", { name: "Close sidebar", exact: true }).click({ position: { x: viewport.width - 4, y: 80 } }); await page.getByRole("button", { name: "Open chat settings", exact: true }).click(); assert.equal(await page.getByRole("combobox", { name: "Model", exact: true }).inputValue(), "fixture-model"); await page.keyboard.press("Escape"); // Both menu and keyboard new-chat actions use the remembered model. await page.keyboard.press("Control+j"); await page.getByRole("button", { name: "Open chat settings", exact: true }).click(); assert.equal(await page.getByRole("combobox", { name: "Model", exact: true }).inputValue(), "custom-chat-model"); await page.keyboard.press("Escape"); await page.keyboard.press("Control+i"); assert.equal(await page.getByRole("combobox", { name: "Model", exact: true }).inputValue(), "quick-new-model", "Quick Questions uses the same preferences"); await page.getByRole("combobox", { name: "Model", exact: true }).fill("quick-inline-model"); await page.getByRole("combobox", { name: "Model", exact: true }).press("Enter"); await page.getByRole("button", { name: "Close quick question", exact: true }).click(); await openSettings("Models"); assert.equal(await quickModel.inputValue(), "quick-inline-model", "inline Quick Questions changes appear in Settings"); await page.reload({ waitUntil: "networkidle" }); await openSettings("Models"); assert.equal(await chatModel.inputValue(), "custom-chat-model"); assert.equal(await quickModel.inputValue(), "quick-inline-model"); // Section navigation is keyboard accessible and restores focus when returning to the mobile list. if (viewport.width < 768) { await settings.getByRole("button", { name: "Back to settings", exact: true }).click(); assert.equal(await settings.getByRole("button", { name: "Models", exact: true }).evaluate(el => el === document.activeElement), true); await page.keyboard.press("Enter"); assert.equal(await settings.getByRole("group", { name: "Models", exact: true }).isVisible(), true); } await selectSection("Notifications"); assert.equal(await settings.getByRole("button", { name: "Enable notifications", exact: true }).isVisible(), true); assert.equal(writes.filter(path => !path.includes("/presence/") && !path.endsWith("/read-state")).length, 0, "model preferences never write to the server"); assert.deepEqual(errors, []); await context.close(); } for (const options of [{ denyWrites: true }, { blockStorage: true }]) { const { page, context, settings, openSettings, errors } = await fixture({ width: 390, height: 450 }, "open", options); await openSettings("Models"); await settings.getByRole("alert").filter({ hasText: "Browser storage is unavailable" }).waitFor(); const model = settings.getByRole("combobox", { name: "OpenAI chat model", exact: true }); await model.fill("session-only-model"); await model.press("Enter"); assert.equal(await model.inputValue(), "session-only-model", "storage failure retains usable in-memory preferences"); if (options.denyWrites) { await page.evaluate(() => { window.__denyPreferenceWrites = false; }); await settings.getByRole("button", { name: "Retry saving", exact: true }).click(); await settings.getByRole("alert").waitFor({ state: "detached" }); assert.equal(await page.evaluate(() => JSON.parse(localStorage.getItem("sybil:modelPreferencesByProvider")).openai), "session-only-model"); } assert.deepEqual(errors, []); await context.close(); } const open = await fixture({ width: 390, height: 844 }, "open"); await open.openSettings(); await open.settings.getByText("Agent notifications require a server with token sign-in.", { exact: true }).waitFor(); assert.equal(await open.settings.getByRole("button", { name: "Enable notifications", exact: true }).count(), 0); await open.selectSection("Connection & session"); await open.settings.getByText("Open mode. No sign-in required.", { exact: true }).waitFor(); assert.equal(await open.settings.getByRole("button", { name: "Sign out", exact: true }).count(), 0); await open.settings.getByRole("button", { name: "Close settings", exact: true }).click(); assert.deepEqual(open.errors, []); await open.context.close(); console.log("PASS app Settings: desktop/mobile sections and focus, model migration/persistence/custom selection, Quick Questions sharing, existing-chat isolation, blocked storage/retry, diagnostics, connection, open mode, hidden /lite and full-app zoom"); } finally { await browser.close(); }