(ns ird.test-env
"Helpers for live integration tests: reads IRD_API_KEY from the
environment, falling back to a .env file in the project root."
(:require
[clojure.java.io :as io]
[clojure.string :as str]))
(defn- parse-dotenv
"Parse KEY=value lines from F (a .env file), ignoring blanks and comments."
[f]
(into {}
(keep (fn [line]
(let [line (str/trim line)]
(when (and (seq line)
(not (str/starts-with? line "#"))
(str/includes? line "="))
(let [[k v] (str/split line #"=" 2)]
[(str/trim k) (str/trim v)])))))
(str/split-lines (slurp f))))
(defn api-key
"The IRD_API_KEY for live tests, from the environment or ./.env. Nil when
unavailable (integration tests should then skip, not fail)."
[]
(or (System/getenv "IRD_API_KEY")
(let [f (io/file ".env")]
(when (.exists f)
(get (parse-dotenv f) "IRD_API_KEY")))))