diff --git a/internal/admin/debug_test.go b/internal/admin/debug_test.go deleted file mode 100644 index 93e11dd..0000000 --- a/internal/admin/debug_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package admin - -import ( - "testing" - - "opencode2api/internal/identity" -) - -// TestBenchSessionIsFresh pins the session the Playground bench sends upstream. -// The bench replays one prompt per click, so a session derived from the prompt -// would reuse a single bucket until it is charged and every later click would -// answer 429 while live traffic succeeded. -func TestBenchSessionIsFresh(t *testing.T) { - first := benchSession() - second := benchSession() - - if first == "" { - t.Fatal("empty session") - } - if first == second { - t.Errorf("two bench requests reused session %q", first) - } - for _, session := range []string{first, second} { - if reencoded := identity.CanonicalSessionID(session); reencoded != session { - t.Errorf("bench session %q is not canonical (re-encoded to %q)", session, reencoded) - } - } -} diff --git a/internal/gateway/probe_test.go b/internal/gateway/probe_test.go deleted file mode 100644 index 559c3c3..0000000 --- a/internal/gateway/probe_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package gateway - -import ( - "testing" - - "opencode2api/internal/identity" -) - -// TestProbeRequestIDsAreFresh pins the identity a probe presents upstream. A -// pinned project or session is one quota bucket shared with every deployment -// that runs the same probe, so a charged bucket on a popular exit made every -// probe 429 while live traffic on the same process and egress succeeded. -func TestProbeRequestIDsAreFresh(t *testing.T) { - first := probeRequestIDs() - second := probeRequestIDs() - - if first.Session == second.Session { - t.Errorf("two probes reused session %q", first.Session) - } - if first.Project == second.Project { - t.Errorf("two probes reused project %q", first.Project) - } - for _, ids := range []identity.RequestIDs{first, second} { - if ids.Session == "" || ids.Project == "" { - t.Fatalf("incomplete identity: %+v", ids) - } - // CanonicalSessionID leaves an official session untouched, so an - // idempotent round trip means the probe already sends the shape the - // free tier accepts instead of relying on the gateway to re-encode it. - if reencoded := identity.CanonicalSessionID(ids.Session); reencoded != ids.Session { - t.Errorf("probe session %q is not canonical (re-encoded to %q)", ids.Session, reencoded) - } - if len(ids.Project) != 40 { - t.Errorf("probe project %q is not a 40-character id", ids.Project) - } - } -} diff --git a/internal/gateway/toolshape_test.go b/internal/gateway/toolshape_test.go deleted file mode 100644 index 8f37d3e..0000000 --- a/internal/gateway/toolshape_test.go +++ /dev/null @@ -1,96 +0,0 @@ -package gateway - -import ( - "encoding/json" - "strings" - "testing" - - wire "opencode2api/internal/protocol" -) - -// TestPrepareAnonymousBodyHidesClientToolNames pins the fix for the free tier's -// client detection: a Claude Code request that declares both of these tools is -// answered with FreeUsageLimitError, and renaming either name lifts the -// refusal. The shaped body must therefore never spell a client tool name. -func TestPrepareAnonymousBodyHidesClientToolNames(t *testing.T) { - body := []byte(`{ - "model": "deepseek-v4.1-flash-free", - "messages": [{"role": "user", "content": "hi"}], - "stream": false, - "tools": [ - {"type": "function", "function": {"name": "Read", "description": "Read a file.", - "parameters": {"type": "object", "properties": {"file_path": {"type": "string"}}}}}, - {"type": "function", "function": {"name": "ReportFindings", "description": "Report findings.", - "parameters": {"type": "object", "properties": {}}}}, - {"type": "function", "function": {"name": "ScheduleWakeup", "description": "Schedule a wakeup.", - "parameters": {"type": "object", "properties": {}}}} - ] - }`) - - shaped, renames := prepareAnonymousBody(body, wire.Chat, false) - if len(shaped) == 0 { - t.Fatal("no shaped body") - } - text := string(shaped) - // The two Claude Code markers are distinctive enough to assert over the - // whole body; a generic name like Read also appears in the official prompt - // prose, so the tool list is what must stay free of it. - for _, marker := range []string{"ReportFindings", "ScheduleWakeup"} { - if strings.Contains(text, marker) { - t.Errorf("shaped body still spells the client tool %q", marker) - } - } - for _, client := range []string{"Read", "ReportFindings", "ScheduleWakeup"} { - if _, ok := renames.Wire(client); !ok { - t.Errorf("no wire name recorded for %q", client) - } - } - - var payload struct { - Stream bool `json:"stream"` - Tools []struct { - Function struct { - Name string `json:"name"` - } `json:"function"` - } `json:"tools"` - } - if err := json.Unmarshal(shaped, &payload); err != nil { - t.Fatalf("shaped body is not JSON: %v", err) - } - if !payload.Stream { - t.Error("shaped body is not streaming") - } - names := make(map[string]bool, len(payload.Tools)) - for _, tool := range payload.Tools { - names[tool.Function.Name] = true - } - // The official set is what makes the request read as an OpenCode session; - // dropping it is refused just like keeping a foreign tool name. - for _, official := range []string{"read", "edit", "shell"} { - if !names[official] { - t.Errorf("shaped body is missing the official tool %q", official) - } - } - for client := range map[string]bool{"Read": true, "ReportFindings": true, "ScheduleWakeup": true} { - wireName, _ := renames.Wire(client) - if !names[wireName] { - t.Errorf("client tool %q is not declared under its wire name %q", client, wireName) - } - } -} - -// TestPrepareAnonymousBodyRestoresToolCalls keeps the response side honest: a -// call the model makes to a renamed client tool comes back under the name the -// client declared. -func TestPrepareAnonymousBodyRestoresToolCalls(t *testing.T) { - body := []byte(`{"model":"deepseek-v4.1-flash-free","messages":[{"role":"user","content":"hi"}], - "tools":[{"type":"function","function":{"name":"ScheduleWakeup","parameters":{"type":"object","properties":{}}}}]}`) - _, renames := prepareAnonymousBody(body, wire.Chat, false) - wireName, ok := renames.Wire("ScheduleWakeup") - if !ok { - t.Fatal("no wire name recorded") - } - if got := renames.Restore(wireName); got != "ScheduleWakeup" { - t.Errorf("Restore(%q) = %q, want ScheduleWakeup", wireName, got) - } -} diff --git a/internal/identity/request_test.go b/internal/identity/request_test.go deleted file mode 100644 index dca733f..0000000 --- a/internal/identity/request_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package identity - -import "testing" - -// TestCanonicalSessionIDRestartSalt verifies that a synthesized session is -// stable within one process but changes when the process salt changes, which is -// what a daemon restart does. -func TestCanonicalSessionIDRestartSalt(t *testing.T) { - original := bootNonce - t.Cleanup(func() { bootNonce = original }) - - first := CanonicalSessionID("conversation seed") - if again := CanonicalSessionID("conversation seed"); again != first { - t.Fatalf("session id changed within one process: %q then %q", first, again) - } - if !isOfficialSessionID(first) { - t.Fatalf("session id %q is not accepted by the free tier pattern", first) - } - - bootNonce = []byte("different boot") - if restarted := CanonicalSessionID("conversation seed"); restarted == first { - t.Fatalf("session id survived a restart: %q", restarted) - } -} - -// TestCanonicalSessionIDKeepsOfficialID verifies that a client-minted official -// session is never re-salted, since its affinity belongs to the client. -func TestCanonicalSessionIDKeepsOfficialID(t *testing.T) { - official := "ses_012345678fc0ABCDEFGHIJKLMN" - if got := CanonicalSessionID(official); got != official { - t.Fatalf("official session rewritten: %q, want %q", got, official) - } -} diff --git a/internal/protocol/tools_test.go b/internal/protocol/tools_test.go deleted file mode 100644 index 257f585..0000000 --- a/internal/protocol/tools_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package protocol - -import ( - "strings" - "testing" -) - -// The free tier refuses a shaped body whose tools still name another client's -// session: Claude Code declaring both ReportFindings and ScheduleWakeup is -// answered with FreeUsageLimitError. The wire name therefore may not echo the -// client name, which is what these tests pin down. -func TestClientToolWireNameIsOpaque(t *testing.T) { - used := map[string]bool{} - for _, client := range []string{"ScheduleWakeup", "ReportFindings", "Read", "Bash", "mcp__server__do_thing"} { - wireName := ClientToolWireName(client, used) - if !strings.HasPrefix(wireName, ClientToolPrefix) { - t.Errorf("wire name %q does not carry the client prefix", wireName) - } - if len(wireName) > toolWireNameLimit { - t.Errorf("wire name %q is longer than %d bytes", wireName, toolWireNameLimit) - } - if strings.Contains(strings.ToLower(wireName), strings.ToLower(client)) { - t.Errorf("wire name %q still spells the client name %q", wireName, client) - } - if strings.ContainsAny(wireName, " \t\r\n") { - t.Errorf("wire name %q is not one token", wireName) - } - } -} - -func TestClientToolWireNameIsStable(t *testing.T) { - first := ClientToolWireName("ReportFindings", map[string]bool{}) - second := ClientToolWireName("ReportFindings", map[string]bool{}) - if first != second { - t.Fatalf("same client tool got %q then %q", first, second) - } - other := ClientToolWireName("ScheduleWakeup", map[string]bool{}) - if first == other { - t.Fatalf("two client tools share the wire name %q", first) - } -} - -func TestClientToolWireNameSuffixesCollisions(t *testing.T) { - used := map[string]bool{} - first := ClientToolWireName("Read", used) - // Hand the allocator a used set that already holds this tool's name, the - // way a second definition of the same client tool would. - retry := ClientToolWireName("Read", used) - if retry == first { - t.Fatalf("a taken wire name was handed out twice: %q", first) - } - if !strings.HasSuffix(retry, "_2") { - t.Errorf("collision suffix missing from %q", retry) - } -} - -func TestToolRenamesRestoreOpaqueNames(t *testing.T) { - renames := NewToolRenames() - used := map[string]bool{} - for _, client := range []string{"ReportFindings", "schedule_wakeup"} { - wireName := ClientToolWireName(client, used) - if wireName == client { - t.Fatalf("%q was not renamed", client) - } - renames.Add(client, wireName) - if got := renames.Restore(wireName); got != client { - t.Errorf("Restore(%q) = %q, want %q", wireName, got, client) - } - } -}