agent/pkg/agentic/paths_test.go

313 lines
9.5 KiB
Go
Raw Permalink Normal View History

// SPDX-License-Identifier: EUPL-1.2
package agentic
import (
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
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core "dappco.re/go/core"
)
func TestPaths_CoreRoot_Good_EnvVar(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core")
assert.Equal(t, "/tmp/test-core", CoreRoot())
}
func TestPaths_CoreRoot_Good_Fallback(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "")
home := HomeDir()
assert.Equal(t, home+"/Code/.core", CoreRoot())
}
func TestPaths_CoreRoot_Good_CoreHome(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "")
t.Setenv("CORE_HOME", "/tmp/core-home")
assert.Equal(t, "/tmp/core-home/Code/.core", CoreRoot())
}
func TestPaths_HomeDir_Good_CoreHome(t *testing.T) {
t.Setenv("CORE_HOME", "/tmp/core-home")
t.Setenv("HOME", "/tmp/home")
t.Setenv("DIR_HOME", "/tmp/dir-home")
assert.Equal(t, "/tmp/core-home", HomeDir())
}
func TestPaths_HomeDir_Good_HomeFallback(t *testing.T) {
t.Setenv("CORE_HOME", "")
t.Setenv("HOME", "/tmp/home")
t.Setenv("DIR_HOME", "/tmp/dir-home")
assert.Equal(t, "/tmp/home", HomeDir())
}
func TestPaths_WorkspaceRoot_Good(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core")
assert.Equal(t, "/tmp/test-core/workspace", WorkspaceRoot())
}
func TestPaths_WorkspaceHelpers_Good(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core")
wsDir := core.JoinPath(WorkspaceRoot(), "core", "go-io", "task-5")
metaDir := WorkspaceMetaDir(wsDir)
assert.Equal(t, core.JoinPath(wsDir, "status.json"), WorkspaceStatusPath(wsDir))
assert.Equal(t, core.JoinPath(wsDir, "repo"), WorkspaceRepoDir(wsDir))
assert.Equal(t, core.JoinPath(wsDir, ".meta"), metaDir)
assert.Equal(t, core.JoinPath(wsDir, "repo", "BLOCKED.md"), WorkspaceBlockedPath(wsDir))
assert.Equal(t, core.JoinPath(wsDir, "repo", "ANSWER.md"), WorkspaceAnswerPath(wsDir))
assert.Equal(t, "core/go-io/task-5", WorkspaceName(wsDir))
assert.True(t, fs.EnsureDir(metaDir).OK)
assert.True(t, fs.Write(core.JoinPath(metaDir, "agent-codex.log"), "done").OK)
assert.Contains(t, WorkspaceLogFiles(wsDir), core.JoinPath(metaDir, "agent-codex.log"))
}
func TestPaths_WorkspaceHelpers_Good_BranchNameWithSlash(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core")
wsDir := core.JoinPath(WorkspaceRoot(), "core", "go-io", "feature", "new-ui")
require.True(t, fs.EnsureDir(WorkspaceRepoDir(wsDir)).OK)
require.True(t, fs.EnsureDir(WorkspaceMetaDir(wsDir)).OK)
require.True(t, fs.Write(WorkspaceStatusPath(wsDir), "{}").OK)
assert.Equal(t, "core/go-io/feature/new-ui", WorkspaceName(wsDir))
assert.Contains(t, WorkspaceStatusPaths(), WorkspaceStatusPath(wsDir))
}
func TestPaths_PlansRoot_Good(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core")
assert.Equal(t, "/tmp/test-core/plans", PlansRoot())
}
func TestPaths_AgentName_Good_EnvVar(t *testing.T) {
t.Setenv("AGENT_NAME", "clotho")
assert.Equal(t, "clotho", AgentName())
}
func TestPaths_AgentName_Good_Fallback(t *testing.T) {
t.Setenv("AGENT_NAME", "")
name := AgentName()
assert.True(t, name == "cladius" || name == "charon", "expected cladius or charon, got %s", name)
}
func TestPaths_GitHubOrg_Good_EnvVar(t *testing.T) {
t.Setenv("GITHUB_ORG", "myorg")
assert.Equal(t, "myorg", GitHubOrg())
}
func TestPaths_GitHubOrg_Good_Fallback(t *testing.T) {
t.Setenv("GITHUB_ORG", "")
assert.Equal(t, "dAppCore", GitHubOrg())
}
// --- DefaultBranch ---
func TestPaths_DefaultBranch_Good(t *testing.T) {
dir := t.TempDir()
// Init git repo with "main" branch
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
testCore.Process().Run(context.Background(), "git", "init", "-b", "main", dir)
testCore.Process().RunIn(context.Background(), dir, "git", "config", "user.name", "Test")
testCore.Process().RunIn(context.Background(), dir, "git", "config", "user.email", "test@test.com")
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
fs.Write(dir+"/README.md", "# Test")
testCore.Process().RunIn(context.Background(), dir, "git", "add", ".")
testCore.Process().RunIn(context.Background(), dir, "git", "commit", "-m", "init")
branch := testPrep.DefaultBranch(dir)
assert.Equal(t, "main", branch)
}
func TestPaths_DefaultBranch_Bad(t *testing.T) {
// Non-git directory — should return "main" (default)
dir := t.TempDir()
branch := testPrep.DefaultBranch(dir)
assert.Equal(t, "main", branch)
}
func TestPaths_DefaultBranch_Ugly(t *testing.T) {
dir := t.TempDir()
// Init git repo with "master" branch
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
testCore.Process().Run(context.Background(), "git", "init", "-b", "master", dir)
testCore.Process().RunIn(context.Background(), dir, "git", "config", "user.name", "Test")
testCore.Process().RunIn(context.Background(), dir, "git", "config", "user.email", "test@test.com")
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
fs.Write(dir+"/README.md", "# Test")
testCore.Process().RunIn(context.Background(), dir, "git", "add", ".")
testCore.Process().RunIn(context.Background(), dir, "git", "commit", "-m", "init")
branch := testPrep.DefaultBranch(dir)
assert.Equal(t, "master", branch)
}
// --- LocalFs Bad/Ugly ---
func TestPaths_LocalFs_Bad_ReadNonExistent(t *testing.T) {
f := LocalFs()
r := f.Read("/tmp/nonexistent-path-" + strings.Repeat("x", 20) + "/file.txt")
assert.False(t, r.OK, "reading a non-existent file should fail")
}
func TestPaths_LocalFs_Ugly_EmptyPath(t *testing.T) {
f := LocalFs()
assert.NotPanics(t, func() {
f.Read("")
})
}
// --- WorkspaceRoot Bad/Ugly ---
func TestPaths_WorkspaceRoot_Bad_EmptyEnv(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "")
home := HomeDir()
// Should fall back to ~/Code/.core/workspace
assert.Equal(t, home+"/Code/.core/workspace", WorkspaceRoot())
}
func TestPaths_WorkspaceHelpers_Bad(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core")
assert.Equal(t, "/status.json", WorkspaceStatusPath(""))
assert.Equal(t, "/repo", WorkspaceRepoDir(""))
assert.Equal(t, "/.meta", WorkspaceMetaDir(""))
assert.Equal(t, "workspace", WorkspaceName(WorkspaceRoot()))
assert.Empty(t, WorkspaceLogFiles("/tmp/missing-workspace"))
}
func TestPaths_WorkspaceRoot_Ugly_TrailingSlash(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/test-core/")
// Verify it still constructs a valid path (JoinPath handles trailing slash)
ws := WorkspaceRoot()
assert.NotEmpty(t, ws)
assert.Contains(t, ws, "workspace")
}
func TestPaths_WorkspaceHelpers_Ugly(t *testing.T) {
root := t.TempDir()
t.Setenv("CORE_WORKSPACE", root)
wsRoot := WorkspaceRoot()
shallow := core.JoinPath(wsRoot, "ws-flat")
deep := core.JoinPath(wsRoot, "core", "go-io", "task-12")
ignored := core.JoinPath(wsRoot, "core", "go-io", "task-12", "extra")
assert.True(t, fs.EnsureDir(shallow).OK)
assert.True(t, fs.EnsureDir(deep).OK)
assert.True(t, fs.EnsureDir(ignored).OK)
assert.True(t, fs.Write(core.JoinPath(shallow, "status.json"), "{}").OK)
assert.True(t, fs.Write(core.JoinPath(deep, "status.json"), "{}").OK)
assert.True(t, fs.Write(core.JoinPath(ignored, "status.json"), "{}").OK)
paths := WorkspaceStatusPaths()
assert.Contains(t, paths, core.JoinPath(shallow, "status.json"))
assert.Contains(t, paths, core.JoinPath(deep, "status.json"))
assert.NotContains(t, paths, core.JoinPath(ignored, "status.json"))
}
// --- CoreRoot Bad/Ugly ---
func TestPaths_CoreRoot_Bad_WhitespaceEnv(t *testing.T) {
t.Setenv("CORE_WORKSPACE", " ")
// Non-empty string (whitespace) will be used as-is
root := CoreRoot()
assert.Equal(t, " ", root)
}
func TestPaths_CoreRoot_Ugly_UnicodeEnv(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/tmp/\u00e9\u00e0\u00fc")
assert.NotPanics(t, func() {
root := CoreRoot()
assert.Equal(t, "/tmp/\u00e9\u00e0\u00fc", root)
})
}
// --- PlansRoot Bad/Ugly ---
func TestPaths_PlansRoot_Bad_EmptyEnv(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "")
home := HomeDir()
assert.Equal(t, home+"/Code/.core/plans", PlansRoot())
}
func TestPaths_PlansRoot_Ugly_NestedPath(t *testing.T) {
t.Setenv("CORE_WORKSPACE", "/a/b/c/d/e/f")
assert.Equal(t, "/a/b/c/d/e/f/plans", PlansRoot())
}
// --- AgentName Bad/Ugly ---
func TestPaths_AgentName_Bad_WhitespaceEnv(t *testing.T) {
t.Setenv("AGENT_NAME", " ")
// Whitespace is non-empty, so returned as-is
assert.Equal(t, " ", AgentName())
}
func TestPaths_AgentName_Ugly_UnicodeEnv(t *testing.T) {
t.Setenv("AGENT_NAME", "\u00e9nchantr\u00efx")
assert.NotPanics(t, func() {
name := AgentName()
assert.Equal(t, "\u00e9nchantr\u00efx", name)
})
}
// --- GitHubOrg Bad/Ugly ---
func TestPaths_GitHubOrg_Bad_WhitespaceEnv(t *testing.T) {
t.Setenv("GITHUB_ORG", " ")
assert.Equal(t, " ", GitHubOrg())
}
func TestPaths_GitHubOrg_Ugly_SpecialChars(t *testing.T) {
t.Setenv("GITHUB_ORG", "org/with/slashes")
assert.NotPanics(t, func() {
org := GitHubOrg()
assert.Equal(t, "org/with/slashes", org)
})
}
// --- parseInt Bad/Ugly ---
func TestPaths_ParseInt_Bad_EmptyString(t *testing.T) {
assert.Equal(t, 0, parseInt(""))
}
func TestPaths_ParseInt_Bad_NonNumeric(t *testing.T) {
assert.Equal(t, 0, parseInt("abc"))
assert.Equal(t, 0, parseInt("12.5"))
assert.Equal(t, 0, parseInt("0xff"))
}
func TestPaths_ParseInt_Bad_WhitespaceOnly(t *testing.T) {
assert.Equal(t, 0, parseInt(" "))
}
func TestPaths_ParseInt_Ugly_NegativeNumber(t *testing.T) {
assert.Equal(t, -42, parseInt("-42"))
}
func TestPaths_ParseInt_Ugly_VeryLargeNumber(t *testing.T) {
assert.Equal(t, 0, parseInt("99999999999999999999999"))
}
func TestPaths_ParseInt_Ugly_LeadingTrailingWhitespace(t *testing.T) {
assert.Equal(t, 42, parseInt(" 42 "))
}
// --- fs (NewUnrestricted) Good ---
func TestPaths_Fs_Good_Unrestricted(t *testing.T) {
assert.NotNil(t, fs, "package-level fs should be non-nil")
assert.IsType(t, &core.Fs{}, fs)
}
// --- parseInt Good ---
func TestPaths_ParseInt_Good(t *testing.T) {
assert.Equal(t, 42, parseInt("42"))
assert.Equal(t, 0, parseInt("0"))
}