CyberCaius on Discord: "请求存档只能存在 s3 存储桶里,不能保存在本地吗". The Usage page's Request archive switch refused without an s3:// sync. Now, with no bucket, the same switch keeps each call in <settings.Dir()>/request-archive/<date>/<id>.json: the same file the bucket gets, masked the same way (Archived, archiveRequest), written to a temporary file beside it, synced and renamed, 0600 in a 0700 folder. Past its size (RequestArchiveLocalMB, 1 GiB unless set) the oldest calls go first, down to a tenth under it; the call just written is never removed, empty day folders go, and a .part file an hour old is removed on the next scan. The running total is counted once and then added to, so a write walks the folder only once it is over. On by default or a switch: a switch, still off unless turned on. Bodies can hold secrets, so nothing is written to a disk where the user didn't ask. The local switch is its own field, RequestArchiveLocal, in KeepOwn with its size, so sync never turns it on at another computer, and RequestArchive synced from a computer with a bucket keeps nothing on one without (archiveDest). The GUI switch writes RequestArchive while sync has a bucket and RequestArchiveLocal while it has none; the tooltip names the folder and the size, and a failed write reads "Not saved" (en, zh, zh-TW, ja, de). GET /api/archive reads this computer's copy first, then the bucket. The Settings page's save keeps both fields (api.go). Docs: sync-backup.md and gui-shell.md say where the archive goes and why the switch is per computer; the privacy pages (en, zh, ja) say what the folder holds, that it is off by default, and how large it grows. The READMEs' Privacy is about what is sent and is unchanged. app.js stays under TestCoreScriptBudget (its comment was shortened, and one t() call folded); no budget raised. Run (worktree diff on5222a4b12, sandbox HOME): - go test -tags nogui -count=1 ./internal/settings/ ./internal/gui/ ./internal/gateway/: ok (29s, 73s, 507s) - node --test request-archive, request-archive-usage, routing-bodies, gui-zh-tw, gui-ja, gui-de, i18n-keys (chromium and webkit): 37/37 pass - gofmt -l internal/: clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean; go build for darwin, linux (nogui), windows: ok - TestCoreScriptBudget: ok After rebasing onto origin/main3840333c5: -run Budget|Archive|SaveKeeps| RequestArchiveLocal in gui, gateway, settings ok; request-archive and i18n-keys 8/8; gofmt clean; linux and windows vet clean. Broken by hand, each red, then put back: - archiveDest's local branch off: TestArchiveOnThisComputer red - RequestArchive also writing to disk: TestArchiveNeverOnDiskUnasked red - no pruning: TestLocalArchiveRemovesOldestPastItsSize red - KeepOwn line removed: TestRequestArchiveLocalIsThisComputers red - POST setting RequestArchive with no bucket: TestArchiveSwitchKeepsOnThisComputer red - GET not reading the local copy: TestArchiveSwitchKeepsOnThisComputer red - api.go not carrying the fields: TestSettingsSaveKeepsWhatItDoesNotSend red - writing in place, not through temp+rename: TestArchiveOnThisComputer red (mode 0644, a .part left) Not tried: the real app with a real agent's calls (the gateway tests drive real requests through serve with a fake vendor); Windows' file modes (0600 is skipped there); the full ./... suite before the push (the machine had a queue of sessions' suites; it is queued after).
196 lines
8.2 KiB
Go
196 lines
8.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/davsync"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// fakeArchive is a bucket of archived calls in memory; size, when set,
|
|
// is what it says an object's size is.
|
|
type fakeArchive struct {
|
|
objs map[string][]byte
|
|
size int64
|
|
}
|
|
|
|
func (f fakeArchive) Open(_ context.Context, name string) (io.ReadCloser, int64, error) {
|
|
b, ok := f.objs[name]
|
|
if !ok {
|
|
return nil, 0, davsync.ErrNoObject
|
|
}
|
|
if f.size != 0 {
|
|
return io.NopCloser(bytes.NewReader(b)), f.size, nil
|
|
}
|
|
return io.NopCloser(bytes.NewReader(b)), int64(len(b)), nil
|
|
}
|
|
|
|
func archiveHome(t *testing.T, f fakeArchive) http.Handler {
|
|
t.Helper()
|
|
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"))
|
|
os.MkdirAll(filepath.Join(h, "Downloads"), 0o700)
|
|
was := archiveBucket
|
|
archiveBucket = func() (archiveStore, bool) { return f, true }
|
|
t.Cleanup(func() { archiveBucket = was })
|
|
return Handler(nil, nil)
|
|
}
|
|
|
|
func archivedCall(t *testing.T, req, res string) []byte {
|
|
t.Helper()
|
|
b, err := json.Marshal(gateway.Archived{ID: "2026-10-02/101010-00000000000000aa", Call: gateway.Call{Model: "p/m", Status: 200},
|
|
Request: gateway.ArchivePart{Method: "POST", Path: "/v1/messages", Body: req, Size: int64(len(req))},
|
|
Response: gateway.ArchivePart{Status: 200, Body: res, Size: int64(len(res))}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
const archiveQuery = "?date=2026-10-02&id=101010-00000000000000aa"
|
|
const archiveObject = "archive/2026-10-02/101010-00000000000000aa.json"
|
|
|
|
// The page is sent a body to show only when it is 256 KB or less: a longer
|
|
// one is told by its size alone, and the file downloads whole, however
|
|
// large (#447) — to the browser, or, in the app, to Downloads.
|
|
func TestArchivePreviewAndDownload(t *testing.T) {
|
|
long := strings.Repeat("a", 20<<20) + "TAIL" // past what Get reads, 16 MiB
|
|
obj := archivedCall(t, `{"q":"short"}`, long)
|
|
h := archiveHome(t, fakeArchive{objs: map[string][]byte{archiveObject: obj}})
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive"+archiveQuery, nil))
|
|
if rec.Code != 200 || rec.Body.Len() > 64<<10 {
|
|
t.Fatalf("%d, %d bytes sent to the page", rec.Code, rec.Body.Len())
|
|
}
|
|
var p archivePreview
|
|
json.Unmarshal(rec.Body.Bytes(), &p)
|
|
if p.Request == nil || p.Request.Body != `{"q":"short"}` || p.Request.Omitted || p.Response == nil || !p.Response.Omitted || p.Response.Body != "" ||
|
|
p.Response.Size != int64(len(long)) || p.Bytes != int64(len(obj)) || p.Call == nil || p.Call.Model != "p/m" {
|
|
t.Fatalf("preview %.400s", rec.Body)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive/file"+archiveQuery, nil))
|
|
if rec.Code != 200 || !bytes.Equal(rec.Body.Bytes(), obj) ||
|
|
rec.Header().Get("Content-Disposition") != `attachment; filename="magpie-request-2026-10-02-101010-00000000000000aa.json"` {
|
|
t.Fatalf("download %d %q, %d of %d bytes", rec.Code, rec.Header().Get("Content-Disposition"), rec.Body.Len(), len(obj))
|
|
}
|
|
|
|
for _, want := range []string{"magpie-request-2026-10-02-101010-00000000000000aa.json", "magpie-request-2026-10-02-101010-00000000000000aa-2.json"} {
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("POST", "/api/archive/export"+archiveQuery, strings.NewReader("{}")))
|
|
var out struct{ Path string }
|
|
json.Unmarshal(rec.Body.Bytes(), &out)
|
|
if rec.Code != 200 || filepath.Base(out.Path) != want {
|
|
t.Fatalf("export %d %s", rec.Code, rec.Body)
|
|
}
|
|
got, err := os.ReadFile(filepath.Join(os.Getenv("HOME"), "Downloads", want))
|
|
if err != nil || !bytes.Equal(got, obj) {
|
|
t.Fatalf("saved %d of %d bytes: %v", len(got), len(obj), err)
|
|
}
|
|
}
|
|
if left, _ := filepath.Glob(filepath.Join(os.Getenv("HOME"), "Downloads", ".*part")); len(left) != 0 {
|
|
t.Fatalf("left %v", left)
|
|
}
|
|
|
|
// one not there yet says so; a name not of the archive's shape is refused
|
|
for _, q := range []string{"?date=2026-10-02&id=101010-00000000000000bb", "?date=2026-10-02&id=../../backup"} {
|
|
for _, path := range []string{"GET /api/archive", "GET /api/archive/file", "POST /api/archive/export"} {
|
|
m, u, _ := strings.Cut(path, " ")
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(m, u+q, strings.NewReader("{}")))
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Errorf("%s%s: %d %s", path, q, rec.Code, rec.Body)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// An archive too large to read for a preview is only said to be, and
|
|
// downloaded.
|
|
func TestArchiveTooLargeToPreview(t *testing.T) {
|
|
h := archiveHome(t, fakeArchive{objs: map[string][]byte{archiveObject: archivedCall(t, "{}", "{}")}, size: archiveReadMost + 1})
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive"+archiveQuery, nil))
|
|
var p archivePreview
|
|
json.Unmarshal(rec.Body.Bytes(), &p)
|
|
if rec.Code != 200 || !p.Large || p.Bytes != archiveReadMost+1 || p.Request != nil {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
|
|
// An archive from before #447, its bodies cut at 256 KB with no size, is
|
|
// shown as it was, its size the body's.
|
|
func TestArchiveOldPreview(t *testing.T) {
|
|
old := `{"id":"2026-10-02/101010-00000000000000aa","call":{"model":"p/m"},"request":{"method":"POST","headers":{},"body":"{}","truncated":true},"response":{"status":200,"headers":{},"body":"ok"}}`
|
|
h := archiveHome(t, fakeArchive{objs: map[string][]byte{archiveObject: []byte(old)}})
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive"+archiveQuery, nil))
|
|
var p archivePreview
|
|
json.Unmarshal(rec.Body.Bytes(), &p)
|
|
if rec.Code != 200 || p.Request == nil || p.Request.Body != "{}" || !p.Request.Truncated || p.Request.Size != 2 || p.Response.Body != "ok" {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
|
|
// With sync not to S3 the switch turns on the archive on this computer,
|
|
// its own setting, not the S3 one sync carries; the page is told the folder
|
|
// and its size, and a call kept there is read back and downloaded from it
|
|
// (CyberCaius on Discord).
|
|
func TestArchiveSwitchKeepsOnThisComputer(t *testing.T) {
|
|
h := archiveHome(t, fakeArchive{objs: map[string][]byte{}})
|
|
archiveBucket = func() (archiveStore, bool) { return nil, false }
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings/archive", strings.NewReader(`{"on":true}`)))
|
|
var a archiveJSON
|
|
json.Unmarshal(rec.Body.Bytes(), &a)
|
|
if rec.Code != 200 || !a.On || a.Bucket != "" || !strings.HasSuffix(a.Folder, "request-archive") || a.Cap != 1<<30 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if s := settings.Load(); !s.RequestArchiveLocal || s.RequestArchive {
|
|
t.Fatalf("switched local %v, synced %v", s.RequestArchiveLocal, s.RequestArchive)
|
|
}
|
|
obj := archivedCall(t, `{"q":"here"}`, `{"a":"kept"}`)
|
|
if err := gateway.LocalArchive().Put(context.Background(), archiveObject, obj); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive"+archiveQuery, nil))
|
|
var p archivePreview
|
|
json.Unmarshal(rec.Body.Bytes(), &p)
|
|
if rec.Code != 200 || p.Request == nil || p.Request.Body != `{"q":"here"}` || p.Bytes != int64(len(obj)) {
|
|
t.Fatalf("read back %d %s", rec.Code, rec.Body)
|
|
}
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive/file"+archiveQuery, nil))
|
|
if rec.Code != 200 || !bytes.Equal(rec.Body.Bytes(), obj) {
|
|
t.Fatalf("download %d, %d bytes", rec.Code, rec.Body.Len())
|
|
}
|
|
// one it never kept says so in this computer's words
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/archive?date=2026-10-02&id=101010-00000000000000bb", nil))
|
|
if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "Not in the archive on this computer") {
|
|
t.Fatalf("missing: %d %s", rec.Code, rec.Body)
|
|
}
|
|
// and off again
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings/archive", strings.NewReader(`{"on":false}`)))
|
|
if s := settings.Load(); rec.Code != 200 || s.RequestArchiveLocal {
|
|
t.Fatalf("off: %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|