91 lines
3.0 KiB
Go
91 lines
3.0 KiB
Go
package claudehost
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseAutomationHookRequiresExactCallbackAndSession(t *testing.T) {
|
|
input := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[{"id":"once"},{"id":"repeat"}]}`)
|
|
if got, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", input, 123); !ok || len(got.IDs) != 2 || got.ObservedAtMS != 123 {
|
|
t.Fatalf("snapshot = %+v, ok=%v", got, ok)
|
|
}
|
|
for _, tc := range []struct{ callback, session string }{
|
|
{"other", "ses_1"}, {NativeAutomationCallbackID, "ses_2"},
|
|
} {
|
|
if _, ok := ParseAutomationHook(tc.callback, tc.session, input, 123); ok {
|
|
t.Fatalf("accepted wrong identity: %+v", tc)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseAutomationHookDistinguishesEmptyFromAbsentAndBounds(t *testing.T) {
|
|
empty := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[]}`)
|
|
got, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", empty, 123)
|
|
if !ok || got == nil || len(got.IDs) != 0 {
|
|
t.Fatalf("empty snapshot = %+v, ok=%v", got, ok)
|
|
}
|
|
absent := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop"}`)
|
|
if _, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", absent, 123); ok {
|
|
t.Fatal("accepted absent metadata")
|
|
}
|
|
bad := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[{"id":""}]}`)
|
|
if _, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", bad, 123); ok {
|
|
t.Fatal("accepted malformed metadata")
|
|
}
|
|
}
|
|
|
|
func TestAutomationObserverAckDoesNotKeepDetachedHostPending(t *testing.T) {
|
|
dir, err := os.MkdirTemp("/tmp", "phauto")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
paths := PathsFor(dir, "ses_1")
|
|
spool, err := os.OpenFile(paths.Spool, os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer spool.Close()
|
|
listener, err := net.Listen("unix", paths.Socket)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
stdout, stdin, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer stdin.Close()
|
|
defer stdout.Close()
|
|
go func() {
|
|
_, _ = io.Copy(io.Discard, stdout)
|
|
}()
|
|
h := &host{
|
|
dir: dir, id: "ses_1", paths: paths, idle: 50 * time.Millisecond,
|
|
spool: spool, listener: listener, stdin: stdin,
|
|
pending: map[string]int64{"hook-1": 0}, modelReqs: map[string]string{}, modeReqs: map[string]string{},
|
|
changed: make(chan struct{}), stopped: make(chan struct{}), claudeEnd: make(chan struct{}),
|
|
}
|
|
h.appendLine([]byte(`{"type":"control_request","request_id":"hook-1","request":{"subtype":"hook_callback","callback_id":"pipelines-native-session-automations-v1","input":{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[]}}}`))
|
|
h.mu.Lock()
|
|
if _, ok := h.pending["hook-1"]; ok {
|
|
h.mu.Unlock()
|
|
t.Fatal("observer callback remained pending after host ACK")
|
|
}
|
|
h.activity = time.Now().Add(-time.Second)
|
|
h.detached = time.Now().Add(-time.Second)
|
|
h.mu.Unlock()
|
|
|
|
go h.watchIdle()
|
|
select {
|
|
case <-h.stopped:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("detached host was not released after observer ACK")
|
|
}
|
|
}
|