Files
magpie/internal/settings/redact_model_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

80 lines
2.7 KiB
Go

package settings
import "testing"
// The masking model is downloaded on one computer: a sync never turns it
// on on another, nor off on this one.
func TestRedactModelIsThisComputers(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
from := Settings{RedactModel: true, RedactModelName: "astrlink-guard"}
from.KeepOwn(Settings{})
if from.RedactModel || from.RedactModelName != "" {
t.Fatal("a sync turned the model on")
}
from.KeepOwn(Settings{RedactModel: true, RedactModelName: "privacy-filter"})
if !from.RedactModel || from.RedactModelName != "privacy-filter" {
t.Fatal("a sync turned the model off, or changed it")
}
if err := SetRedactModel(true); err != nil {
t.Fatal(err)
}
if !Load().RedactModel {
t.Fatal("not saved")
}
if err := SetRedactModel(false); err != nil {
t.Fatal(err)
}
if Load().RedactModel {
t.Fatal("not turned off")
}
if err := UseRedactModel("astrlink-guard"); err != nil {
t.Fatal(err)
}
if st := Load(); !st.RedactModel || st.RedactModelName != "astrlink-guard" {
t.Fatalf("not saved: %v %q", st.RedactModel, st.RedactModelName)
}
// turned off, the model picked is kept for turning it on again
if err := SetRedactModel(false); err != nil {
t.Fatal(err)
}
if st := Load(); st.RedactModel || st.RedactModelName != "astrlink-guard" {
t.Fatalf("off: %v %q", st.RedactModel, st.RedactModelName)
}
}
// Off is the user's own, which the model on by default never turns on
// again; the default's download, done, is saved as the default's only
// where the user neither picked a model nor turned it off meanwhile.
func TestRedactModelOffIsTheUsers(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if ok, err := UseDefaultRedactModel("astrlink-guard"); !ok || err != nil {
t.Fatalf("default not saved: %v %v", ok, err)
}
if st := Load(); st.RedactModel || !st.RedactModelAuto || st.RedactModelName != "astrlink-guard" {
t.Fatalf("default: %+v", st)
}
if err := SetRedactModel(false); err != nil {
t.Fatal(err)
}
if st := Load(); st.RedactModel || !st.RedactModelOff {
t.Fatalf("off: %v %v", st.RedactModel, st.RedactModelOff)
}
if ok, _ := UseDefaultRedactModel("astrlink-guard"); ok {
t.Fatal("the default turned on what the user turned off")
}
if err := UseRedactModel("privacy-filter"); err != nil {
t.Fatal(err)
}
if st := Load(); !st.RedactModel || st.RedactModelOff {
t.Fatalf("on: %v %v", st.RedactModel, st.RedactModelOff)
}
if ok, _ := UseDefaultRedactModel("astrlink-guard"); ok || Load().RedactModelName != "privacy-filter" {
t.Fatal("the default changed the user's pick")
}
from := Settings{RedactModelOff: true, RedactModelAuto: true}
from.KeepOwn(Settings{})
if from.RedactModelOff || from.RedactModelAuto {
t.Fatal("a sync turned the model off, or on by default")
}
}