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

browserProfile.test.ts

99 lines3.2 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 work: BrowserProfile = { id: "profile-work", name: "Work", kind: "persistent" };
17​
18describe("resolveBrowserProfiles", () => {
19 it("lists built-ins ahead of the user's own profiles", () => {
20 const resolved = resolveBrowserProfiles([work]);
21​
22 expect(resolved.map((profile) => profile.id)).toEqual([
23 DEFAULT_BROWSER_PROFILE_ID,
24 INCOGNITO_BROWSER_PROFILE_ID,
25 work.id,
26 ]);
27 });
28​
29 it("drops stored entries that collide with a built-in id", () => {
30 const resolved = resolveBrowserProfiles([
31 { id: DEFAULT_BROWSER_PROFILE_ID, name: "Hijacked", kind: "persistent" },
32 { id: INCOGNITO_BROWSER_PROFILE_ID, name: "Not incognito", kind: "persistent" },
33 work,
34 ]);
35​
36 expect(resolved).toEqual([...BUILT_IN_BROWSER_PROFILES, work]);
37 });
38​
39 it("keeps incognito ephemeral", () => {
40 const incognito = findBrowserProfile(resolveBrowserProfiles([]), INCOGNITO_BROWSER_PROFILE_ID);
41​
42 expect(incognito?.kind).toBe("incognito");
43 });
44});
45​
46describe("findBrowserProfile", () => {
47 it("returns nothing for an id that no longer exists", () => {
48 expect(findBrowserProfile(resolveBrowserProfiles([]), work.id)).toBeUndefined();
49 expect(findBrowserProfile(resolveBrowserProfiles([work]), undefined)).toBeUndefined();
50 });
51});
52​
53describe("isBuiltInBrowserProfileId", () => {
54 it("separates built-ins from user profiles", () => {
55 expect(isBuiltInBrowserProfileId(DEFAULT_BROWSER_PROFILE_ID)).toBe(true);
56 expect(isBuiltInBrowserProfileId(INCOGNITO_BROWSER_PROFILE_ID)).toBe(true);
57 expect(isBuiltInBrowserProfileId(work.id)).toBe(false);
58 });
59});
60​
61describe("resolveBrowserProfiles normalization", () => {
62 it("keeps only the first entry for a repeated id", () => {
63 const resolved = resolveBrowserProfiles([
64 { id: "work", name: "Work", kind: "persistent" },
65 { id: "work", name: "Work (old)", kind: "persistent" },
66 ]);
67​
68 expect(resolved.filter((profile) => profile.id === "work")).toEqual([
69 { id: "work", name: "Work", kind: "persistent" },
70 ]);
71 });
72​
73 it("reports a custom incognito profile as persistent", () => {
74 const resolved = resolveBrowserProfiles([
75 { id: "throwaway", name: "Throwaway", kind: "incognito" },
76 ]);
77​
78 expect(resolved.find((profile) => profile.id === "throwaway")).toEqual({
79 id: "throwaway",
80 name: "Throwaway",
81 kind: "persistent",
82 });
83 });
84​
85 it("still lets the built-in incognito profile stay ephemeral", () => {
86 const incognito = resolveBrowserProfiles([]).find(
87 (profile) => profile.id === INCOGNITO_BROWSER_PROFILE_ID,
88 );
89​
90 expect(incognito?.kind).toBe("incognito");
91 });
92});
93​
94describe("BrowserProfileId", () => {
95 it("rejects control characters", () => {
96 expect(Schema.is(BrowserProfileId)("profile-a\u0000b")).toBe(false);
97 expect(Schema.is(BrowserProfileId)("profile-a")).toBe(true);
98 });
99});