Files
magpie/internal/library/market_more_test.go
T
yetone df0320156b library: the skill market offers every skill skills.sh lists, 60 at a time
The owner: the Library's markets had too little in them ("这两个市场的
东西都太少了"). The skill market showed at most 60, four a repository.

Now it offers every skill on skills.sh's front page (its 600 most
installed), then each one on its trending page that isn't there, with no
count of installs: trending counts recent installs only, so the card
shows none rather than a wrong one. A repository's first skill comes
before anyone's second, so the first page still isn't one author's, and
nothing is cut. A search asks skills.sh for 200, not 40. The snapshot
magpie ships with has the same 772.

The page shows 60 cards and a "Show more (N left)" button that adds 60
under them; what was shown stays where it is, and skills' about is asked
for the cards shown only. MCP's list pages the same way. Strings in zh,
zh-TW, ja and de.

Run on mote at 0de6f2e57 (origin/main 1d8ded3ec plus this; pushed on 7c712847b, an agent row only): go test
./internal/library ok, gofmt clean, and the GUI files i18n-keys, market-*
and every-skill in both engines, 88/88. Main had changed internal/library
twice since the base below (399385307, 655d23f79).

At 073b3dfbb (origin/main c991733dc plus this):
- gofmt -l internal/library internal/gui: clean; go vet ./internal/library
- go test ./internal/library: ok
- go test -skip TestMainWindowCorners ./internal/gui: ok. That test is
  red on origin/main too (#3949).
- GUI: i18n-keys, market-*, every-skill in Chromium and WebKit: 88/88.
  i18n-keys was red at first, from a Go module cache an earlier run of
  mine had left in mote's copy of the tree; with it removed, 4/4.
- At 098f28929 (origin/main 5dfbca627 plus this), the wider set:
  i18n-keys, market-*, library-*, skeleton-bird, search-mcp-library,
  skill-install-agents-all, every-skill in both engines, 1128/1128.
  Between that base and this one, main changed only internal/library's
  MCP hub, browser and gateway code; none of it is in these pages.
- An earlier run of the same files at 647323409 plus this, with mote at
  load ~130, had 6 reds. All three files pass alone, both on main and on
  this change (110/110), and each has an issue: #4756
  (search-mcp-library), #4483 (library-mcp-hub) and #4125 (browser
  launch).

Broken by hand, red each time: the 4-per-repository cap back
(TestMarketSkillsAll), search limit 40, no trending page, and installs
shown for a trending skill (market-more.test.cjs). Removing the button
before the new cards are put in still passes, because the click scroll
keeper holds the reader's card. So nothing claims that order matters.

Not tried: the real skills.sh from the app. fetchPopular's parsing is the
one it had; the snapshot was made from the live pages today.
2026-11-01 17:00:04 +08:00

105 lines
3.4 KiB
Go

package library
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
)
// The owner: the skill market had too little in it. Every skill skills.sh's
// front page lists is offered, then its trending page's that aren't there
// (with no count, theirs being only recent installs), each repository's
// most installed first and its others after, and a search asks for 200.
func TestMarketSkillsAll(t *testing.T) {
sandbox(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
popular.Lock()
popular.list = nil
popular.Unlock()
t.Cleanup(func() { popular.Lock(); popular.list = nil; popular.Unlock() })
var front, trending []string
// big/r has 100 skills, the most installed; 50 other repositories one each
for i := range 100 {
front = append(front, fmt.Sprintf(`{"source":"big/r","skillId":"b%d","name":"b%d","installs":%d}`, i, i, 100000-i))
}
for i := range 50 {
front = append(front, fmt.Sprintf(`{"source":"o%d/r","skillId":"s","name":"s","installs":%d}`, i, 1000-i))
}
// trending: one on the front page already, two new
trending = append(trending, `{"source":"big/r","skillId":"b3","name":"b3","installs":7}`,
`{"source":"new/r","skillId":"t1","name":"t1","installs":40}`, `{"source":"new/r","skillId":"t2","name":"t2","installs":30}`)
page := func(list []string) []byte {
js, _ := json.Marshal(`{"initialSkills":[` + strings.Join(list, ",") + `]}`)
return []byte(`<html><script>self.__next_f.push([1,` + string(js) + `])</script></html>`)
}
var limit string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
w.Write(page(front))
case "/trending":
w.Write(page(trending))
case "/api/search":
limit = r.URL.Query().Get("limit")
w.Write([]byte(`{"skills":[]}`))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
was := skillsShURL
skillsShURL = srv.URL
t.Cleanup(func() { skillsShURL = was })
list, err := MarketSkills("")
if err != nil {
t.Fatal(err)
}
var ids []string
for _, m := range list {
if !m.Featured {
ids = append(ids, m.ID)
}
}
if len(ids) != 152 {
t.Fatalf("%d offered, want all 152: %v", len(ids), ids)
}
// the first round has each repository's most installed: big/r's b0,
// the 50 others, new/r's t1; then big/r's b1 and new/r's t2
if ids[0] != "big/r/b0" || ids[1] != "o0/r/s" || ids[50] != "o49/r/s" || ids[51] != "new/r/t1" || ids[52] != "big/r/b1" || ids[53] != "new/r/t2" || ids[151] != "big/r/b99" {
t.Fatalf("order: %v", ids[:60])
}
i := slices.IndexFunc(list, func(m MarketSkill) bool { return m.ID == "new/r/t1" })
j := slices.IndexFunc(list, func(m MarketSkill) bool { return m.ID == "big/r/b3" })
if list[i].Installs != 0 || list[j].Installs != 100000-3 {
t.Fatalf("installs: trending %d, front page's %d", list[i].Installs, list[j].Installs)
}
// a search: the matches among them, all of them, and skills.sh asked for 200
list, _ = MarketSkills("big")
if len(list) != 100 || limit != "200" {
t.Fatalf("search: %d, limit %q", len(list), limit)
}
// the trending page failing leaves the front page's
popular.Lock()
popular.list = nil
popular.Unlock()
trending = nil
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Write(page(front))
return
}
http.Error(w, "", http.StatusBadGateway)
})
if list, err := fetchPopular(); err != nil || len(list) != 150 {
t.Fatalf("no trending: %d %v", len(list), err)
}
}