MYCF711's #3608 (and plugins#394): a repository tagged magpie-plugin outside the community's list showed its repository's name and GitHub's description on its card under Discover → Unofficial, on GitHub, whatever its package.json said of itself. Ported from MYCF711's 1cc337001. Now readTagged keeps package.json's magpie.name (ownName, as main's row already does sinced8cf4f74f) and magpie.summary (ownSummary, by language), and npm's copy's when the card installs from npm and it says them. The card shows them in place of the repository's, with an "Its own words" chip whose tip says nobody reviewed them; no community badge, and they never resolve a name. GitHub's description stays the English line for a package whose summary has none. Changed from 1cc337001, reviewed line by line: - ownSummary also refuses control and format characters (a bidi override, a zero width space) and bad UTF-8, as main's ownName does. - magpie.summary is read as raw JSON: in 1cc337001 a summary that isn't an object of strings ("summary": "text") failed the whole package.json parse, and readTagged then left the repository out of the list. - npm's copy's words win over the repository's when npm's is the one installed; 1cc337001 read them from npm only for the NPM type, which no card shows. - Left out: the row chip and InstalledName/ownName on /api/plugins (main has them fromd8cf4f74f), NPM.Name/Summary (no page reads them), and the core budget raise: the card's code is paid for by cutting three comments in plugins.js, 1,533,388 of 1,533,802. Run (mote, at this tree on538026e62): - go test -count=1 ./internal/plugin ./internal/gui: internal/plugin ok (145s); internal/gui red only in TestMainWindowCorners/macOS_26_SDK, filed as #3949 (macOS 27) - go vet ./internal/plugin ./internal/gui, GOOS=linux -tags nogui and GOOS=windows: clean; gofmt -l: nothing. - node --test internal/gui/tests/plugin*.test.cjs, plugins-more-discover and i18n-keys, Chromium and WebKit: 1028 of 1032; the 4 reds are plugin-move-unserved's "Try again takes the models unticked" (en, zh, both engines), filed as #4410 and #4392 - plugin-own-words.test.cjs: en, zh, zh-TW, ja, de at 980 and 420, both engines, 22 of 22; with main's plugins.js 0 of 22 ('magpie-officeace' for 'OfficeAce'). After the rebase on823e7c250(mote): TestCoreScriptBudget ok, go test -run 'OwnWords|Tagged' ./internal/plugin ok, plugin-own-words, i18n-keys and plugins-more-discover 32 of 32. Broken by hand: - npm's words not taken: TestReadTaggedOwnWords red (name "OfficeAce dev"). - summary read as map[string]string: TestReadTaggedOwnWords red (the package with "summary": "text" not kept). - ownSummary without the character check: TestOwnWordsBounds red (two lines, tab, bidi override, zero width). Not tried: a real repository with magpie.summary (none tagged has one); the desktop window itself. Co-authored-by: MYCF711 <323101370+MYCF711@users.noreply.github.com>
116 lines
5.1 KiB
Go
116 lines
5.1 KiB
Go
package plugin
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"reflect"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// A repository's own package.json says what the package is called and
|
||
// what it is (magpie.name, magpie.summary), read with the rest of it: its
|
||
// card under Discover → Unofficial shows the package's own words rather
|
||
// than the repository's name and GitHub's description (MYCF711, #3608).
|
||
// npm's copy, the one the card installs, says them when it says any.
|
||
func TestReadTaggedOwnWords(t *testing.T) {
|
||
read := func(pkg, npm string) Tagged {
|
||
t.Helper()
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
switch {
|
||
case strings.HasSuffix(r.URL.Path, "/package.json"):
|
||
w.Write([]byte(pkg))
|
||
case strings.HasPrefix(r.URL.Path, "/npm/"):
|
||
if npm == "" {
|
||
http.Error(w, `"Not Found"`, 404)
|
||
return
|
||
}
|
||
w.Write([]byte(npm))
|
||
default:
|
||
w.Write([]byte("module.exports = {}"))
|
||
}
|
||
}))
|
||
defer srv.Close()
|
||
raw, reg := githubRaw, npmRegistry
|
||
githubRaw, npmRegistry = srv.URL+"/raw", srv.URL+"/npm"
|
||
defer func() { githubRaw, npmRegistry = raw, reg }()
|
||
got, keep, known, err := readTagged(context.Background(), Tagged{Spec: "github:someone/magpie-officeace", Repo: "someone/magpie-officeace"}, "main")
|
||
if err != nil || !keep || !known {
|
||
t.Fatalf("read %s: %v kept %v known %v", pkg, err, keep, known)
|
||
}
|
||
return got
|
||
}
|
||
both := map[string]string{"en": "Unofficial: OfficeAce", "zh": "非官方:OfficeAce"}
|
||
|
||
got := read(`{"name":"magpie-officeace","version":"0.3.0","main":"index.js","magpie":{"name":"OfficeAce","summary":{"en":"Unofficial: OfficeAce","zh":"非官方:OfficeAce"}}}`, "")
|
||
if got.Name != "OfficeAce" || !reflect.DeepEqual(got.Summary, both) {
|
||
t.Errorf("repository's words: name %q summary %v", got.Name, got.Summary)
|
||
}
|
||
|
||
// published from this repository: npm's copy is installed, and its
|
||
// words are the card's
|
||
got = read(`{"name":"magpie-officeace","version":"0.3.0","main":"index.js","magpie":{"name":"OfficeAce dev","summary":{"en":"dev"}}}`,
|
||
`{"version":"0.2.0","main":"index.js","repository":"github:someone/magpie-officeace","magpie":{"name":"OfficeAce","summary":{"en":"Unofficial: OfficeAce","zh":"非官方:OfficeAce"}}}`)
|
||
if got.Spec != "magpie-officeace" || got.Name != "OfficeAce" || !reflect.DeepEqual(got.Summary, both) {
|
||
t.Errorf("npm's words: spec %q name %q summary %v", got.Spec, got.Name, got.Summary)
|
||
}
|
||
// npm's copy saying nothing of itself leaves the repository's
|
||
got = read(`{"name":"magpie-officeace","version":"0.3.0","main":"index.js","magpie":{"name":"OfficeAce","summary":{"en":"Unofficial: OfficeAce","zh":"非官方:OfficeAce"}}}`,
|
||
`{"version":"0.2.0","main":"index.js","repository":"github:someone/magpie-officeace"}`)
|
||
if got.Spec != "magpie-officeace" || got.Name != "OfficeAce" || !reflect.DeepEqual(got.Summary, both) {
|
||
t.Errorf("npm silent: spec %q name %q summary %v", got.Spec, got.Name, got.Summary)
|
||
}
|
||
|
||
// a package.json that says nothing of itself keeps the card it had
|
||
got = read(`{"name":"magpie-officeace","version":"1.0.0","main":"index.js"}`, "")
|
||
if got.Name != "" || got.Summary != nil {
|
||
t.Errorf("saying nothing: name %q summary %v", got.Name, got.Summary)
|
||
}
|
||
// a summary that isn't a map of strings is none, and the package is
|
||
// still listed: never a failed read of the whole package.json
|
||
got = read(`{"name":"magpie-officeace","version":"1.0.0","main":"index.js","magpie":{"name":"OfficeAce","summary":"one sentence"}}`, "")
|
||
if got.Name != "OfficeAce" || got.Summary != nil {
|
||
t.Errorf("summary a string: name %q summary %v", got.Name, got.Summary)
|
||
}
|
||
}
|
||
|
||
// magpie.summary is a sentence a language on one line, as written, and
|
||
// nothing that could make it read as other text.
|
||
func TestOwnWordsBounds(t *testing.T) {
|
||
sum := func(v any) map[string]string {
|
||
b, _ := json.Marshal(v)
|
||
return ownSummary(b)
|
||
}
|
||
if got := sum(map[string]string{"en": " OfficeAce for Word \n", "zh": "非官方"}); !reflect.DeepEqual(got, map[string]string{"en": "OfficeAce for Word", "zh": "非官方"}) {
|
||
t.Errorf("trimmed: %v", got)
|
||
}
|
||
for name, said := range map[string]any{
|
||
"two lines": map[string]string{"en": "one\ntwo"},
|
||
"tab": map[string]string{"en": "a\tb"},
|
||
"bidi override": map[string]string{"en": "Qoder"},
|
||
"zero width": map[string]string{"en": "zerowidth"},
|
||
"too long": map[string]string{"en": strings.Repeat("a", 301)},
|
||
"empty": map[string]string{"en": " "},
|
||
"long language": map[string]string{"thirteen-char": "x"},
|
||
"no language": map[string]string{" ": "x"},
|
||
"not a string": map[string]any{"en": 3},
|
||
"not an object": "x",
|
||
"a list": []string{"x"},
|
||
} {
|
||
if got := sum(said); got != nil {
|
||
t.Errorf("%s: %v, want none", name, got)
|
||
}
|
||
}
|
||
if got := ownSummary(json.RawMessage(`{"en":"bad \xff utf-8"}`)); got != nil {
|
||
t.Errorf("bad utf-8: %v", got)
|
||
}
|
||
if got := sum(map[string]string{"en": strings.Repeat("a", 300), "zh": "x\ny"}); len(got) != 1 || len(got["en"]) != 300 {
|
||
t.Errorf("one good language of two: %v", got)
|
||
}
|
||
if got := ownSummary(nil); got != nil {
|
||
t.Errorf("none: %v", got)
|
||
}
|
||
}
|