test(agentic): cover message and dispatch sync contracts
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
2daabf27f7
commit
43568cae01
4 changed files with 257 additions and 3 deletions
|
|
@ -40,7 +40,12 @@ func (s *PrepSubsystem) DispatchSync(ctx context.Context, input DispatchSyncInpu
|
|||
prepContext, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
_, prepOut, err := s.prepWorkspace(prepContext, nil, prepInput)
|
||||
prepWorkspace := s.prepWorkspace
|
||||
if s.dispatchSyncPrep != nil {
|
||||
prepWorkspace = s.dispatchSyncPrep
|
||||
}
|
||||
|
||||
_, prepOut, err := prepWorkspace(prepContext, nil, prepInput)
|
||||
if err != nil {
|
||||
return DispatchSyncResult{Error: core.E("agentic.DispatchSync", "prep workspace failed", err)}
|
||||
}
|
||||
|
|
@ -54,7 +59,12 @@ func (s *PrepSubsystem) DispatchSync(ctx context.Context, input DispatchSyncInpu
|
|||
core.Print(nil, " workspace: %s", workspaceDir)
|
||||
core.Print(nil, " branch: %s", prepOut.Branch)
|
||||
|
||||
pid, processID, _, err := s.spawnAgent(input.Agent, prompt, workspaceDir)
|
||||
spawnAgent := s.spawnAgent
|
||||
if s.dispatchSyncSpawn != nil {
|
||||
spawnAgent = s.dispatchSyncSpawn
|
||||
}
|
||||
|
||||
pid, processID, _, err := spawnAgent(input.Agent, prompt, workspaceDir)
|
||||
if err != nil {
|
||||
return DispatchSyncResult{Error: core.E("agentic.DispatchSync", "spawn agent failed", err)}
|
||||
}
|
||||
|
|
@ -67,7 +77,12 @@ func (s *PrepSubsystem) DispatchSync(ctx context.Context, input DispatchSyncInpu
|
|||
runtime = s.Core()
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(3 * time.Second)
|
||||
tick := 3 * time.Second
|
||||
if s.dispatchSyncTick > 0 {
|
||||
tick = s.dispatchSyncTick
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(tick)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
|
|
|
|||
|
|
@ -3,9 +3,14 @@
|
|||
package agentic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDispatchsync_ContainerCommand_Good(t *testing.T) {
|
||||
|
|
@ -28,3 +33,123 @@ func TestDispatchsync_ContainerCommand_Ugly_EmptyArgs(t *testing.T) {
|
|||
containerCommand("codex", nil, "", "")
|
||||
})
|
||||
}
|
||||
|
||||
func TestDispatchsync_HandleDispatchSync_Good_Completed(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
||||
workspaceDir := core.JoinPath(WorkspaceRoot(), "core", "go-io", "task-7")
|
||||
s := &PrepSubsystem{dispatchSyncTick: 10 * time.Millisecond}
|
||||
|
||||
s.dispatchSyncPrep = func(ctx context.Context, _ *mcp.CallToolRequest, input PrepInput) (*mcp.CallToolResult, PrepOutput, error) {
|
||||
require.Equal(t, "core", input.Org)
|
||||
require.Equal(t, "go-io", input.Repo)
|
||||
require.Equal(t, "codex", input.Agent)
|
||||
require.Equal(t, "Fix tests", input.Task)
|
||||
require.Equal(t, 7, input.Issue)
|
||||
|
||||
require.True(t, fs.EnsureDir(workspaceDir).OK)
|
||||
require.True(t, fs.Write(core.JoinPath(workspaceDir, "status.json"), core.JSONMarshalString(&WorkspaceStatus{
|
||||
Status: "completed",
|
||||
PRURL: "https://forge.test/core/go-io/pulls/7",
|
||||
})).OK)
|
||||
|
||||
return nil, PrepOutput{
|
||||
Success: true,
|
||||
WorkspaceDir: workspaceDir,
|
||||
Branch: "agent/fix-tests",
|
||||
Prompt: "prompt",
|
||||
}, nil
|
||||
}
|
||||
s.dispatchSyncSpawn = func(agent, prompt, dir string) (int, string, string, error) {
|
||||
require.Equal(t, "codex", agent)
|
||||
require.Equal(t, "prompt", prompt)
|
||||
require.Equal(t, workspaceDir, dir)
|
||||
return 321, "process-321", core.JoinPath(dir, ".meta", "agent.log"), nil
|
||||
}
|
||||
|
||||
result := s.handleDispatchSync(context.Background(), core.NewOptions(
|
||||
core.Option{Key: "org", Value: "core"},
|
||||
core.Option{Key: "repo", Value: "go-io"},
|
||||
core.Option{Key: "agent", Value: "codex"},
|
||||
core.Option{Key: "task", Value: "Fix tests"},
|
||||
core.Option{Key: "issue", Value: "7"},
|
||||
))
|
||||
|
||||
require.True(t, result.OK)
|
||||
output, ok := result.Value.(DispatchSyncResult)
|
||||
require.True(t, ok)
|
||||
assert.True(t, output.OK)
|
||||
assert.Equal(t, "completed", output.Status)
|
||||
assert.Equal(t, "https://forge.test/core/go-io/pulls/7", output.PRURL)
|
||||
}
|
||||
|
||||
func TestDispatchsync_HandleDispatchSync_Bad_PrepFailure(t *testing.T) {
|
||||
s := &PrepSubsystem{}
|
||||
s.dispatchSyncPrep = func(context.Context, *mcp.CallToolRequest, PrepInput) (*mcp.CallToolResult, PrepOutput, error) {
|
||||
return nil, PrepOutput{}, core.E("prepWorkspace", "boom", nil)
|
||||
}
|
||||
|
||||
result := s.handleDispatchSync(context.Background(), core.NewOptions(
|
||||
core.Option{Key: "repo", Value: "go-io"},
|
||||
core.Option{Key: "task", Value: "Fix tests"},
|
||||
))
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "prep workspace failed")
|
||||
}
|
||||
|
||||
func TestDispatchsync_HandleDispatchSync_Bad_PrepIncomplete(t *testing.T) {
|
||||
s := &PrepSubsystem{}
|
||||
s.dispatchSyncPrep = func(context.Context, *mcp.CallToolRequest, PrepInput) (*mcp.CallToolResult, PrepOutput, error) {
|
||||
return nil, PrepOutput{
|
||||
Success: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
result := s.handleDispatchSync(context.Background(), core.NewOptions(
|
||||
core.Option{Key: "repo", Value: "go-io"},
|
||||
core.Option{Key: "task", Value: "Fix tests"},
|
||||
))
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "prep failed")
|
||||
}
|
||||
|
||||
func TestDispatchsync_HandleDispatchSync_Ugly_SpawnFailure(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
||||
workspaceDir := core.JoinPath(WorkspaceRoot(), "core", "go-io", "task-7")
|
||||
s := &PrepSubsystem{dispatchSyncTick: 10 * time.Millisecond}
|
||||
|
||||
s.dispatchSyncPrep = func(context.Context, *mcp.CallToolRequest, PrepInput) (*mcp.CallToolResult, PrepOutput, error) {
|
||||
require.True(t, fs.EnsureDir(workspaceDir).OK)
|
||||
require.True(t, fs.Write(core.JoinPath(workspaceDir, "status.json"), core.JSONMarshalString(&WorkspaceStatus{
|
||||
Status: "running",
|
||||
})).OK)
|
||||
|
||||
return nil, PrepOutput{
|
||||
Success: true,
|
||||
WorkspaceDir: workspaceDir,
|
||||
Branch: "agent/fix-tests",
|
||||
Prompt: "prompt",
|
||||
}, nil
|
||||
}
|
||||
s.dispatchSyncSpawn = func(agent, prompt, dir string) (int, string, string, error) {
|
||||
require.Equal(t, "codex", agent)
|
||||
return 0, "", "", core.E("spawn", "boom", nil)
|
||||
}
|
||||
|
||||
result := s.handleDispatchSync(context.Background(), core.NewOptions(
|
||||
core.Option{Key: "repo", Value: "go-io"},
|
||||
core.Option{Key: "agent", Value: "codex"},
|
||||
core.Option{Key: "task", Value: "Fix tests"},
|
||||
))
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "spawn agent failed")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -136,6 +136,117 @@ func TestMessage_MessageSend_Bad_MissingRequiredFields(t *testing.T) {
|
|||
assert.Contains(t, result.Value.(error).Error(), "required")
|
||||
}
|
||||
|
||||
func TestMessage_MessageSend_Ugly_WhitespaceContent(t *testing.T) {
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageSend(core.NewOptions(
|
||||
core.Option{Key: "_arg", Value: "core/go-io/task-5"},
|
||||
core.Option{Key: "from", Value: "codex"},
|
||||
core.Option{Key: "to", Value: "claude"},
|
||||
core.Option{Key: "content", Value: " "},
|
||||
))
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "required")
|
||||
}
|
||||
|
||||
func TestMessage_MessageInbox_Good_NoMessages(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageInbox(core.NewOptions(
|
||||
core.Option{Key: "_arg", Value: "core/go-io/task-empty"},
|
||||
core.Option{Key: "agent", Value: "claude"},
|
||||
))
|
||||
|
||||
require.True(t, result.OK)
|
||||
output, ok := result.Value.(MessageListOutput)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, 0, output.Count)
|
||||
assert.Empty(t, output.Messages)
|
||||
}
|
||||
|
||||
func TestMessage_MessageInbox_Bad_MissingRequiredFields(t *testing.T) {
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageInbox(core.NewOptions())
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "required")
|
||||
}
|
||||
|
||||
func TestMessage_HandleMessageInbox_Ugly_CorruptStore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
||||
require.True(t, fs.EnsureDir(messageRoot()).OK)
|
||||
require.True(t, fs.Write(messagePath("core/go-io/task-5"), "{broken json").OK)
|
||||
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageInbox(core.NewOptions(
|
||||
core.Option{Key: "_arg", Value: "core/go-io/task-5"},
|
||||
core.Option{Key: "agent", Value: "claude"},
|
||||
))
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "failed to parse message store")
|
||||
}
|
||||
|
||||
func TestMessage_MessageConversation_Good_NoMessages(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageConversation(core.NewOptions(
|
||||
core.Option{Key: "_arg", Value: "core/go-io/task-empty"},
|
||||
core.Option{Key: "agent", Value: "codex"},
|
||||
core.Option{Key: "with", Value: "claude"},
|
||||
))
|
||||
|
||||
require.True(t, result.OK)
|
||||
output, ok := result.Value.(MessageListOutput)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, 0, output.Count)
|
||||
assert.Empty(t, output.Messages)
|
||||
}
|
||||
|
||||
func TestMessage_MessageConversation_Bad_MissingRequiredFields(t *testing.T) {
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageConversation(core.NewOptions())
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "required")
|
||||
}
|
||||
|
||||
func TestMessage_MessageConversation_Ugly_CorruptStore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
||||
require.True(t, fs.EnsureDir(messageRoot()).OK)
|
||||
require.True(t, fs.Write(messagePath("core/go-io/task-5"), "{broken json").OK)
|
||||
|
||||
s := newTestPrep(t)
|
||||
|
||||
result := s.cmdMessageConversation(core.NewOptions(
|
||||
core.Option{Key: "_arg", Value: "core/go-io/task-5"},
|
||||
core.Option{Key: "agent", Value: "codex"},
|
||||
core.Option{Key: "with", Value: "claude"},
|
||||
))
|
||||
|
||||
assert.False(t, result.OK)
|
||||
require.Error(t, result.Value.(error))
|
||||
assert.Contains(t, result.Value.(error).Error(), "failed to parse message store")
|
||||
}
|
||||
|
||||
func TestMessage_MessageInbox_Ugly_CorruptStore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
setTestWorkspace(t, dir)
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ type PrepSubsystem struct {
|
|||
startupContext context.Context
|
||||
drainCh chan struct{}
|
||||
pokeCh chan struct{}
|
||||
dispatchSyncPrep func(context.Context, *mcp.CallToolRequest, PrepInput) (*mcp.CallToolResult, PrepOutput, error)
|
||||
dispatchSyncSpawn func(agent, prompt, workspaceDir string) (int, string, string, error)
|
||||
dispatchSyncTick time.Duration
|
||||
frozen bool
|
||||
backoff map[string]time.Time
|
||||
failCount map[string]int
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue