|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createNewSessionPageE2eSuite, |
| 4 | + installMockGateway, |
| 5 | +} from "./new-session-page.test-support.ts"; |
| 6 | + |
| 7 | +const suite = createNewSessionPageE2eSuite(); |
| 8 | + |
| 9 | +async function openDraft( |
| 10 | + operatorScopes: string[], |
| 11 | + featureMethods = ["chat.metadata", "chat.startup", "sessions.create", "sessions.dispatch"], |
| 12 | +) { |
| 13 | + const context = await suite.browser.newContext({ |
| 14 | + locale: "en-US", |
| 15 | + serviceWorkers: "block", |
| 16 | + viewport: { height: 900, width: 1280 }, |
| 17 | + }); |
| 18 | + const page = await context.newPage(); |
| 19 | + const gateway = await installMockGateway(page, { |
| 20 | + featureMethods, |
| 21 | + operatorScopes, |
| 22 | + methodResponses: { |
| 23 | + "sessions.create": { key: "agent:main:operator-scope-proof", runStarted: true }, |
| 24 | + }, |
| 25 | + }); |
| 26 | + await page.goto(`${suite.server.baseUrl}new`); |
| 27 | + await page.locator(".new-session-page__message").fill("scope proof"); |
| 28 | + return { context, gateway, page }; |
| 29 | +} |
| 30 | + |
| 31 | +suite.define(() => { |
| 32 | + it("keeps read-scoped operators out of new-session entry and submission paths", async () => { |
| 33 | + const { context, gateway, page } = await openDraft(["operator.read"]); |
| 34 | + try { |
| 35 | + const sidebarCreate = page.locator(".sidebar-brand__new-thread"); |
| 36 | + const submit = page.getByRole("button", { name: "Start thread" }); |
| 37 | + const incognito = page.getByRole("switch", { name: "Incognito" }); |
| 38 | + |
| 39 | + await expect.poll(() => sidebarCreate.isDisabled()).toBe(true); |
| 40 | + await expect.poll(() => submit.isDisabled()).toBe(true); |
| 41 | + await expect.poll(() => incognito.isDisabled()).toBe(true); |
| 42 | + expect(await incognito.getAttribute("title")).toBe( |
| 43 | + "This action requires operator.admin access.", |
| 44 | + ); |
| 45 | + await submit.click({ force: true }); |
| 46 | + expect(await gateway.getRequests("sessions.create")).toHaveLength(0); |
| 47 | + } finally { |
| 48 | + await context.close(); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + it("allows write-scoped normal creation while keeping incognito admin-only", async () => { |
| 53 | + const { context, gateway, page } = await openDraft(["operator.read", "operator.write"]); |
| 54 | + try { |
| 55 | + const submit = page.getByRole("button", { name: "Start thread" }); |
| 56 | + const incognito = page.getByRole("switch", { name: "Incognito" }); |
| 57 | + |
| 58 | + await expect.poll(() => page.locator(".sidebar-brand__new-thread").isEnabled()).toBe(true); |
| 59 | + await expect.poll(() => submit.isEnabled()).toBe(true); |
| 60 | + await expect.poll(() => incognito.isDisabled()).toBe(true); |
| 61 | + await submit.click(); |
| 62 | + |
| 63 | + await expect(gateway.waitForRequest("sessions.create")).resolves.toMatchObject({ |
| 64 | + params: { agentId: "main", message: "scope proof" }, |
| 65 | + }); |
| 66 | + } finally { |
| 67 | + await context.close(); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it("allows admin-scoped incognito creation with exact dynamic parameters", async () => { |
| 72 | + const { context, gateway, page } = await openDraft([ |
| 73 | + "operator.admin", |
| 74 | + "operator.read", |
| 75 | + "operator.write", |
| 76 | + ]); |
| 77 | + try { |
| 78 | + const incognito = page.getByRole("switch", { name: "Incognito" }); |
| 79 | + await expect.poll(() => incognito.isEnabled()).toBe(true); |
| 80 | + await incognito.click(); |
| 81 | + await page.getByRole("button", { name: "Start thread" }).click(); |
| 82 | + |
| 83 | + await expect(gateway.waitForRequest("sessions.create")).resolves.toMatchObject({ |
| 84 | + params: { incognito: true, message: "scope proof" }, |
| 85 | + }); |
| 86 | + } finally { |
| 87 | + await context.close(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it("blocks creation when the connected Gateway explicitly omits sessions.create", async () => { |
| 92 | + const { context, gateway, page } = await openDraft( |
| 93 | + ["operator.admin", "operator.read", "operator.write"], |
| 94 | + ["chat.metadata", "chat.startup"], |
| 95 | + ); |
| 96 | + try { |
| 97 | + await expect.poll(() => page.locator(".sidebar-brand__new-thread").isDisabled()).toBe(true); |
| 98 | + const submit = page.getByRole("button", { name: "Start thread" }); |
| 99 | + await expect.poll(() => submit.isDisabled()).toBe(true); |
| 100 | + await submit.click({ force: true }); |
| 101 | + expect(await gateway.getRequests("sessions.create")).toHaveLength(0); |
| 102 | + } finally { |
| 103 | + await context.close(); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it("blocks creation while the Gateway is disconnected", async () => { |
| 108 | + const { context, gateway, page } = await openDraft([ |
| 109 | + "operator.admin", |
| 110 | + "operator.read", |
| 111 | + "operator.write", |
| 112 | + ]); |
| 113 | + try { |
| 114 | + await gateway.setOnline(false); |
| 115 | + await gateway.closeLatest(1001, "new-session scope proof"); |
| 116 | + await expect |
| 117 | + .poll(() => |
| 118 | + page.evaluate(() => { |
| 119 | + const app = document.querySelector("openclaw-app") as HTMLElement & { |
| 120 | + runtime?: { context: { gateway: { snapshot: { phase: string } } } }; |
| 121 | + }; |
| 122 | + return app.runtime?.context.gateway.snapshot.phase; |
| 123 | + }), |
| 124 | + ) |
| 125 | + .toBe("reconnecting"); |
| 126 | + |
| 127 | + const submit = page.getByRole("button", { name: "Start thread" }); |
| 128 | + await expect.poll(() => submit.isDisabled()).toBe(true); |
| 129 | + await submit.click({ force: true }); |
| 130 | + expect(await gateway.getRequests("sessions.create")).toHaveLength(0); |
| 131 | + } finally { |
| 132 | + await gateway.setOnline(true); |
| 133 | + await context.close(); |
| 134 | + } |
| 135 | + }); |
| 136 | +}); |
0 commit comments