Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

14.2 KB
Raw Blame
// SQL Editor — connection lifecycle (CUJ coverage).
//
// Verifies the user-facing contracts of opening, restoring, and
// switching SQL Editor connections. These are happy-path flows: each
// test should PASS today, and a regression in routing / project
// context / connection-panel state surfaces as a failure here.
//
// Scope: CUJs C1, C2, C3, C5, C13 from PHASE-4-PLAN.md (Batch 5,
// Arc 1 — "Open + connect"). C6 schema dropdown and C7 BatchQuerySelect
// are deferred to a follow-up extension of this file.

import { test, expect, type BrowserContext, type Page } from "@playwright/test";
import { loadTestEnv, type TestEnv } from "../framework/env";
import type { BytebaseApiClient } from "../framework/api-client";
import { SqlEditorPage } from "./sql-editor.page";
import {
  ensureSecondaryProject,
  SECONDARY_PROJECT_TITLE,
} from "../framework/seed-test-data";

let env: TestEnv & { api: BytebaseApiClient };
let sharedContext: BrowserContext;
let page: Page;
let sqlEditor: SqlEditorPage;

test.beforeAll(async ({ browser }) => {
  env = loadTestEnv();
  await env.api.login(env.adminEmail, env.adminPassword);
  sharedContext = await browser.newContext({ storageState: ".auth/state.json" });
  page = await sharedContext.newPage();
  sqlEditor = new SqlEditorPage(page, env.baseURL);
});

test.afterAll(async () => {
  await sharedContext?.close();
});

test.describe("Cold-open lands on the SQL Editor home view", () => {
  // C1 — visiting /sql-editor with no project / sheet / database in
  // the URL must render the editor shell (project picker visible,
  // gutter rail visible) without a hard redirect or a blank page.
  // The user opens the editor from the nav and expects to land in a
  // usable state.

  test("opens with project picker and gutter rail rendered", async () => {
    test.setTimeout(120_000);

    await sqlEditor.gotoHome();
    await page.waitForTimeout(800);

    // Project switching now lives in the shared SQL Editor header
    // (SQLEditorHeader → HeaderBreadcrumb → ProjectSegment). The route-ownership
    // refactor removed the old AsidePanel `.project-select` combobox, so the
    // stable home-view landmark is the header's "Select project" switcher.
    await expect(
      page.getByRole("button", { name: "Select project" }).first(),
    ).toBeVisible({ timeout: 10_000 });

    // Gutter rail buttons are stable user-facing labels (SavedQuery /
    // Schema / History gutter tabs).
    await expect(sqlEditor.gutterSavedQueryTab).toBeVisible();
    await expect(sqlEditor.gutterSchemaTab).toBeVisible();
    await expect(sqlEditor.gutterHistoryTab).toBeVisible();

    // The URL is either the home route or auto-redirected to a default
    // project (whichever the SQL editor's bootstrap picked) — not a
    // last-visited deep link the user can't recognize. Both shapes are
    // user-recoverable; what matters is that we landed inside
    // /sql-editor and not somewhere else (404, /auth, /settings).
    expect(page.url()).toMatch(/\/sql-editor(\/projects\/[^/]+)?\/?$/);
  });
});

test.describe("Opening a saved query by UUID restores its title in a tab", () => {
  // C2 — deep-linking into /projects/<p>/savedQueries/<uuid> must hydrate
  // the saved query and render its title as the active tab. This is the
  // shareable-link contract: a URL pasted from chat / the Recent menu
  // must reproducibly land the user on that savedQuery.

  const SOURCE_TITLE = `e2e-cuj-c2-${Date.now()}`;
  const SOURCE_CONTENT = "SELECT 1;";

  let sourceSavedQuery = "";

  test.beforeAll(async () => {
    const created = await env.api.createSavedQuery(
      env.project,
      SOURCE_TITLE,
      env.database,
      SOURCE_CONTENT,
    );
    sourceSavedQuery = created.name;
  });

  test.afterAll(async () => {
    if (sourceSavedQuery) await env.api.deleteSavedQuery(sourceSavedQuery);
  });

  test("active tab text matches the saved query title after deep-link", async () => {
    test.setTimeout(120_000);

    const projectId = env.project.split("/").pop()!;
    const sheetUuid = sourceSavedQuery.split("/").pop()!;
    await sqlEditor.gotoSheet(projectId, sheetUuid);
    await page.waitForTimeout(1500);

    await expect(sqlEditor.activeTab()).toContainText(SOURCE_TITLE, {
      timeout: 10_000,
    });
  });
});

