Files
passepartout/tests/system-integration-tests.lisp
Amr Gharbeia c227877302 v0.8.3: TUI stabilization — box calls, package fixes, sandbox, configure
Bug fixes:
- Fix box() calls: set color-pair before box, pass ACS default chtype integers
- Fix markdown functions: move to passepartout.channel-tui package where
  Croatoan is imported; use add-attributes/remove-attributes instead of
  :bold/:underline kwargs to add-string; call theme-color in gate-trace-lines
  to convert theme keys to Croatoan colors
- Fix sandbox: remove dex:get/dex:post from restricted symbols
  (blocked neuro-provider from loading)
- Export *log-lock* from passepartout (was unbound in jailed skill packages)
- Fix configure: always deploy to XDG, skip cp when source==dest
- Fix bash crash handler format string (~~ escaping)
- Revert test reorder in 28 files (caused package leakage in skill loader)

Design cleanup:
- Extract tui-run-screen from tui-main for clean separation
- Remove inject-stimulus alias
- Merge *backend-registry* into *probabilistic-backends*
- Fix read-framed-message whitespace DoS (4096-iteration max)
- Add *read-eval* nil to dispatcher-approvals-process read-from-string
2026-05-13 09:17:48 -04:00

242 lines
9.5 KiB
Common Lisp

