Files
magpie/internal/update/lang_test.go
T
yetone aa42142ea5 update: What's new and the waiting update's notes follow the app's language (freecss on Discord: 更新后的公告的语言是不是应该设置里面的走) — the release notes were English alone. A release's notes are to carry a Simplified Chinese translation below a <!-- lang:zh --> marker (the magpie-releases workflow, changed separately); usemagpie.ai's /api/latest and /api/notes take ?lang=zh for the Chinese where a release has it, and give anything else, an app from before included, the English alone, everything above the marker. The page asks /api/whatsnew, /api/update and /api/update/check in its own language (?lang=); magpie asks the site in it, keeps What's new's notes per language, and asks again for the waiting update's notes when a page's language isn't the one they are held in (at most once a minute); a check by itself asks in the pages' last language, else the app's, as the tray menu has it. Notes that come back whole (a site not yet deployed) are cut in the app too; notes without the marker (every release so far) are English, in either language. The worker must be deployed (npx wrangler deploy in site/) before the workflow change ships, or apps would get both languages. TestInLang, TestFeedsAskInLang, TestAskedLang, TestWhatsNewInPageLang, TestUpdateNotesInPageLang, site/worker.test.mjs (node --test site/worker.test.mjs), whats-new.test.cjs's language check (Chromium and WebKit, en and zh); update-check, update-hide, update-auto, sync-update pass in WebKit. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (TestClaudeRunEndsWhenTheClientLeaves flaked once under the full run, passes alone; gateway untouched).
2026-10-02 11:31:11 +08:00

73 lines
2.4 KiB
Go

package update
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
const (
enNotes = "### Features\n\n- One thing (#1)\n\n### Install\n\nDownload it."
zhNotes = "### 新功能\n\n- 一件事 (#1)"
twoNotes = enNotes + "\n\n<!-- lang:zh -->\n\n" + zhNotes + "\n"
)
// A release's notes are English, then Chinese below the marker (freecss on
// Discord: What's new should follow Settings' language). Notes from before
// are English alone, in any language.
func TestInLang(t *testing.T) {
for _, c := range []struct{ md, lang, want string }{
{twoNotes, "zh", zhNotes},
{twoNotes, "en", enNotes},
{twoNotes, "", enNotes},
{"- old\n", "zh", "- old\n"},
{enNotes + "\n<!-- lang:zh -->\n \n", "zh", enNotes}, // no Chinese after all
{zhNotes, "zh", zhNotes}, // the site cut them already
} {
if got := InLang(c.md, c.lang); got != c.want {
t.Errorf("InLang(%q, %q) = %q, want %q", c.md, c.lang, got, c.want)
}
}
}
// The feed and the notes are asked in the language, and whatever comes
// back whole (a site from before it cut them) is cut here.
func TestFeedsAskInLang(t *testing.T) {
var asked []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked = append(asked, r.URL.Path+"?"+r.URL.RawQuery)
if r.URL.Path == "/notes" {
json.NewEncoder(w).Encode(map[string]any{"releases": []Note{{Version: "0.1.604", Notes: twoNotes}, {Version: "0.1.603", Notes: "- three"}}})
return
}
json.NewEncoder(w).Encode(Release{Version: "0.1.604", Notes: twoNotes})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL+"/latest")
t.Setenv("MAGPIE_NOTES_FEED", srv.URL+"/notes")
ctx := context.Background()
rel, err := LatestIn(ctx, "zh")
if err != nil || rel.Notes != zhNotes {
t.Fatalf("zh: %+v %v", rel, err)
}
if rel, err = Latest(ctx); err != nil || rel.Notes != enNotes {
t.Fatalf("no lang: %+v %v", rel, err)
}
notes, err := NotesBetween(ctx, "0.1.602", "0.1.604", "zh")
if err != nil || len(notes) != 2 || notes[0].Notes != zhNotes || notes[1].Notes != "- three" {
t.Fatalf("notes: %+v %v", notes, err)
}
want := []string{"/latest?lang=zh", "/latest?", "/notes?after=0.1.602&lang=zh&upto=0.1.604"}
if len(asked) != len(want) {
t.Fatalf("asked %q", asked)
}
for i := range want {
if asked[i] != want[i] {
t.Errorf("asked %q, want %q", asked[i], want[i])
}
}
}