Files
magpie/internal/gateway/thirdparty_app_test.go
T
yetone df95ac6bc2 docs, site: Integrating your app or agent with magpie, at docs/integrating.md and usemagpie.ai/docs/integrate (#918, Magixyne)
A developer of an AI app asked how to work with magpie and whether an app can
register itself after one confirmation. The page says what exists: the
gateway (find it via /api/hello and settings.json's port, the OpenAI,
Anthropic, Responses and Gemini endpoints, /v1/models' fields), how a key
magpie-<id> or the User-Agent's first word names the app in Usage, a row on
the Agents page by an adapter PR in internal/agent (config-edit, Cindy-style
own import link confirmed in the app, or a local API) with what the PR needs,
what plugins can and can't do, and what isn't there: no link or API registers
an app or installs a plugin, and magpie://import adds providers only. English
and Chinese pages, linked from every docs page's nav and footer.
TestThirdPartyAppIsCountedAsItself pins the attribution the page promises.
go vet and the gateway tests pass; site node tests pass; pages checked in
headless Chromium (no errors, TOC, no horizontal scroll at 390px).
2026-10-05 22:46:35 +08:00

56 lines
1.9 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// What docs/integrating.md promises an app magpie has no adapter for (#918):
// /api/hello finds the gateway, and its calls are counted as its own in
// Usage, by the key it sends (magpie-<app>) or else by its User-Agent.
func TestThirdPartyAppIsCountedAsItself(t *testing.T) {
setHome(t, t.TempDir())
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/api/hello", nil))
var hello struct{ Name string }
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &hello) != nil || hello.Name != "magpie" {
t.Fatalf("GET /api/hello: %d %s", rec.Code, rec.Body.String())
}
f := &fake{t: t, reply: sse(
`data: {"id":"c1","choices":[{"delta":{"content":"OK"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2}}`,
`data: [DONE]`)}
setup(t, provider.Chat, f)
body := `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`
for i, c := range []struct{ key, ua, want string }{
{TokenFor("my-app"), "node-fetch/1.0", "my-app"},
{Token, "MyApp/1.2 (darwin)", "MyApp"},
} {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+c.key)
req.Header.Set("User-Agent", c.ua)
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "OK") {
t.Fatalf("%+v: %d %s", c, rec.Code, rec.Body.String())
}
var got []usage.Record
for range 100 {
if got = usage.Load(time.Time{}); len(got) > i {
break
}
time.Sleep(20 * time.Millisecond)
}
if len(got) <= i || got[i].Agent != c.want {
t.Fatalf("%+v: records %+v, want the last %s's", c, got, c.want)
}
}
}