1package extensions
2
3import (
4 "context"
5 "encoding/json"
6 "os"
7
8 "github.com/patriceckhart/zot/packages/agent/extproto"
9 "path/filepath"
10 "runtime"
11 "strings"
12 "sync"
13 "testing"
14 "time"
15)
16
17// stubHooks records every callback so the test can assert on them.
18type stubHooks struct {
19 mu sync.Mutex
20 notifies []string
21 displays []string
22 clearNotes []string
23 panels []extproto.PanelSpec
24 panelExts []string
25}
26
27func (s *stubHooks) Notify(name, level, message string) {
28 s.mu.Lock()
29 defer s.mu.Unlock()
30 s.notifies = append(s.notifies, name+":"+level+":"+message)
31}
32func (s *stubHooks) Submit(string) {}
33func (s *stubHooks) SubmitSlash(string) {}
34func (s *stubHooks) Insert(string) {}
35func (s *stubHooks) Display(name, text string) {
36 s.mu.Lock()
37 defer s.mu.Unlock()
38 s.displays = append(s.displays, name+":"+text)
39}
40func (s *stubHooks) ClearNotes(name string) {
41 s.mu.Lock()
42 defer s.mu.Unlock()
43 s.clearNotes = append(s.clearNotes, name)
44}
45func (s *stubHooks) OpenPanel(extName string, spec extproto.PanelSpec) {
46 s.mu.Lock()
47 defer s.mu.Unlock()
48 s.panelExts = append(s.panelExts, extName)
49 s.panels = append(s.panels, spec)
50}
51func (s *stubHooks) UpdatePanel(string, string, string, []string, string) {}
52func (s *stubHooks) ClosePanel(string, string) {}
53
54// writeMockExtension creates a minimal extension on disk that uses a
55// shell script (or batch file on windows) to drive the protocol. The
56// script reads commands from stdin and emits hard-coded responses,
57// exercising the manager's spawn/handshake/dispatch path without
58// needing the SDK.
59func writeMockExtension(t *testing.T, root string) {
60 t.Helper()
61 if runtime.GOOS == "windows" {
62 t.Skip("mock extension uses /bin/sh; skip on windows")
63 }
64
65 dir := filepath.Join(root, "mock")
66 if err := os.MkdirAll(dir, 0o755); err != nil {
67 t.Fatal(err)
68 }
69
70 // Shell script: emit hello, read frames, respond. Reads until
71 // stdin closes; tail's -F keeps the pipe alive long enough for
72 // the manager to send command_invoked.
73 script := `#!/bin/sh
74printf '%s\n' '{"type":"hello","name":"mock","version":"0.0.1","capabilities":["commands"]}'
75printf '%s\n' '{"type":"register_command","name":"ping","description":"ping/pong"}'
76while IFS= read -r line; do
77 case "$line" in
78 *'"type":"command_invoked"'*)
79 id=$(printf '%s' "$line" | sed -n 's/.*"id":"\([^"]*\)".*/\1/p')
80 printf '%s\n' "{\"type\":\"command_response\",\"id\":\"$id\",\"action\":\"display\",\"display\":\"pong\"}"
81 ;;
82 *'"type":"shutdown"'*)
83 printf '%s\n' '{"type":"shutdown_ack"}'
84 exit 0
85 ;;
86 esac
87done
88`
89 scriptPath := filepath.Join(dir, "run.sh")
90 if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil {
91 t.Fatal(err)
92 }
93
94 manifest := map[string]any{
95 "name": "mock",
96 "version": "0.0.1",
97 "exec": "./run.sh",
98 }
99 mfb, _ := json.Marshal(manifest)
100 if err := os.WriteFile(filepath.Join(dir, "extension.json"), mfb, 0o644); err != nil {
101 t.Fatal(err)
102 }
103}
104
105func TestDiscoverLoadsThemeOnlyExtension(t *testing.T) {
106 tmp := t.TempDir()
107 extDir := filepath.Join(tmp, "extensions", "theme-only")
108 if err := os.MkdirAll(extDir, 0o755); err != nil {
109 t.Fatal(err)
110 }
111 manifest := `{"name":"theme-only","version":"1.0.0","description":"theme only"}`
112 if err := os.WriteFile(filepath.Join(extDir, "extension.json"), []byte(manifest), 0o644); err != nil {
113 t.Fatal(err)
114 }
115 theme := `{"name":"Theme Only","description":"theme from extension","colors":{"dark":{"accent":204}}}`
116 if err := os.WriteFile(filepath.Join(extDir, "theme.json"), []byte(theme), 0o644); err != nil {
117 t.Fatal(err)
118 }
119
120 mgr := New(tmp, "", "0.0.0-test", "anthropic", "claude-opus-4-7", nil)
121 if errs := mgr.Discover(context.Background()); len(errs) > 0 {
122 t.Fatalf("discover errors: %v", errs)
123 }
124 defer mgr.Stop(10 * time.Millisecond)
125
126 opts := mgr.ThemeOptions()
127 if len(opts) != 1 {
128 t.Fatalf("theme options = %d, want 1", len(opts))
129 }
130 if opts[0].Label != "Theme Only" || opts[0].Path != filepath.Join(extDir, "theme.json") {
131 t.Fatalf("unexpected theme option: %#v", opts[0])
132 }
133 if !strings.Contains(opts[0].Description, "from extension theme-only") {
134 t.Fatalf("description missing extension source: %q", opts[0].Description)
135 }
136}
137
138func TestManagerSpawnAndInvoke(t *testing.T) {
139 tmp := t.TempDir()
140 extRoot := filepath.Join(tmp, "extensions")
141 if err := os.MkdirAll(extRoot, 0o755); err != nil {
142 t.Fatal(err)
143 }
144 writeMockExtension(t, extRoot)
145
146 hooks := &stubHooks{}
147 mgr := New(tmp, "", "0.0.0-test", "anthropic", "claude-opus-4-7", hooks)
148
149 if errs := mgr.Discover(context.Background()); len(errs) > 0 {
150 t.Fatalf("discover errors: %v", errs)
151 }
152 defer mgr.Stop(2 * time.Second)
153
154 // Give the extension a beat to send register_command frames after
155 // the hello handshake.
156 time.Sleep(150 * time.Millisecond)
157
158 cmds := mgr.Commands()
159 found := false
160 for _, c := range cmds {
161 if c.Name == "ping" {
162 found = true
163 break
164 }
165 }
166 if !found {
167 t.Fatalf("expected command 'ping', got %#v", cmds)
168 }
169 if !mgr.HasCommand("ping") {
170 t.Fatal("HasCommand(\"ping\") = false")
171 }
172
173 resp, err := mgr.Invoke(context.Background(), "ping", "", 2*time.Second)
174 if err != nil {
175 t.Fatalf("invoke: %v", err)
176 }
177 if resp.Action != "display" {
178 t.Errorf("expected action=display, got %q", resp.Action)
179 }
180 if resp.Display != "pong" {
181 t.Errorf("expected display=pong, got %q", resp.Display)
182 }
183}
184
185// TestSpontaneousOpenPanel verifies that an extension sending an
186// open_panel frame outside of any command response causes the manager
187// to call hooks.OpenPanel with the correct PanelSpec fields.
188func TestSpontaneousOpenPanel(t *testing.T) {
189 if runtime.GOOS == "windows" {
190 t.Skip("mock extension uses /bin/sh; skip on windows")
191 }
192
193 tmp := t.TempDir()
194 extDir := filepath.Join(tmp, "extensions", "panel-mock")
195 if err := os.MkdirAll(extDir, 0o755); err != nil {
196 t.Fatal(err)
197 }
198
199 // Extension emits hello + ready, then immediately fires a
200 // spontaneous open_panel, then waits for shutdown.
201 script := `#!/bin/sh
202printf '%s\n' '{"type":"hello","name":"panel-mock","version":"0.1","capabilities":["panels"]}'
203printf '%s\n' '{"type":"ready"}'
204printf '%s\n' '{"type":"open_panel","panel":{"id":"test-panel","title":"Hello Panel","lines":["line one","line two"],"footer":"esc close"}}'
205while IFS= read -r line; do
206 case "$line" in
207 *'"type":"shutdown"'*)
208 printf '%s\n' '{"type":"shutdown_ack"}'
209 exit 0
210 ;;
211 esac
212done
213`
214 if err := os.WriteFile(filepath.Join(extDir, "run.sh"), []byte(script), 0o755); err != nil {
215 t.Fatal(err)
216 }
217 mfb, _ := json.Marshal(map[string]any{"name": "panel-mock", "exec": "./run.sh"})
218 if err := os.WriteFile(filepath.Join(extDir, "extension.json"), mfb, 0o644); err != nil {
219 t.Fatal(err)
220 }
221
222 hooks := &stubHooks{}
223 mgr := New(tmp, "", "0.0.0-test", "anthropic", "claude-opus-4-7", hooks)
224 if errs := mgr.Discover(context.Background()); len(errs) > 0 {
225 t.Fatalf("discover errors: %v", errs)
226 }
227 defer mgr.Stop(2 * time.Second)
228
229 // Give the extension time to flush its open_panel frame.
230 deadline := time.Now().Add(2 * time.Second)
231 for time.Now().Before(deadline) {
232 hooks.mu.Lock()
233 n := len(hooks.panels)
234 hooks.mu.Unlock()
235 if n > 0 {
236 break
237 }
238 time.Sleep(20 * time.Millisecond)
239 }
240
241 hooks.mu.Lock()
242 defer hooks.mu.Unlock()
243
244 if len(hooks.panels) == 0 {
245 t.Fatal("hooks.OpenPanel was never called")
246 }
247 spec := hooks.panels[0]
248 if spec.ID != "test-panel" {
249 t.Errorf("panel id: want %q, got %q", "test-panel", spec.ID)
250 }
251 if spec.Title != "Hello Panel" {
252 t.Errorf("panel title: want %q, got %q", "Hello Panel", spec.Title)
253 }
254 if len(spec.Lines) != 2 || spec.Lines[0] != "line one" || spec.Lines[1] != "line two" {
255 t.Errorf("panel lines: want [line one line two], got %v", spec.Lines)
256 }
257 if spec.Footer != "esc close" {
258 t.Errorf("panel footer: want %q, got %q", "esc close", spec.Footer)
259 }
260 if hooks.panelExts[0] != "panel-mock" {
261 t.Errorf("ext name: want %q, got %q", "panel-mock", hooks.panelExts[0])
262 }
263}