Files
magpie/internal/plugin/plugin_icon_test.go
T
yetone 5611e7274e plugin: a plugin's sign-in words show in the window's language when it gives them
mahiro233 on Discord (v0.1.3372, Kiro plugin 0.1.34): in a Chinese
window, the Kiro sheet's "Choose a sign-in method" and its choices read in
English, while the rest of the sheet is Chinese. magpie showed a plugin's
method labels, questions and choices only as the plugin wrote them, and no
plugin had a way to give them in another language.

A plugin may now give them by language, magpie's own fields beside
OpenCode's English, as a page gives its titles: a method's labels, a
question's messages, a select option's labels and hints, each
{"zh": "…", "zh-TW": "…", "ja": "…", "de": "…"}. host.js passes them on
(byLang: string values, codes of 1-16 characters, at most 16, empty ones
left out); plugin.Method and plugin.Prompt carry them; the app shows the
window's language, then its first part, else the English (inLang), on the
way picker, a question, its choices and their hints, a key's field and
the site step's other ways. `magpie plugin login` stays English. Kiro
0.1.35 gives its words in all four (plugins 588b3611).

Tests: TestPluginSignInWordsByLanguage (a plugin's words reach magpie as
given, bad ones left out, none for a plugin that gives none);
TestPluginOwnIconAndKeyHint compares a Method with reflect.DeepEqual now
that it holds a map. The GUI side is checked by plugin-ask-ways.test.cjs
in the next commit.

Run on mote at this tree: gofmt -l empty; go vet ./internal/plugin
./internal/provider ./internal/gui (darwin), linux -tags nogui, windows:
ok; go test -count=1 ./internal/plugin ./internal/provider ./internal/cli:
ok. Broken by hand: with origin/main's host.js the new test fails (no
labels, messages or hints reach magpie). Not tried: a real Kiro sign-in
(no keys or passwords entered anywhere); the full suite is queued on mote
under suite.lock after the push.
2026-11-09 18:29:55 +08:00

73 lines
2.6 KiB
Go

package plugin
import (
"context"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
// Lemon on Discord: can a plugin give its provider its own icon, and say
// what its API key is asked as? The auth hook's icon, else package.json's
// magpie.icon, is passed on when it is an https URL or a data:image URI;
// an "api" method's placeholder is its key's hint, and its label the
// key's title unless that only says "API key".
func TestPluginOwnIconAndKeyHint(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/ownicon")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
ps, err := Providers(ctx)
if err != nil {
t.Fatal(err)
}
by := map[string]Provider{}
for _, p := range ps {
by[p.ID] = p
}
lemon, lime := by["lemon"], by["lime"]
if !strings.HasPrefix(lemon.Icon, "data:image/svg+xml;base64,") {
t.Errorf("lemon's icon = %.60q", lemon.Icon)
}
if lime.Icon != "https://ownicon.example/icon.png" {
t.Errorf("lime's icon = %q, want package.json's", lime.Icon)
}
// how many requests each account takes at once (Discord, Lemon): the
// auth hook's, else package.json's when the hook's isn't a whole number
if lemon.MaxConcurrency != 3 || lime.MaxConcurrency != 5 {
t.Errorf("maxConcurrency = %d, %d, want 3, 5", lemon.MaxConcurrency, lime.MaxConcurrency)
}
// how many of its accounts take requests at once (01huadalang on
// Discord: ZCode bans an address three accounts are used from at once):
// read the same way
if lemon.MaxAccounts != 4 || lime.MaxAccounts != 2 {
t.Errorf("maxAccounts = %d, %d, want 4, 2", lemon.MaxAccounts, lime.MaxAccounts)
}
if len(lemon.Methods) != 2 || !reflect.DeepEqual(lemon.Methods[0], Method{Type: "api", Label: "Lemon API key (from lemon.example/keys)", Placeholder: "sk-lemon-…"}) || lemon.Methods[1].Placeholder != "" {
t.Fatalf("lemon's methods = %+v", lemon.Methods)
}
if len(lime.Methods) != 1 || lime.Methods[0].Placeholder != "" {
t.Fatalf("lime's methods = %+v", lime.Methods)
}
if got := lemon.Methods[0].KeyTitle("Lemon"); got != "Lemon API key (from lemon.example/keys)" {
t.Errorf("lemon's key title = %q", got)
}
if got := lime.Methods[0].KeyTitle("Lime"); got != "Lime API key" {
t.Errorf("lime's key title = %q", got)
}
if got := (Method{Type: "api"}).KeyTitle("Lime"); got != "Lime API key" {
t.Errorf("no label's key title = %q", got)
}
// kept for when magpie has only just started
for _, p := range Cached() {
if p.ID == "lemon" && p.Icon != lemon.Icon {
t.Errorf("cached icon = %.60q", p.Icon)
}
}
}