Files
magpie/internal/gui/webview_data_test.go
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

45 lines
1.3 KiB
Go

//go:build !nogui
package gui
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/appdir"
)
// #508: a portable magpie on Windows still made %APPDATA%\magpie.exe,
// WebView2's profile at Wails' default. Portable, the app's options put it
// in data\webview2; installed, they leave the default (and what is there).
func TestWindowsOptionsWebViewData(t *testing.T) {
t.Setenv("APPIMAGE", "")
t.Cleanup(func() { appdir.UseExecutable("") })
r, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
appdir.UseExecutable("")
if o := windowsOptions(); o.WebviewUserDataPath != "" || !o.DisableQuitOnLastWindowClosed {
t.Fatalf("installed: %+v, want WebView2's default folder and no quit on last close", o)
}
exe := filepath.Join(r, "Magpie", "magpie.exe")
if err := os.MkdirAll(filepath.Join(r, "Magpie", "data"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(exe, nil, 0o755); err != nil {
t.Fatal(err)
}
appdir.UseExecutable(exe)
o := windowsOptions()
if want := filepath.Join(r, "Magpie", "data", "webview2"); o.WebviewUserDataPath != want {
t.Fatalf("portable: WebviewUserDataPath = %q, want %q", o.WebviewUserDataPath, want)
}
if !o.DisableQuitOnLastWindowClosed {
t.Fatal("portable: quits on last window closed")
}
}