agent/pkg/agentic/runtime_state_test.go
Snider 9c6f10902e fix(agent): mcp.Register startup panic + test isolation + CLI test standard
- Replace broken registerMCPService with mcp.Register (fixes nil ServiceRuntime panic)
- Remove dead mcp_service.go, update tests to use mcp.Register directly
- Add setTestWorkspace() helper to clear workspaceRootOverride between tests
- Fix 40+ test files with workspace state poisoning from loadAgentConfig
- Fix forge.lthn.ai → dappco.re in findConsumersList test
- Fix BranchWorkspaceCount test to use isolated temp dir
- Add CLI test standard: 32 tests across 19 subsystems (tests/cli/)
- All 9 packages pass, 0 failures

Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-08 16:15:14 +01:00

73 lines
1.7 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package agentic
import (
"testing"
"time"
core "dappco.re/go/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRuntimeState_PersistLoad_Good_RoundTrip(t *testing.T) {
root := t.TempDir()
setTestWorkspace(t, root)
expectedBackoff := time.Date(2026, 4, 1, 12, 0, 0, 0, time.UTC)
subsystem := &PrepSubsystem{
backoff: map[string]time.Time{
"codex": expectedBackoff,
},
failCount: map[string]int{
"codex": 2,
},
}
subsystem.persistRuntimeState()
loaded := &PrepSubsystem{
backoff: make(map[string]time.Time),
failCount: make(map[string]int),
}
loaded.loadRuntimeState()
require.Len(t, loaded.backoff, 1)
assert.True(t, loaded.backoff["codex"].Equal(expectedBackoff))
assert.Equal(t, 2, loaded.failCount["codex"])
}
func TestRuntimeState_Read_Bad_InvalidJSON(t *testing.T) {
root := t.TempDir()
setTestWorkspace(t, root)
require.True(t, fs.EnsureDir(runtimeStateDir()).OK)
require.True(t, fs.WriteAtomic(runtimeStatePath(), "{not-json").OK)
result := readRuntimeState()
assert.False(t, result.OK)
}
func TestRuntimeState_Persist_Ugly_EmptyStateDeletesFile(t *testing.T) {
root := t.TempDir()
setTestWorkspace(t, root)
require.True(t, fs.EnsureDir(runtimeStateDir()).OK)
require.True(t, fs.WriteAtomic(runtimeStatePath(), core.JSONMarshalString(runtimeState{
Backoff: map[string]time.Time{
"codex": time.Date(2026, 4, 1, 12, 0, 0, 0, time.UTC),
},
FailCount: map[string]int{
"codex": 1,
},
})).OK)
subsystem := &PrepSubsystem{
backoff: map[string]time.Time{},
failCount: map[string]int{},
}
subsystem.persistRuntimeState()
assert.False(t, fs.Read(runtimeStatePath()).OK)
}