feat(17-04): faq:import-winter and the read-only Winter export SQL

- JSON lines import keeping ids, locale_id looked up by locale code (an unknown code aborts before any write)
- one transaction, setval past the copied ids, --force replaces, --dry-run reports, an empty file copies nothing
- scripts/winter-export.sql: SELECT JSON_OBJECT with the translate locale code
- TestFaqImportSmoke
This commit is contained in:
2026-10-12 17:35:30 +02:00
parent 1c5491232a
commit 7413c6905d
5 changed files with 575 additions and 0 deletions

View File

@@ -15,9 +15,11 @@ import (
"time"
_ "git.golem15.com/golem15/sm-faq-plugin"
"git.golem15.com/golem15/sm-faq-plugin/console"
"git.golem15.com/golem15/sm-faq-plugin/models"
_ "git.golem15.com/golem15/sm-translate-plugin"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/bouncer"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/compass"
@@ -250,3 +252,115 @@ func TestFaqAdminSmoke(t *testing.T) {
t.Fatalf("list without permission: %d %s", rec.Code, rec.Body.String())
}
}
func writeFile(t *testing.T, dir, name, body string) string {
t.Helper()
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func testOutput() (bonfire.Output, *bytes.Buffer) {
var buf bytes.Buffer
return bonfire.NewOutput(strings.NewReader(""), &buf, &buf), &buf
}
func (e *faqEnv) countFAQs(t *testing.T) int64 {
t.Helper()
var n int64
if err := e.gdb.Model(&models.FAQ{}).Count(&n).Error; err != nil {
t.Fatal(err)
}
return n
}
// TestFaqImportSmoke copies a Winter export through faq:import-winter: two
// locales by code, ids kept, the sequence moved past them, an unknown code
// aborting before any write, a second copy refused without --force and an
// empty file reporting zero.
func TestFaqImportSmoke(t *testing.T) {
e := bootFAQ(t)
ctx := t.Context()
dir := t.TempDir()
en, pl := e.localeID(t, "en"), e.localeID(t, "pl")
content, _ := json.Marshal(`[{"question":"How long?","answer":"<p>Two days.</p>"},{"question":"Where?","answer":"<p>Everywhere.</p>"}]`)
good := writeFile(t, dir, "faqs.jsonl",
`{"id": 4, "name": "Shipping", "slug": "shipping", "locale_code": "en", "is_active": 1, "description": "<p>About shipping.</p>", "content": `+string(content)+`, "created_at": "2024-03-01 10:00:00.000000", "updated_at": "2024-03-02 11:30:00.000000"}`+"\n"+
`{"id": 9, "name": "Wysyłka", "slug": "shipping", "locale_code": "pl", "is_active": 0, "description": null, "content": [{"question":"Jak długo?","answer":"<p>Dwa dni.</p>"}], "created_at": null, "updated_at": null}`+"\n\n")
unknown := writeFile(t, dir, "unknown.jsonl",
`{"id": 1, "name": "A", "slug": "a", "locale_code": "en", "is_active": 1, "content": null}`+"\n"+
`{"id": 2, "name": "B", "slug": "b", "locale_code": "de", "is_active": 1, "content": null}`+"\n")
empty := writeFile(t, dir, "empty.jsonl", "\n")
// An unknown locale code aborts before anything is written.
out, buf := testOutput()
if _, err := console.ImportWinterFile(ctx, e.gdb, console.WinterOptions{File: unknown}, out); err == nil || !strings.Contains(err.Error(), "'de'") {
t.Fatalf("unknown locale: %v\n%s", err, buf.String())
}
if n := e.countFAQs(t); n != 0 {
t.Fatalf("unknown locale wrote %d rows", n)
}
// A dry run reports and writes nothing.
out, buf = testOutput()
if report, err := console.ImportWinterFile(ctx, e.gdb, console.WinterOptions{File: good, DryRun: true}, out); err != nil || report.FAQs != 2 {
t.Fatalf("dry run: %+v %v\n%s", report, err, buf.String())
}
if n := e.countFAQs(t); n != 0 || !strings.Contains(buf.String(), "Dry run complete") {
t.Fatalf("dry run wrote %d rows\n%s", n, buf.String())
}
out, buf = testOutput()
report, err := console.ImportWinterFile(ctx, e.gdb, console.WinterOptions{File: good}, out)
if err != nil || report.FAQs != 2 {
t.Fatalf("import: %+v %v\n%s", report, err, buf.String())
}
var rows []models.FAQ
if err := e.gdb.Order("id").Find(&rows).Error; err != nil {
t.Fatal(err)
}
if len(rows) != 2 || rows[0].ID != 4 || rows[0].LocaleID != en || !rows[0].IsActive || rows[1].ID != 9 || rows[1].LocaleID != pl || rows[1].IsActive {
t.Fatalf("copied rows = %+v", rows)
}
if items := rows[0].Items(); len(items) != 2 || items[1]["question"] != "Where?" || rows[0].CreatedAt == nil || !rows[0].CreatedAt.Equal(time.Date(2024, 3, 1, 10, 0, 0, 0, time.UTC)) {
t.Fatalf("copied content = %v created %v", items, rows[0].CreatedAt)
}
if items := rows[1].Items(); len(items) != 1 || items[0]["answer"] != "<p>Dwa dni.</p>" {
t.Fatalf("copied pl content = %v", items)
}
// The sequence moved past the copied ids: an admin create gets id 10.
token := e.admin(t, permFAQs)
rec := e.call(t, http.MethodPost, "/golem15/faq/faqs", token, map[string]any{"name": "Returns", "slug": "returns", "locale": en})
if rec.Code != http.StatusCreated || !strings.Contains(rec.Body.String(), `"id":10`) {
t.Fatalf("create after import: %d %s", rec.Code, rec.Body.String())
}
// A second copy is refused without --force and replaces with it.
out, buf = testOutput()
if _, err := console.ImportWinterFile(ctx, e.gdb, console.WinterOptions{File: good}, out); err == nil || !strings.Contains(err.Error(), "--force") {
t.Fatalf("second import: %v\n%s", err, buf.String())
}
if n := e.countFAQs(t); n != 3 {
t.Fatalf("refused import changed the table: %d rows", n)
}
out, buf = testOutput()
if _, err := console.ImportWinterFile(ctx, e.gdb, console.WinterOptions{File: good, Force: true}, out); err != nil {
t.Fatalf("forced import: %v\n%s", err, buf.String())
}
if n := e.countFAQs(t); n != 2 {
t.Fatalf("forced import left %d rows", n)
}
// An empty file writes nothing and reports zero.
out, buf = testOutput()
if report, err := console.ImportWinterFile(ctx, e.gdb, console.WinterOptions{File: empty}, out); err != nil || report.FAQs != 0 || !strings.Contains(buf.String(), "0 FAQs") {
t.Fatalf("empty file: %+v %v\n%s", report, err, buf.String())
}
if n := e.countFAQs(t); n != 2 {
t.Fatalf("empty file changed the table: %d rows", n)
}
}