| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { DEFAULT_BROWSER_PROFILE_ID, INCOGNITO_BROWSER_PROFILE_ID } from "@t3tools/contracts"; |
| 3 | |
| 4 | import { ensureClientSettingsHydrated } from "~/hooks/useSettings"; |
| 5 | |
| 6 | const settings = vi.hoisted(() => ({ current: {} as Record<string, unknown> })); |
| 7 | |
| 8 | vi.mock("~/hooks/useSettings", () => ({ |
| 9 | getClientSettings: () => settings.current, |
| 10 | useClientSettings: () => undefined, |
| 11 | ensureClientSettingsHydrated: vi.fn(async () => undefined), |
| 12 | })); |
| 13 | |
| 14 | const { getBrowserDefaults, resolveBrowserDefaults } = await import("./browserDefaults"); |
| 15 | |
| 16 | const withDefaultProfile = (browserDefaultProfileId: string) => { |
| 17 | settings.current = { |
| 18 | browserDefaultViewport: { _tag: "fill" }, |
| 19 | browserDefaultZoomFactor: 1, |
| 20 | browserDefaultAppearance: "system", |
| 21 | browserAutoShowFloatingPreview: true, |
| 22 | browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }], |
| 23 | browserDefaultProfileId, |
| 24 | }; |
| 25 | return getBrowserDefaults(); |
| 26 | }; |
| 27 | |
| 28 | describe("getBrowserDefaults profile resolution", () => { |
| 29 | it("keeps a configured persistent profile", () => { |
| 30 | expect(withDefaultProfile("work").profileId).toBe("work"); |
| 31 | }); |
| 32 | |
| 33 | it("falls back for an unknown profile", () => { |
| 34 | expect(withDefaultProfile("deleted").profileId).toBe(DEFAULT_BROWSER_PROFILE_ID); |
| 35 | }); |
| 36 | |
| 37 | it("refuses incognito as the default", () => { |
| 38 | // A stored incognito default would open every new tab into storage that is |
| 39 | // discarded on close, and the settings list no longer offers it — so the |
| 40 | // row badged "Default" must be the one tabs actually open under. |
| 41 | expect(withDefaultProfile(INCOGNITO_BROWSER_PROFILE_ID).profileId).toBe( |
| 42 | DEFAULT_BROWSER_PROFILE_ID, |
| 43 | ); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe("resolveBrowserDefaults", () => { |
| 48 | it("rejects failed reads and uses the saved profile after a successful retry", async () => { |
| 49 | withDefaultProfile("work"); |
| 50 | settings.current.browserDefaultZoomFactor = 1.25; |
| 51 | settings.current.browserDefaultAppearance = "dark"; |
| 52 | const failure = new Error("Settings read failed"); |
| 53 | vi.mocked(ensureClientSettingsHydrated).mockRejectedValueOnce(failure); |
| 54 | |
| 55 | await expect(resolveBrowserDefaults()).rejects.toBe(failure); |
| 56 | await expect(resolveBrowserDefaults()).resolves.toMatchObject({ |
| 57 | viewport: { _tag: "fill" }, |
| 58 | zoomFactor: 1.25, |
| 59 | appearance: "dark", |
| 60 | autoShowFloatingPreview: true, |
| 61 | profileId: "work", |
| 62 | }); |
| 63 | }); |
| 64 | }); |