Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserProfile.test.ts

105 lines3.8 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import * as Schema from "effect/Schema";
4​
5import {
6 BrowserProfileId,
7 BUILT_IN_BROWSER_PROFILES,
8 DEFAULT_BROWSER_PROFILE_ID,
9 INCOGNITO_BROWSER_PROFILE_ID,
10 findBrowserProfile,
11 isBuiltInBrowserProfileId,
12 resolveBrowserProfiles,
13 type BrowserProfile,
14} from "./browserProfile.ts";
15​
16const isBrowserProfileId = Schema.is(BrowserProfileId);
17​
18const work: BrowserProfile = { id: "profile-work", name: "Work", kind: "persistent" };
19​
20describe("resolveBrowserProfiles", () => {
21 it("lists built-ins ahead of the user's own profiles", () => {
22 const resolved = resolveBrowserProfiles([work]);
23​
24 expect(resolved.map((profile) => profile.id)).toEqual([
25 DEFAULT_BROWSER_PROFILE_ID,
26 INCOGNITO_BROWSER_PROFILE_ID,
27 work.id,
28 ]);
29 });
30​
31 it("drops stored entries that collide with a built-in id", () => {
32 // Built-ins are synthesized rather than stored, so a hand-edited settings
33 // file must not be able to shadow Default with a persistent partition of
34 // its own — every tab already opened under Default would follow it.
35 const resolved = resolveBrowserProfiles([
36 { id: DEFAULT_BROWSER_PROFILE_ID, name: "Hijacked", kind: "persistent" },
37 { id: INCOGNITO_BROWSER_PROFILE_ID, name: "Not incognito", kind: "persistent" },
38 work,
39 ]);
40​
41 expect(resolved).toEqual([...BUILT_IN_BROWSER_PROFILES, work]);
42 });
43​
44 it("keeps incognito ephemeral", () => {
45 const incognito = findBrowserProfile(resolveBrowserProfiles([]), INCOGNITO_BROWSER_PROFILE_ID);
46​
47 expect(incognito?.kind).toBe("incognito");
48 });
49});
50​
51describe("findBrowserProfile", () => {
52 it("returns nothing for an id that no longer exists", () => {
53 // The settings UI relies on this to fall back rather than opening tabs
54 // into a partition with no profile behind it.
55 expect(findBrowserProfile(resolveBrowserProfiles([]), work.id)).toBeUndefined();
56 expect(findBrowserProfile(resolveBrowserProfiles([work]), undefined)).toBeUndefined();
57 });
58});
59​
60describe("isBuiltInBrowserProfileId", () => {
61 it("separates built-ins from user profiles", () => {
62 expect(isBuiltInBrowserProfileId(DEFAULT_BROWSER_PROFILE_ID)).toBe(true);
63 expect(isBuiltInBrowserProfileId(INCOGNITO_BROWSER_PROFILE_ID)).toBe(true);
64 expect(isBuiltInBrowserProfileId(work.id)).toBe(false);
65 });
66});
67​
68describe("resolveBrowserProfiles normalization", () => {
69 it("keeps only the first entry for a repeated id", () => {
70 // Both map to the same Electron partition, so presenting two would offer
71 // isolated identities that in fact share every cookie.
72 const resolved = resolveBrowserProfiles([
73 { id: "work", name: "Work", kind: "persistent" },
74 { id: "work", name: "Work (old)", kind: "persistent" },
75 ]);
76​
77 expect(resolved.filter((profile) => profile.id === "work")).toEqual([
78 { id: "work", name: "Work", kind: "persistent" },
79 ]);
80 });
81​
82 it("reports a custom incognito profile as persistent", () => {
83 // Partition persistence is keyed off the built-in incognito id alone, so
84 // a custom profile claiming that kind keeps its cookies across restarts.
85 // Labelling it ephemeral would be a promise the partition layer breaks.
86 const resolved = resolveBrowserProfiles([
87 { id: "throwaway", name: "Throwaway", kind: "incognito" },
88 ]);
89​
90 expect(resolved.find((profile) => profile.id === "throwaway")).toEqual({
91 id: "throwaway",
92 name: "Throwaway",
93 kind: "persistent",
94 });
95 });
96});
97​
98describe("BrowserProfileId", () => {
99 it("rejects control characters", () => {
100 // Ids are folded into delimiter-joined cache keys on the client, so one
101 // carrying the delimiter would resolve to another profile's partition.
102 expect(isBrowserProfileId("profile-a\u0000b")).toBe(false);
103 expect(isBrowserProfileId("profile-a")).toBe(true);
104 });
105});