Open History to see the last commit
1import * as NodeEvents from "node:events";
2
3import { INCOGNITO_BROWSER_PROFILE_ID } from "@t3tools/contracts";
4import { chromium, type Browser, type BrowserContext, type BrowserType } from "playwright-core";
5import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
6
7import { ServerBrowserContexts } from "./ServerBrowserContexts.ts";
8
9const launches = vi.hoisted(() => ({
10 launch: vi.fn<BrowserType["launch"]>(),
11 persistent: vi.fn<BrowserType["launchPersistentContext"]>(),
12 mkdir: vi.fn().mockResolvedValue(undefined),
13 rm: vi.fn().mockResolvedValue(undefined),
14}));
15vi.spyOn(chromium, "launch").mockImplementation(launches.launch);
16vi.spyOn(chromium, "launchPersistentContext").mockImplementation(launches.persistent);
17vi.mock("node:fs/promises", () => ({ mkdir: launches.mkdir, rm: launches.rm }));
18
19const makeContext = () => {
20 const events = new NodeEvents.EventEmitter();
21 let closed = false;
22 return {
23 on: vi.fn((event: string, listener: () => void) => events.on(event, listener)),
24 pages: vi.fn(() => []),
25 close: vi.fn(async () => {
26 if (!closed) {
27 closed = true;
28 events.emit("close");
29 }
30 }),
31 };
32};
33
34const makeBrowser = () => {
35 const events = new NodeEvents.EventEmitter();
36 const contexts: ReturnType<typeof makeContext>[] = [];
37 return {
38 contexts,
39 on: vi.fn((event: string, listener: () => void) => events.on(event, listener)),
40 newContext: vi.fn(async () => {
41 const context = makeContext();
42 contexts.push(context);
43 return context as unknown as BrowserContext;
44 }),
45 close: vi.fn(async () => {
46 for (const context of contexts) await context.close();
47 events.emit("disconnected");
48 }),
49 };
50};
51
52const options = () => ({
53 profilesDir: "/test/profiles",
54 executable: vi.fn(async () => "/test/chromium"),
55 env: {},
56});
57
58beforeEach(() => {
59 launches.launch.mockReset();
60 launches.persistent.mockReset();
61 launches.mkdir.mockClear();
62 launches.rm.mockClear();
63});
64
65describe("ServerBrowserContexts", () => {
66 it("lazily shares one browser across concurrent isolated sessions and reuses each context", async () => {
67 const browser = makeBrowser();
68 const launch = Promise.withResolvers<Browser>();
69 launches.launch.mockReturnValue(launch.promise);
70 const configuration = options();
71 const pool = new ServerBrowserContexts(configuration);
72 expect(configuration.executable).not.toHaveBeenCalled();
73 const first = pool.contextFor("default", "agent-a");
74 const again = pool.contextFor("default", "agent-a");
75 const second = pool.contextFor("default", "agent-b");
76 const incognito = pool.contextFor(INCOGNITO_BROWSER_PROFILE_ID);
77 launch.resolve(browser as unknown as Browser);
78 const [a, reused, b, privateContext] = await Promise.all([first, again, second, incognito]);
79 expect(a).toBe(reused);
80 expect(new Set([a, b, privateContext]).size).toBe(3);
81 expect(launches.launch).toHaveBeenCalledTimes(1);
82 expect(launches.persistent).not.toHaveBeenCalled();
83 expect(browser.newContext).toHaveBeenCalledWith({
84 viewport: { width: 1280, height: 800 },
85 deviceScaleFactor: 2,
86 });
87 await pool.close();
88 expect(browser.close).toHaveBeenCalledTimes(1);
89 for (const context of browser.contexts) expect(context.close).toHaveBeenCalled();
90 });
91
92 it("keeps human profiles persistent without sharing their storage with agents", async () => {
93 const human = makeContext();
94 const browser = makeBrowser();
95 launches.persistent.mockResolvedValue(human as unknown as BrowserContext);
96 launches.launch.mockResolvedValue(browser as unknown as Browser);
97 const pool = new ServerBrowserContexts(options());
98 const persistent = await pool.contextFor("work/team");
99 expect(await pool.contextFor("work/team")).toBe(persistent);
100 expect(await pool.contextFor("work/team", "agent")).not.toBe(persistent);
101 expect(launches.persistent).toHaveBeenCalledExactlyOnceWith(
102 "/test/profiles/work%2Fteam",
103 expect.objectContaining({ chromiumSandbox: true, executablePath: "/test/chromium" }),
104 );
105 expect(launches.mkdir).toHaveBeenCalledWith("/test/profiles/work%2Fteam", { recursive: true });
106 await pool.close();
107 expect(human.close).toHaveBeenCalledTimes(1);
108 });
109
110 it("clears a profile by closing its open context before deleting its storage", async () => {
111 const human = makeContext();
112 const browser = makeBrowser();
113 launches.persistent.mockResolvedValue(human as unknown as BrowserContext);
114 launches.launch.mockResolvedValue(browser as unknown as Browser);
115 const onContextClose = vi.fn();
116 const pool = new ServerBrowserContexts({ ...options(), onContextClose });
117 const persistent = await pool.contextFor("work/team");
118 const agent = await pool.contextFor("work/team", "agent");
119 launches.rm.mockImplementationOnce(async () => {
120 expect(human.close).toHaveBeenCalledTimes(1);
121 });
122 await pool.clearProfile("work/team");
123 expect(onContextClose).toHaveBeenCalledExactlyOnceWith(persistent);
124 expect(launches.rm).toHaveBeenCalledExactlyOnceWith("/test/profiles/work%2Fteam", {
125 recursive: true,
126 force: true,
127 });
128 expect(await pool.contextFor("work/team", "agent")).toBe(agent);
129 expect(browser.contexts[0]?.close).not.toHaveBeenCalled();
130 launches.persistent.mockResolvedValue(makeContext() as unknown as BrowserContext);
131 expect(await pool.contextFor("work/team")).not.toBe(persistent);
132 await pool.clearProfile("never-opened");
133 expect(launches.rm).toHaveBeenLastCalledWith("/test/profiles/never-opened", {
134 recursive: true,
135 force: true,
136 });
137 await pool.close();
138 });
139
140 it("opens a profile requested during its clear only after the storage is deleted", async () => {
141 launches.persistent.mockImplementation(async () => makeContext() as unknown as BrowserContext);
142 const configuration = options();
143 const pool = new ServerBrowserContexts(configuration);
144 await pool.contextFor("work/team");
145 const removing = Promise.withResolvers<void>();
146 const removed = Promise.withResolvers<void>();
147 let deleted = false;
148 launches.rm.mockImplementationOnce(async () => {
149 removing.resolve();
150 await removed.promise;
151 deleted = true;
152 });
153 const launchedAfterDelete: Array<boolean> = [];
154 configuration.executable.mockImplementation(async () => {
155 launchedAfterDelete.push(deleted);
156 return "/test/chromium";
157 });
158 const clearing = pool.clearProfile("work/team");
159 const reopening = pool.contextFor("work/team");
160 await removing.promise;
161 removed.resolve();
162 await Promise.all([clearing, reopening]);
163 expect(launchedAfterDelete).toEqual([true]);
164 await pool.close();
165 });
166
167 it.each([undefined, "default"])(
168 "never retries a failed %s launch with the sandbox disabled",
169 async (profile) => {
170 const launch = profile ? launches.persistent : launches.launch;
171 launch.mockRejectedValue(new Error("sandbox unavailable"));
172 const pool = new ServerBrowserContexts(options());
173 await expect(pool.contextFor(profile ?? INCOGNITO_BROWSER_PROFILE_ID)).rejects.toThrow(
174 "sandbox unavailable",
175 );
176 expect(launch).toHaveBeenCalledTimes(1);
177 expect(
178 profile ? launches.persistent.mock.calls[0]?.[1] : launches.launch.mock.calls[0]?.[0],
179 ).toMatchObject({ chromiumSandbox: true });
180 await pool.close();
181 },
182 );
183
184 it("allows only an explicit sandbox opt-out", async () => {
185 const browser = makeBrowser();
186 launches.launch.mockResolvedValue(browser as unknown as Browser);
187 const pool = new ServerBrowserContexts({
188 ...options(),
189 env: { T3CODE_SERVER_BROWSER_SANDBOX: "0" },
190 });
191 await pool.contextFor(INCOGNITO_BROWSER_PROFILE_ID);
192 expect(launches.launch).toHaveBeenCalledWith(
193 expect.objectContaining({ chromiumSandbox: false }),
194 );
195 await pool.close();
196 });
197
198 it("retries a failed startup on a later request with sandboxing still enabled", async () => {
199 const browser = makeBrowser();
200 launches.launch
201 .mockRejectedValueOnce(new Error("executable unavailable"))
202 .mockResolvedValue(browser as unknown as Browser);
203 const pool = new ServerBrowserContexts(options());
204 await expect(pool.contextFor("default", "agent")).rejects.toThrow("executable unavailable");
205 await pool.contextFor("default", "agent");
206 expect(launches.launch).toHaveBeenCalledTimes(2);
207 expect(launches.launch).toHaveBeenLastCalledWith(
208 expect.objectContaining({ chromiumSandbox: true }),
209 );
210 await pool.close();
211 });
212
213 it("cleans up a browser whose launch finishes during shutdown and rejects future work", async () => {
214 const browser = makeBrowser();
215 const launch = Promise.withResolvers<Browser>();
216 launches.launch.mockReturnValue(launch.promise);
217 const pool = new ServerBrowserContexts(options());
218 const opening = expect(pool.contextFor("default", "agent")).rejects.toThrow("closed");
219 const closing = pool.close();
220 launch.resolve(browser as unknown as Browser);
221 await Promise.all([opening, closing]);
222 await expect(pool.contextFor("default", "agent")).rejects.toThrow("closed");
223 await pool.close();
224 expect(browser.close).toHaveBeenCalledTimes(1);
225 expect(browser.contexts[0]?.close).toHaveBeenCalled();
226 });
227});