(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :fiveam :silent t)
(ql:quickload :usocket :silent t))
(defpackage :passepartout-integration-tests
(:use :cl :passepartout)
(:export #:integration-suite))
(in-package :passepartout-integration-tests)
(fiveam:def-suite integration-suite :description "Integration tests across process boundaries")
(fiveam:in-suite integration-suite)
(defvar *daemon-port* nil)
(defun find-free-port ()
(let ((socket (usocket:socket-listen "127.0.0.1" 0 :reuse-address t)))
(unwind-protect (usocket:get-local-port socket)
(usocket:socket-close socket))))
(defmacro with-daemon (() &body body)
`(let ((*daemon-port* (find-free-port)))
(unwind-protect
(progn
(passepartout:actuator-initialize)
(passepartout:skill-initialize-all)
(passepartout:start-daemon :port *daemon-port*)
(sleep 2)
,@body)
(values)))
(defun daemon-connect ()
(let* ((sock (usocket:socket-connect "127.0.0.1" *daemon-port*))
(stream (usocket:socket-stream sock)))
(read-framed-message stream) ;; discard handshake
(values stream sock)))
(defun daemon-send (stream msg)
(write-string (frame-message msg) stream)
(finish-output stream))
(defun daemon-recv (stream &key (timeout 5))
(let ((deadline (+ (get-universal-time) timeout)))
(loop
(when (listen stream)
(return (read-framed-message stream)))
(when (> (get-universal-time) deadline) (return nil))
(sleep 0.1))))
(fiveam:test test-daemon-starts
"Contract 1: daemon binds port and sends valid handshake."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(is (open-stream-p stream))
(usocket:socket-close sock))))
(fiveam:test test-pipeline-user-input
"Contract 2: :user-input traverses pipeline and produces a response."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(unwind-protect
(progn
(daemon-send stream
'(:TYPE :EVENT :PAYLOAD (:SENSOR :user-input :TEXT "test")))
(let ((resp (daemon-recv stream :timeout 10)))
(is (not (null resp)) "Expected a response")))
(usocket:socket-close sock)))))
(fiveam:test test-pipeline-heartbeat
"Contract 2: heartbeat signals do not crash the daemon."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(unwind-protect
(daemon-send stream
'(:TYPE :EVENT :PAYLOAD (:SENSOR :heartbeat)))
(usocket:socket-close sock))
(pass))))
(fiveam:test test-tcp-round-trip
"Contract 3: framed health-check survives TCP round-trip."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(unwind-protect
(progn
(daemon-send stream '(:TYPE :health-check))
(let ((resp (daemon-recv stream :timeout 5)))
(is (not (null resp)))
(is (member (getf resp :type) '(:HEALTH-RESPONSE)))))
(usocket:socket-close sock)))))
(fiveam:test test-daemon-survives-junk
"Contract 3: daemon does not crash on junk input."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(write-string "ZZZZZZ" stream)
(finish-output stream)
(sleep 1)
(usocket:socket-close sock))
;; Connect again to verify daemon is still alive
(multiple-value-bind (stream2 sock2) (daemon-connect)
(is (open-stream-p stream2))
(usocket:socket-close sock2))))
(fiveam:test test-skill-registry-populated
"Contract 4: *skill-registry* is populated after daemon start."
(with-daemon ()
(is (hash-table-p passepartout::*skill-registry*))
(is (>= (hash-table-count passepartout::*skill-registry*) 1)
"Expected at least 1 skill in registry, got ~a"
(hash-table-count passepartout::*skill-registry*))))
(fiveam:test test-shell-safe-echo
"Contract 5: safe shell command does not crash the daemon."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(unwind-protect
(daemon-send stream
'(:TYPE :REQUEST :TARGET :shell
:PAYLOAD (:ACTION :execute :CMD "echo hello")))
(usocket:socket-close sock))
(pass))))
(fiveam:test test-shell-dangerous-blocked
"Contract 5: rm -rf / is blocked by the security dispatcher."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(unwind-protect
(daemon-send stream
'(:TYPE :REQUEST :TARGET :shell
:PAYLOAD (:ACTION :execute :CMD "rm -rf /")))
(usocket:socket-close sock))
(pass))))
(fiveam:test test-cli-gateway-input
"Contract 6: text via TCP produces a response."
(with-daemon ()
(multiple-value-bind (stream sock) (daemon-connect)
(unwind-protect
(daemon-send stream
'(:TYPE :EVENT :META (:SOURCE :CLI)
:PAYLOAD (:SENSOR :user-input :TEXT "hello from CLI")))
(usocket:socket-close sock))
(pass))))
(fiveam:test test-gateway-registry
"Contract 7: gateway-registry-initialize is available."
(with-daemon ()
(is (fboundp 'gateway-registry-initialize))
(gateway-registry-initialize)
(pass)))
(defun has-api-key (env-var)
"Returns T if env-var is set and non-empty."
(let ((val (uiop:getenv env-var)))
(and val (> (length val) 0))))
(defmacro skip-unless (env-var &body body)
"Execute body if env-var is set, otherwise skip the test."
`(if (has-api-key ,env-var)
(progn ,@body)
(progn
(format t " [SKIP] ~a not set~%" ,env-var)
(skip "~a not set" ,env-var))))
(fiveam:test test-provider-openai-request
"Contract Phase2: provider-openai-request returns :success with valid API key."
(skip-unless "OPENROUTER_API_KEY"
(let ((result (provider-openai-request "Say hello" "Be brief."
:provider :openrouter
:model "openrouter/auto")))
(is (or (eq (getf result :status) :success)
(eq (getf result :status) :error))
"Expected :success or :error, got: ~a" result))))
(fiveam:test test-backend-cascade-real
"Contract Phase2: backend-cascade-call returns string content with real provider."
(skip-unless "OPENROUTER_API_KEY"
(let ((passepartout::*provider-cascade* '(:openrouter)))
(let ((result (backend-cascade-call "Say hello" :system-prompt "Be brief.")))
(is (stringp result) "Expected string response, got: ~a" result)))))
(fiveam:test test-provider-cascade-parsing
"Contract Phase2: PROVIDER_CASCADE env var parses to clean keywords matching backends."
(provider-cascade-initialize)
(let ((cascade passepartout::*provider-cascade*))
(is (listp cascade) "Cascade must be a list")
(is (>= (length cascade) 1) "Cascade must have at least one entry")
(dolist (entry cascade)
(is (keywordp entry) "Entry ~s must be a keyword" entry)
(let ((name (symbol-name entry)))
(is (not (find #\" name)) "Entry ~s must not contain double-quote" entry)
(is (not (find #\' name)) "Entry ~s must not contain single-quote" entry)))
(is (some (lambda (e) (gethash e passepartout::*probabilistic-backends*)) cascade)
"At least one cascade entry must match a registered backend")))
(fiveam:test test-messaging-link-unlink
"Contract Phase2: messaging-link stores token, configured-p returns T, unlink removes it."
(with-daemon ()
(messaging-link :test-platform :token "fake-token-123")
(is (gateway-configured-p :test-platform)
"Expected test-platform to be configured after linking")
(messaging-unlink :test-platform)
(is (not (gateway-configured-p :test-platform))
"Expected test-platform to be unconfigured after unlinking")))
(fiveam:test test-gateway-configured-p-false
"Contract Phase2: gateway-configured-p returns nil for unknown platform."
(with-daemon ()
(is (not (gateway-configured-p :nonexistent-platform-xyz)))))
(fiveam:test test-gateway-start-messaging
"Contract Phase2: gateway registry initializes with expected platforms."
(with-daemon ()
(gateway-registry-initialize)
(is (hash-table-p passepartout::*gateway-registry*))
(is (>= (hash-table-count passepartout::*gateway-registry*) 1))))
(fiveam:test test-flight-plan-message-format
"Contract Phase3: dispatcher-flight-plan-create returns valid message."
(with-daemon ()
(load (merge-pathnames ".local/share/passepartout/lisp/security-dispatcher.lisp"
(user-homedir-pathname)))
(let ((plan (dispatcher-flight-plan-create
'(:TYPE :REQUEST :TARGET :shell :PAYLOAD (:CMD "sudo restart")))))
(is (eq :REQUEST (getf plan :type)))
(is (eq :emacs (getf plan :target)))
(is (eq :insert-node (getf (getf plan :payload) :action)))
(let ((attrs (getf (getf plan :payload) :attributes)))
(is (string= "Flight Plan: High-Risk Action" (getf attrs :TITLE)))
(is (string= "PLAN" (getf attrs :TODO)))
(is (member "FLIGHT_PLAN" (getf attrs :TAGS) :test #'string-equal))))))
(fiveam:test test-emacs-daemon-connect
"Contract Phase3: Emacs daemon is reachable via emacsclient."
(handler-case
(let ((result (uiop:run-program '("emacsclient" "--eval" "(+ 1 2)")
:output :string
:ignore-error-status t)))
(is (search "3" result) "Expected '3' from emacsclient, got: ~a" result))
(error (c)
(skip "Emacs daemon not available: ~a" c)))))