(ns ird.integration-test
"Live tests against https://api.ird.ro. Tagged :integration and skipped by
default (see tests.edn); run with
clojure -M:test --focus-meta :integration
Only read-only GET endpoints are exercised — nothing here mutates the
account or charges credits beyond metered reads."
(:require
[clojure.test :refer [deftest is testing]]
[ird.client :as ird]
[ird.credits :as credits]
[ird.domains :as domains]
[ird.ipfs :as ipfs]
[ird.p2p :as p2p]
[ird.test-env :as test-env]
[ird.vps :as vps]))
(defn- live-client []
(when-let [key (test-env/api-key)]
(ird/make-client {:api-key key})))
(deftest ^:integration anonymous-domain-check
(let [client (ird/make-client {})
resp (domains/check client "example.ro")]
(is (= "example.ro" (:domain resp)))
(is (string? (:status resp)))))
(deftest ^:integration authenticated-read-endpoints
(if-let [client (live-client)]
(do
(testing "credits balance"
(is (map? (credits/balance client))))
(testing "credits prices"
(is (map? (credits/prices client))))
(testing "domain check with key"
(is (= "registered" (:status (domains/check client "ird.ro")))))
(testing "vps tiers"
(is (map? (vps/list-tiers client))))
(testing "ipfs usage"
(is (map? (ipfs/usage client))))
;; ird.sharing and ird.transfers reads are account-JWT-only, so they
;; cannot be exercised live with an API key; the coverage table covers
;; their request shapes.
(testing "gateway domains carry their served-pin count"
(let [domains (:domains (ipfs/list-domains client))]
(is (vector? domains))
;; pin_count is required on every row, so it is there even at zero
(doseq [d domains]
(is (int? (:pin_count d)) (:domain d)))
(if-let [d (first domains)]
(testing "and their pins page"
(let [page (ipfs/list-domain-pins client (:id d) {:limit 1})]
(is (int? (:total page)))
(is (sequential? (:pins page)))
(is (= (:total page) (count (ipfs/list-all-domain-pins client (:id d))))))
(testing "an unknown gateway domain is a 404, not a silent empty page"
(let [e (try (ipfs/list-domain-pins
client "00000000-0000-0000-0000-000000000000")
(catch clojure.lang.ExceptionInfo ex ex))]
(is (= 404 (:status (ex-data e)))))))
(println "no gateway domains on this account - skipping the pins page"))))
(testing "p2p apps report their mailbox state"
(doseq [app (:apps (p2p/list-apps client))]
(is (boolean? (:mailbox_enabled app)) (:name app))
(is (boolean? (:mailbox_open_mint app)) (:name app))
;; null until the mailbox is on and the node has acked the app
(is (or (nil? (:mailbox_url app)) (string? (:mailbox_url app)))))
(let [usage (:usage (p2p/usage client))]
(is (int? (:cached_bytes usage)))
(is (int? (:mailbox_bytes_total usage)))
;; prices are decimal STRINGS across the API, never JSON numbers -
;; they carry more precision than a double survives
(is (string? (:per_mb_hour_cached usage)))))
(testing "error envelope maps to :ird/api-error (JWT-only endpoint with API key)"
(let [e (try ((requiring-resolve 'ird.sharing/list-grants) client)
(catch clojure.lang.ExceptionInfo ex ex))]
(is (= :ird/api-error (:type (ex-data e))))
(is (= 401 (:status (ex-data e)))))))
(println "IRD_API_KEY not set - skipping authenticated integration tests")))