import { describe, expect, test } from "bun:test";
import { analyzeMigrationSafety } from "../src/index.ts";

describe("expand and contract migration analysis", () => {
  test("detects destructive and rollout-unsafe SQL with guidance", () => {
    const issues = analyzeMigrationSafety({
      name: "0002_breaking",
      up: `ALTER TABLE users RENAME COLUMN name TO full_name;
        ALTER TABLE users ADD COLUMN tenant_id UUID NOT NULL;
        ALTER TABLE users ALTER COLUMN age TYPE BIGINT;
        DROP TABLE legacy_users;
        CREATE INDEX users_email_idx ON users(email);`,
      down: "",
    });
    expect(issues.map((issue) => issue.code)).toEqual([
      "WRN-DB-RENAME",
      "WRN-DB-ADD-REQUIRED",
      "WRN-DB-TYPE-CHANGE",
      "WRN-DB-DROP-TABLE",
      "WRN-DB-BLOCKING-INDEX",
    ]);
    expect(issues.every((issue) => issue.recommendation.length > 20)).toBe(true);
  });

  test("accepts the additive phase of an expand/contract rollout", () => {
    expect(
      analyzeMigrationSafety({
        name: "0002_expand",
        up: "ALTER TABLE users ADD COLUMN full_name TEXT;",
        down: "",
      }),
    ).toEqual([]);
  });
});
