Files
magpie/internal/gui/privacymodel_test.go
T
yetone 958d4fcaf1 pii: the local models' folder is chosen under Advanced, and Move… moves them (MetalHqa on X)
On Windows both models (Privacy Filter 1.6 GB, AstrLink Guard 39 MB)
went onto the C: drive with no way to choose. Settings > Privacy now has
Advanced > Models folder: Move… picks a folder in the system's picker and
moves what is downloaded there; Move back returns it to magpie's cache.
The default is unchanged.

- settings.RedactModelDir is this computer's own (kept by KeepOwn, never
  set by a settings save).
- pii.Root() is the one place every path is made from (dir, guardRoot,
  so the model, its cache files, pf-serve, the CoreML program).
- pii.MoveTo: a rename on one disk, else a copy checked by sha256 read
  back; the setting switches after it and the old subfolders are deleted
  last (never the picked folder); a failure puts back what it moved and
  keeps the old copy. Refuses a relative path, an unwritable folder and
  one inside what is moved. A picked folder that has gone (disk
  unplugged) is not recreated by a download. Download and Remove wait
  out a move.
- GUI: pmfolder.js, loaded on first draw so the core script is no bigger;
  strings in zh, zh-TW, ja, de. Docs: gateway-routing.md, masking.html
  (en, zh, ja), site index regenerated with docs-index.mjs.

Run (mica; mote had no room, load 35/18 and 29/12):
- go test of internal/pii folder tests, internal/gui TestPrivacyModel*,
  internal/settings and settingsapi keep tests: ok. Each broken by hand
  and red: guardRoot ignoring the folder, no rollback, no missing-folder
  check, KeepOwn line removed, settingsapi line removed, folder action
  not calling MoveTo.
- Windows box (win-run): the move tests ok, TestMoveAcrossDisks on a real
  C: to D: move ok.
- GUI privacy-model.test.cjs, Chromium and WebKit, 5 languages, 1100 and
  640px: 104 of 104.