test.describe("Instance-only URL lands without a connected database", () => {
  // C3 — visiting /projects/<p>/instances/<i> (no /databases/<db>) is
  // a partial connection: the user picked an instance but hasn't
  // chosen a database yet. The breadcrumb should show "Select a
  // database to start" rather than acting as if the user is connected.

  test('breadcrumb shows the "select a database" prompt', async () => {
    test.setTimeout(120_000);

    const projectId = env.project.split("/").pop()!;
    await page.goto(
      `${env.baseURL}/sql-editor/projects/${projectId}/instances/${env.instanceId}`,
    );
    await page.waitForTimeout(1500);

    // DatabaseChooser renders `t("sql-editor.select-a-database-to-start")`
    // when the connection has an instance but no database. We anchor by
    // the visible label to keep the test resilient to translation
    // refactors that don't change the user-facing copy.
    await expect(
      page.getByText("Select a database to start", { exact: true }).first(),
    ).toBeVisible({ timeout: 10_000 });
  });
});

test.describe("Project switcher updates the editor's project context", () => {
  // C5 — choosing a different project from the picker must update the
  // editor's project context (URL changes to /projects/<newProj>) so
  // the sidebar tree, schema panel, and saved query list all refresh
  // against the new project's databases.
  //
  // The post-demo bootstrap (#20393) only creates one sample project
  // ("Sample Project" / `project-sample`); the switcher needs a SECOND
  // project to be a meaningful test. We provision it on-demand here
  // (NOT in the global seedTestData) so that adding a second project
  // doesn't shift the SQL editor's default landing project for other
  // specs (e.g. sql-editor-saved-query.spec.ts uses gotoHome() and
  // assumes `project-sample` is current).

  test.beforeAll(async () => {
    // Created on demand and intentionally NOT deleted afterward.
    // ensureSecondaryProject is idempotent (tolerates 409) and the disposable
    // server is fresh per run. A soft delete would reserve the resource ID,
    // and a Playwright CI retry reuses the SAME server — the swallowed-409
    // recreate would then leave the switcher with no visible second project.
    // Leaving it in place is retry-stable; no later spec enumerates projects.
    await ensureSecondaryProject(env.api);
  });

  test("picking a different project switches the editor's project context", async () => {
    test.setTimeout(120_000);

    // Navigate to the discovered project explicitly (not gotoHome) so
    // the test starts in a deterministic place even after the
    // secondary project briefly becomes the default-landing target.
    const projectId = env.project.split("/").pop()!;
    await sqlEditor.gotoWithDb(projectId, env.instanceId, env.databaseId);
    await page.waitForTimeout(800);

    // Project switching moved to the header breadcrumb (ProjectSegment). Open
    // its "Select project" switcher; the popover shows Recent/All tabs, a
    // filter, and a project table whose rows select on click.
    const projectSwitcher = page
      .getByRole("button", { name: "Select project" })
      .first();
    await expect(projectSwitcher).toBeVisible({ timeout: 10_000 });
    await projectSwitcher.click();

    // Filter by the secondary project's name so it surfaces regardless of
    // recency, then click its row.
    const filter = page.getByPlaceholder("Filter by name");
    await expect(filter).toBeVisible({ timeout: 5000 });
    await filter.fill(SECONDARY_PROJECT_TITLE);
    const option = page
      .getByText(SECONDARY_PROJECT_TITLE, { exact: true })
      .first();
    await expect(option).toBeVisible({ timeout: 5000 });
    await option.click();

    // The editor's project context switches — the header breadcrumb's project
    // segment now displays the picked project. (The SQL-editor route is
    // database-centric: the URL's project segment only changes once a database
    // in the new project is opened, so an empty project like the secondary one
    // keeps the prior DB URL while the context switches.)
    await expect(
      page.locator("header").getByText(SECONDARY_PROJECT_TITLE, { exact: true }),
    ).toBeVisible({ timeout: 10_000 });
  });
});

test.describe("Connection panel exposes Databases + Database Group tabs", () => {
  // C13 — clicking the breadcrumb opens a connection-panel Sheet that
  // lets the user pick from a database tree (Databases tab) OR a
  // saved database group (Database Group tab). Both tabs must be
  // present so the user can route the active tab to either kind of
  // target.

  test("both tabs render after opening the connection panel", async () => {
    test.setTimeout(120_000);

    const projectId = env.project.split("/").pop()!;
    await sqlEditor.gotoWithDb(projectId, env.instanceId, env.databaseId);
    await page.waitForTimeout(800);

    // Click the breadcrumb to open the connection panel. The
    // breadcrumb currently shows the env / instance / db chain; we
    // anchor by the database short name (env.databaseId) which the
    // chooser renders verbatim.
    const breadcrumb = page
      .locator("button")
      .filter({ hasText: env.databaseId })
      .first();
    await expect(breadcrumb).toBeVisible({ timeout: 10_000 });
    await breadcrumb.click();

    const connectionPanel = page.getByRole("dialog");
    await expect(connectionPanel).toBeVisible({ timeout: 5000 });

    await expect(
      connectionPanel.getByRole("tab", { name: "Databases", exact: true }),
    ).toBeVisible({ timeout: 5000 });
    await expect(
      connectionPanel.getByRole("tab", {
        name: "Database Group",
        exact: true,
      }),
    ).toBeVisible({ timeout: 5000 });
  });
});

