test(postcard): unit tests for overrides, fallback, partials, samples, validation, sync and branding
- Pin override precedence, fallback, partials, samples, draft validation and branding - Postgres TestStore/TestSync for index sync, drift hashes and reset - Pact row-state and mail capability method-set pins; attach AbsolutePublicURL cases Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
168
modules/postcard/store_postgres_test.go
Normal file
168
modules/postcard/store_postgres_test.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package postcard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStore(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("requires testcontainers postgres")
|
||||
}
|
||||
gdb := mailPostgres(t)
|
||||
app := appWith(t, gdb)
|
||||
m, _, _ := bootMailApp(t, gdb)
|
||||
_ = m
|
||||
src := NewDBSource(app)
|
||||
|
||||
t.Run("reads_only_is_custom", func(t *testing.T) {
|
||||
customizeWelcome(t, gdb, "Custom {{ .name }}", "Body", "acme.blog::mail.layouts.blog")
|
||||
if err := gdb.Exec(`INSERT INTO `+TemplatesTable+` (code, source, subject, body, is_custom) VALUES (?, 'acme.blog', 'Index', 'Index', FALSE)
|
||||
ON CONFLICT (code) DO NOTHING`, acmeNotice).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ov, err := src.MailOverrides(context.Background(), acmeWelcome)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ov.Template == nil || ov.Template.Subject != "Custom {{ .name }}" {
|
||||
t.Fatalf("custom template = %+v", ov.Template)
|
||||
}
|
||||
ov2, err := src.MailOverrides(context.Background(), acmeNotice)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ov2.Template != nil {
|
||||
t.Fatalf("index row leaked: %+v", ov2.Template)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSync(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("requires testcontainers postgres")
|
||||
}
|
||||
gdb := mailPostgres(t)
|
||||
plugin := acmeBlog{}
|
||||
cat := NewCatalog()
|
||||
if err := cat.Register(plugin.ID(), plugin.MailTemplatesFS(), plugin.MailTemplates(), plugin.MailLayouts()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("idempotent_stable_ids", func(t *testing.T) {
|
||||
if err := SyncIndex(ctx, gdb, cat, []string{"en"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var id1 int64
|
||||
if err := gdb.Raw(`SELECT id FROM `+TemplatesTable+` WHERE code = ?`, acmeWelcome).Scan(&id1).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SyncIndex(ctx, gdb, cat, []string{"en"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var id2 int64
|
||||
if err := gdb.Raw(`SELECT id FROM `+TemplatesTable+` WHERE code = ?`, acmeWelcome).Scan(&id2).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if id1 == 0 || id1 != id2 {
|
||||
t.Fatalf("ids = %d, %d", id1, id2)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("concurrent", func(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, 8)
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
errs <- SyncIndex(ctx, gdb, cat, []string{"en"})
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("customized_keeps_override_columns_and_drifts", func(t *testing.T) {
|
||||
customizeWelcome(t, gdb, "Kept {{ .name }}", "Kept body", "acme.blog::mail.layouts.blog")
|
||||
if err := gdb.Exec(`UPDATE `+TemplatesTable+` SET file_hash = 'old', base_hash = 'old' WHERE code = ?`, acmeWelcome).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SyncIndex(ctx, gdb, cat, []string{"en"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var subject, body, fileHash, baseHash string
|
||||
var isCustom bool
|
||||
if err := gdb.Raw(`SELECT subject, body, is_custom, COALESCE(file_hash,''), COALESCE(base_hash,'') FROM `+TemplatesTable+` WHERE code = ?`, acmeWelcome).
|
||||
Row().Scan(&subject, &body, &isCustom, &fileHash, &baseHash); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isCustom || subject != "Kept {{ .name }}" || body != "Kept body" {
|
||||
t.Fatalf("override columns changed: %q %q custom=%v", subject, body, isCustom)
|
||||
}
|
||||
if fileHash == "" || fileHash == "old" {
|
||||
t.Fatalf("file_hash not refreshed: %q", fileHash)
|
||||
}
|
||||
if baseHash != "old" {
|
||||
t.Fatalf("base_hash should stay: %q", baseHash)
|
||||
}
|
||||
if fileHash == baseHash {
|
||||
t.Fatal("expected drift: file_hash == base_hash")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("database_only_layouts_untouched", func(t *testing.T) {
|
||||
if err := gdb.Exec(`INSERT INTO `+LayoutsTable+` (code, source, name, content_html, is_custom) VALUES ('promo', NULL, 'Promo', '<div>{{ .Content }}</div>', TRUE)
|
||||
ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name`).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SyncIndex(ctx, gdb, cat, []string{"en"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var name string
|
||||
var source *string
|
||||
if err := gdb.Raw(`SELECT name, source FROM `+LayoutsTable+` WHERE code = 'promo'`).Row().Scan(&name, &source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if name != "Promo" || source != nil {
|
||||
t.Fatalf("database-only layout touched: name=%q source=%v", name, source)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reset_override", func(t *testing.T) {
|
||||
customizeWelcome(t, gdb, "Custom", "Body", "acme.blog::mail.layouts.blog")
|
||||
if err := ResetOverride(ctx, gdb, cat, KindTemplate, acmeWelcome); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var isCustom bool
|
||||
var subject string
|
||||
if err := gdb.Raw(`SELECT is_custom, subject FROM `+TemplatesTable+` WHERE code = ?`, acmeWelcome).Row().Scan(&isCustom, &subject); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if isCustom {
|
||||
t.Fatal("still custom after reset")
|
||||
}
|
||||
if subject == "Custom" {
|
||||
t.Fatalf("subject still %q after reset", subject)
|
||||
}
|
||||
if err := gdb.Exec(`INSERT INTO `+LayoutsTable+` (code, source, name, content_html, is_custom) VALUES ('promo2', NULL, 'Promo2', 'x', TRUE)
|
||||
ON CONFLICT (code) DO NOTHING`).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := ResetOverride(ctx, gdb, cat, KindLayout, "promo2")
|
||||
if err == nil || !strings.Contains(err.Error(), "database-only") && !strings.Contains(err.Error(), "custom layout") && !strings.Contains(err.Error(), "promo2") {
|
||||
// Accept any refusal; must not succeed.
|
||||
if err == nil {
|
||||
t.Fatal("reset of database-only layout must fail")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user