- go vet on pii, gui, settings, settingsapi for linux (nogui), windows
  and darwin: clean. node --test site/*.test.mjs: 135 of 135.

Not run before this push: the whole GUI suite, go test ./... (both
follow at the pushed head), the size-budget and i18n tests (run after the
rebase), gofmt over the whole tree (touched files only), and a real
Privacy Filter download into a picked folder on a real account.
2026-11-20 00:12:34 +08:00

305 lines
11 KiB
Go

package gui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/pii"
"github.com/yetone/magpie/internal/redact"
"github.com/yetone/magpie/internal/settings"
)
// The local masking model is on only once it is on disk: turned on where
// it can't be downloaded, nothing is saved; the Settings page's own save
// never turns it on or off; and off is saved at once.
func TestPrivacyModelOnlyOnOnceDownloaded(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
srv := webPageHandler(t, webHost{func() {}}, nil)
do := func(method, path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
return rec
}
var st privacyModelJSON
if rec := do("GET", "/api/privacy-model", ""); rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil {
t.Fatalf("state: %d %s", rec.Code, rec.Body)
}
// on by default, not by the user, and not downloaded yet
if !st.On || st.Chosen || st.Installed || st.Size == 0 {
t.Fatalf("state %+v", st)
}
if !pii.Supported() {
if rec := do("POST", "/api/privacy-model", `{"do":"on","model":"privacy-filter"}`); rec.Code == http.StatusOK {
t.Fatalf("on where it can't run: %d %s", rec.Code, rec.Body)
}
if settings.Load().RedactModel {
t.Fatal("saved on with no model")
}
}
if rec := do("POST", "/api/privacy-model", `{"do":"on!"}`); rec.Code == http.StatusOK {
t.Fatalf("a bad do: %d", rec.Code)
}
if rec := do("POST", "/api/settings", `{"theme":"dark","lang":"en","redactModel":true}`); rec.Code != http.StatusOK {
t.Fatalf("settings save: %d %s", rec.Code, rec.Body)
}
if settings.Load().RedactModel {
t.Fatal("the Settings page's save turned it on")
}
if err := settings.SetRedactModel(true); err != nil {
t.Fatal(err)
}
if rec := do("POST", "/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK {
t.Fatalf("settings save: %d %s", rec.Code, rec.Body)
}
if !settings.Load().RedactModel {
t.Fatal("the Settings page's save turned it off")
}
if rec := do("POST", "/api/privacy-model", `{"do":"off"}`); rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil || st.On {
t.Fatalf("off: %d %s", rec.Code, rec.Body)
}
if st := settings.Load(); st.RedactModel || !st.RedactModelOff {
t.Fatal("off not saved")
}
}
// The model on by default whose download is cancelled is turned off, the
// user's own off: the next request would start the download again.
func TestPrivacyModelCancelledDefaultIsOff(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
srv := webPageHandler(t, webHost{func() {}}, nil)
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/privacy-model", strings.NewReader(`{"do":"cancel"}`)))
var st privacyModelJSON
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil || st.On {
t.Fatalf("cancel: %d %s", rec.Code, rec.Body)
}
if !settings.Load().RedactModelOff {
t.Fatal("cancelled default not saved off")
}
}
// fakeModel is a pii.Model that is on disk, or downloads at once, and
// says what was done to it.
type fakeModel struct {
id string
installed bool
did *[]string
}
func (f *fakeModel) ID() string { return f.id }
func (f *fakeModel) Supported() bool { return true }
func (f *fakeModel) Installed() bool { return f.installed }
func (f *fakeModel) MinScore() float64 { return 0.5 }
func (f *fakeModel) Size() int64 { return 1 }
func (f *fakeModel) Find(context.Context, []string) ([][]redact.Found, error) {
return nil, nil
}
func (f *fakeModel) Now() pii.Status {
return pii.Status{Model: f.id, Supported: true, Installed: f.installed, Size: 1}
}
func (f *fakeModel) Download(done func()) error {
*f.did = append(*f.did, "download "+f.id)
f.installed = true
done()
return nil
}
func (f *fakeModel) Cancel() {}
func (f *fakeModel) Warm() { *f.did = append(*f.did, "warm "+f.id) }
func (f *fakeModel) Stop() { *f.did = append(*f.did, "stop "+f.id) }
func (f *fakeModel) Remove() error {
*f.did = append(*f.did, "remove "+f.id)
f.installed = false
return nil
}
// A new user's local model is AstrLink Guard; one picked while it is off
// is kept for turning it on, and one picked while it is on is downloaded
// and then masks, the one before let go.
func TestPrivacyModelPick(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
var did []string
fakes := map[string]*fakeModel{
pii.PrivacyFilter: {id: pii.PrivacyFilter, did: &did},
pii.AstrLinkGuard: {id: pii.AstrLinkGuard, did: &did},
}
wasOf, wasModels := modelOf, models
t.Cleanup(func() { modelOf, models = wasOf, wasModels })
modelOf = func(id string) pii.Model { return fakes[id] }
models = func() []pii.Model { return []pii.Model{fakes[pii.PrivacyFilter], fakes[pii.AstrLinkGuard]} }
srv := webPageHandler(t, webHost{func() {}}, nil)
do := func(body string) privacyModelJSON {
t.Helper()
rec := httptest.NewRecorder()
method := "POST"
if body == "" {
method = "GET"
}
srv.ServeHTTP(rec, httptest.NewRequest(method, "/api/privacy-model", strings.NewReader(body)))
var st privacyModelJSON
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil {
t.Fatalf("%s: %d %s", body, rec.Code, rec.Body)
}
return st
}
if st := do(""); !st.On || st.Chosen || st.Model != pii.AstrLinkGuard || len(st.Models) != 2 || st.Models[0].Model != pii.PrivacyFilter {
t.Fatalf("a new user's state: %+v", st)
}
if st := do(`{"do":"off"}`); st.On || !settings.Load().RedactModelOff {
t.Fatalf("turned off: %+v", st)
}
did = nil
if st := do(`{"do":"pick","model":"privacy-filter"}`); st.On || st.Model != pii.PrivacyFilter || len(did) != 0 {
t.Fatalf("picked while off: %+v, %v", st, did)
}
if st := do(`{"do":"on"}`); !st.On || st.Model != pii.PrivacyFilter || settings.Load().RedactModelName != pii.PrivacyFilter {
t.Fatalf("on: %+v", st)
}
did = nil
if st := do(`{"do":"pick","model":"astrlink-guard"}`); !st.On || st.Model != pii.AstrLinkGuard {
t.Fatalf("picked while on: %+v", st)
}
if strings.Join(did, ", ") != "download astrlink-guard, stop privacy-filter, warm astrlink-guard" {
t.Fatalf("did %v", did)
}
// the threshold: the model's own until one is set, for the model
// masking uses with none named; 0 goes back to its own, and one out
// of range is refused
if st := do(""); st.MinScore != 0.5 || st.DefaultScore != 0.5 {
t.Fatalf("threshold before one is set: %+v", st)
}
if st := do(`{"do":"score","score":0.9}`); st.MinScore != 0.9 || st.DefaultScore != 0.5 || settings.Load().RedactModelScore[pii.AstrLinkGuard] != 0.9 {
t.Fatalf("threshold set: %+v, %v", st, settings.Load().RedactModelScore)
}
if st := do(`{"do":"score","model":"privacy-filter","score":0.6}`); st.MinScore != 0.9 || settings.Load().RedactModelScore[pii.PrivacyFilter] != 0.6 {
t.Fatalf("another model's threshold: %+v, %v", st, settings.Load().RedactModelScore)
}
for _, bad := range []string{`0.3`, `1`, `-1`} {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/privacy-model", strings.NewReader(`{"do":"score","score":`+bad+`}`)))
if rec.Code == http.StatusOK || settings.Load().RedactModelScore[pii.AstrLinkGuard] != 0.9 {
t.Fatalf("threshold %s: %d", bad, rec.Code)
}
}
if st := do(`{"do":"score","score":0}`); st.MinScore != 0.5 || len(settings.Load().RedactModelScore) != 1 {
t.Fatalf("back to the default: %+v, %v", st, settings.Load().RedactModelScore)
}
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/privacy-model", strings.NewReader(`{"do":"pick","model":"gpt"}`)))
if rec.Code == http.StatusOK || settings.Load().RedactModelName != pii.AstrLinkGuard {
t.Fatalf("a model there's none of: %d", rec.Code)
}
did = nil
if st := do(`{"do":"remove"}`); st.On || strings.Join(did, ", ") != "remove privacy-filter, remove astrlink-guard" {
t.Fatalf("remove: %+v %v", st, did)
}
}
// Models folder (MetalHqa on X: both went onto the C: drive): Move… takes
// the picker's folder and moves what is downloaded there, the state says
// where the models are and that the user picked it, Move back returns them
// to magpie's cache, and a cancelled picker moves nothing.
func TestPrivacyModelFolder(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Cleanup(func() { settings.SetRedactModelDir("") })
def := pii.Root()
held := filepath.Join(def, "astrlink-guard", "rev", "model.onnx")
if err := os.MkdirAll(filepath.Dir(held), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(held, []byte("weights"), 0o644); err != nil {
t.Fatal(err)
}
picked := filepath.Join(t.TempDir(), "models")
pick := &pickFolder{dir: picked}
srv := webPageHandler(t, folderPick{p: pick}, nil)
do := func(body string) privacyModelJSON {
t.Helper()
rec := httptest.NewRecorder()
method := "POST"
if body == "" {
method = "GET"
}
srv.ServeHTTP(rec, httptest.NewRequest(method, "/api/privacy-model", strings.NewReader(body)))
var st privacyModelJSON
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil {
t.Fatalf("%s: %d %s", body, rec.Code, rec.Body)
}
return st
}
settle := func() privacyModelJSON {
t.Helper()
for end := time.Now().Add(30 * time.Second); time.Now().Before(end); time.Sleep(10 * time.Millisecond) {
if st := do(""); !st.Moving.Moving {
return st
}
}
t.Fatal("the move never ended")
return privacyModelJSON{}
}
if st := do(""); st.FolderSet || st.Folder != tilde(def) {
t.Fatalf("before: %+v", st)
}
pick.dir = ""
do(`{"do":"folder"}`)
if st := settle(); st.FolderSet {
t.Fatalf("a cancelled picker moved: %+v", st)
}
pick.dir = picked
do(`{"do":"folder"}`)
st := settle()
if !st.FolderSet || st.Folder != tilde(picked) || st.Moving.Error != "" || settings.Load().RedactModelDir != picked {
t.Fatalf("moved: %+v", st)
}
if b, err := os.ReadFile(filepath.Join(picked, "astrlink-guard", "rev", "model.onnx")); err != nil || string(b) != "weights" {
t.Fatalf("the model isn't in the picked folder: %v", err)
}
if _, err := os.Stat(held); !os.IsNotExist(err) {
t.Fatalf("the old copy is still there: %v", err)
}
do(`{"do":"folder","dir":"default"}`)
st = settle()
if st.FolderSet || st.Folder != tilde(def) || settings.Load().RedactModelDir != "" {
t.Fatalf("back: %+v", st)
}
if _, err := os.Stat(held); err != nil {
t.Fatalf("the model didn't come back: %v", err)
}
// a folder that can't be used is an error, not a move
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/privacy-model", strings.NewReader(`{"do":"folder","dir":"relative/path"}`)))
if rec.Code == http.StatusOK {
t.Fatalf("a relative folder was accepted")
}
}
// folderPick answers the picker with whatever its pointer holds now.
type folderPick struct {
folderOnly
p *pickFolder
}
func (f folderPick) ChooseFolder(s string) (string, error) { return f.p.ChooseFolder(s) }