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 nexus database target", () => {
    const result = inspectDatabaseUrl(
      "postgresql://nexus:secret@localhost:5432/nexus",
      "nexus",
    );

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

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

  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);
  });
});
