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

240 lines
6.6 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package agentic
import (
"context"
"os"
"strconv"
"testing"
"time"
core "dappco.re/go/core"
"dappco.re/go/core/process"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testPrep is the package-level PrepSubsystem for tests that need process execution.
var testPrep *PrepSubsystem
// testCore is the package-level Core with go-process registered.
var testCore *core.Core
// TestMain sets up a PrepSubsystem with go-process registered for all tests in the package.
func TestMain(m *testing.M) {
testCore = core.New(
core.WithService(process.Register),
)
testCore.ServiceStartup(context.Background(), nil)
// Enable pipeline feature flags (matches Register defaults)
testCore.Config().Enable("auto-qa")
testCore.Config().Enable("auto-pr")
testCore.Config().Enable("auto-merge")
testCore.Config().Enable("auto-ingest")
testPrep = &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}),
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
}
os.Exit(m.Run())
}
// newPrepWithProcess creates a PrepSubsystem wired to testCore for tests that
// need process execution via s.Core().Process().
func newPrepWithProcess() *PrepSubsystem {
return &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}),
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
}
}
// --- runCmd ---
func TestProc_RunCmd_Good(t *testing.T) {
dir := t.TempDir()
r := testPrep.runCmd(context.Background(), dir, "echo", "hello")
assert.True(t, r.OK)
assert.Contains(t, core.Trim(r.Value.(string)), "hello")
}
func TestProc_RunCmd_Bad(t *testing.T) {
dir := t.TempDir()
r := testPrep.runCmd(context.Background(), dir, "nonexistent-command-xyz")
assert.False(t, r.OK)
}
func TestProc_RunCmd_Ugly(t *testing.T) {
dir := t.TempDir()
// Empty command string — should error
r := testPrep.runCmd(context.Background(), dir, "")
assert.False(t, r.OK)
}
// --- runCmdEnv ---
func TestProc_RunCmdEnv_Good(t *testing.T) {
dir := t.TempDir()
r := testPrep.runCmdEnv(context.Background(), dir, []string{"MY_CUSTOM_VAR=hello_test"}, "env")
assert.True(t, r.OK)
assert.Contains(t, r.Value.(string), "MY_CUSTOM_VAR=hello_test")
}
func TestProc_RunCmdEnv_Bad(t *testing.T) {
dir := t.TempDir()
r := testPrep.runCmdEnv(context.Background(), dir, []string{"FOO=bar"}, "nonexistent-command-xyz")
assert.False(t, r.OK)
}
func TestProc_RunCmdEnv_Ugly(t *testing.T) {
dir := t.TempDir()
// Empty env slice — should work fine, just no extra vars
r := testPrep.runCmdEnv(context.Background(), dir, []string{}, "echo", "works")
assert.True(t, r.OK)
assert.Contains(t, core.Trim(r.Value.(string)), "works")
}
// --- runCmdOK ---
func TestProc_RunCmdOK_Good(t *testing.T) {
dir := t.TempDir()
assert.True(t, testPrep.runCmdOK(context.Background(), dir, "echo", "ok"))
}
func TestProc_RunCmdOK_Bad(t *testing.T) {
dir := t.TempDir()
assert.False(t, testPrep.runCmdOK(context.Background(), dir, "nonexistent-command-xyz"))
}
func TestProc_RunCmdOK_Ugly(t *testing.T) {
dir := t.TempDir()
// "false" command returns exit 1
assert.False(t, testPrep.runCmdOK(context.Background(), dir, "false"))
}
// --- gitCmd ---
func TestProc_GitCmd_Good(t *testing.T) {
dir := t.TempDir()
r := testPrep.gitCmd(context.Background(), dir, "--version")
assert.True(t, r.OK)
}
func TestProc_GitCmd_Bad(t *testing.T) {
// git log in a non-git dir should fail
dir := t.TempDir()
r := testPrep.gitCmd(context.Background(), dir, "log")
assert.False(t, r.OK)
}
func TestProc_GitCmd_Ugly(t *testing.T) {
dir := t.TempDir()
// Empty args — git with no arguments exits 1
r := testPrep.gitCmd(context.Background(), dir)
assert.False(t, r.OK)
}
// --- gitCmdOK ---
func TestProc_GitCmdOK_Good(t *testing.T) {
dir := t.TempDir()
assert.True(t, testPrep.gitCmdOK(context.Background(), dir, "--version"))
}
func TestProc_GitCmdOK_Bad(t *testing.T) {
// git log in non-git dir returns false
dir := t.TempDir()
assert.False(t, testPrep.gitCmdOK(context.Background(), dir, "log"))
}
func TestProc_GitCmdOK_Ugly(t *testing.T) {
// Empty dir string — git may use cwd, which may or may not be a repo
// Just ensure no panic
assert.NotPanics(t, func() {
testPrep.gitCmdOK(context.Background(), "", "--version")
})
}
// --- gitOutput ---
func TestProc_GitOutput_Good(t *testing.T) {
dir := initTestRepo(t)
branch := testPrep.gitOutput(context.Background(), dir, "rev-parse", "--abbrev-ref", "HEAD")
assert.Equal(t, "main", branch)
}
func TestProc_GitOutput_Bad(t *testing.T) {
// Non-git dir returns empty string
dir := t.TempDir()
out := testPrep.gitOutput(context.Background(), dir, "rev-parse", "--abbrev-ref", "HEAD")
assert.Equal(t, "", out)
}
func TestProc_GitOutput_Ugly(t *testing.T) {
// Failed command returns empty string
dir := t.TempDir()
out := testPrep.gitOutput(context.Background(), dir, "log", "--oneline", "-5")
assert.Equal(t, "", out)
}
// --- processIsRunning ---
func TestProc_ProcessIsRunning_Good(t *testing.T) {
// Own PID should be running
pid, _ := strconv.Atoi(core.Env("PID"))
assert.True(t, processIsRunning("", pid))
}
func TestProc_ProcessIsRunning_Bad(t *testing.T) {
// PID 999999 should not be running (extremely unlikely to exist)
assert.False(t, processIsRunning("", 999999))
}
func TestProc_ProcessIsRunning_Ugly(t *testing.T) {
// PID 0 — should return false (invalid PID guard: pid > 0 is false for 0)
assert.False(t, processIsRunning("", 0))
}
// --- processKill ---
func TestProc_ProcessKill_Good(t *testing.T) {
t.Skip("would need real process to kill")
}
func TestProc_ProcessKill_Bad(t *testing.T) {
// PID 999999 should fail to kill
assert.False(t, processKill("", 999999))
}
func TestProc_ProcessKill_Ugly(t *testing.T) {
// PID 0 — pid > 0 guard returns false
assert.False(t, processKill("", 0))
}
// --- initTestRepo creates a git repo with commits for proc tests ---
func initTestRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
gitEnv := []string{
"GIT_AUTHOR_NAME=Test",
"GIT_AUTHOR_EMAIL=test@test.com",
"GIT_COMMITTER_NAME=Test",
"GIT_COMMITTER_EMAIL=test@test.com",
}
run := func(args ...string) {
t.Helper()
r := testCore.Process().RunWithEnv(context.Background(), dir, gitEnv, args[0], args[1:]...)
require.True(t, r.OK, "cmd %v failed: %s", args, r.Value)
}
run("git", "init", "-b", "main")
run("git", "config", "user.name", "Test")
run("git", "config", "user.email", "test@test.com")
require.True(t, fs.Write(core.JoinPath(dir, "README.md"), "# Test").OK)
run("git", "add", "README.md")
run("git", "commit", "-m", "initial commit")
return dir
}