Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
// @vitest-environment node
import { describe, expect, test } from "vitest";
import {
buildBehaviorAnalyticsConfig,
classifyBehaviorRoute,
createBehaviorMetric,
sanitizeBehaviorProperties,
} from "./behavior";
describe("behavior analytics config", () => {
test("stays disabled unless a PostHog key and host are both present", () => {
expect(
buildBehaviorAnalyticsConfig({
posthogKey: "",
posthogHost: "https://us.i.posthog.com",
deployment: "cloud",
recordingSampleRate: 0.1,
})
).toBeNull();
expect(
buildBehaviorAnalyticsConfig({
posthogKey: "phc_test",
deployment: "cloud",
recordingSampleRate: 0.1,
})
).toBeNull();
});
test("uses PostHog-native pageview and recording defaults when enabled", () => {
const config = buildBehaviorAnalyticsConfig({
posthogKey: "phc_test",
posthogHost: "https://us.i.posthog.com",
deployment: "cloud",
recordingSampleRate: 0.25,
});
expect(config).toMatchObject({
apiKey: "phc_test",
options: {
api_host: "https://us.i.posthog.com",
autocapture: {
capture_copied_text: false,
css_selector_ignorelist: [
".ph-no-autocapture",
"[data-ph-no-autocapture]",
".ph-no-capture",
"[data-sensitive]",
],
dom_event_allowlist: ["click", "submit"],
element_allowlist: ["a", "button", "form", "label"],
element_attribute_ignorelist: [
"aria-label",
"href",
"title",
"data-sensitive",
],
},
capture_pageview: "history_change",
capture_pageleave: true,
disable_session_recording: false,
person_profiles: "identified_only",
session_recording: {
maskAllInputs: true,
blockClass: "ph-no-capture",
sampleRate: 0.25,
},
},
});
if (!config) {
throw new Error("Expected behavior analytics config");
}
expect(
(config.options.session_recording as Record<string, unknown>)
.maskTextSelector
).toBeUndefined();
});
test("adds common metadata to PostHog event properties", () => {
const config = buildBehaviorAnalyticsConfig({
posthogKey: "phc_test",
posthogHost: "https://us.i.posthog.com",
deployment: "cloud",
recordingSampleRate: 0.25,
gitCommit: "abc123",
resolveRouteId: (url) =>
url.includes("/projects/acme/")
? "workspace.project.database"
: undefined,
});
if (!config) {
throw new Error("Expected behavior analytics config");
}
expect(config.properties).toEqual({
deployment: "cloud",
git_commit: "abc123",
});
const sanitizeProperties = config.options.sanitize_properties as (
properties: Record<string, unknown>,
eventName?: string
) => Record<string, unknown>;
expect(
sanitizeProperties(
{
$current_url:
"https://cloud.bytebase.com/projects/acme/databases?token=secret",
title: "Acme production - Bytebase",
},
"$pageview"
)
).toEqual({
deployment: "cloud",
git_commit: "abc123",
route_id: "workspace.project.database",
});
});
});
describe("behavior analytics routes", () => {
test("allows page sessions on any named route", () => {
expect(
classifyBehaviorRoute({
name: "auth.setup",
})
).toMatchObject({
routeId: "auth.setup",
recording: "allow",
});
expect(
classifyBehaviorRoute({
name: "sql-editor.database",
})
).toMatchObject({
routeId: "sql-editor.database",
recording: "allow",
});
expect(
classifyBehaviorRoute({
name: "workspace.audit-log",
})
).toMatchObject({
routeId: "workspace.audit-log",
recording: "allow",
});
});
test("ignores missing route names", () => {
expect(
classifyBehaviorRoute({
name: undefined,
})
).toEqual({
recording: "ignore",
});
});
});
describe("behavior analytics privacy helpers", () => {
test("removes URL-like and raw error properties before provider capture", () => {
expect(
sanitizeBehaviorProperties({
route_id: "workspace.instance.create",
$current_url:
"https://cloud.bytebase.com/projects/customer-a/[email protected]",
current_url:
"https://cloud.bytebase.com/sql-editor/projects/p1/sheets/s1",
raw_error: "dial tcp db.internal:5432 failed",
safe: true,
})
).toEqual({
route_id: "workspace.instance.create",
safe: true,
});
});
test("removes page URL properties for PostHog pageviews", () => {
expect(
sanitizeBehaviorProperties(
{
$current_url:
"https://cloud.bytebase.com/projects/customer-a/[email protected]#token",
$pathname: "/projects/customer-a/databases",
current_url:
"https://cloud.bytebase.com/sql-editor/projects/p1/sheets/s1",
},
"$pageview"
)
).toEqual({});
});
test("removes visible element text from autocapture payloads", () => {
expect(
sanitizeBehaviorProperties(
{
$el_text: "users/[email protected]",
$elements: [
{
$el_text: "users/[email protected]",
attr__href: "/users/[email protected]",
tag_name: "a",
},
],
$elements_chain: "a.users attr__href=/users/[email protected]",
safe: true,
},
"$autocapture"
)
).toEqual({
safe: true,
});
});
});
describe("behavior analytics metrics", () => {
test("creates allowlisted page session metrics with route context", () => {
expect(
createBehaviorMetric("page session", {
routeId: "workspace.instance.create",
durationMs: 12_000,
visibleDurationMs: 9_000,
isBounce: false,
})
).toEqual({
event: "page session",
properties: {
route_id: "workspace.instance.create",
duration_ms: 12_000,
visible_duration_ms: 9_000,
is_bounce: false,
},
});
});
test("creates allowlisted custom action metrics and drops unsafe properties", () => {
expect(
createBehaviorMetric("workspace setup submitted", {
routeId: "workspace.landing",
resource: "projects/demo",
properties: {
scenario: "query-data",
result: "finished",
sample_enabled: true,
},
})
).toEqual({
event: "workspace setup submitted",
properties: {
route_id: "workspace.landing",
resource: "projects/demo",
scenario: "query-data",
result: "finished",
sample_enabled: true,
},
});
});
test("creates locked feature click metrics with gate context", () => {
expect(
createBehaviorMetric("locked feature clicked", {
routeId: "workspace.profile",
properties: {
feature: "FEATURE_TWO_FA",
lock_reason: "subscription_plan",
required_plan: "ENTERPRISE",
},
})
).toEqual({
event: "locked feature clicked",
properties: {
route_id: "workspace.profile",
feature: "FEATURE_TWO_FA",
lock_reason: "subscription_plan",
required_plan: "ENTERPRISE",
},
});
});
test("drops unset metric properties", () => {
const metric = createBehaviorMetric("instance connection test clicked", {
routeId: "workspace.instance.detail",
});
expect(Object.keys(metric.properties)).toEqual(["route_id"]);
});
});