import { describe, expect, it, afterEach } from "vitest";
import { requireEnv } from "./env";

describe("requireEnv", () => {
  const originalEnv = process.env.SOME_KEY;

  afterEach(() => {
    if (originalEnv === undefined) delete process.env.SOME_KEY;
    else process.env.SOME_KEY = originalEnv;
  });

  it("returns the value when set", () => {
    process.env.SOME_KEY = "value";
    expect(requireEnv("SOME_KEY")).toBe("value");
  });

  it("throws when missing", () => {
    delete process.env.SOME_KEY;
    expect(() => requireEnv("SOME_KEY")).toThrow(
      /Missing required env var: SOME_KEY/,
    );
  });

  it("throws when empty string", () => {
    process.env.SOME_KEY = "";
    expect(() => requireEnv("SOME_KEY")).toThrow();
  });
});
