import { describe, expect, it } from "vitest";
import {
  buildContainerName,
  buildDatabaseUrl,
  buildTemplateDatabaseName,
  buildWorkerDatabaseName,
  escapeIdentifier,
  escapeSqlLiteral,
  slugSegment,
} from "./naming.mjs";

describe("database-naming", () => {
  it("builds deterministic names and URLs", () => {
    expect(buildContainerName("/tmp/My Product")).toMatch(/^testkit_pg_my_product_/);
    expect(buildTemplateDatabaseName("api", "1234567890abcdef1234")).toBe(
      "tk_tpl_api_1234567890abcdef"
    );
    expect(buildWorkerDatabaseName("api", "/tmp/state", "abcdef1234567890")).toMatch(
      /^tk_api_[a-f0-9]{10}_abcdef123456$/
    );
    expect(
      buildDatabaseUrl(
        {
          user: "a user",
          password: "p@ss word",
          host: "127.0.0.1",
          port: 5432,
        },
        "dbname"
      )
    ).toContain("postgresql://a%20user:p%40ss%20word@127.0.0.1:5432/dbname");
  });

  it("escapes identifiers and literals", () => {
    expect(slugSegment("My API")).toBe("my_api");
    expect(escapeIdentifier('bad"name')).toBe('bad""name');
    expect(escapeSqlLiteral("it's")).toBe("it''s");
  });
});
