(ns ajet.chat.shared.config-unit-test
  (:require [clojure.test :refer [deftest testing is]]
            [ajet.chat.shared.config :as config]))

;;; ---------------------------------------------------------------------------
;;; load-config tests
;;; ---------------------------------------------------------------------------

(deftest load-config-returns-defaults
  (let [cfg (config/load-config {:resource "nonexistent.edn"})]
    (is (= "localhost" (get-in cfg [:db :host])))
    (is (= 5432 (get-in cfg [:db :port])))
    (is (= "ajet_chat" (get-in cfg [:db :dbname])))
    (is (= "nats://localhost:4222" (get-in cfg [:nats :url])))))

(deftest load-config-missing-file-throws
  (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Config file not found"
        (config/load-config {:file "/nonexistent/path.edn"}))))

(deftest load-config-default-minio
  (let [cfg (config/load-config {:resource "nonexistent.edn"})]
    (is (= "http://localhost:9000" (get-in cfg [:minio :endpoint])))
    (is (= "minioadmin" (get-in cfg [:minio :access-key])))
    (is (= "ajet-chat" (get-in cfg [:minio :bucket])))))

(deftest load-config-db-pool-defaults
  (let [cfg (config/load-config {:resource "nonexistent.edn"})]
    (is (= 10 (get-in cfg [:db :pool-size])))))

;;; ---------------------------------------------------------------------------
;;; redact tests
;;; ---------------------------------------------------------------------------

(deftest redact-secrets
  (let [cfg {:db {:password "hunter2" :host "localhost"}}
        redacted (config/redact cfg)]
    (is (= "[REDACTED]" (get-in redacted [:db :password])))
    (is (= "localhost" (get-in redacted [:db :host])))))

(deftest redact-nested-secrets
  (let [cfg {:minio {:secret-key "abc123" :endpoint "http://localhost:9000"}}
        redacted (config/redact cfg)]
    (is (= "[REDACTED]" (get-in redacted [:minio :secret-key])))
    (is (= "http://localhost:9000" (get-in redacted [:minio :endpoint])))))

(deftest redact-non-map-passthrough
  (is (= "hello" (config/redact "hello")))
  (is (= 42 (config/redact 42))))

(deftest redact-sequential
  (let [cfg [{:token "secret"} {:name "visible"}]
        redacted (config/redact cfg)]
    (is (= "[REDACTED]" (:token (first redacted))))
    (is (= "visible" (:name (second redacted))))))
