import { describe, expect, it } from "vitest";
import { resolveDatabaseUrlFromParts } from "@/core/config/env";

describe("resolveDatabaseUrlFromParts", () => {
  it("prefers DATABASE_URL when present", () => {
    expect(
      resolveDatabaseUrlFromParts({
        DATABASE_URL: "postgresql://a:b@h:5432/db",
        DB_HOST: "ignored",
      }),
    ).toBe("postgresql://a:b@h:5432/db");
  });

  it("builds URL from discrete DB_* vars", () => {
    expect(
      resolveDatabaseUrlFromParts({
        DB_HOST: "dpg-example.render.com",
        DB_PORT: "5432",
        DB_NAME: "architects_db",
        DB_USER: "architects_db_user",
        DB_PASSWORD: "s3cret",
        DB_SSL: "true",
      }),
    ).toBe(
      "postgresql://architects_db_user:s3cret@dpg-example.render.com:5432/architects_db?sslmode=require",
    );
  });

  it("returns undefined when parts are incomplete", () => {
    expect(
      resolveDatabaseUrlFromParts({
        DB_HOST: "localhost",
        DB_USER: "archi",
      }),
    ).toBeUndefined();
  });
});
