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

preview.test.ts

147 lines5.3 KB

1import { it as effectIt } from "@effect/vitest";
2import {
3 DEFAULT_BROWSER_PROFILE_ID,
4 INCOGNITO_BROWSER_PROFILE_ID,
5 PreviewAutomationStatus,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import * as Effect from "effect/Effect";
9import * as Exit from "effect/Exit";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
13​
14import * as PreviewManager from "../../preview/Manager.ts";
15import * as BrowserImport from "../../preview/BrowserImport/BrowserImport.ts";
16import * as BrowserSession from "../../preview/BrowserSession.ts";
17import * as PreviewIpc from "./preview.ts";
18​
19const { fromPartition } = vi.hoisted(() => ({
20 fromPartition: vi.fn(() => {
21 throw new Error("Session can only be received when app is ready");
22 }),
23}));
24​
25vi.mock("electron", () => ({
26 BrowserWindow: {
27 getAllWindows: vi.fn(() => []),
28 },
29 session: {
30 fromPartition,
31 },
32 webContents: {
33 fromId: vi.fn(() => null),
34 },
35}));
36​
37describe("preview IPC methods", () => {
38 beforeEach(() => {
39 fromPartition.mockClear();
40 });
41​
42 it("does not access the Electron session while the module loads", async () => {
43 await expect(import("./preview.ts")).resolves.toBeDefined();
44 expect(fromPartition).not.toHaveBeenCalled();
45 });
46​
47 it("derives distinct partition scopes when identifiers contain the delimiter", () => {
48 const first = BrowserSession.resolvePartitionScope("a", "b::c");
49 const second = BrowserSession.resolvePartitionScope("a::b", "c");
50​
51 expect(first).toEqual({ scope: '["a","b::c"]', persistent: true, namespace: "profile" });
52 expect(second).toEqual({ scope: '["a::b","c"]', persistent: true, namespace: "profile" });
53 expect(first.scope).not.toBe(second.scope);
54 });
55​
56 it("preserves lone surrogates without collapsing them to replacement characters", () => {
57 const highSurrogate = BrowserSession.resolvePartitionScope("environment", "profile-\ud800");
58 const lowSurrogate = BrowserSession.resolvePartitionScope("environment", "profile-\udc00");
59 const replacement = BrowserSession.resolvePartitionScope("environment", "profile-�");
60​
61 expect(highSurrogate.scope).toBe('["environment","profile-\\ud800"]');
62 expect(lowSurrogate.scope).toBe('["environment","profile-\\udc00"]');
63 expect(highSurrogate.scope).not.toBe(lowSurrogate.scope);
64 expect(highSurrogate.scope).not.toBe(replacement.scope);
65 expect(lowSurrogate.scope).not.toBe(replacement.scope);
66 });
67​
68 it("keeps the legacy default partition scope and incognito persistence", () => {
69 expect(BrowserSession.resolvePartitionScope("environment::legacy", undefined)).toEqual({
70 scope: "environment::legacy",
71 persistent: true,
72 });
73 expect(
74 BrowserSession.resolvePartitionScope("environment::legacy", DEFAULT_BROWSER_PROFILE_ID),
75 ).toEqual({ scope: "environment::legacy", persistent: true });
76 expect(
77 BrowserSession.resolvePartitionScope("environment::legacy", INCOGNITO_BROWSER_PROFILE_ID),
78 ).toEqual({
79 scope: '["environment::legacy","incognito"]',
80 persistent: false,
81 namespace: "profile",
82 });
83 });
84​
85 effectIt.effect("targets imports at the same partition tuple as the renderer", () => {
86 const received: Array<Parameters<BrowserImport.BrowserImport["Service"]["importCookies"]>[0]> =
87 [];
88 const browserImport = BrowserImport.BrowserImport.of({
89 listSources: Effect.succeed([]),
90 importCookies: (input) =>
91 Effect.sync(() => {
92 received.push(input);
93 return { imported: 0, skipped: 0, skippedDomains: [] };
94 }),
95 });
96 const request = (environmentId: string, targetProfileId: string) =>
97 PreviewIpc.importBrowserCookies.handler({
98 environmentId,
99 sourceId: "helium",
100 sourceProfileDirectory: "Default",
101 targetProfileId,
102 });
103​
104 return Effect.gen(function* () {
105 yield* request("a", "b");
106 yield* request("a::b", DEFAULT_BROWSER_PROFILE_ID);
107​
108 expect(received[0]).toMatchObject(BrowserSession.resolvePartitionScope("a", "b"));
109 expect(received[1]).toMatchObject(
110 BrowserSession.resolvePartitionScope("a::b", DEFAULT_BROWSER_PROFILE_ID),
111 );
112 expect(received[0]?.namespace).toBe("profile");
113 expect(received[1]?.namespace).toBeUndefined();
114 }).pipe(Effect.provideService(BrowserImport.BrowserImport, browserImport));
115 });
116​
117 effectIt.effect("rejects invalid webContents ids before resolving the preview service", () =>
118 Effect.map(
119 PreviewIpc.registerWebview
120 .handler({ tabId: "tab-1", webContentsId: 0 })
121 .pipe(Effect.provideService(PreviewManager.PreviewManager, null as never), Effect.exit),
122 (exit) => {
123 expect(Exit.isFailure(exit)).toBe(true);
124 if (Exit.isSuccess(exit)) return;
125 const error = Cause.findErrorOption(exit.cause);
126 expect(Option.isSome(error) && Schema.isSchemaError(error.value)).toBe(true);
127 expect(fromPartition).not.toHaveBeenCalled();
128 },
129 ),
130 );
131​
132 it("keeps the public automation status tab id limit", () => {
133 const encode = Schema.encodeUnknownSync(PreviewAutomationStatus);
134 const tabId = "t".repeat(129);
135​
136 expect(() =>
137 encode({
138 available: false,
139 visible: true,
140 tabId,
141 url: null,
142 title: null,
143 loading: false,
144 }),
145 ).toThrow();
146 });
147});