(in-package #:lispsearch/test)

;;;; Two top-level suites. OFFLINE is network-free and deterministic (run by
;;;; asdf:test-system). NETWORK hits the live catalog/registry and is opt-in.

(define-test offline)
(define-test network)

;;; --- shared helpers ---------------------------------------------------------

(defun fresh-db ()
  "A freshly migrated in-memory database."
  (let ((db (ls:connect ":memory:")))
    (ls:migrate db)
    db))

(defmacro with-fresh-db ((db) &body body)
  "Bind DB to a migrated in-memory database; disconnect on exit."
  `(let ((,db (fresh-db)))
     (unwind-protect (progn ,@body)
       (sqlite:disconnect ,db))))

(defparameter +sample-systems+
  '((:registry-name "cl-html5-parser" :system-name "cl-html5-parser"
     :description "A HTML parser implementing the HTML5 spec for Common Lisp"
     :latest "20240101-aaa" :deps "[\"flexi-streams\"]")
    (:registry-name "plump" :system-name "plump"
     :description "A lenient HTML / XML parser that aims to parse invalid markup"
     :latest "20231201-bbb" :deps "[\"array-utils\"]")
    (:registry-name "cl-ppcre" :system-name "cl-ppcre"
     :description "Perl-compatible regular expression library"
     :latest "20250606-ccc" :deps "[]")
    (:registry-name "drakma" :system-name "drakma"
     :description "A fully-featured HTTP client"
     :latest "20240301-ddd" :deps "[\"cl-ppcre\",\"flexi-streams\"]"))
  "Four enriched records used across the offline DB/FTS tests.")

(defun load-sample (db)
  "Insert +SAMPLE-SYSTEMS+ as enriched rows."
  (dolist (r +sample-systems+) (ls:upsert-system db r)))
