feat(14.2.1-05): hydrate ML create/GET/save through TranslatedExact

Form schema meta lists enabled locales, Show/save expand mltext maps
without D-11 fallback, and the SPA seeds and adopt-merges every locale.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jakub Zych
2026-10-06 15:37:00 +02:00
parent ed81f2a0c1
commit 0f8d13738d
11 changed files with 309 additions and 34 deletions

View File

@@ -3,6 +3,7 @@ package cabana
import (
"context"
"errors"
"reflect"
"strings"
"testing"
@@ -66,7 +67,13 @@ func TestMLNestedSave(t *testing.T) {
if err != nil {
t.Fatalf("create: %v", err)
}
if rec["title"] != "Hello" {
if rec["title"] != nil {
want := map[string]string{"en": "Hello", "pl": "Witaj"}
got, ok := rec["title"].(map[string]string)
if !ok || !reflect.DeepEqual(got, want) {
t.Fatalf("projected title = %#v", rec["title"])
}
} else {
t.Fatalf("projected title = %#v", rec["title"])
}
var row mlPost
@@ -97,6 +104,55 @@ func TestMLNestedSave(t *testing.T) {
}
}
func TestMLHydration(t *testing.T) {
_, db := newListService(t)
if err := db.Migrator().DropTable(&mlPost{}); err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&mlPost{}); err != nil {
t.Fatal(err)
}
writer := &recordingWriter{defaultLocale: "en", enabled: []string{"en", "pl"}}
svc := CRUDService{DB: db, writer: writer}
cc := mlCompiled(t)
ctx := context.Background()
created, err := svc.CreateRecord(ctx, cc, RecordInput{Body: map[string]any{
"title": map[string]any{"en": "Hello", "pl": "Witaj"},
}})
if err != nil {
t.Fatalf("create: %v", err)
}
got, ok := created.Data["title"].(map[string]string)
if !ok || got["en"] != "Hello" || got["pl"] != "Witaj" {
t.Fatalf("create title = %#v", created.Data["title"])
}
shown, err := svc.ShowRecord(ctx, cc, created.Data["id"])
if err != nil {
t.Fatalf("show: %v", err)
}
got, ok = shown.Data["title"].(map[string]string)
if !ok || got["en"] != "Hello" || got["pl"] != "Witaj" {
t.Fatalf("show title = %#v", shown.Data["title"])
}
englishOnly := &recordingWriter{defaultLocale: "en", enabled: []string{"en", "pl"}}
svcOnly := CRUDService{DB: db, writer: englishOnly}
row := mlPost{Title: "Hello"}
if err := db.Create(&row).Error; err != nil {
t.Fatal(err)
}
shown, err = svcOnly.ShowRecord(ctx, cc, row.ID)
if err != nil {
t.Fatalf("show english-only: %v", err)
}
got, ok = shown.Data["title"].(map[string]string)
if !ok || got["en"] != "Hello" || got["pl"] != "" {
t.Fatalf("english-only title = %#v", shown.Data["title"])
}
}
func TestML(t *testing.T) {
t.Run("writer failure rolls back host write", func(t *testing.T) {
_, db := newListService(t)