Files
magpie/internal/update/notes_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

111 lines
3.9 KiB
Go

package update
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
// What changed is shown after an upgrade between releases only.
func TestShowNotes(t *testing.T) {
for _, c := range []struct {
name, last, current string
existed bool
after string
show bool
}{
{"upgrade", "0.1.600", "0.1.604", true, "0.1.600", true},
{"upgrade, v kept", "v0.1.600", "0.1.604", true, "0.1.600", true},
{"minor upgrade", "0.1.604", "0.2.0", true, "0.1.604", true},
{"fresh install", "", "0.1.604", false, "", false},
{"from before versions were kept", "", "0.1.604", true, "", true},
{"same version", "0.1.604", "0.1.604", true, "", false},
{"downgrade", "0.1.604", "0.1.600", true, "", false},
{"source build", "0.1.600", "dev", true, "", false},
{"source build after a release", "0.1.600", "v0.1.604-3-g0bcb2cc", true, "", false},
{"last unreadable", "dev", "0.1.604", true, "", false},
} {
after, show := ShowNotes(c.last, c.current, c.existed)
if after != c.after || show != c.show {
t.Errorf("%s: ShowNotes(%q, %q, %v) = %q, %v; want %q, %v", c.name, c.last, c.current, c.existed, after, show, c.after, c.show)
}
}
}
func TestStripInstall(t *testing.T) {
md := "## Bug Fixes\n\n- Fixed a thing. (#464)\n\n### Install\n\nDownload from [usemagpie.ai](https://usemagpie.ai).\n\n- **macOS:** `magpie.dmg`\n"
if got, want := StripInstall(md), "## Bug Fixes\n\n- Fixed a thing. (#464)"; got != want {
t.Errorf("got %q, want %q", got, want)
}
// a section after it at its level or above stays
md = "## Install\n\nlinks\n\n### more links\n\n## New Features\n\n- A feature. (#1)"
if got, want := StripInstall(md), "## New Features\n\n- A feature. (#1)"; got != want {
t.Errorf("got %q, want %q", got, want)
}
if got := StripInstall("- Install the CLI faster. (#2)"); got != "- Install the CLI faster. (#2)" {
t.Errorf("a bullet naming Install was taken: %q", got)
}
}
func TestBetween(t *testing.T) {
notes := []Note{
{Version: "0.1.598", Notes: "- old"},
{Version: "v0.1.604", Notes: "- four\n\n### Install\n\nlinks"},
{Version: "0.1.605", Notes: "- not out here yet"},
{Version: "0.1.601", Notes: "- one"},
{Version: "0.1.600", Notes: "- zero"},
{Version: "0.1.602", Notes: "### Install\n\nlinks"}, // nothing left
{Version: "0.1.603", Notes: "- three"},
}
vs := func(ns []Note) (out []string) {
for _, n := range ns {
out = append(out, n.Version)
}
return out
}
got := Between(notes, "0.1.600", "0.1.604")
if want := []string{"0.1.604", "0.1.603", "0.1.601"}; !reflect.DeepEqual(vs(got), want) {
t.Fatalf("got %v, want %v", vs(got), want)
}
if got[0].Notes != "- four" {
t.Errorf("Install kept: %q", got[0].Notes)
}
if got := Between(notes, "", "0.1.604"); !reflect.DeepEqual(vs(got), []string{"0.1.604"}) {
t.Errorf("current alone: %v", vs(got))
}
if got := Between(notes, "0.1.604", "0.1.604"); len(got) != 0 {
t.Errorf("nothing between: %v", vs(got))
}
}
func TestNotesBetween(t *testing.T) {
var asked string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked = r.URL.RawQuery
json.NewEncoder(w).Encode(map[string]any{"releases": []Note{
{Version: "0.1.604", Notes: "- four", URL: "https://example.com/4"},
{Version: "0.1.600", Notes: "- zero"},
{Version: "0.1.602", Notes: "- two"},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_NOTES_FEED", srv.URL)
got, err := NotesBetween(context.Background(), "0.1.600", "0.1.604", "")
if err != nil {
t.Fatal(err)
}
if asked != "after=0.1.600&upto=0.1.604" {
t.Errorf("asked %q", asked)
}
if len(got) != 2 || got[0].Version != "0.1.604" || got[1].Version != "0.1.602" || got[0].URL != "https://example.com/4" {
t.Errorf("%+v", got)
}
srv.Close()
if _, err := NotesBetween(context.Background(), "0.1.600", "0.1.604", ""); err == nil {
t.Error("no error offline")
}
}