iop/apps/node/internal/adapters/cli/cli_test.go

117 lines
3.1 KiB
Go

package cli_test
import (
"context"
"strings"
"testing"
"go.uber.org/zap"
"iop/apps/node/internal/adapters/cli"
"iop/apps/node/internal/adapters/cli/status"
"iop/apps/node/internal/runtime"
"iop/packages/config"
)
func TestCLIHandleCommandUsageStatusUsesSelectedAgent(t *testing.T) {
c := cli.New(config.CLIConf{
Enabled: true,
Profiles: map[string]config.CLIProfileConf{
"codex": {
Command: "/tmp/fake-codex",
},
"cline-m1": {
Command: "cline-m1",
},
},
}, zap.NewNop())
// Inject fake checker
c.StatusChecker = func(ctx context.Context, agent string, profile config.CLIProfileConf) (*status.UsageStatus, error) {
if agent == "codex" {
if profile.Command != "/tmp/fake-codex" {
t.Errorf("expected command /tmp/fake-codex, got %q", profile.Command)
}
return &status.UsageStatus{RawOutput: "fake-codex-status"}, nil
}
return nil, nil
}
// Test missing agent
_, err := c.HandleCommand(context.Background(), runtime.CommandRequest{
Type: runtime.CommandTypeUsageStatus,
Model: "unknown",
})
if err == nil || !strings.Contains(err.Error(), "unknown agent") {
t.Errorf("expected unknown agent error, got %v", err)
}
// Test codex
resp, err := c.HandleCommand(context.Background(), runtime.CommandRequest{
RequestID: "req-123",
Type: runtime.CommandTypeUsageStatus,
Model: "codex",
SessionID: "sess-456",
Adapter: "cli",
})
if err != nil {
t.Fatalf("HandleCommand failed: %v", err)
}
if resp.UsageStatus == nil || resp.UsageStatus.RawOutput != "fake-codex-status" {
t.Errorf("unexpected usage status: %+v", resp.UsageStatus)
}
if resp.RequestID != "req-123" {
t.Errorf("expected RequestID req-123, got %s", resp.RequestID)
}
if resp.SessionID != "sess-456" {
t.Errorf("expected SessionID sess-456, got %s", resp.SessionID)
}
// Test unsupported command type
_, err = c.HandleCommand(context.Background(), runtime.CommandRequest{
Type: runtime.CommandType("invalid"),
Model: "codex",
})
if err == nil || !strings.Contains(err.Error(), "unsupported command") {
t.Errorf("expected unsupported command error, got %v", err)
}
}
func TestCapabilities_OnlyConfiguredProfiles(t *testing.T) {
c := cli.New(config.CLIConf{
Enabled: true,
Profiles: map[string]config.CLIProfileConf{
"foo": {Command: "foo"},
"bar": {Command: "bar"},
},
}, zap.NewNop())
caps, err := c.Capabilities(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := []string{"bar", "foo"}
if len(caps.Models) != len(want) {
t.Fatalf("expected models %v, got %v", want, caps.Models)
}
for i, m := range want {
if caps.Models[i] != m {
t.Errorf("Models[%d] = %q, want %q", i, caps.Models[i], m)
}
}
}
func TestCapabilities_EmptyProfilesReturnsEmptyModels(t *testing.T) {
c := cli.New(config.CLIConf{
Enabled: true,
Profiles: map[string]config.CLIProfileConf{},
}, zap.NewNop())
caps, err := c.Capabilities(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(caps.Models) != 0 {
t.Errorf("expected empty models, got %v", caps.Models)
}
}