Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

2.5 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { buildHunks } from "@/components/diff/DiffHunkBuilder";
import { findFirstSchemaDiffChangedLineNumber, findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";

const field: SchemaDiffObject = {
  id: "col-agents-context_window",
  operationType: "modify",
  objectKind: "column",
  name: "context_window",
  selected: true,
  parentId: "table-agents",
};

describe("schema diff DDL location", () => {
  it("finds a moved field independently on both DDL sides", () => {
    const source = ["CREATE TABLE `agents` (", "  `id` bigint NOT NULL,", "  `context_window` int NOT NULL", ");"].join("\n");
    const target = ["CREATE TABLE `agents` (", "  `context_window` int DEFAULT NULL,", "  `id` bigint NOT NULL", ");"].join("\n");

    expect(findSchemaDiffDdlLineNumber(source, field, "source")).toBe(3);
    expect(findSchemaDiffDdlLineNumber(target, field, "target")).toBe(2);
  });

  it("returns null on the side where a created field does not exist", () => {
    const createdField = { ...field, operationType: "create" as const, name: "next_step_prompt", sourceName: "next_step_prompt", targetName: undefined };
    const source = ["CREATE TABLE `agents` (", "  `next_step_prompt` longtext", ");"].join("\n");
    const target = ["CREATE TABLE `agents` (", "  `id` bigint NOT NULL", ");"].join("\n");

    expect(findSchemaDiffDdlLineNumber(source, createdField, "source")).toBe(2);
    expect(findSchemaDiffDdlLineNumber(target, createdField, "target")).toBeNull();
  });

  it("finds the first changed line when a modified table row is selected", () => {
    const source = ["CREATE TABLE `agents` (", "  `id` bigint NOT NULL,", "  `context_window` int NOT NULL", ");"].join("\n");
    const target = ["CREATE TABLE `agents` (", "  `id` bigint NOT NULL,", "  `context_window` int DEFAULT NULL", ");"].join("\n");
    const hunks = buildHunks(source, target);

    expect(findFirstSchemaDiffChangedLineNumber(hunks, "source")).toBe(3);
    expect(findFirstSchemaDiffChangedLineNumber(hunks, "target")).toBe(3);
  });

  it("skips padding when a table change exists on only one side", () => {
    const hunks = [
      {
        leftLines: [{ type: "equal", lineNumber: null, isPadding: true }],
        rightLines: [{ type: "insert", lineNumber: 4, isPadding: false }],
      },
    ];

    expect(findFirstSchemaDiffChangedLineNumber(hunks, "source")).toBeNull();
    expect(findFirstSchemaDiffChangedLineNumber(hunks, "target")).toBe(4);
  });
});