cliddO11 on X asked for a tag or colour on the provider editor that shows
which key each model came from when several keys fetch models. fetchPerKey
already marks each merged model with the keys whose list has it
(catalog.Model.Keys), but the editor never received that. providerInfo now
sends it as modelJSON.keys: the ids of the keys in use that list the model,
in the keys' order. The field is left out when every key in use lists the
model, when only an off key does, or when the list was not asked per key.
At most six ids are sent, with keyCount when there are more, so a provider
with a thousand keys does not send a thousand ids with every model.
On the chip, after the name, a grey tag in the 1M tag's look (never the
accent, no stripe) shows one key's name (or its masked value, as its row
shows it), two names, or "3 keys". The tooltip lists the keys. One new
string, "Listed by {keys} only", in zh, zh-TW, ja and de.
Run (on mote, at this tree):
- go test -tags nogui -run 'TestModelSaysItsKeys|TestModelKeysCapped'
./internal/gui: ok. Broken by hand (every model tagged, keysApart not
dropping "all keys"): both red.
- node --test model-key-tag, model-same-name-tell, i18n-keys: 32/32 ok
(Chromium + WebKit, en zh zh-TW ja de at 1100px, en zh at 420px). With
the tag call removed from app.js: webkit 0/7.
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gui: ok.
- gofmt -l: clean.
- Full go + GUI suite queued behind ~/suite.lock on mote; its result is
posted on the follow-up.
Not tried: a real relay that gives each key its own models; the tag reads
catalog.Model.Keys, which fetchPerKey has filled since #904.
80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A provider whose keys each list their own models (cliddO11 on X) says
|
|
// in its editor which keys list a model, when not all of them in use do:
|
|
// a model every key lists, one only an off key lists, and one of a list
|
|
// not asked per key carry none.
|
|
func TestModelSaysItsKeysWhenTheyDiffer(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
a, b, c, d := provider.KeyID("sk-aaaa1111"), provider.KeyID("sk-bbbb2222"), provider.KeyID("sk-cccc3333"), provider.KeyID("sk-dddd4444")
|
|
p := provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example/v1", Key: "sk-aaaa1111", Keys: []provider.KeyAccount{
|
|
{Key: "sk-bbbb2222", Name: "Team"}, {Key: "sk-cccc3333", Off: true}, {Key: "sk-dddd4444"},
|
|
}}
|
|
if err := catalog.SaveLive("relay", "https://relay.example/v1", []catalog.Model{
|
|
{ID: "every", Keys: []string{d, c, b, a}},
|
|
{ID: "two", Keys: []string{d, a}},
|
|
{ID: "one", Keys: []string{b}},
|
|
{ID: "off-only", Keys: []string{c}},
|
|
{ID: "unasked"},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]modelJSON{}
|
|
for _, m := range providerInfo(p, nil).Models {
|
|
got[m.ID] = m
|
|
}
|
|
want := map[string][]string{"every": nil, "two": {a, d}, "one": {b}, "off-only": nil, "unasked": nil}
|
|
for id, keys := range want {
|
|
m, ok := got[id]
|
|
if !ok {
|
|
t.Fatalf("%s not listed: %v", id, got)
|
|
}
|
|
if !slices.Equal(m.Keys, keys) || m.KeyCount != 0 {
|
|
t.Errorf("%s: keys %v (count %d), want %v", id, m.Keys, m.KeyCount, keys)
|
|
}
|
|
}
|
|
|
|
// one key in use: nothing to tell apart
|
|
p.Keys = nil
|
|
for _, m := range providerInfo(p, nil).Models {
|
|
if m.Keys != nil {
|
|
t.Errorf("single key: %s says keys %v", m.ID, m.Keys)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Past keysShown a model names no more keys and says how many: a provider
|
|
// of a thousand keys sends no thousand ids with each model.
|
|
func TestModelKeysCapped(t *testing.T) {
|
|
var on []string
|
|
at := map[string]int{}
|
|
for i := range 1000 {
|
|
id := fmt.Sprintf("k%04d", i)
|
|
at[id] = len(on)
|
|
on = append(on, id)
|
|
}
|
|
ids, n := keysApart(slices.Concat(on[:999], []string{"gone"}), on, at)
|
|
if n != 999 || !slices.Equal(ids, on[:keysShown]) {
|
|
t.Fatalf("999 of 1000: %v, %d", ids, n)
|
|
}
|
|
if ids, n := keysApart(on, on, at); ids != nil || n != 0 {
|
|
t.Fatalf("every key: %v, %d", ids, n)
|
|
}
|
|
if ids, n := keysApart([]string{"k0002", "k0001", "k0002"}, on, at); n != 2 || !slices.Equal(ids, []string{"k0001", "k0002"}) {
|
|
t.Fatalf("in the keys' order, once each: %v, %d", ids, n)
|
|
}
|
|
}
|