agent/pkg/agentic/remote_status_test.go
Snider 537226bd4d feat: AX v0.8.0 upgrade — Core features + quality gates
AX Quality Gates (RFC-025):
- Eliminate os/exec from all test + production code (12+ files)
- Eliminate encoding/json from all test files (15 files, 66 occurrences)
- Eliminate os from all test files except TestMain (Go runtime contract)
- Eliminate path/filepath, net/url from all files
- String concat: 39 violations replaced with core.Concat()
- Test naming AX-7: 264 test functions renamed across all 6 packages
- Example test 1:1 coverage complete

Core Features Adopted:
- Task Composition: agent.completion pipeline (QA → PR → Verify → Ingest → Poke)
- PerformAsync: completion pipeline runs with WaitGroup + progress tracking
- Config: agents.yaml loaded once, feature flags (auto-qa/pr/merge/ingest)
- Named Locks: c.Lock("drain") for queue serialisation
- Registry: workspace state with cross-package QUERY access
- QUERY: c.QUERY(WorkspaceQuery{Status: "running"}) for cross-service queries
- Action descriptions: 25+ Actions self-documenting
- Data mounts: prompts/tasks/flows/personas/workspaces via c.Data()
- Content Actions: agentic.prompt/task/flow/persona callable via IPC
- Drive endpoints: forge + brain registered with tokens
- Drive REST helpers: DriveGet/DrivePost/DriveDo for Drive-aware HTTP
- HandleIPCEvents: auto-discovered by WithService (no manual wiring)
- Entitlement: frozen-queue gate on write Actions
- CLI dispatch: workspace dispatch wired to real dispatch method
- CLI: --quiet/-q and --debug/-d global flags
- CLI: banner, version, check (with service/action/command counts), env
- main.go: minimal — 5 services + c.Run(), no os import
- cmd tests: 84.2% coverage (was 0%)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-26 06:38:02 +00:00

133 lines
4 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
// Tests for remote_status.go — statusRemote.
package agentic
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
core "dappco.re/go/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// --- statusRemote ---
func TestRemotestatus_StatusRemote_Good(t *testing.T) {
callCount := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.Header().Set("Mcp-Session-Id", "s")
w.Header().Set("Content-Type", "text/event-stream")
switch callCount {
case 1:
core.Print(w, "data: {\"result\":{}}\n")
case 2:
w.WriteHeader(200)
case 3:
result := map[string]any{
"result": map[string]any{
"content": []map[string]any{
{"text": `{"total":5,"running":2,"completed":3,"failed":0}`},
},
},
}
core.Print(w, "data: %s\n", core.JSONMarshalString(result))
}
}))
t.Cleanup(srv.Close)
s := &PrepSubsystem{ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}), backoff: make(map[string]time.Time), failCount: make(map[string]int)}
_, out, err := s.statusRemote(context.Background(), nil, RemoteStatusInput{
Host: srv.Listener.Addr().String(),
})
require.NoError(t, err)
assert.True(t, out.Success)
assert.Equal(t, 5, out.Stats.Total)
assert.Equal(t, 2, out.Stats.Running)
}
func TestRemotestatus_StatusRemote_Bad(t *testing.T) {
s := &PrepSubsystem{ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}), backoff: make(map[string]time.Time), failCount: make(map[string]int)}
// Missing host
_, _, err := s.statusRemote(context.Background(), nil, RemoteStatusInput{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "host is required")
// Unreachable
_, out, err := s.statusRemote(context.Background(), nil, RemoteStatusInput{Host: "127.0.0.1:1"})
assert.NoError(t, err)
assert.Contains(t, out.Error, "unreachable")
// Call fails after init
callCount := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.Header().Set("Mcp-Session-Id", "s")
w.Header().Set("Content-Type", "text/event-stream")
switch callCount {
case 1:
core.Print(w, "data: {\"result\":{}}\n")
case 2:
w.WriteHeader(200)
case 3:
w.WriteHeader(500)
}
}))
t.Cleanup(srv.Close)
_, out2, _ := s.statusRemote(context.Background(), nil, RemoteStatusInput{Host: srv.Listener.Addr().String()})
assert.Contains(t, out2.Error, "call failed")
}
func TestRemotestatus_StatusRemote_Ugly(t *testing.T) {
callCount := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.Header().Set("Mcp-Session-Id", "s")
w.Header().Set("Content-Type", "text/event-stream")
switch callCount {
case 1:
core.Print(w, "data: {\"result\":{}}\n")
case 2:
w.WriteHeader(200)
case 3:
// JSON-RPC error
result := map[string]any{"error": map[string]any{"code": -32000, "message": "internal error"}}
core.Print(w, "data: %s\n", core.JSONMarshalString(result))
}
}))
t.Cleanup(srv.Close)
s := &PrepSubsystem{ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}), backoff: make(map[string]time.Time), failCount: make(map[string]int)}
_, out, _ := s.statusRemote(context.Background(), nil, RemoteStatusInput{Host: srv.Listener.Addr().String()})
assert.False(t, out.Success)
assert.Contains(t, out.Error, "internal error")
// Unparseable response
callCount2 := 0
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount2++
w.Header().Set("Mcp-Session-Id", "s")
w.Header().Set("Content-Type", "text/event-stream")
switch callCount2 {
case 1:
core.Print(w, "data: {\"result\":{}}\n")
case 2:
w.WriteHeader(200)
case 3:
core.Print(w, "data: not-json\n")
}
}))
t.Cleanup(srv2.Close)
_, out2, _ := s.statusRemote(context.Background(), nil, RemoteStatusInput{Host: srv2.Listener.Addr().String()})
assert.False(t, out2.Success)
assert.Contains(t, out2.Error, "failed to parse")
}