|
| 1 | +import { describe, expect, it } from "@effect/vitest" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { afterEach, beforeEach, vi } from "vitest" |
| 4 | + |
| 5 | +import { openProjectBrowserById, openSelectedProjectBrowser } from "../../src/web/actions-browser.js" |
| 6 | +import type { ProjectBrowserSession } from "../../src/web/api.js" |
| 7 | +import { makeBrowserActionContext, waitForAssertion } from "./browser-action-context-fixture.js" |
| 8 | + |
| 9 | +const loadProjectBrowserMock = vi.hoisted(() => vi.fn()) |
| 10 | + |
| 11 | +vi.mock("../../src/web/api.js", () => ({ |
| 12 | + loadProjectBrowser: loadProjectBrowserMock, |
| 13 | + projectBrowserCdpUrl: (browser: { readonly cdpPath: string }) => browser.cdpPath, |
| 14 | + projectBrowserNoVncUrl: (browser: { readonly noVncPath: string }) => browser.noVncPath |
| 15 | +})) |
| 16 | + |
| 17 | +const runningBrowser: ProjectBrowserSession = { |
| 18 | + cdpPath: "/api/projects/project-1/browser/cdp", |
| 19 | + cdpUrl: "ws://172.17.0.2:9222/devtools/browser/session", |
| 20 | + containerName: "dg-browser-project-1", |
| 21 | + noVncPath: "/api/projects/project-1/browser/novnc", |
| 22 | + noVncUrl: "https://172.17.0.2:6080/vnc.html", |
| 23 | + projectId: "project-1", |
| 24 | + projectKey: "octocat/hello-world", |
| 25 | + status: "running" |
| 26 | +} |
| 27 | + |
| 28 | +const missingBrowser: ProjectBrowserSession = { |
| 29 | + ...runningBrowser, |
| 30 | + cdpPath: "", |
| 31 | + cdpUrl: "", |
| 32 | + noVncPath: "", |
| 33 | + noVncUrl: "", |
| 34 | + status: "missing" |
| 35 | +} |
| 36 | + |
| 37 | +describe("web browser actions", () => { |
| 38 | + beforeEach(() => { |
| 39 | + loadProjectBrowserMock.mockReset() |
| 40 | + }) |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + vi.unstubAllGlobals() |
| 44 | + }) |
| 45 | + |
| 46 | + it.effect("opens a running project browser by id", () => |
| 47 | + Effect.gen(function*(_) { |
| 48 | + const openMock = vi.fn<NonNullable<typeof globalThis.open>>(() => null) |
| 49 | + vi.stubGlobal("open", openMock) |
| 50 | + loadProjectBrowserMock.mockImplementation((projectId: string) => Effect.succeed({ ...runningBrowser, projectId })) |
| 51 | + |
| 52 | + const { context, setMessage, setProjectBrowser } = makeBrowserActionContext({ |
| 53 | + selectedProjectName: "octocat/hello-world" |
| 54 | + }) |
| 55 | + |
| 56 | + openProjectBrowserById("project-1", context) |
| 57 | + |
| 58 | + yield* _(waitForAssertion(() => { |
| 59 | + expect(setProjectBrowser).toHaveBeenCalledWith(runningBrowser) |
| 60 | + })) |
| 61 | + |
| 62 | + expect(openMock).toHaveBeenCalledWith("/api/projects/project-1/browser/novnc", "_blank", "noopener") |
| 63 | + expect(setMessage).toHaveBeenLastCalledWith( |
| 64 | + "Browser popup was blocked. Open /api/projects/project-1/browser/novnc manually. CDP endpoint: /api/projects/project-1/browser/cdp." |
| 65 | + ) |
| 66 | + })) |
| 67 | + |
| 68 | + it.effect("reports browser sidecar status instead of opening non-running browsers", () => |
| 69 | + Effect.gen(function*(_) { |
| 70 | + const openMock = vi.fn<NonNullable<typeof globalThis.open>>(() => null) |
| 71 | + vi.stubGlobal("open", openMock) |
| 72 | + loadProjectBrowserMock.mockImplementation(() => Effect.succeed(missingBrowser)) |
| 73 | + |
| 74 | + const { context, setMessage, setProjectBrowser } = makeBrowserActionContext({ |
| 75 | + selectedProjectId: "project-1", |
| 76 | + selectedProjectName: "octocat/hello-world" |
| 77 | + }) |
| 78 | + |
| 79 | + openSelectedProjectBrowser(context) |
| 80 | + |
| 81 | + yield* _(waitForAssertion(() => { |
| 82 | + expect(setProjectBrowser).toHaveBeenCalledWith(missingBrowser) |
| 83 | + })) |
| 84 | + |
| 85 | + expect(openMock).not.toHaveBeenCalled() |
| 86 | + expect(setMessage).toHaveBeenLastCalledWith( |
| 87 | + "Browser sidecar is missing. Enable Playwright MCP and start the project first." |
| 88 | + ) |
| 89 | + })) |
| 90 | + |
| 91 | + it("does not call the browser endpoint when no project is selected", () => { |
| 92 | + const { context, setMessage } = makeBrowserActionContext() |
| 93 | + |
| 94 | + openSelectedProjectBrowser(context) |
| 95 | + |
| 96 | + expect(loadProjectBrowserMock).not.toHaveBeenCalled() |
| 97 | + expect(setMessage).toHaveBeenLastCalledWith("No project selected.") |
| 98 | + }) |
| 99 | +}) |
0 commit comments