(in-package #:lispsearch/test)

;;;; CLI layer: config-special binding (with-config) and global-option wiring.
;;;; Offline — no clingon parse-internals coupling beyond reading the command's
;;;; option list (via ::, which is stable regardless of clingon's export set).

(define-test cli-with-config-binding :parent offline
  (let ((ls:*registry* "default-reg")
        (ls:*catalog-url* "default-url"))
    ;; a supplied (non-NIL) value overrides
    (ls::with-config (:registry "r.example/ns" :catalog-url "https://x/list.txt")
      (is string= "r.example/ns"        ls:*registry*)
      (is string= "https://x/list.txt"  ls:*catalog-url*))
    ;; NIL falls back to the compiled default, and the binding is unwound
    (ls::with-config (:registry nil :catalog-url nil)
      (is string= "default-reg" ls:*registry*)
      (is string= "default-url" ls:*catalog-url*))
    (is string= "default-reg" ls:*registry* "special restored after with-config")))

(define-test cli-options-wired :parent offline
  ;; --db is global; --registry / --catalog-url live on index & refresh so they
  ;; read naturally after the subcommand (e.g. `index --registry ...`).
  (flet ((longs (cmd) (mapcar #'clingon::option-long-name
                              (clingon::command-options cmd))))
    (let ((top (longs (ls:top-command)))
          (idx (longs (ls::index-command)))
          (ref (longs (ls::refresh-command))))
      (true  (member "db" top :test #'string=) "--db is global")
      (false (member "registry" top :test #'string=) "--registry is not global")
      (true  (member "registry" idx :test #'string=) "index --registry")
      (true  (member "catalog-url" idx :test #'string=) "index --catalog-url")
      (true  (member "registry" ref :test #'string=) "refresh --registry")
      (true  (member "catalog-url" ref :test #'string=) "refresh --catalog-url"))))
