-;; Copyright (c) 2026 Jakub Czajka <jakub@ekhem.eu.org>
-;; License: GPL-3.0 or later.
-;;
-;; Run the Guix Home activate script and then the home tests.
-
-(define-module (tests vps-home)
- #:use-module (gnu packages admin)
- #:use-module (gnu packages base)
- #:use-module (guix gexp)
- #:use-module (guix packages)
- #:use-module (conf home pi)
- #:use-module (tests vps-system)
- #:use-module (vps-home)
- #:export (vps-home-test-cases))
-
-(define %home-proxy-port
- 16890)
-
-(define %activate
- (file-append vps-home-environment "/activate"))
-(define %herd
- (file-append vps-home-environment "/profile/bin/herd"))
-(define %claude
- (file-append vps-home-environment "/profile/bin/claude"))
-(define %pi
- (file-append vps-home-environment "/profile/bin/pi"))
-(define %timeout
- (file-append coreutils "/bin/timeout"))
-
-(define %home-prelude
- `(begin
- (use-modules (ice-9 popen)
- (ice-9 rdelim)
- (ice-9 rw))
-
- (define (run-command-output command)
- (let* ((port (open-input-pipe command))
- (output (read-string port)))
- (list (eqv? 0
- (status:exit-val (close-pipe port))) output)))
-
- (define one-second
- internal-time-units-per-second)
- (define (read-timed socket start)
- (let loop
- ((chunks '())
- (first-tick #f))
- (let* ((buffer (make-string 8192))
- (n (or (false-if-exception (read-string!/partial buffer socket))
- 0)))
- (cond
- ((positive? n)
- (loop (cons (substring buffer 0 n) chunks)
- (or first-tick
- (- (get-internal-real-time) start))))
- ((pair? (car (select (list socket)
- '()
- '() 0)))
- (list first-tick
- (- (get-internal-real-time) start)
- (string-concatenate-reverse chunks)))
- (else #f)))))
-
- (define (proxy-request request)
- (false-if-exception (let ((socket (socket PF_INET SOCK_STREAM 0))
- (address (make-socket-address AF_INET
- INADDR_LOOPBACK
- ,%home-proxy-port))
- (start (get-internal-real-time)))
- (connect socket address)
- (setsockopt socket SOL_SOCKET SO_RCVTIMEO
- (cons 4 0))
- (display request socket)
- (force-output socket)
- (let ((response (read-timed socket start)))
- (close-port socket) response))))
-
- (define (fetch path body)
- (let ((head (string-append "POST " path " HTTP/1.1\r\n"
- "Host: 127.0.0.1\r\n"))
- (tail (string-append "Content-Length: "
- (number->string (string-length body))
- "\r\n\r\n" body)))
- (proxy-request (string-append head
- "Content-Type: application/json\r\n"
- "x-api-key: sk-test\r\n" tail))))
-
- (define (wait-for-port port)
- (let ((address (make-socket-address AF_INET INADDR_LOOPBACK port)))
- (let loop
- ((remaining 20))
- (let ((socket (socket PF_INET SOCK_STREAM 0)))
- (let ((accepting? (false-if-exception (connect socket address))))
- (close-port socket)
- (if accepting? #t
- (and (positive? remaining)
- (sleep 1)
- (loop (- remaining 1)))))))))
-
- (define-syntax ok?
- (syntax-rules ()
- ((_ result body check ...)
- (let ((response result))
- (and response
- (let ((body (caddr response)))
- (and check ...)))))))
-
- (define runtime-dir
- (format #f "/run/user/~a"
- (passwd:uid (getpwnam ,%test-user))))
- (system* ,(file-append coreutils "/bin/install") "--directory"
- "--mode=700"
- ,(string-append "--owner=" %test-user) runtime-dir)
- (define (run command)
- (let ((prefix (format #f
- "~a 90 ~a -n -u ~a -- ~a XDG_RUNTIME_DIR="
- ,%timeout
- ,(file-append sudo "/bin/sudo")
- ,%test-user
- ,(file-append coreutils "/bin/env"))))
- (run-command-output (string-append prefix runtime-dir " " command
- " 2>&1"))))
-
- (define (run-contains command needle)
- (let ((result (run command)))
- (and (car result)
- (string-contains (cadr result) needle) #t)))))
-
-(define (req-with-options stream fields)
- (string-append "{\"stream\":" stream
- ",\"model\":\"deepseek-chat\",\"max_tokens\":16," fields
- "\"messages\":[{\"role\":\"user\",\"content\":\"hello\"}]}"))
-
-(define %req-classifier-fields
- "\"reasoning_effort\":\"high\",\"output_config\":{\"budget\":\"auto\"},")
-
-(define %stream-request
- (req-with-options "true" ""))
-
-(define %req-thinking-fields
- "\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":1000},")
-
-(define %count-tokens-request
- (string-append "{\"model\":\"deepseek-chat\"," %req-classifier-fields
- "\"messages\":[{\"role\":\"user\",\"content\":\"hello\"}]}"))
-
-(define %home-activation-body
- `(let* ((env (string-append "CLAUDE_PROXY_UPSTREAM=https://127.0.0.1:"
- (number->string ,fake-upstream-port)
- " GUILE_TLS_CERTIFICATE_DIRECTORY="
- ,claude-proxy-test-ca-dir " "))
- (result (run (string-append env
- ,%activate " > /tmp/home-activate.log"))))
- (unless (car result)
- (format (current-error-port) "~a"
- (call-with-input-file "/tmp/home-activate.log"
- read-string)))
- (car result)))
-
-(define (home-herd-body service)
- `(run-contains (string-append ,%herd " status "
- ,service) "It is running"))
-
-(define %health-stream-body
- `(let ((stream (fetch "/v1/messages"
- ,%stream-request)))
- (and (ok? (proxy-request "GET /health HTTP/1.1\nHost: 127.0.0.1\n\n")
- body
- (string-contains body "200")
- (string-contains body "\"status\":\"ok\""))
- (ok? stream body
- (string-contains body "data: [DONE]")
- (string-contains body "message_start"))
- (< (car stream)
- (* 5/2 one-second))
- (>= (cadr stream)
- (* 5/2 one-second)))))
-
-(define %classifier-rewrite-body
- `(let* ((plain (fetch "/v1/messages"
- ,(req-with-options "false" %req-classifier-fields)))
- (explicit (fetch "/v1/messages"
- ,(req-with-options "false" %req-thinking-fields)))
- (tokens (fetch "/v1/messages/count_tokens"
- ,%count-tokens-request)))
- (and (ok? plain
- body
- (string-contains body "200")
- (string-contains body "\"type\":\"disabled\"")
- (not (string-contains body "reasoning_effort"))
- (not (string-contains body "output_config")))
- (ok? explicit body
- (string-contains body "\"type\":\"enabled\"")
- (string-contains body "budget_tokens"))
- (ok? tokens
- body
- (string-contains body "200")
- (string-contains body "reasoning_effort")
- (string-contains body "output_config")
- (not (string-contains body "thinking"))))))
-
-(define %relay-and-fail-fast-body
- `(and (ok? (fetch "/other"
- "{\"stream\":false,\"model\":\"x\",\"max_tokens\":1}")
- body
- (string-contains body "401")
- (string-contains body "authentication_error"))
- (stop-service 'fake-upstream)
- (let ((down (fetch "/v1/messages"
- ,%stream-request)))
- (and (ok? down body
- (string-contains body "502")
- (string-contains body "upstream unavailable"))
- (< (cadr down)
- (* 2 one-second))
- (start-service 'fake-upstream)
- (wait-for-port ,fake-upstream-port)))))
-
-(define %claude-e2e-body
- `(let* ((env (string-append
- "HOME=/tmp/claude-e2e ANTHROPIC_API_KEY=test-key "
- "ANTHROPIC_BASE_URL=http://127.0.0.1:"
- (number->string ,%home-proxy-port) " DISABLE_TELEMETRY=1 "
- "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 "))
- (claude ,%claude)
- (args " --bare --model claude-sonnet-4-6 -p hello 2>&1 < /dev/null")
- (command (string-append env
- ,%timeout " 90 " claude args))
- (result (run-command-output command)))
- (and (car result)
- (string-contains (cadr result) "Hello from fake") #t)))
-
-(define %pi-version-body
- `(run-contains (string-append "HOME=/tmp/pi-e2e "
- ,%pi " --version")
- ,(package-version pi)))
-
-(define (assert-home marionette label body)
- #~(test-assert #$label
- (let ((result (marionette-eval '#$body
- #$marionette)))
- (or (eq? result #t)
- (begin
- (format (current-error-port) "~a: ~s~%"
- #$label result) #f)))))
-
-(define (vps-home-test-cases marionette)
- #~(begin
- (marionette-eval '#$%home-prelude
- #$marionette)
- #$(assert-home marionette "home: vps-home activates"
- %home-activation-body)
- #$(assert-home marionette "home: claude-proxy is listening"
- `(wait-for-port ,%home-proxy-port))
- #$(assert-home marionette "home: claude-proxy is running"
- (home-herd-body "claude-proxy"))
- #$(assert-home marionette "home: paseo-daemon is running"
- (home-herd-body "paseo-daemon"))
- #$(assert-home marionette "home: pi runs" %pi-version-body)
- #$(assert-home marionette
- "home: claude-proxy: health and incremental SSE"
- %health-stream-body)
- #$(assert-home marionette
- "home: claude-proxy: thinking rewrite only for classifier calls"
- %classifier-rewrite-body)
- #$(assert-home marionette
- "home: claude-proxy: Claude Code -p gets a reply"
- %claude-e2e-body)
- #$(assert-home marionette
- "home: claude-proxy: upstream errors relay and fail fast"
- %relay-and-fail-fast-body)))