Files
magpie/internal/gui/whatsnew_test.go
T
yetone f22218a092 gui: after an update the window shows what changed since the version last run (a Discord user: 点更新后能弹个窗显示更新了啥吗, to see whether their issue was fixed) — magpie updated itself silently, and its release notes were only on GitHub. The version last run is now kept in magpie's own folder (last-version); started on a newer release, the window or magpie web's page asks usemagpie.ai's new /api/notes (beside /api/latest; site/worker.js lists the releases' notes from GitHub, cached as the feed is) for every release since, newest first, without their Install section, and shows them once in a What's new dialog. A fresh install, the same version, a downgrade or a build from source shows nothing; a magpie from before the version was kept, already in use, shows its own version's notes; offline nothing is shown and nothing waits on it; until the site is deployed, the update feed's newest notes stand in when they are the current version's. The tray's panel shows no dialog. The notes are drawn as text, never as HTML: headings, bullets, bold, code, http(s) links, and #NNN links to the issue, opened in the browser as the app's other links are. Settings has a What's new row under Version that opens them again, with a waiting update's notes first, marked Not installed yet; /api/update's notes leave out the Install section too. TestShowNotes, TestStripInstall, TestBetween, TestNotesBetween, TestWhatsNewAfterUpgrade, TestWhatsNewFromBefore, TestUpdateNotesWithoutInstall and whats-new.test.cjs (Chromium, WebKit, en, zh) fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit, update-check, update-hide pass.
2026-10-02 03:57:55 +08:00

121 lines
3.9 KiB
Go

package gui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/update"
)
// The version last run is kept in magpie's folder, and the notes since are
// shown once after an upgrade: not on a fresh install, nor offline.
func TestWhatsNewAfterUpgrade(t *testing.T) {
home := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", home)
dir := filepath.Join(home, "magpie")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{"releases": []update.Note{
{Version: "0.1.604", Notes: "- four (#464)\n\n### Install\n\nlinks"},
{Version: "0.1.603", Notes: "- three"},
{Version: "0.1.600", Notes: "- zero"},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_NOTES_FEED", srv.URL)
t.Setenv("MAGPIE_UPDATE_FEED", "http://127.0.0.1:1/")
old := Version
defer func() { Version = old }()
last := func() string {
b, _ := os.ReadFile(filepath.Join(dir, lastRunFile))
return strings.TrimSpace(string(b))
}
run := func(v string) (*whatsNew, whatsNewJSON) {
Version = v
n := &whatsNew{}
n.start()
return n, n.get(context.Background(), false)
}
// a fresh install: nothing shown, the version kept
if _, j := run("0.1.600"); j.Show || len(j.Releases) != 0 {
t.Fatalf("fresh install: %+v", j)
}
if last() != "0.1.600" {
t.Fatalf("kept %q", last())
}
// the same again: nothing
if _, j := run("0.1.600"); j.Show {
t.Fatalf("same version: %+v", j)
}
// upgraded: what came since, newest first, without Install
n, j := run("0.1.604")
if !j.Show || len(j.Releases) != 2 || j.Releases[0].Version != "0.1.604" || j.Releases[1].Version != "0.1.603" || j.Releases[0].Notes != "- four (#464)" {
t.Fatalf("upgrade: %+v", j)
}
if last() != "0.1.604" {
t.Fatalf("kept %q", last())
}
// shown once; Settings still opens them
n.seen()
if j := n.get(context.Background(), false); j.Show || len(j.Releases) != 0 {
t.Fatalf("after seen: %+v", j)
}
if j := n.get(context.Background(), true); j.Show || len(j.Releases) != 2 {
t.Fatalf("asked again: %+v", j)
}
// a downgrade: nothing
if _, j := run("0.1.603"); j.Show {
t.Fatalf("downgrade: %+v", j)
}
// a build from source leaves the release it came after
if _, j := run("dev"); j.Show || last() != "0.1.603" {
t.Fatalf("dev: %+v, kept %q", j, last())
}
// offline: nothing shown, nothing in the way
srv.Close()
if _, j := run("0.1.604"); j.Show || len(j.Releases) != 0 {
t.Fatalf("offline: %+v", j)
}
}
// The waiting update's notes, shown before it is installed, leave out the
// download links.
func TestUpdateNotesWithoutInstall(t *testing.T) {
u := &updater{state: "ready", latest: &update.Release{Version: "0.1.605", Notes: "## New Features\n\n- A thing. (#470)\n\n### Install\n\nDownload it."}}
if j := u.json(); j.Notes != "## New Features\n\n- A thing. (#470)" {
t.Fatalf("%q", j.Notes)
}
}
// A magpie from before the version was kept, already in use, shows its own
// version's notes; the site without the list yet leaves the update feed's.
func TestWhatsNewFromBefore(t *testing.T) {
home := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", home)
dir := filepath.Join(home, "magpie")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "settings.json"), []byte("{}"), 0o644)
os.Chtimes(filepath.Join(dir, "settings.json"), started.Add(-time.Hour), started.Add(-time.Hour))
feed := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(update.Release{Version: "0.1.604", Notes: "## Bug Fixes\n\n- four"})
}))
defer feed.Close()
t.Setenv("MAGPIE_UPDATE_FEED", feed.URL)
t.Setenv("MAGPIE_NOTES_FEED", feed.URL+"/404")
old := Version
defer func() { Version = old }()
Version = "0.1.604"
n := &whatsNew{}
n.start()
if j := n.get(context.Background(), false); !j.Show || len(j.Releases) != 1 || j.Releases[0].Notes != "## Bug Fixes\n\n- four" {
t.Fatalf("%+v", j)
}
}