1package extensions
2
3import (
4 "context"
5 "encoding/json"
6 "os"
7
8 "github.com/patriceckhart/zot/internal/extproto"
9 "path/filepath"
10 "runtime"
11 "sync"
12 "testing"
13 "time"
14)
15
16// stubHooks records every callback so the test can assert on them.
17type stubHooks struct {
18 mu sync.Mutex
19 notifies []string
20 displays []string
21}
22
23func (s *stubHooks) Notify(name, level, message string) {
24 s.mu.Lock()
25 defer s.mu.Unlock()
26 s.notifies = append(s.notifies, name+":"+level+":"+message)
27}
28func (s *stubHooks) Submit(string) {}
29func (s *stubHooks) Insert(string) {}
30func (s *stubHooks) Display(name, text string) {
31 s.mu.Lock()
32 defer s.mu.Unlock()
33 s.displays = append(s.displays, name+":"+text)
34}
35func (s *stubHooks) OpenPanel(string, extproto.PanelSpec) {}
36func (s *stubHooks) UpdatePanel(string, string, string, []string, string) {}
37func (s *stubHooks) ClosePanel(string, string) {}
38
39// writeMockExtension creates a minimal extension on disk that uses a
40// shell script (or batch file on windows) to drive the protocol. The
41// script reads commands from stdin and emits hard-coded responses,
42// exercising the manager's spawn/handshake/dispatch path without
43// needing the SDK.
44func writeMockExtension(t *testing.T, root string) {
45 t.Helper()
46 if runtime.GOOS == "windows" {
47 t.Skip("mock extension uses /bin/sh; skip on windows")
48 }
49
50 dir := filepath.Join(root, "mock")
51 if err := os.MkdirAll(dir, 0o755); err != nil {
52 t.Fatal(err)
53 }
54
55 // Shell script: emit hello, read frames, respond. Reads until
56 // stdin closes; tail's -F keeps the pipe alive long enough for
57 // the manager to send command_invoked.
58 script := `#!/bin/sh
59printf '%s\n' '{"type":"hello","name":"mock","version":"0.0.1","capabilities":["commands"]}'
60printf '%s\n' '{"type":"register_command","name":"ping","description":"ping/pong"}'
61while IFS= read -r line; do
62 case "$line" in
63 *'"type":"command_invoked"'*)
64 id=$(printf '%s' "$line" | sed -n 's/.*"id":"\([^"]*\)".*/\1/p')
65 printf '%s\n' "{\"type\":\"command_response\",\"id\":\"$id\",\"action\":\"display\",\"display\":\"pong\"}"
66 ;;
67 *'"type":"shutdown"'*)
68 printf '%s\n' '{"type":"shutdown_ack"}'
69 exit 0
70 ;;
71 esac
72done
73`
74 scriptPath := filepath.Join(dir, "run.sh")
75 if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil {
76 t.Fatal(err)
77 }
78
79 manifest := map[string]any{
80 "name": "mock",
81 "version": "0.0.1",
82 "exec": "./run.sh",
83 }
84 mfb, _ := json.Marshal(manifest)
85 if err := os.WriteFile(filepath.Join(dir, "extension.json"), mfb, 0o644); err != nil {
86 t.Fatal(err)
87 }
88}
89
90func TestManagerSpawnAndInvoke(t *testing.T) {
91 tmp := t.TempDir()
92 extRoot := filepath.Join(tmp, "extensions")
93 if err := os.MkdirAll(extRoot, 0o755); err != nil {
94 t.Fatal(err)
95 }
96 writeMockExtension(t, extRoot)
97
98 hooks := &stubHooks{}
99 mgr := New(tmp, "", "0.0.0-test", "anthropic", "claude-opus-4-7", hooks)
100
101 if errs := mgr.Discover(context.Background()); len(errs) > 0 {
102 t.Fatalf("discover errors: %v", errs)
103 }
104 defer mgr.Stop(2 * time.Second)
105
106 // Give the extension a beat to send register_command frames after
107 // the hello handshake.
108 time.Sleep(150 * time.Millisecond)
109
110 cmds := mgr.Commands()
111 if len(cmds) != 1 || cmds[0].Name != "ping" {
112 t.Fatalf("expected one command 'ping', got %#v", cmds)
113 }
114 if !mgr.HasCommand("ping") {
115 t.Fatal("HasCommand(\"ping\") = false")
116 }
117
118 resp, err := mgr.Invoke(context.Background(), "ping", "", 2*time.Second)
119 if err != nil {
120 t.Fatalf("invoke: %v", err)
121 }
122 if resp.Action != "display" {
123 t.Errorf("expected action=display, got %q", resp.Action)
124 }
125 if resp.Display != "pong" {
126 t.Errorf("expected display=pong, got %q", resp.Display)
127 }
128}