|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { afterEach, beforeEach, test } from "node:test"; |
| 3 | + |
| 4 | +import { BrowserUseProvider } from "./browser-use.js"; |
| 5 | + |
| 6 | +const originalFetch = globalThis.fetch; |
| 7 | +const originalApiKey = process.env.BROWSER_USE_API_KEY; |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + process.env.BROWSER_USE_API_KEY = "test"; |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + globalThis.fetch = originalFetch; |
| 15 | + if (originalApiKey === undefined) delete process.env.BROWSER_USE_API_KEY; |
| 16 | + else process.env.BROWSER_USE_API_KEY = originalApiKey; |
| 17 | +}); |
| 18 | + |
| 19 | +test("requests recording when the benchmark asks for it", async () => { |
| 20 | + let requestUrl = ""; |
| 21 | + let requestInit: RequestInit | undefined; |
| 22 | + globalThis.fetch = (async (input, init) => { |
| 23 | + requestUrl = String(input); |
| 24 | + requestInit = init; |
| 25 | + return new Response(JSON.stringify({ |
| 26 | + id: "browser-1", |
| 27 | + cdpUrl: "https://cdp.browser-use.com/browser-1", |
| 28 | + })); |
| 29 | + }) as typeof fetch; |
| 30 | + |
| 31 | + const session = await new BrowserUseProvider().create({ recording: true }); |
| 32 | + |
| 33 | + assert.equal(requestUrl, "https://api.browser-use.com/api/v4/browsers"); |
| 34 | + assert.deepEqual(JSON.parse(String(requestInit?.body)), { |
| 35 | + proxyCountryCode: null, |
| 36 | + enableRecording: true, |
| 37 | + }); |
| 38 | + assert.equal(session.cdpUrl, "wss://cdp.browser-use.com/browser-1"); |
| 39 | +}); |
| 40 | + |
| 41 | +test("downloads the recording from the V4 browser record", async () => { |
| 42 | + const urls: string[] = []; |
| 43 | + globalThis.fetch = (async (input) => { |
| 44 | + const url = String(input); |
| 45 | + urls.push(url); |
| 46 | + if (url.endsWith("/browsers/browser-1")) { |
| 47 | + return new Response(JSON.stringify({ |
| 48 | + status: "stopped", |
| 49 | + recordingUrl: "https://recordings.example/browser-1.mp4", |
| 50 | + })); |
| 51 | + } |
| 52 | + return new Response(new Uint8Array([1, 2, 3])); |
| 53 | + }) as typeof fetch; |
| 54 | + |
| 55 | + const recording = await new BrowserUseProvider().downloadRecording("browser-1"); |
| 56 | + |
| 57 | + assert.deepEqual(urls, [ |
| 58 | + "https://api.browser-use.com/api/v4/browsers/browser-1", |
| 59 | + "https://recordings.example/browser-1.mp4", |
| 60 | + ]); |
| 61 | + assert.deepEqual([...recording.data], [1, 2, 3]); |
| 62 | + assert.equal(recording.format, "mp4"); |
| 63 | +}); |
| 64 | + |
| 65 | +test("stops the V4 browser session", async () => { |
| 66 | + let requestUrl = ""; |
| 67 | + let requestInit: RequestInit | undefined; |
| 68 | + globalThis.fetch = (async (input, init) => { |
| 69 | + requestUrl = String(input); |
| 70 | + requestInit = init; |
| 71 | + return new Response(JSON.stringify({ status: "stopped" })); |
| 72 | + }) as typeof fetch; |
| 73 | + |
| 74 | + await new BrowserUseProvider().release("browser-1"); |
| 75 | + |
| 76 | + assert.equal(requestUrl, "https://api.browser-use.com/api/v4/browsers/browser-1"); |
| 77 | + assert.equal(requestInit?.method, "PATCH"); |
| 78 | + assert.deepEqual(JSON.parse(String(requestInit?.body)), { action: "stop" }); |
| 79 | +}); |
0 commit comments