Files
Agent-System/internal/agent/backend_test.go
T

104 lines
2.6 KiB
Go

package agent_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"chat-agent-system/internal/agent"
"chat-agent-system/internal/config"
)
func TestStubBackendExecution(t *testing.T) {
tmpDir := t.TempDir()
cfg := &config.Config{AgentBackend: "stub"}
backend := agent.NewBackend("stub", cfg)
if backend.Name() != "stub" {
t.Errorf("expected stub backend, got %s", backend.Name())
}
req := agent.Request{
WorkDir: tmpDir,
Prompt: "请编写用户初始需求收集表",
Targets: []string{"output.md"},
Timeout: 5 * time.Second,
}
res, err := backend.Execute(context.Background(), req)
if err != nil {
t.Fatalf("stub execute failed: %v", err)
}
if !res.Success {
t.Fatalf("expected success")
}
outputPath := filepath.Join(tmpDir, "output.md")
if _, err := os.Stat(outputPath); err != nil {
t.Fatalf("expected output.md to exist: %v", err)
}
}
func TestPiAgentRemoteRunner(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/run" {
http.NotFound(w, r)
return
}
var payload map[string]any
_ = json.NewDecoder(r.Body).Decode(&payload)
resp := map[string]any{
"ok": true,
"patches": []map[string]string{
{
"rel_path": "output.md",
"content": "# Remote Generated Deliverable\nValidated from Container Runner",
},
},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}))
defer ts.Close()
tmpDir := t.TempDir()
cfg := &config.Config{
AgentBackend: "pi-agent",
PiAgentRunnerURL: ts.URL,
PiAgentModel: "MiniMax-M3",
PiAgentProvider: "minimax",
AgentTimeout: 5 * time.Second,
}
backend := agent.NewBackend("pi-agent", cfg)
res, err := backend.Execute(context.Background(), agent.Request{
WorkDir: tmpDir,
Prompt: "Generate remote test document",
Targets: []string{"output.md"},
})
if err != nil {
t.Fatalf("remote execute returned unexpected error: %v", err)
}
if !res.Success {
t.Fatalf("expected success, got error: %s", res.ErrorReason)
}
if string(res.Patches["output.md"]) != "# Remote Generated Deliverable\nValidated from Container Runner" {
t.Errorf("unexpected content: %s", string(res.Patches["output.md"]))
}
// Verify local file written
localContent, err := os.ReadFile(filepath.Join(tmpDir, "output.md"))
if err != nil {
t.Fatalf("expected local file written: %v", err)
}
if string(localContent) != "# Remote Generated Deliverable\nValidated from Container Runner" {
t.Errorf("unexpected local file content: %s", string(localContent))
}
}