|
| 1 | +;;; test_ai-code-open-interpreter-cli.el --- Tests for Open Interpreter CLI -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Author: Kang Tu <tninja@gmail.com> |
| 4 | +;; SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +;;; Commentary: |
| 7 | +;; Tests for the ai-code-open-interpreter-cli module. |
| 8 | + |
| 9 | +;;; Code: |
| 10 | + |
| 11 | +(require 'ert) |
| 12 | +(require 'cl-lib) |
| 13 | +(require 'subr-x) |
| 14 | +(unless (featurep 'magit) |
| 15 | + (defun magit-toplevel (&optional _dir) nil) |
| 16 | + (defun magit-get-current-branch () nil) |
| 17 | + (defun magit-git-lines (&rest _args) nil) |
| 18 | + (provide 'magit)) |
| 19 | +(require 'ai-code-open-interpreter-cli) |
| 20 | +(require 'ai-code-mcp-agent nil t) |
| 21 | + |
| 22 | +(ert-deftest ai-code-test-open-interpreter-cli-backend-contract () |
| 23 | + "Open Interpreter should expose the expected backend and MCP defaults." |
| 24 | + (should (equal ai-code-open-interpreter-cli-program "interpreter")) |
| 25 | + (let ((spec (cdr (ai-code--backend-spec 'open-interpreter)))) |
| 26 | + (should spec) |
| 27 | + (should (eq (plist-get spec :require) 'ai-code-open-interpreter-cli)) |
| 28 | + (should (eq (plist-get spec :start) 'ai-code-open-interpreter-cli)) |
| 29 | + (should (eq (plist-get spec :switch) |
| 30 | + 'ai-code-open-interpreter-cli-switch-to-buffer)) |
| 31 | + (should (eq (plist-get spec :send) |
| 32 | + 'ai-code-open-interpreter-cli-send-command)) |
| 33 | + (should (eq (plist-get spec :resume) |
| 34 | + 'ai-code-open-interpreter-cli-resume)) |
| 35 | + (should (equal (plist-get spec :config) |
| 36 | + "~/.openinterpreter/config.toml")) |
| 37 | + (should (equal (plist-get spec :cli) "interpreter"))) |
| 38 | + (should (memq 'open-interpreter ai-code-mcp-agent-enabled-backends))) |
| 39 | + |
| 40 | +(ert-deftest ai-code-test-open-interpreter-cli-resume-appends-last () |
| 41 | + "Resuming Open Interpreter should request the most recent session." |
| 42 | + (let ((captured-arg nil) |
| 43 | + (captured-switches nil) |
| 44 | + (ai-code-open-interpreter-cli-program-switches '("--model" "test"))) |
| 45 | + (cl-letf (((symbol-function 'ai-code-open-interpreter-cli) |
| 46 | + (lambda (&optional arg) |
| 47 | + (setq captured-arg arg |
| 48 | + captured-switches |
| 49 | + ai-code-open-interpreter-cli-program-switches)))) |
| 50 | + (ai-code-open-interpreter-cli-resume 'prefix-arg)) |
| 51 | + (should (eq captured-arg 'prefix-arg)) |
| 52 | + (should (equal captured-switches |
| 53 | + '("--model" "test" "resume" "--last"))))) |
| 54 | + |
| 55 | +(ert-deftest ai-code-test-open-interpreter-cli-start-injects-session-mcp-config () |
| 56 | + "Starting Open Interpreter should inject MCP config and lifecycle hooks." |
| 57 | + (should (fboundp 'ai-code-open-interpreter-cli)) |
| 58 | + (let ((captured-command nil) |
| 59 | + (captured-cleanup-fn nil) |
| 60 | + (captured-post-start-fn nil) |
| 61 | + (registered nil) |
| 62 | + (unregistered nil) |
| 63 | + (builtins-called nil) |
| 64 | + (ensure-called nil) |
| 65 | + (ai-code-mcp-agent-enabled-backends '(open-interpreter)) |
| 66 | + (session-buffer |
| 67 | + (generate-new-buffer " *ai-code-open-interpreter-mcp*"))) |
| 68 | + (unwind-protect |
| 69 | + (cl-letf (((symbol-function |
| 70 | + 'ai-code-backends-infra--session-working-directory) |
| 71 | + (lambda () "/tmp/test-open-interpreter")) |
| 72 | + ((symbol-function |
| 73 | + 'ai-code-backends-infra--resolve-start-command) |
| 74 | + (lambda (&rest _args) |
| 75 | + (list :command "interpreter --model test"))) |
| 76 | + ((symbol-function 'ai-code-mcp-builtins-setup) |
| 77 | + (lambda () (setq builtins-called t))) |
| 78 | + ((symbol-function 'ai-code-mcp-http-server-ensure) |
| 79 | + (lambda () |
| 80 | + (setq ensure-called t) |
| 81 | + 8765)) |
| 82 | + ((symbol-function 'ai-code-mcp-register-session) |
| 83 | + (lambda (session-id project-dir buffer) |
| 84 | + (setq registered (list session-id project-dir buffer)))) |
| 85 | + ((symbol-function 'ai-code-mcp-unregister-session) |
| 86 | + (lambda (session-id) |
| 87 | + (setq unregistered session-id))) |
| 88 | + ((symbol-function |
| 89 | + 'ai-code-backends-infra--toggle-or-create-session) |
| 90 | + (lambda (&rest args) |
| 91 | + (cl-destructuring-bind |
| 92 | + (_working-dir _buffer-name _process-table command |
| 93 | + &optional _escape-fn cleanup-fn |
| 94 | + _instance-name _prefix _force-prompt |
| 95 | + _env-vars _multiline-input-sequence |
| 96 | + post-start-fn) |
| 97 | + args |
| 98 | + (setq captured-command command |
| 99 | + captured-cleanup-fn cleanup-fn |
| 100 | + captured-post-start-fn post-start-fn)) |
| 101 | + nil))) |
| 102 | + (ai-code-open-interpreter-cli) |
| 103 | + (should builtins-called) |
| 104 | + (should ensure-called) |
| 105 | + (should (string-match-p |
| 106 | + "\\`interpreter --model test " captured-command)) |
| 107 | + (should (string-match-p |
| 108 | + "mcp_servers\\.emacs_tools" captured-command)) |
| 109 | + (should (string-match-p |
| 110 | + "mcp/open-interpreter-" captured-command)) |
| 111 | + (should (functionp captured-cleanup-fn)) |
| 112 | + (should (functionp captured-post-start-fn)) |
| 113 | + (funcall captured-post-start-fn session-buffer nil "default") |
| 114 | + (should (string-prefix-p "open-interpreter-" (car registered))) |
| 115 | + (should (equal "/tmp/test-open-interpreter" (nth 1 registered))) |
| 116 | + (should (eq session-buffer (nth 2 registered))) |
| 117 | + (with-current-buffer session-buffer |
| 118 | + (let ((status (ai-code-mcp-agent-buffer-status))) |
| 119 | + (should (eq 'open-interpreter (plist-get status :backend))) |
| 120 | + (should (string-match-p |
| 121 | + "^http://127\\.0\\.0\\.1:8765/mcp/open-interpreter-" |
| 122 | + (plist-get status :server-url))))) |
| 123 | + (funcall captured-cleanup-fn) |
| 124 | + (should (equal (car registered) unregistered))) |
| 125 | + (when (buffer-live-p session-buffer) |
| 126 | + (kill-buffer session-buffer))))) |
| 127 | + |
| 128 | +(provide 'test_ai-code-open-interpreter-cli) |
| 129 | + |
| 130 | +;;; test_ai-code-open-interpreter-cli.el ends here |
0 commit comments