summaryrefslogtreecommitdiff
path: root/internal/executor/google_routing_test.go
blob: 9690a4b0ec9f519d14351e244d42392f523f0899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package executor

import (
	"context"
	"log/slog"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"sync/atomic"
	"testing"

	"github.com/thepeterstone/claudomator/internal/provider/google"
)

// TestPool_Submit_GoogleAgentType_RoutesThroughGoogleProvider is the
// NativeRunner-level confirmation (Phase 4 verification item 5) that a task
// with agent.type: "google" is actually routed through the new
// internal/provider/google.Provider when the pool is wired with a "google"
// runner — exactly how internal/cli/serve.go and internal/cli/run.go
// register it from cfg.Providers["google"] when a non-empty APIKey is
// configured. This exercises the full path: Pool.Submit -> execute() ->
// NativeRunner.Run() -> agentloop.Loop.Run() -> google.Provider.Chat() ->
// HTTP POST to the (fake) Gemini generateContent API endpoint. Mirrors
// internal/executor/anthropic_routing_test.go.
func TestPool_Submit_GoogleAgentType_RoutesThroughGoogleProvider(t *testing.T) {
	var calls int32
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		atomic.AddInt32(&calls, 1)
		if !strings.Contains(r.URL.Path, ":generateContent") {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		if r.URL.Query().Get("key") != "test-google-key" {
			t.Errorf("wrong key query param: %q", r.URL.Query().Get("key"))
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte(`{
			"candidates": [{
				"content": {"role": "model", "parts": [{"text": "## Summary\ndone"}]},
				"finishReason": "STOP"
			}],
			"usageMetadata": {"promptTokenCount": 12, "candidatesTokenCount": 4, "totalTokenCount": 16}
		}`))
	}))
	defer srv.Close()

	// Mirrors the exact construction in internal/cli/serve.go's
	// cfg.Providers["google"] wiring block: google.New(apiKey, endpoint,
	// timeout) wrapped in a NativeRunner registered under the "google"
	// runner-map key.
	runners := map[string]Runner{
		"google": &NativeRunner{
			Provider: google.New("test-google-key", srv.URL, 0),
			Logger:   slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})),
			LogDir:   t.TempDir(),
		},
	}

	store := testStore(t)
	logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
	pool := NewPool(2, runners, store, logger)

	tk := makeTask("google-routing-1")
	tk.Agent.Type = "google"
	tk.Agent.Model = "gemini-2.5-flash"
	tk.Agent.SkipPlanning = true
	if err := store.CreateTask(tk); err != nil {
		t.Fatalf("CreateTask: %v", err)
	}

	if err := pool.Submit(context.Background(), tk); err != nil {
		t.Fatalf("Submit: %v", err)
	}

	result := <-pool.Results()
	if result.Err != nil {
		t.Fatalf("expected no error, got: %v", result.Err)
	}
	if got := atomic.LoadInt32(&calls); got != 1 {
		t.Fatalf("expected exactly 1 call to the fake Gemini server, got %d", got)
	}
	if result.Execution.Agent != "google" {
		t.Errorf("execution.Agent: want %q, got %q", "google", result.Execution.Agent)
	}
	if result.Execution.TokensIn != 12 || result.Execution.TokensOut != 4 {
		t.Errorf("tokens should come from the google provider's usage: got in=%d out=%d",
			result.Execution.TokensIn, result.Execution.TokensOut)
	}
}