Files
magpie/internal/redact/put_test.go
T
yetone 9b57261b62 masking: a record keeps its request as sent, how long masking took and what the answer put back
For the 观测 › 脱敏 page's request view. In memory only: the masked body
of the latest 100 requests (each up to 4MB, 64MB in all), and by
placeholder how many places of the answer had the value put back
(redact.Writer.Put, counted once per place even when a stream splits
the placeholder). Took (µs) is written to disk too; it holds nothing of
the request.

GET /api/masking/sent?key= gives the body, POST /api/masking/values the
values the masker still remembers, both no-store.

Run: go test ./internal/maskrec ./internal/redact ./internal/gui
-run TestMaskingRecordsAPI and ./internal/gateway -run TestMasked; ok.
Broken by hand: without toldBack, TestMaskedRecordKeepsSentAndBack is red.
2026-10-24 19:41:02 +08:00

58 lines
1.8 KiB
Go

package redact
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// The writer counts each place it put a value back once: a placeholder a
// stream splits across events is one place, and a whole answer's places
// are each counted.
func TestWriterPutCounts(t *testing.T) {
pw, _ := Mask(openaiKey, Options{Secrets: true})
event := func(s string) string {
b, _ := json.Marshal(map[string]any{"type": "content_block_delta", "index": 0, "delta": map[string]any{"type": "text_delta", "text": s}})
return "event: content_block_delta\ndata: " + string(b) + "\n\n"
}
all := event("use "+pw[:5]) + event(pw[5:12]) + event(pw[12:]+" and ") + event(pw+".") + "data: [DONE]\n\n"
rec := httptest.NewRecorder()
rec.Header().Set("Content-Type", "text/event-stream")
w := NewWriter(rec)
w.WriteHeader(200)
for i := 0; i < len(all); i += 7 {
w.Write([]byte(all[i:min(i+7, len(all))]))
}
w.Finish()
if !strings.Contains(rec.Body.String(), openaiKey) {
t.Fatalf("not put back: %s", rec.Body.String())
}
if got := w.Put(); len(got) != 1 || got[pw] != 2 {
t.Fatalf("stream put %v, want %s twice", got, pw)
}
b, _ := json.Marshal(map[string]any{"content": []any{map[string]any{"type": "text", "text": pw + " " + pw + " " + pw}}})
rec = httptest.NewRecorder()
rec.Header().Set("Content-Type", "application/json")
w = NewWriter(rec)
w.WriteHeader(http.StatusOK)
w.Write(b)
w.Finish()
if got := w.Put(); got[pw] != 3 {
t.Fatalf("whole put %v, want 3", got)
}
// nothing to put back: nothing counted
rec = httptest.NewRecorder()
rec.Header().Set("Content-Type", "application/json")
w = NewWriter(rec)
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"content":"plain"}`))
w.Finish()
if got := w.Put(); len(got) != 0 {
t.Fatalf("plain put %v", got)
}
}