import { describe, expect, it } from "vitest";
import { assertLocalDatabaseUrl } from "./guard";

describe("assertLocalDatabaseUrl", () => {
  it("accepte localhost, 127.0.0.1 et ::1", () => {
    expect(() =>
      assertLocalDatabaseUrl("postgres://seoinsight:seoinsight@localhost:5432/auth"),
    ).not.toThrow();
    expect(() =>
      assertLocalDatabaseUrl("postgres://seoinsight:seoinsight@127.0.0.1:5432/auth"),
    ).not.toThrow();
    expect(() =>
      assertLocalDatabaseUrl("postgres://seoinsight:seoinsight@[::1]:5432/auth"),
    ).not.toThrow();
  });

  it("refuse tout autre hôte en le nommant", () => {
    expect(() =>
      assertLocalDatabaseUrl("postgres://seoinsight:secret@db.example.com:5432/auth"),
    ).toThrow(/db\.example\.com/);
    expect(() =>
      assertLocalDatabaseUrl("postgres://seoinsight:secret@10.0.0.5:5432/auth"),
    ).toThrow();
    expect(() =>
      assertLocalDatabaseUrl("postgres://seoinsight:secret@localhost.example.com:5432/auth"),
    ).toThrow();
  });

  it("refuse une URL sans hôte", () => {
    expect(() => assertLocalDatabaseUrl("postgres:///auth")).toThrow();
  });
});
