aboutsummaryrefslogtreecommitdiff
path: root/tests/testutil-request.el
diff options
context:
space:
mode:
Diffstat (limited to 'tests/testutil-request.el')
-rw-r--r--tests/testutil-request.el49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/testutil-request.el b/tests/testutil-request.el
new file mode 100644
index 0000000..a517bb6
--- /dev/null
+++ b/tests/testutil-request.el
@@ -0,0 +1,49 @@
+;;; testutil-request.el --- request mocking helpers for pearl tests -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2026 Craig Jennings
+
+;;; Commentary:
+
+;; Shared helpers for stubbing the `request' library at the HTTP boundary.
+;; A stub invokes the package's own :success or :error callback synchronously
+;; with canned, json-read-shaped data, so the response-parsing and callback
+;; logic runs for real without any network. Not a test file itself (no
+;; test- prefix), so the suite runner ignores it; test files `require' it.
+
+;;; Code:
+
+(require 'cl-lib)
+
+(defun testutil-linear-request-success (data)
+ "Return a `request' replacement that invokes its :success callback with DATA."
+ (lambda (_url &rest args)
+ (let ((cb (plist-get args :success)))
+ (when cb (funcall cb :data data)))))
+
+(defun testutil-linear-request-error (msg)
+ "Return a `request' replacement that invokes its :error callback with MSG.
+The :response carries a real `request-response' struct, matching what the
+live library passes, so the package's status-code logging doesn't choke."
+ (lambda (_url &rest args)
+ (let ((cb (plist-get args :error)))
+ (when cb
+ (funcall cb :error-thrown msg
+ :response (make-request-response :status-code 500)
+ :data nil)))))
+
+(defmacro testutil-linear-with-response (data &rest body)
+ "Run BODY with `request' stubbed to succeed with DATA and an API key set."
+ (declare (indent 1))
+ `(let ((pearl-api-key "test-key"))
+ (cl-letf (((symbol-function 'request) (testutil-linear-request-success ,data)))
+ ,@body)))
+
+(defmacro testutil-linear-with-error (msg &rest body)
+ "Run BODY with `request' stubbed to fail with MSG and an API key set."
+ (declare (indent 1))
+ `(let ((pearl-api-key "test-key"))
+ (cl-letf (((symbol-function 'request) (testutil-linear-request-error ,msg)))
+ ,@body)))
+
+(provide 'testutil-request)
+;;; testutil-request.el ends here