import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
  inspectDatabaseUrl,
  shouldGuardPrismaCommand,
} from "./db-target-guard.mjs";

describe("db target guard", () => {
  it("accepts the expected capakraken database target", () => {
    const result = inspectDatabaseUrl(
      "postgresql://capakraken:secret@localhost:5432/capakraken",
      "capakraken",
    );

    assert.equal(result.databaseName, "capakraken");
    assert.equal(result.expectedDatabase, "capakraken");
    assert.equal(result.target, "postgresql://capakraken@localhost:5432/capakraken");
  });

  it("rejects a mismatched database target", () => {
    assert.throws(
      () => inspectDatabaseUrl("postgresql://capakraken:secret@localhost:5432/planarchy", "capakraken"),
      /Unexpected database target 'planarchy'\. Expected 'capakraken'\./,
    );
  });

  it("guards only prisma commands that actually target a database", () => {
    assert.equal(shouldGuardPrismaCommand(["generate"]), false);
    assert.equal(shouldGuardPrismaCommand(["validate", "--schema", "./prisma/schema.prisma"]), false);
    assert.equal(shouldGuardPrismaCommand(["db", "push", "--schema", "./prisma/schema.prisma"]), true);
    assert.equal(shouldGuardPrismaCommand(["migrate", "deploy", "--schema", "./prisma/schema.prisma"]), true);
    assert.equal(shouldGuardPrismaCommand(["studio", "--schema", "./prisma/schema.prisma"]), true);
    assert.equal(shouldGuardPrismaCommand(["db", "push", "--help"]), false);
  });
});
