feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
package websearch
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-04-12 02:11:50 +08:00
|
|
|
"fmt"
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestNewManager_PreservesOrder(t *testing.T) {
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
configs := []ProviderConfig{
|
2026-04-12 13:11:46 +08:00
|
|
|
{Type: "brave", APIKey: "k3"},
|
|
|
|
|
{Type: "tavily", APIKey: "k1"},
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
m := NewManager(configs, nil)
|
2026-04-12 13:11:46 +08:00
|
|
|
require.Equal(t, "brave", m.configs[0].Type)
|
|
|
|
|
require.Equal(t, "tavily", m.configs[1].Type)
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestManager_SearchWithBestProvider_EmptyQuery(t *testing.T) {
|
|
|
|
|
m := NewManager([]ProviderConfig{{Type: "brave", APIKey: "k"}}, nil)
|
|
|
|
|
_, _, err := m.SearchWithBestProvider(context.Background(), SearchRequest{Query: ""})
|
|
|
|
|
require.ErrorContains(t, err, "empty search query")
|
|
|
|
|
|
|
|
|
|
_, _, err = m.SearchWithBestProvider(context.Background(), SearchRequest{Query: " "})
|
|
|
|
|
require.ErrorContains(t, err, "empty search query")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestManager_SearchWithBestProvider_SkipEmptyAPIKey(t *testing.T) {
|
|
|
|
|
m := NewManager([]ProviderConfig{{Type: "brave", APIKey: ""}}, nil)
|
|
|
|
|
_, _, err := m.SearchWithBestProvider(context.Background(), SearchRequest{Query: "test"})
|
|
|
|
|
require.ErrorContains(t, err, "no available provider")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestManager_SearchWithBestProvider_SkipExpired(t *testing.T) {
|
|
|
|
|
past := time.Now().Add(-1 * time.Hour).Unix()
|
|
|
|
|
m := NewManager([]ProviderConfig{
|
|
|
|
|
{Type: "brave", APIKey: "k", ExpiresAt: &past},
|
|
|
|
|
}, nil)
|
|
|
|
|
_, _, err := m.SearchWithBestProvider(context.Background(), SearchRequest{Query: "test"})
|
|
|
|
|
require.ErrorContains(t, err, "no available provider")
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestManager_SearchWithBestProvider_UsesFirstAvailable(t *testing.T) {
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
srvBrave := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
|
resp := braveResponse{}
|
|
|
|
|
resp.Web.Results = []braveResult{{URL: "https://brave.com", Title: "Brave", Description: "from brave"}}
|
|
|
|
|
json.NewEncoder(w).Encode(resp)
|
|
|
|
|
}))
|
|
|
|
|
defer srvBrave.Close()
|
|
|
|
|
|
|
|
|
|
origURL := *braveSearchURL
|
|
|
|
|
u, _ := http.NewRequest("GET", srvBrave.URL, nil)
|
|
|
|
|
*braveSearchURL = *u.URL
|
|
|
|
|
defer func() { *braveSearchURL = origURL }()
|
|
|
|
|
|
|
|
|
|
m := NewManager([]ProviderConfig{
|
2026-04-12 13:11:46 +08:00
|
|
|
{Type: "brave", APIKey: "k1"},
|
|
|
|
|
{Type: "tavily", APIKey: "k2"},
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}, nil)
|
|
|
|
|
m.clientCache[srvBrave.URL] = srvBrave.Client()
|
|
|
|
|
m.clientCache[""] = srvBrave.Client()
|
|
|
|
|
|
|
|
|
|
resp, providerName, err := m.SearchWithBestProvider(context.Background(), SearchRequest{Query: "test"})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, "brave", providerName)
|
|
|
|
|
require.Len(t, resp.Results, 1)
|
|
|
|
|
require.Equal(t, "from brave", resp.Results[0].Snippet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestManager_SearchWithBestProvider_NilRedis(t *testing.T) {
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
|
resp := braveResponse{}
|
|
|
|
|
resp.Web.Results = []braveResult{{URL: "https://test.com", Title: "Test", Description: "result"}}
|
|
|
|
|
json.NewEncoder(w).Encode(resp)
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
origURL := *braveSearchURL
|
|
|
|
|
u, _ := http.NewRequest("GET", srv.URL, nil)
|
|
|
|
|
*braveSearchURL = *u.URL
|
|
|
|
|
defer func() { *braveSearchURL = origURL }()
|
|
|
|
|
|
|
|
|
|
m := NewManager([]ProviderConfig{
|
2026-04-12 13:11:46 +08:00
|
|
|
{Type: "brave", APIKey: "k", QuotaLimit: 100},
|
|
|
|
|
}, nil)
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
m.clientCache[""] = srv.Client()
|
|
|
|
|
|
|
|
|
|
resp, _, err := m.SearchWithBestProvider(context.Background(), SearchRequest{Query: "test"})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, resp.Results, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestManager_GetUsage_NilRedis(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
2026-04-12 13:11:46 +08:00
|
|
|
used, err := m.GetUsage(context.Background(), "brave")
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, int64(0), used)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestManager_GetAllUsage_NilRedis(t *testing.T) {
|
|
|
|
|
m := NewManager([]ProviderConfig{
|
2026-04-12 13:11:46 +08:00
|
|
|
{Type: "brave"},
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}, nil)
|
|
|
|
|
usage := m.GetAllUsage(context.Background())
|
|
|
|
|
require.Equal(t, int64(0), usage["brave"])
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
// --- Quota TTL from subscription ---
|
|
|
|
|
|
|
|
|
|
func TestQuotaTTLFromSubscription_NilSubscription(t *testing.T) {
|
|
|
|
|
ttl := quotaTTLFromSubscription(nil)
|
|
|
|
|
require.Equal(t, defaultQuotaTTL, ttl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestQuotaTTLFromSubscription_ZeroSubscription(t *testing.T) {
|
|
|
|
|
zero := int64(0)
|
|
|
|
|
ttl := quotaTTLFromSubscription(&zero)
|
|
|
|
|
require.Equal(t, defaultQuotaTTL, ttl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestQuotaTTLFromSubscription_ValidSubscription(t *testing.T) {
|
|
|
|
|
// Subscribed 10 days ago — next reset in ~20 days
|
|
|
|
|
sub := time.Now().Add(-10 * 24 * time.Hour).Unix()
|
|
|
|
|
ttl := quotaTTLFromSubscription(&sub)
|
|
|
|
|
require.Greater(t, ttl, 15*24*time.Hour) // at least 15 days
|
|
|
|
|
require.Less(t, ttl, 25*24*time.Hour+quotaTTLBuffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNextMonthlyReset_SubscribedRecentPast(t *testing.T) {
|
|
|
|
|
// Subscribed on the 10th of this month (always valid day)
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
sub := time.Date(now.Year(), now.Month(), 10, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
next := nextMonthlyReset(sub)
|
|
|
|
|
require.True(t, next.After(now) || next.Equal(now), "next reset should be in the future or now")
|
|
|
|
|
require.True(t, next.Before(now.AddDate(0, 1, 1)))
|
|
|
|
|
}
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestNextMonthlyReset_SubscribedLongAgo(t *testing.T) {
|
|
|
|
|
// Subscribed 6 months ago on the 1st
|
|
|
|
|
sub := time.Now().UTC().AddDate(0, -6, 0)
|
|
|
|
|
sub = time.Date(sub.Year(), sub.Month(), 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
next := nextMonthlyReset(sub)
|
|
|
|
|
require.True(t, next.After(time.Now().UTC()))
|
|
|
|
|
// Should be within the next 31 days
|
|
|
|
|
require.True(t, next.Before(time.Now().UTC().AddDate(0, 1, 1)))
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestNextMonthlyReset_FutureSubscription(t *testing.T) {
|
|
|
|
|
sub := time.Now().UTC().AddDate(0, 0, 5)
|
|
|
|
|
next := nextMonthlyReset(sub)
|
|
|
|
|
require.True(t, next.After(time.Now().UTC()))
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestAddMonthsClamped_Jan31ToFeb(t *testing.T) {
|
|
|
|
|
sub := time.Date(2026, 1, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
next := addMonthsClamped(sub, 1)
|
|
|
|
|
require.Equal(t, time.Month(2), next.Month())
|
|
|
|
|
require.Equal(t, 28, next.Day()) // Feb 28 (2026 is not a leap year)
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestAddMonthsClamped_Jan31ToFebLeapYear(t *testing.T) {
|
|
|
|
|
sub := time.Date(2028, 1, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
next := addMonthsClamped(sub, 1)
|
|
|
|
|
require.Equal(t, time.Month(2), next.Month())
|
|
|
|
|
require.Equal(t, 29, next.Day()) // Feb 29 (2028 is a leap year)
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestAddMonthsClamped_Mar31ToApr(t *testing.T) {
|
|
|
|
|
sub := time.Date(2026, 3, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
next := addMonthsClamped(sub, 1)
|
|
|
|
|
require.Equal(t, time.Month(4), next.Month())
|
|
|
|
|
require.Equal(t, 30, next.Day()) // Apr has 30 days
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
func TestAddMonthsClamped_NormalDay(t *testing.T) {
|
|
|
|
|
sub := time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
next := addMonthsClamped(sub, 1)
|
|
|
|
|
require.Equal(t, time.Month(2), next.Month())
|
|
|
|
|
require.Equal(t, 15, next.Day()) // no clamping needed
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
// --- Redis key ---
|
|
|
|
|
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
func TestQuotaRedisKey_Format(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
key := quotaRedisKey("brave")
|
|
|
|
|
require.Equal(t, "websearch:quota:brave", key)
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
}
|
2026-04-12 02:11:50 +08:00
|
|
|
|
|
|
|
|
// --- isProviderAvailable ---
|
|
|
|
|
|
|
|
|
|
func TestIsProviderAvailable_EmptyAPIKey(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
require.False(t, m.isProviderAvailable(ProviderConfig{APIKey: ""}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProviderAvailable_Expired(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
past := time.Now().Add(-1 * time.Hour).Unix()
|
|
|
|
|
require.False(t, m.isProviderAvailable(ProviderConfig{APIKey: "k", ExpiresAt: &past}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProviderAvailable_Valid(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
future := time.Now().Add(1 * time.Hour).Unix()
|
|
|
|
|
require.True(t, m.isProviderAvailable(ProviderConfig{APIKey: "k", ExpiresAt: &future}))
|
|
|
|
|
require.True(t, m.isProviderAvailable(ProviderConfig{APIKey: "k"})) // no expiry
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- resolveProxyID ---
|
|
|
|
|
|
|
|
|
|
func TestResolveProxyID_AccountProxyOverrides(t *testing.T) {
|
|
|
|
|
cfg := ProviderConfig{ProxyID: 42}
|
|
|
|
|
require.Equal(t, int64(0), resolveProxyID(cfg, "http://account-proxy:8080"))
|
|
|
|
|
require.Equal(t, int64(42), resolveProxyID(cfg, ""))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- isProxyError ---
|
|
|
|
|
|
|
|
|
|
func TestIsProxyError_Nil(t *testing.T) {
|
|
|
|
|
require.False(t, isProxyError(nil))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProxyError_ConnectionRefused(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
require.True(t, isProxyError(fmt.Errorf("dial tcp: connection refused")))
|
2026-04-12 02:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProxyError_Timeout(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
require.True(t, isProxyError(fmt.Errorf("i/o timeout while connecting to proxy")))
|
2026-04-12 02:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProxyError_SOCKS(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
require.True(t, isProxyError(fmt.Errorf("socks connect failed")))
|
2026-04-12 02:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProxyError_TLSHandshake(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
require.True(t, isProxyError(fmt.Errorf("tls handshake timeout")))
|
2026-04-12 02:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProxyError_APIError_NotProxy(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
require.False(t, isProxyError(fmt.Errorf("API rate limit exceeded")))
|
2026-04-12 02:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- isProxyAvailable (nil Redis) ---
|
|
|
|
|
|
|
|
|
|
func TestIsProxyAvailable_NilRedis(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
require.True(t, m.isProxyAvailable(context.Background(), 42))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsProxyAvailable_ZeroID(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
require.True(t, m.isProxyAvailable(context.Background(), 0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- selectByQuotaWeight ---
|
|
|
|
|
|
|
|
|
|
func TestSelectByQuotaWeight_NoQuotaLast(t *testing.T) {
|
2026-04-12 13:11:46 +08:00
|
|
|
m := NewManager(nil, nil)
|
2026-04-12 02:11:50 +08:00
|
|
|
candidates := []ProviderConfig{
|
2026-04-12 13:11:46 +08:00
|
|
|
{Type: "brave", APIKey: "k1", QuotaLimit: 0},
|
|
|
|
|
{Type: "tavily", APIKey: "k2", QuotaLimit: 100},
|
2026-04-12 02:11:50 +08:00
|
|
|
}
|
|
|
|
|
result := m.selectByQuotaWeight(context.Background(), candidates)
|
|
|
|
|
require.Len(t, result, 2)
|
|
|
|
|
require.Equal(t, "tavily", result[0].Type)
|
|
|
|
|
require.Equal(t, "brave", result[1].Type)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectByQuotaWeight_AllNoQuota(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
candidates := []ProviderConfig{
|
|
|
|
|
{Type: "brave", APIKey: "k1", QuotaLimit: 0},
|
|
|
|
|
{Type: "tavily", APIKey: "k2", QuotaLimit: 0},
|
|
|
|
|
}
|
|
|
|
|
result := m.selectByQuotaWeight(context.Background(), candidates)
|
|
|
|
|
require.Len(t, result, 2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectByQuotaWeight_Empty(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
result := m.selectByQuotaWeight(context.Background(), nil)
|
|
|
|
|
require.Empty(t, result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- newHTTPClient ---
|
|
|
|
|
|
|
|
|
|
func TestNewHTTPClient_NoProxy(t *testing.T) {
|
|
|
|
|
c, err := newHTTPClient("")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewHTTPClient_InvalidProxy(t *testing.T) {
|
|
|
|
|
_, err := newHTTPClient("://bad-url")
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
require.Contains(t, err.Error(), "invalid proxy URL")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewHTTPClient_ValidHTTPProxy(t *testing.T) {
|
|
|
|
|
c, err := newHTTPClient("http://proxy.example.com:8080")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewHTTPClient_ValidSOCKS5Proxy(t *testing.T) {
|
|
|
|
|
c, err := newHTTPClient("socks5://proxy.example.com:1080")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, c)
|
|
|
|
|
}
|
2026-04-14 08:42:28 +08:00
|
|
|
|
|
|
|
|
// --- ResetUsage ---
|
|
|
|
|
|
|
|
|
|
func TestManager_ResetUsage_NilRedis(t *testing.T) {
|
|
|
|
|
m := NewManager(nil, nil)
|
|
|
|
|
err := m.ResetUsage(context.Background(), "brave")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|