<commit_before>(ns puppetlabs.cthun.testutils.client
  (:require [clojure.test :refer :all]
            [clojure.core.async :as async :refer [timeout alts!! chan >!! <!!]]
            [http.async.client :as http]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.cthun.message :as message]
            [puppetlabs.ssl-utils.core :as ssl-utils]))

;; A simple websockets client with some assertions - for non-testing uses use pcp-client.

(defprotocol WsClient
  (close [this])
  (send! [this message])
  (recv! [this] [this timeout]
    "Returns nil on timeout, [code reason] on close, message/Message on message"))

(defrecord ChanClient [http-client ws-client message-channel]
  WsClient
  (close [this]
    (async/close! (:message-channel this))
    (.close (:ws-client this))
    (.close (:http-client this)))
  (send! [this message]
    (http/send (:ws-client this) :byte (message/encode message)))
  (recv! [this] (recv! this (* 10 1000)))
  (recv! [this timeout-ms]
    (let [[message channel] (alts!! [(:message-channel this) (timeout timeout-ms)])]
      message)))

(defn http-client-with-cert
  [certname]
  (let [cert        (format "./test-resources/ssl/certs/%s.pem" certname)
        private-key (format "./test-resources/ssl/private_keys/%s.pem" certname)
        ca-cert     "./test-resources/ssl/ca/ca_crt.pem"
        ssl-context (ssl-utils/pems->ssl-context cert private-key ca-cert)]
    (http/create-client :ssl-context ssl-context)))

(defn make-association-request
  [uri]
  (-> (message/make-message)
      (assoc :id (ks/uuid)
             :message_type "http://puppetlabs.com/associate_request"
             :targets ["cth:///server"]
             :sender uri)
      (message/set-expiry 3 :seconds)))

(defn connect
  "Makes a client for testing"
  [certname identity check-ok]
  (let [association-request (make-association-request identity)
        client              (http-client-with-cert certname)
        message-chan        (chan)
        ws                  (http/websocket client "wss://127.0.0.1:8081/cthun"<commit_msg>(maint) apply learning about defrecord<commit_after>(ns puppetlabs.cthun.testutils.client
  (:require [clojure.test :refer :all]
            [clojure.core.async :as async :refer [timeout alts!! chan >!! <!!]]
            [http.async.client :as http]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.cthun.message :as message]
            [puppetlabs.ssl-utils.core :as ssl-utils]))

;; A simple websockets client with some assertions - for non-testing uses use pcp-client.

(defprotocol WsClient
  (close [_])
  (send! [_ message])
  (recv! [_] [_ timeout]
    "Returns nil on timeout, [code reason] on close, message/Message on message"))

(defrecord ChanClient [http-client ws-client message-channel]
  WsClient
  (close [_]
    (async/close! message-channel)
    (.close ws-client)
    (.close http-client))
  (send! [_ message]
    (http/send ws-client :byte (message/encode message)))
  (recv! [this] (recv! this (* 10 1000)))
  (recv! [_ timeout-ms]
    (let [[message channel] (alts!! [message-channel (timeout timeout-ms)])]
      message)))

(defn http-client-with-cert
  [certname]
  (let [cert        (format "./test-resources/ssl/certs/%s.pem" certname)
        private-key (format "./test-resources/ssl/private_keys/%s.pem" certname)
        ca-cert     "./test-resources/ssl/ca/ca_crt.pem"
        ssl-context (ssl-utils/pems->ssl-context cert private-key ca-cert)]
    (http/create-client :ssl-context ssl-context)))

(defn make-association-request
  [uri]
  (-> (message/make-message)
      (assoc :id (ks/uuid)
             :message_type "http://puppetlabs.com/associate_request"
             :targets ["cth:///server"]
             :sender uri)
      (message/set-expiry 3 :seconds)))

(defn connect
  "Makes a client for testing"
  [certname identity check-ok]
  (let [association-request (make-association-request identity)
        client              (http-client-with-cert certname)
        message-chan        (chan)
        ws                  (http/websocket client "wss://127.0.0.1:8081/cthun"
{'fraction_non_alphanumeric': 0.14677341096555072, 'fraction_numerical': 0.008248423095584668, 'mean_word_length': 2.7413793103448274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6877303', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1361, 'n_words': 319}