test.describe("SQL Editor entry from the database page connects the tab (BYT-9616)", () => {
  // BYT-9616 (FIXED, #20473): opening the SQL Editor from a database detail page
  // (the "SQL Editor" button, which window.open's a new tab at
  // /sql-editor/projects/<p>/instances/<i>/databases/<db>) landed on a tab that
  // was NOT actually connected — Run was grayed out for every database and admin
  // mode couldn't be entered. It only reproduced in a COLD session: the editor
  // shell hydrated databases into the React app store, but isConnectedSQLEditorTab
  // still read the (empty-on-cold-deep-link) Pinia store, so the tab was treated
  // as disconnected. A warm session masked it.
  //
  // The lock uses a FRESH context (cold store, like the incognito repro) and does
  // NOT visit /sql-editor first, then drives the database-page entry button.

  let ctx9616: BrowserContext;
  let dbPage: Page;

  test.beforeAll(async ({ browser }) => {
    // Fresh context = cold SQL-editor store, matching the incognito-only repro.
    ctx9616 = await browser.newContext({ storageState: ".auth/state.json" });
    dbPage = await ctx9616.newPage();
  });

  test.afterAll(async () => {
    await ctx9616?.close();
  });

  test("clicking 'SQL Editor' on the database page opens a connected, runnable tab", async () => {
    test.setTimeout(120_000);

    const projectId = env.project.split("/").pop()!;
    // Database detail page (cold load — do NOT visit /sql-editor first).
    // Navigate exactly as the product links to it (autoDatabaseRoute): the
    // `parent` query carries the project-parented instance name. Without it the
    // page falls back to the legacy `instances/<id>` parent, GetDatabase 404s,
    // and the route bounces to the landing page.
    const parent = env.database.replace(/\/databases\/[^/]+$/, "");
    await dbPage.goto(
      `${env.baseURL}/projects/${projectId}/instances/${env.instanceId}/databases/${env.databaseId}?parent=${encodeURIComponent(parent)}`,
    );
    await dbPage.keyboard.press("Escape").catch(() => {});
    await dbPage.waitForTimeout(1500);

    // The database page's entry is the "Open SQL Editor" link in its action bar
    // (DatabaseSQLEditorButton.tsx), which opens the deep link in a new tab.
    // DashboardHeader also renders a "SQL Editor" link to the SAME deep link on
    // a database route, so match the page entry's full name exactly: a loose
    // "SQL Editor" match would pass even if this entry were disabled or its
    // handler regressed.
    const entry = dbPage.getByRole("link", {
      name: "Open SQL Editor",
      exact: true,
    });
    await expect(entry).toBeVisible({ timeout: 10_000 });

    const [popup] = await Promise.all([
      ctx9616.waitForEvent("page"),
      entry.click(),
    ]);
    await popup.waitForLoadState("domcontentloaded");
    await popup.keyboard.press("Escape").catch(() => {});
    await popup.waitForTimeout(2000);

    // The popup must be the SQL-editor DB deep link for this database.
    expect(popup.url()).toContain(
      `/sql-editor/projects/${projectId}/instances/${env.instanceId}/databases/${env.databaseId}`,
    );

    const popupEditor = new SqlEditorPage(popup, env.baseURL);
    await expect(popupEditor.runButton).toBeVisible({ timeout: 15_000 });

    // (1) Type a query, THEN assert Run is enabled. Run is correctly disabled on
    // an EMPTY editor, so the discriminating check is "enabled once a query is
    // present" — pre-fix Run stayed grayed even with a query because the tab was
    // treated as disconnected.
    await popupEditor.setEditorContent("SELECT 1 AS n;");
    await expect(
      popupEditor.runButton,
      "Run must become enabled once a query is typed on the connected tab " +
        "(pre-fix it stayed grayed because the tab was disconnected)",
    ).toBeEnabled({ timeout: 15_000 });

    // (2) Running it returns a result — proves the tab is truly connected.
    await popupEditor.runButton.click();
    await expect(popup.getByText(/^1\s+rows?$/i).first()).toBeVisible({
      timeout: 15_000,
    });

    // (3) Admin mode (the wrench) is reachable — it was unavailable pre-fix.
    await expect(
      popupEditor.adminModeButton,
      "admin-mode wrench must be available on the database-page-opened tab",
    ).toBeVisible({ timeout: 10_000 });

    await popup.close();
  });
});