Files
llmgw/internal/router/licences_test.go
T
Ilker ManapandClaude Opus 5.5 587d8d5059
CI / test (push) Failing after 6m30s
A licence left out by its name stays out once its entry gives an id
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-11 16:28:40 +02:00

92 lines
3.3 KiB
Go

package router
import (
"crypto/sha256"
"net/http"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
func TestAPersonsLicenceChoiceLeavesModelsOutOfTheirKeys(t *testing.T) {
// A game sold in the EU cannot ship what a licence excluding the EU
// made: its maker says so once, and no key of theirs reaches such a model.
yes, no := true, false
names := []string{"mit", "nc", "eu-out", "plain"}
e := newFakeEngine(t, "m")
cfg := baseConfig(config.Endpoint{ID: "a", URL: e.srv.URL, Models: names})
mk := func(name string, l *catalog.License) catalog.Model {
m := catModel(name, nil)
m.License = l
return m
}
cfg.Models = []catalog.Model{
mk("mit", &catalog.License{ID: "MIT", Name: "MIT", Commercial: &yes}),
mk("nc", &catalog.License{ID: "CC-BY-NC-4.0", Name: "CC BY-NC 4.0", Commercial: &no}),
mk("eu-out", &catalog.License{ID: "tencent-hunyuan-community", Name: "Tencent Hunyuan Community",
ExcludedRegions: []string{"EU", "UK", "KR"}}),
mk("plain", nil),
}
cfg.Auth.Enabled = true
cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db")
rt := newTestRouter(t, cfg)
ctx := t.Context()
if err := rt.store.CreateUser(ctx, store.User{ID: "usr-g", Email: "g@game.test", State: store.UserActive,
CreatedAt: time.Now()}); err != nil {
t.Fatal(err)
}
secret := store.KeyPrefix + strings.Repeat("g", 43)
sum := sha256.Sum256([]byte(secret))
if err := rt.store.CreateUserKey(ctx, "usr-g", store.Key{ID: "key-g", Hash: sum[:], Prefix: "gggg",
Name: "game", CreatedAt: time.Now()}); err != nil {
t.Fatal(err)
}
if err := rt.store.SetUserExcludedLicences(ctx, "usr-g",
[]string{"rule:non-commercial", "rule:region:EU", "unspecified"}); err != nil {
t.Fatal(err)
}
h := rt.Handler()
_, list, raw := call(t, h, secret, http.MethodGet, "/v1/models", nil)
var listed []string
for _, m := range list["data"].([]any) {
listed = append(listed, m.(map[string]any)["id"].(string))
}
if !slices.Equal(listed, []string{"mit"}) {
t.Errorf("listed %v: %s", listed, raw)
}
for _, name := range []string{"nc", "eu-out", "plain"} {
code, _, raw := call(t, h, secret, http.MethodPost, "/v1/chat/completions",
map[string]any{"model": name, "messages": []any{map[string]any{"role": "user", "content": "hi"}}})
if code != http.StatusForbidden || !strings.Contains(raw, "licence_excluded") {
t.Errorf("%s: %d %s", name, code, raw)
}
}
if code, _, raw := call(t, h, secret, http.MethodPost, "/v1/chat/completions",
map[string]any{"model": "mit", "messages": []any{map[string]any{"role": "user", "content": "hi"}}}); code != 200 {
t.Errorf("mit: %d %s", code, raw)
}
// A choice made by a licence's name holds once its entry gives an id.
if why := licenceExcluded([]string{"cc by-nc 4.0"}, cfg.Models[1]); why == "" {
t.Error("a choice by name did not hold against an entry with an id")
}
// The profile page is offered every licence, and the regions in play.
c := rt.licenceChoices()
var keys []string
for _, l := range c.Licences {
keys = append(keys, l.Key)
}
if !slices.Equal(keys, []string{"cc-by-nc-4.0", "mit", "tencent-hunyuan-community", "unspecified"}) ||
!slices.Equal(c.Regions, []string{"EU", "KR", "UK"}) || !c.AnyNonCommercial {
t.Errorf("choices %+v", c)
}
}