Files
magpie/internal/shortcut/ensure_test.go
T
yetone fff1607a30 portable: a portable magpie on Windows keeps WebView2's profile in data\webview2 and makes no Start-menu shortcut, and an installed one makes the shortcut once and never puts back one the user deleted (#508, suixinex: 便携模式时还是会创建 C:\Users\<username>\AppData\Roaming\magpie.exe\ 文件夹; 开始菜单的快捷方式删除后再打开magpie会重新添加) — the AppData\Roaming\magpie.exe folder is WebView2's user data folder: Wails v3 passes WindowsOptions.WebviewUserDataPath to go-webview2 as its DataPath, and left empty it is %APPDATA%\<exe name>, which portable mode (appdir) never set. The app's Windows options (windowsOptions) now give it appdir.WebView(): data\webview2 when portable, "" (WebView2's default, so the pages' storage and cookies there are kept) when installed. The shortcut came back because shortcut.Ensure ran at every start of the desktop app and wrote Magpie.lnk whenever it was missing or stale, with nothing recording that it had been made. Ensure now does nothing when portable (no shortcut, no App Paths entry); installed, it makes the shortcut on the first start and records that in start-menu-shortcut in magpie's folder (with the exe), keeps one that is still there pointed at the exe (moved exe, update), and makes none once the file is recorded and the shortcut is gone. A user who deleted it under an older magpie isn't given it back on updating either: a missing shortcut with no record but an App Paths\magpie.exe entry already there (every older magpie wrote it at each start) counts as deleted and is recorded as such. The Start menu, the shortcut writer and App Paths are behind function variables, so the decision is tested with fakes on any OS. Checked for other leaks: autostart only refreshes an entry the user turned on (Refresh never enables), the magpie:// link registration is needed for import links and stays, as appdir's doc says, as do temp folders; the update stages beside the exe, caches already follow appdir. Not changed: the Mac's WKWebView and Linux's WebKitGTK keep their own data in the profile (Wails has no option for it), which #508 didn't report. Nothing ran on Windows (the test box isn't reachable): no real WebView2 was started with the data path and no real shortcut was made or deleted. TestEnsureOnce, TestEnsureAfterOlderMagpie, TestEnsurePortable (fake Start menu in a temp folder), TestWebView and TestWindowsOptionsWebViewData fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and vet ./..., GOOS=windows go test -c for shortcut, gui and appdir, go test -tags nogui -count=1 ./... under a temp HOME pass (TestUsageKeepsTimeToFirstToken and TestUserPath failed once on timing, pass rerun alone; untouched).
2026-10-02 14:39:49 +08:00

108 lines
3.4 KiB
Go

package shortcut
import (
"os"
"path/filepath"
"testing"
)
// fakeSystem is a Start menu in a temp folder: writeLink makes the file and
// counts, register records the App Paths entry.
type fakeSystem struct {
lnk string
writes int
paths []string // App Paths entries written
before bool // an older magpie registered itself
}
func fake(t *testing.T) (*fakeSystem, string) {
t.Helper()
dir := t.TempDir()
f := &fakeSystem{lnk: filepath.Join(dir, "Start Menu", "Programs", Name+".lnk")}
os.MkdirAll(filepath.Dir(f.lnk), 0o755)
oldMenu, oldWrite, oldReg, oldRegd := startMenu, writeLink, register, registered
t.Cleanup(func() { startMenu, writeLink, register, registered = oldMenu, oldWrite, oldReg, oldRegd })
startMenu = func() (string, error) { return f.lnk, nil }
writeLink = func(lnk, exe string) (bool, error) {
f.writes++
return true, os.WriteFile(lnk, []byte(exe), 0o644)
}
register = func(exe string) { f.paths = append(f.paths, exe) }
registered = func() bool { return f.before }
return f, filepath.Join(dir, "magpie", StateName)
}
func exists(p string) bool { _, err := os.Stat(p); return err == nil }
// #508: the shortcut is made on the first start, kept while it is there,
// and not made again once the user deleted it.
func TestEnsureOnce(t *testing.T) {
f, state := fake(t)
exe := `C:\Tools\magpie.exe`
if err := ensure(exe, "", state); err != nil {
t.Fatal(err)
}
if f.writes != 1 || !exists(f.lnk) || !exists(state) {
t.Fatalf("first start: %d writes, shortcut %v, state %v", f.writes, exists(f.lnk), exists(state))
}
// still there: kept pointed at this magpie (write leaves a good one be)
if err := ensure(exe, "", state); err != nil {
t.Fatal(err)
}
if f.writes != 2 {
t.Fatalf("second start with the shortcut there: %d writes, want 2", f.writes)
}
// the user deletes it
os.Remove(f.lnk)
for i := 0; i < 3; i++ {
if err := ensure(exe, "", state); err != nil {
t.Fatal(err)
}
}
if exists(f.lnk) || f.writes != 2 {
t.Fatalf("deleted shortcut came back: shortcut %v, %d writes", exists(f.lnk), f.writes)
}
if len(f.paths) != 5 {
t.Errorf("App Paths written %d times, want at each installed start (5)", len(f.paths))
}
}
// A user who deleted the shortcut under a magpie from before the state
// file (it registered App Paths at every start) doesn't get it back on
// updating; one who kept it has it kept.
func TestEnsureAfterOlderMagpie(t *testing.T) {
f, state := fake(t)
f.before = true
if err := ensure(`C:\Tools\magpie.exe`, "", state); err != nil {
t.Fatal(err)
}
if exists(f.lnk) || f.writes != 0 || !exists(state) {
t.Fatalf("deleted before the update: shortcut %v, %d writes, state %v", exists(f.lnk), f.writes, exists(state))
}
f, state = fake(t)
f.before = true
os.WriteFile(f.lnk, []byte("old"), 0o644)
if err := ensure(`C:\Tools\magpie.exe`, "", state); err != nil {
t.Fatal(err)
}
if f.writes != 1 || !exists(state) {
t.Fatalf("kept before the update: %d writes, state %v", f.writes, exists(state))
}
}
// #508: a portable magpie makes no shortcut, registers nothing and keeps
// no state.
func TestEnsurePortable(t *testing.T) {
f, state := fake(t)
for i := 0; i < 2; i++ {
if err := ensure(`E:\Magpie\magpie.exe`, `E:\Magpie\data`, state); err != nil {
t.Fatal(err)
}
}
if f.writes != 0 || exists(f.lnk) || len(f.paths) != 0 || exists(state) {
t.Fatalf("portable: %d writes, shortcut %v, App Paths %v, state %v", f.writes, exists(f.lnk), f.paths, exists(state))
}
}