|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 12 | +) |
| 13 | + |
| 14 | +// Agent-step style: mock returns JSON that unmarshals into a fixed schema (issue #17 acceptance). |
| 15 | +func TestMockClient_usableForAgentStructuredOutput(t *testing.T) { |
| 16 | + ctx := context.Background() |
| 17 | + cli := &MockClient{ |
| 18 | + Content: `{"summary":"done","findings":[{"id":"f1"}]}`, |
| 19 | + Meta: &GenerateMeta{DurationMs: 42, CostUSD: 0.02}, |
| 20 | + } |
| 21 | + resp, err := cli.Generate(ctx, GenerateRequest{ |
| 22 | + Model: "mock/test", |
| 23 | + Messages: []ChatMessage{{Role: "user", Content: "run"}}, |
| 24 | + }) |
| 25 | + if err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + var decoded struct { |
| 29 | + Summary string `json:"summary"` |
| 30 | + Findings []struct { |
| 31 | + ID string `json:"id"` |
| 32 | + } `json:"findings"` |
| 33 | + } |
| 34 | + if err := json.Unmarshal([]byte(resp.Content), &decoded); err != nil { |
| 35 | + t.Fatalf("decode mock output: %v", err) |
| 36 | + } |
| 37 | + if decoded.Summary != "done" || len(decoded.Findings) != 1 || decoded.Findings[0].ID != "f1" { |
| 38 | + t.Fatalf("got %+v", decoded) |
| 39 | + } |
| 40 | + if resp.Meta.DurationMs != 42 || resp.Meta.CostUSD != 0.02 { |
| 41 | + t.Fatalf("meta %+v", resp.Meta) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestRegistry_unknownProviderNamespace(t *testing.T) { |
| 46 | + g := &spec.ProjectGraph{ |
| 47 | + Spec: spec.ProjectSpec{ |
| 48 | + Providers: &spec.ProjectProviders{ |
| 49 | + Models: map[string]spec.ModelProviderConfig{ |
| 50 | + "openai": {Type: "openai", APIKeyFrom: "env:OPENAI_API_KEY"}, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + reg := NewRegistry(g) |
| 56 | + _, _, err := reg.ClientFor("anthropic/claude-3") |
| 57 | + if err == nil { |
| 58 | + t.Fatal("expected error") |
| 59 | + } |
| 60 | + if !strings.Contains(err.Error(), "unknown provider namespace") || !strings.Contains(err.Error(), "anthropic") { |
| 61 | + t.Fatalf("got %v", err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestRegistry_modelRefFormat(t *testing.T) { |
| 66 | + g := &spec.ProjectGraph{ |
| 67 | + Spec: spec.ProjectSpec{ |
| 68 | + Providers: &spec.ProjectProviders{ |
| 69 | + Models: map[string]spec.ModelProviderConfig{ |
| 70 | + "openai": {Type: "openai", APIKeyFrom: "env:OPENAI_API_KEY"}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + reg := NewRegistry(g) |
| 76 | + _, _, err := reg.ClientFor("badref") |
| 77 | + if err == nil { |
| 78 | + t.Fatal("expected error") |
| 79 | + } |
| 80 | + if !strings.Contains(err.Error(), "namespace/model_id") { |
| 81 | + t.Fatalf("got %v", err) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestRegistry_resolvesOpenAIAndModelID(t *testing.T) { |
| 86 | + t.Setenv("OPENAI_API_KEY", "sk-test") |
| 87 | + g := &spec.ProjectGraph{ |
| 88 | + Spec: spec.ProjectSpec{ |
| 89 | + Providers: &spec.ProjectProviders{ |
| 90 | + Models: map[string]spec.ModelProviderConfig{ |
| 91 | + "openai": {Type: "openai", APIKeyFrom: "env:OPENAI_API_KEY"}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + reg := NewRegistry(g) |
| 97 | + cli, id, err := reg.ClientFor("openai/gpt-4.1") |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + if id != "gpt-4.1" { |
| 102 | + t.Fatalf("model id %q", id) |
| 103 | + } |
| 104 | + if _, ok := cli.(*OpenAIClient); !ok { |
| 105 | + t.Fatalf("want *OpenAIClient, got %T", cli) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestOpenAIClient_Generate_usesChatCompletions(t *testing.T) { |
| 110 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 111 | + if r.URL.Path != "/v1/chat/completions" { |
| 112 | + t.Errorf("path %s", r.URL.Path) |
| 113 | + http.NotFound(w, r) |
| 114 | + return |
| 115 | + } |
| 116 | + auth := r.Header.Get("Authorization") |
| 117 | + if auth != "Bearer sk-mock" { |
| 118 | + t.Errorf("Authorization %q", auth) |
| 119 | + } |
| 120 | + w.Header().Set("Content-Type", "application/json") |
| 121 | + _, _ = w.Write([]byte(`{"choices":[{"message":{"content":"hello"}}]}`)) |
| 122 | + })) |
| 123 | + defer srv.Close() |
| 124 | + |
| 125 | + c := &OpenAIClient{APIKey: "sk-mock", BaseURL: srv.URL + "/v1", HTTPClient: srv.Client()} |
| 126 | + resp, err := c.Generate(context.Background(), GenerateRequest{ |
| 127 | + Model: "gpt-4.1", |
| 128 | + Messages: []ChatMessage{ |
| 129 | + {Role: "user", Content: "hi"}, |
| 130 | + }, |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + if resp.Content != "hello" { |
| 136 | + t.Fatalf("content %q", resp.Content) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestResolveAPIKeyFrom_env(t *testing.T) { |
| 141 | + t.Setenv("MY_KEY", "abc") |
| 142 | + got, err := ResolveAPIKeyFrom("env:MY_KEY") |
| 143 | + if err != nil || got != "abc" { |
| 144 | + t.Fatalf("%q %v", got, err) |
| 145 | + } |
| 146 | + _, err = ResolveAPIKeyFrom("env:MISSING_XYZ_404") |
| 147 | + if err == nil { |
| 148 | + t.Fatal("expected error") |
| 149 | + } |
| 150 | +} |
0 commit comments