agent/pkg/agentic/handlers_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

214 lines
5.7 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package agentic
import (
"testing"
"time"
"dappco.re/go/agent/pkg/messages"
core "dappco.re/go/core"
"github.com/stretchr/testify/assert"
)
// newCoreForHandlerTests creates a Core with PrepSubsystem registered via
// RegisterService — HandleIPCEvents is auto-discovered.
func newCoreForHandlerTests(t *testing.T) (*core.Core, *PrepSubsystem) {
t.Helper()
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
s := &PrepSubsystem{
codePath: t.TempDir(),
pokeCh: make(chan struct{}, 1),
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
workspaces: core.NewRegistry[*WorkspaceStatus](),
}
c := core.New()
c.Config().Enable("auto-ingest")
s.ServiceRuntime = core.NewServiceRuntime(c, AgentOptions{})
// RegisterService auto-discovers HandleIPCEvents on PrepSubsystem
c.RegisterService("agentic", s)
return c, s
}
// --- HandleIPCEvents ---
func TestHandlers_HandleIPCEvents_Good(t *testing.T) {
c, _ := newCoreForHandlerTests(t)
// HandleIPCEvents was auto-registered — Core should not panic on ACTION
assert.NotPanics(t, func() {
c.ACTION(messages.AgentCompleted{Workspace: "nonexistent", Repo: "test", Status: "completed"})
})
}
func TestHandlers_PokeOnCompletion_Good(t *testing.T) {
_, s := newCoreForHandlerTests(t)
// Drain any existing poke
select {
case <-s.pokeCh:
default:
}
// HandleIPCEvents receives AgentCompleted → calls Poke
s.HandleIPCEvents(s.Core(), messages.AgentCompleted{
Workspace: "ws-test", Repo: "go-io", Status: "completed",
})
select {
case <-s.pokeCh:
// poke received
default:
t.Log("poke signal may not have been received synchronously")
}
}
func TestHandlers_IngestOnCompletion_Good(t *testing.T) {
c, _ := newCoreForHandlerTests(t)
root := WorkspaceRoot()
wsName := "core/test/task-2"
wsDir := core.JoinPath(root, wsName)
repoDir := core.JoinPath(wsDir, "repo")
fs.EnsureDir(repoDir)
st := &WorkspaceStatus{
Status: "completed",
Repo: "test",
Agent: "codex",
Task: "Review code",
}
writeStatus(wsDir, st)
// Should not panic — ingest handler runs but no findings file
c.ACTION(messages.AgentCompleted{
Workspace: wsName,
Repo: "test",
Status: "completed",
})
}
func TestHandlers_IgnoresNonCompleted_Good(t *testing.T) {
c, _ := newCoreForHandlerTests(t)
// Non-completed status — ingest still runs (it handles all completions)
assert.NotPanics(t, func() {
c.ACTION(messages.AgentCompleted{
Workspace: "nonexistent",
Repo: "test",
Status: "failed",
})
})
}
func TestHandlers_PokeQueue_Good(t *testing.T) {
c, s := newCoreForHandlerTests(t)
s.frozen = true // frozen so drainQueue is a no-op
// PokeQueue message → drainQueue called
c.ACTION(messages.PokeQueue{})
// Should call drainQueue without panic
}
func TestHandlers_IngestDisabled_Bad(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
s := &PrepSubsystem{
pokeCh: make(chan struct{}, 1),
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
workspaces: core.NewRegistry[*WorkspaceStatus](),
}
c := core.New()
c.Config().Disable("auto-ingest") // disabled
s.ServiceRuntime = core.NewServiceRuntime(c, AgentOptions{})
c.RegisterService("agentic", s)
wsDir := core.JoinPath(WorkspaceRoot(), "ws-test")
fs.EnsureDir(core.JoinPath(wsDir, "repo"))
writeStatus(wsDir, &WorkspaceStatus{Status: "completed", Repo: "test", Agent: "codex"})
// With auto-ingest disabled, should still not panic
c.ACTION(messages.AgentCompleted{Workspace: "ws-test", Repo: "test", Status: "completed"})
}
func TestHandlers_ResolveWorkspace_Good(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
wsRoot := core.JoinPath(root, "workspace")
ws := core.JoinPath(wsRoot, "core", "go-io", "task-15")
fs.EnsureDir(ws)
result := resolveWorkspace("core/go-io/task-15")
assert.Equal(t, ws, result)
}
func TestHandlers_ResolveWorkspace_Bad(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
result := resolveWorkspace("nonexistent")
assert.Empty(t, result)
}
func TestHandlers_FindWorkspaceByPR_Good(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
wsRoot := core.JoinPath(root, "workspace")
ws := core.JoinPath(wsRoot, "ws-test")
fs.EnsureDir(ws)
st := &WorkspaceStatus{Repo: "go-io", Branch: "agent/fix", Status: "completed"}
fs.Write(core.JoinPath(ws, "status.json"), core.JSONMarshalString(st))
result := findWorkspaceByPR("go-io", "agent/fix")
assert.Equal(t, ws, result)
}
func TestHandlers_FindWorkspaceByPR_Ugly(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
wsRoot := core.JoinPath(root, "workspace")
// Deep layout: org/repo/task
ws := core.JoinPath(wsRoot, "core", "agent", "task-5")
fs.EnsureDir(ws)
st := &WorkspaceStatus{Repo: "agent", Branch: "agent/tests", Status: "completed"}
fs.Write(core.JoinPath(ws, "status.json"), core.JSONMarshalString(st))
result := findWorkspaceByPR("agent", "agent/tests")
assert.Equal(t, ws, result)
}
// --- command registration ---
func TestHandlers_Commandsforge_Good(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
s := &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(core.New(), AgentOptions{}),
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
}
assert.NotPanics(t, func() { s.registerForgeCommands() })
}
func TestHandlers_Commandsworkspace_Good(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
s := &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(core.New(), AgentOptions{}),
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
}
assert.NotPanics(t, func() { s.registerWorkspaceCommands() })
}