Files
magpie/internal/gui/privacymodel_test.go
T
yetone ae03279458 masking: the local model is on by default; the rules mask alone until it is downloaded
The owner's plan (10-28, after the xuanwo/_kaichen reports under the
masking demo): with AstrLink Guard picked, the Chinese-name heuristic
is off and only nicknames like 小王/老李 stay a rule; with Privacy Filter
it is as before; every format rule stays; the model is on by default,
masks with the rules alone until its download finishes, and no longer
answers 503 for it.

- masking.ModelWanted(st): on when the user turned it on, off when the
  user turned it off (new RedactModelOff, set by SetRedactModel(false),
  remove, and cancelling the default's own download), on when the
  default's download finished (RedactModelAuto). A settings file from
  before this, with a model name picked or files on disk and the model
  off, reads as the user's off: magpie's default doesn't override it.
- Until the picked model is installed, requests are masked by the rules
  alone and fetch starts its download (one try per model per hour,
  never under testing.Testing(), nothing where the model can't run).
  When it finishes, settings.UseDefaultRedactModel records the default
  (it refuses when the user has set anything) and the model warms.
- model(..., strict): a model that fails holds requests back (503)
  only when the user turned it on; under the default the rules mask
  alone. The Privacy page says which ("…the rules mask requests alone"
  in en, zh, zh-TW, ja, de; /api/privacy-model has `chosen`).
- redact.Options.NamesByModel (AstrLink Guard picked): zhNameSpans
  keeps only zhNick candidates (小/老 + a surname).
- KeepOwn and POST /api/settings keep both new fields.
- Docs: gateway-routing.md, site masking docs (en, zh, ja).

Run (mote, Go 1.26.8):
- gofmt clean; go vet of masking, settings, redact, gui; GOOS=linux go
  vet of masking, settings, redact; GOOS=windows go vet of the four.
  (GOOS=linux vet of internal/gui needs cgo, as on main.)
- go test -count=1 -timeout 45m ./... at base 7fd77a944: every package
  ok except internal/gui. Its reds: TestCoreScriptBudget (+64 bytes,
  fixed by dropping a comment, budget not raised), TestPrivacyModelPick
  and TestSettingsSaveKeepsWhatItDoesNotSend (updated for the new
  default and fields), TestMainWindowCorners (#3949, red on origin/main
  e8c7dddcb too), and three AppKit tests killed at 20-30s under load
  just before mote rebooted, which pass in the rerun.
- Rerun at base 6bec526f7: internal/gui (only #3949 red), masking,
  settings, redact ok. GUI node tests privacy-model, masking-records,
  masking-demo, routing-masking, i18n-keys, gui-zh-tw, gui-ja, gui-de:
  Chromium 199/199, WebKit 106/106.
- At the pushed base 9e33962c0: TestCoreScriptBudget, TestSettingsSave*,
  TestPrivacyModel* ok; i18n-keys and privacy-model 68/68 in both
  engines.
- At the pushed base 7fcf642ee (masking in the request archive):
  -run 'Mask|Redact|Archive|Privacy' in masking, gateway, redact ok.

Broken by hand, each red: the nick filter off
(TestChineseNamesByModelOnlyNicks), ModelWanted needing RedactModel
(TestModelOnByDefault), strict ignored (TestDefaultModelFailingIsTheRules,
TestNoModelYetMasksByTheRules).

Not tried: a real default download in a sandbox HOME (the tests fake the
model). Known limit: a user who turned the model on before model picks
existed and then deleted it has no name or files left, so reads as new
and gets the default on.
2026-10-28 16:55:24 +08:00

213 lines
8.3 KiB
Go

package gui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"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 := Handler(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 := Handler(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 := Handler(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)
}
}