Files
magpie/internal/provider/longcat_test.go
T
yetone 066ece93bb provider: LongCat is a vendor preset, Meituan's LongCat API platform with chat completions, Responses and Anthropic messages (hoobnn on Discord)
LongCat makes its own models (LongCat-2.5, launched 11-02) and serves them
at api.longcat.chat. magpie had LongCat's icon but no preset for it.

The new preset is longcat ("LongCat", KindVendor, icon longcat, catalog
models.dev's longcat). It uses:
- chat completions and Responses at https://api.longcat.chat/openai/v1;
- Anthropic messages at https://api.longcat.chat/anthropic;
- docs at longcat.chat/platform/docs/zh/api-docs, keys at
  longcat.chat/platform/api_keys.
The key goes as a Bearer. magpie sends both headers, and the docs ask for
Bearer on both endpoints; x-api-key alone gets "missing_api_key". The model
list is /openai/v1/models. longcat.chat's and longcat.ai's accounts use the
same API host (both sites' api-docs pages name api.longcat.chat).

An entry at those endpoints imports as the preset, for example CC Switch's
Claude Code and Codex entries from LongCat's cc-switch page. OpenCode's
auth.json "longcat" (models.dev's id) maps to the preset too. The README
(en, zh), docs/reference.md and the site's import pages (en, zh, ja, and
import.html, with the icon) list it.

Sources, read 11-02: api-docs, api/models, api/model, api/model-params,
api/chat, api/messages, image-video-understanding, cc-switch, pricing/
longcat-2.5, change-log, faq.

Probed without a key, from this machine: /openai/v1/models and
/anthropic/v1/models answer 401 authentication_error "missing_api_key".
/openai/v1/chat/completions, /openai/v1/responses and
/anthropic/v1/messages with a bad Bearer answer 401 "incorrect api key".

Run at this head (base d24be3c25, origin/main at push), on mote:
- TestLongCatPreset and TestLongCatList (the docs' literal /models
  example) pass. TestLongCatPreset goes red with the OpenCode mapping taken
  out ("OpenCode import: … Skip:it has no base URL").
- gofmt -l and GOOS=linux / GOOS=windows go vet ./internal/provider are
  clean.
- go test -tags nogui -timeout 45m ./... is ok except three tests that
  have issues already:
  - TestEveryRouteHasACommandOrIsNamed (#4826) is red on a clean
    d24be3c25 too.
  - TestKeepOffWaitsForTheSetting (#3687) is red on a clean d24be3c25 too.
  - TestCodexPoolSaysTheClientsVersion (#4807) is red in the full package
    run and passes 3 of 3 alone on d24be3c25.
- GUI add-sheet and add-button pass in Chromium and WebKit (12/12). No GUI
  file changed: the add sheet draws presets from /api.

Not tried:
- a real LongCat key or a real request (none available).
- LongCat-2.5's window (1M), output cap (256K) and image input. These come
  from models.dev, which so far lists only LongCat-2.0 and
  LongCat-2.5-Preview, and LongCat's /models gives no facts. Until
  models.dev adds LongCat-2.5, magpie doesn't know them for it.
- LongCat's thinking switch ({"type":"enabled"|"disabled"}).
2026-11-02 13:44:11 +08:00

72 lines
2.8 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// LongCat (hoobnn on Discord) is a vendor preset: Meituan's own models at
// api.longcat.chat, chat completions and Responses under /openai/v1 and
// Anthropic messages under /anthropic, as its API docs and its CC Switch
// page give them, and an entry another app has there is the preset.
func TestLongCatPreset(t *testing.T) {
p, err := FromPreset("longcat")
if err != nil {
t.Fatal(err)
}
if p.Chat != "https://api.longcat.chat/openai/v1" || p.Responses != "https://api.longcat.chat/openai/v1" || p.Anthropic != "https://api.longcat.chat/anthropic" {
t.Fatalf("endpoints: %q %q %q", p.Chat, p.Responses, p.Anthropic)
}
if p.Name != "LongCat" || p.Icon != "longcat" || p.Catalog != "longcat" || p.KeysURL != "https://longcat.chat/platform/api_keys" {
t.Fatalf("preset: %+v", p)
}
if pr := Preset("longcat"); pr.Kind != KindVendor || pr.Hosts || pr.NoKey {
t.Fatalf("kind %q hosts %v noKey %v", pr.Kind, pr.Hosts, pr.NoKey)
}
// CC Switch's Claude Code entry (ANTHROPIC_BASE_URL) and its Codex one
// (base_url), as LongCat's cc-switch page shows them
for _, e := range []endpoints{{anthropic: "https://api.longcat.chat/anthropic"}, {responses: "https://api.longcat.chat/openai/v1"}} {
im, why := imported("Longcat", "k", e, nil)
if why != "" || im.Preset != "longcat" || im.Icon != "longcat" || im.Anthropic != "https://api.longcat.chat/anthropic" {
t.Fatalf("imported %+v: %q %+v", e, why, im)
}
}
// OpenCode keeps models.dev's provider id, longcat, in auth.json
dir := t.TempDir()
path := filepath.Join(dir, "auth.json")
if err := os.WriteFile(path, []byte(`{"longcat": {"type": "api", "key": "lc-key"}}`), 0o600); err != nil {
t.Fatal(err)
}
ims, err := readOpenCodeAuth(path)
if err != nil || len(ims) != 1 || ims[0].Skip != "" || ims[0].Provider.Preset != "longcat" || ims[0].Provider.Key != "lc-key" {
t.Fatalf("OpenCode import: %v %+v", err, ims)
}
}
// Its list, /openai/v1/models with the key as a Bearer, as the docs'
// example answers it.
func TestLongCatList(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/openai/v1/models" || r.Header.Get("Authorization") != "Bearer k" {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(`{"error":{"code":"invalid_api_key","message":"missing_api_key","type":"authentication_error"}}`))
return
}
rw.Write([]byte(`{ "object" : "list" , "data" : [ { "id" : "LongCat-2.5" , "object" : "model" , "owned_by" : "LongCat" } ] }`))
}))
defer srv.Close()
ms, _, err := catalog.FetchAt(context.Background(), srv.URL+"/openai/v1", "k", false, nil)
if err != nil {
t.Fatal(err)
}
if len(ms) != 1 || ms[0].ID != "LongCat-2.5" {
t.Fatalf("%+v", ms)
}
}