feat: batches with dry run, undo, and CSV import and export

- POST /_seo/api/v1/batch runs changes in one transaction: dry_run
  answers the diff and the conflicts (loops, chains, noindex on a
  published record, canonicals to redirected or noindex addresses) and
  rolls back; a refused change refuses the batch (D-12)
- undo of a change or a whole batch from History and through MCP, with
  the undo_stale, undo_loop and undo_gone refusals (D-13)
- CSV export and import of redirects, rules and records by URL: BOM,
  comma or semicolon, formula cells escaped, a dry-run preview, all or
  nothing with via import; the screen's apply token is HMAC-SHA256 over
  the file and a fingerprint of the data (transfer.stale)
- Import and export singleton with the seo_transfer widget action,
  History record actions undo and undo_batch, navigation entry
- MCP tools undo_change, undo_batch, apply_batch, export_csv, import_csv
- TestBatch, TestUndo, TestCSVTransfer, TestLoopRefused mcp and import
This commit is contained in:
2026-10-27 12:35:26 +01:00
parent aa0c3bcacf
commit ab3b219fec
27 changed files with 3319 additions and 96 deletions

View File

@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sort"
@@ -423,6 +424,40 @@ func TestLoopRefused(t *testing.T) {
if rows := env.revisions(t, "redirect"); len(rows) < 4 {
t.Fatalf("redirect history rows = %d, want every admin save recorded", len(rows))
}
// The same loop is refused from MCP (a tool error with the path) and
// from a CSV import (a refused row that blocks the apply), SC-3.
t.Run("mcp", func(t *testing.T) {
agent := env.principal(t, env.admin(t, false, "golem15.seo.manage"))
before := env.redirectCount(t)
rec := env.mcp(t, http.MethodPost, "/_seo/api/v1/redirects", agent, map[string]any{"source": "/lb", "target": "/la"})
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), `"error":"redirect_loop"`) || !strings.Contains(rec.Body.String(), `"/lb"`) {
t.Fatalf("MCP loop = %d %s, want 422 redirect_loop with the path", rec.Code, rec.Body.String())
}
rec = env.mcp(t, http.MethodPost, "/_seo/api/v1/batch", agent, map[string]any{"changes": []any{map[string]any{"op": "create_redirect", "redirect": map[string]any{"source": "/lb", "target": "/la"}}}})
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), `"error":"redirect_loop"`) {
t.Fatalf("MCP batch loop = %d %s", rec.Code, rec.Body.String())
}
if after := env.redirectCount(t); after != before {
t.Fatalf("redirects after the MCP loop = %d, want %d", after, before)
}
})
t.Run("import", func(t *testing.T) {
agent := env.principal(t, env.admin(t, false, "golem15.seo.manage"))
before := env.redirectCount(t)
file := "source,target\n/lb,/la\n"
preview := env.mcpExpect(t, http.StatusOK, http.MethodPost, "/_seo/api/v1/import", agent, map[string]any{"dataset": "redirects", "csv": file, "delimiter": ",", "dry_run": true})
if !strings.Contains(fmt.Sprint(preview["rows"]), "Creates a redirect loop: /lb → /la → /lb") {
t.Fatalf("import preview = %v, want transfer.row_loop", preview)
}
rec := env.mcp(t, http.MethodPost, "/_seo/api/v1/import", agent, map[string]any{"dataset": "redirects", "csv": file, "delimiter": ","})
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), `"error":"import_refused"`) {
t.Fatalf("import apply = %d %s, want 422 import_refused", rec.Code, rec.Body.String())
}
if after := env.redirectCount(t); after != before {
t.Fatalf("redirects after the refused import = %d, want %d", after, before)
}
})
}
// report posts a 404 report with token ("" sends none).