|
| 1 | +/// <reference types="cypress" /> |
| 2 | + |
| 3 | +// E2E coverage for background (passive) agent notebook edits over the MCP |
| 4 | +// bridge: creating and editing a notebook never steals the active tab, the |
| 5 | +// footer popper announces the change, and View lands on the edited notebook |
| 6 | +// with the agent's cells persisted. |
| 7 | + |
| 8 | +const contextPath = process.env.QDB_HTTP_CONTEXT_WEB_CONSOLE || "" |
| 9 | +const baseUrl = `http://localhost:9999${contextPath}` |
| 10 | + |
| 11 | +const { |
| 12 | + installFakeWebSocket, |
| 13 | + TEST_BRIDGE_TOKEN, |
| 14 | + TEST_BRIDGE_URL, |
| 15 | +} = require("../../utils/mcpFakeWebSocket") |
| 16 | + |
| 17 | +const deepLinkSuffix = () => |
| 18 | + `?mcp-pair=1&mcp-ws=${encodeURIComponent(TEST_BRIDGE_URL)}` + |
| 19 | + `&mcp-token=${encodeURIComponent(TEST_BRIDGE_TOKEN)}` |
| 20 | + |
| 21 | +const loginAndVisitDeepLink = () => { |
| 22 | + cy.visit(`${baseUrl}/${deepLinkSuffix()}`, { |
| 23 | + onBeforeLoad: (win) => { |
| 24 | + win.localStorage.clear() |
| 25 | + win.sessionStorage.clear() |
| 26 | + win.indexedDB.deleteDatabase("web-console") |
| 27 | + win.localStorage.setItem( |
| 28 | + "mcp:permissions", |
| 29 | + JSON.stringify({ grantSchemaAccess: true, read: true, write: true }), |
| 30 | + ) |
| 31 | + installFakeWebSocket(win) |
| 32 | + }, |
| 33 | + }) |
| 34 | + cy.loginWithUserAndPassword() |
| 35 | +} |
| 36 | + |
| 37 | +const waitForPaired = () => { |
| 38 | + cy.window({ timeout: 10000 }).its("__mcpFakeWS").should("exist") |
| 39 | + cy.window({ timeout: 10000 }).should((win) => { |
| 40 | + expect(win.__mcpFakeWS.framesOfType("hello").length).to.be.greaterThan(0) |
| 41 | + }) |
| 42 | + cy.window().then((win) => win.__mcpFakeWS.helloAck()) |
| 43 | + cy.getByDataHook("mcp-bridge-status-pill", { timeout: 10000 }).should( |
| 44 | + "contain", |
| 45 | + "MCP connected", |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +const toolResult = (win, requestId) => |
| 50 | + win.__mcpFakeWS |
| 51 | + .framesOfType("tool_result") |
| 52 | + .find((r) => r.requestId === requestId) |
| 53 | + |
| 54 | +const activeTabTitle = () => |
| 55 | + cy.get(".chrome-tab[active]").first().invoke("attr", "data-tab-title") |
| 56 | + |
| 57 | +const expectActiveTabTitle = (title) => |
| 58 | + cy.get(".chrome-tab[active]").should("have.attr", "data-tab-title", title) |
| 59 | + |
| 60 | +const awaitToolResult = (id) => { |
| 61 | + cy.window({ timeout: 10000 }).should((win) => { |
| 62 | + expect(toolResult(win, id), `result for ${id}`).to.exist |
| 63 | + }) |
| 64 | + return cy.window().then((win) => { |
| 65 | + const result = toolResult(win, id) |
| 66 | + expect(result.isError, `tool ${id} errored`).to.not.equal(true) |
| 67 | + // The single-line JSON payload may be wrapped by a permissions notice |
| 68 | + // above and a since-last-check block below. |
| 69 | + const text = result.content[0].text |
| 70 | + const payloadLine = text |
| 71 | + .split("\n") |
| 72 | + .find((line) => line.trimStart().startsWith("{")) |
| 73 | + expect(payloadLine, `JSON payload in result for ${id}`).to.exist |
| 74 | + return JSON.parse(payloadLine) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +describe("agent background notebook edits (e2e)", () => { |
| 79 | + it("creates and fills a notebook in the background; View opens it with the cells persisted", () => { |
| 80 | + loginAndVisitDeepLink() |
| 81 | + cy.getByDataHook("mcp-pair-consent-connect").click() |
| 82 | + waitForPaired() |
| 83 | + |
| 84 | + activeTabTitle().then((initialTab) => { |
| 85 | + // Agent creates a notebook — the active tab must not change. |
| 86 | + cy.window() |
| 87 | + .then((win) => |
| 88 | + awaitToolResult( |
| 89 | + win.__mcpFakeWS.toolCall("create_notebook", { |
| 90 | + label: "Agent NB", |
| 91 | + }), |
| 92 | + ), |
| 93 | + ) |
| 94 | + .then((created) => { |
| 95 | + expect(created.bufferId).to.be.a("number") |
| 96 | + expect(created.hint).to.match(/background/i) |
| 97 | + cy.get(`.chrome-tab[data-tab-title="Agent NB"]`).should("exist") |
| 98 | + expectActiveTabTitle(initialTab) |
| 99 | + |
| 100 | + // Read → edit, per the freshness gate; still fully in the background. |
| 101 | + cy.window().then((win) => |
| 102 | + awaitToolResult( |
| 103 | + win.__mcpFakeWS.toolCall("get_notebook_state", { |
| 104 | + buffer_id: created.bufferId, |
| 105 | + }), |
| 106 | + ), |
| 107 | + ) |
| 108 | + cy.window().then((win) => |
| 109 | + awaitToolResult( |
| 110 | + win.__mcpFakeWS.toolCall("add_cell", { |
| 111 | + buffer_id: created.bufferId, |
| 112 | + sql: "SELECT 123 as agent_made_this", |
| 113 | + after_cell_id: null, |
| 114 | + run: false, |
| 115 | + type: "sql", |
| 116 | + }), |
| 117 | + ), |
| 118 | + ) |
| 119 | + expectActiveTabTitle(initialTab) |
| 120 | + |
| 121 | + // The footer popper announces the background change. |
| 122 | + cy.getByDataHook("agent-changes-popper", { timeout: 10000 }) |
| 123 | + .should("be.visible") |
| 124 | + .and("contain", "Agent NB") |
| 125 | + |
| 126 | + // View lands on the notebook with the agent's cell persisted. |
| 127 | + cy.getByDataHook("agent-changes-view").click() |
| 128 | + expectActiveTabTitle("Agent NB") |
| 129 | + cy.contains("agent_made_this", { timeout: 10000 }).should("exist") |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments