import { describe, expect, it } from "vitest";

import { buildTestNamespace, readTestkitContext } from "./test-context.mjs";

describe("buildTestNamespace", () => {
  it("prefers the lease id when present", () => {
    expect(buildTestNamespace({ runtimeId: "runtime-2", leaseId: "lease-4" })).toBe("lease-4");
  });

  it("falls back to the runtime id", () => {
    expect(buildTestNamespace({ runtimeId: "runtime-2" })).toBe("runtime-2");
  });

  it("normalizes unsafe characters", () => {
    expect(buildTestNamespace({ leaseId: " Lease 4 / Weird " })).toBe("lease-4-weird");
  });

  it("falls back to standalone when no runtime identifiers exist", () => {
    expect(buildTestNamespace({})).toBe("standalone");
  });
});

describe("readTestkitContext", () => {
  it("reads active state and normalized identifiers", () => {
    expect(
      readTestkitContext({
        TESTKIT_ACTIVE: "1",
        TESTKIT_RUNTIME_ID: "Runtime 2",
        TESTKIT_LEASE_ID: "Lease 3",
      })
    ).toEqual({
      active: true,
      runtimeId: "runtime-2",
      leaseId: "lease-3",
      namespace: "lease-3",
      rawEnv: {
        TESTKIT_ACTIVE: "1",
        TESTKIT_RUNTIME_ID: "Runtime 2",
        TESTKIT_LEASE_ID: "Lease 3",
      },
    });
  });
});
