Files
eec-campaigns/internal/campaign/template_test.go
T
will a630a8f8ab Add template-as-code sync (campaigns template NAME PATH)
Answers "can the unsubscribe template be added via git+API too": commit
listmonk's own stock campaign template (already unsubscribe-capable) as
email-templates/campaign.html, add a small find-or-create-or-update
Template client (confirmed against knadh/listmonk's actual model/handlers),
a new `template` subcommand, and a manual-dispatch-only workflow to push it
in as the default campaign template. Unlike campaign sync there's no
draft/live status to protect, so this is always a safe overwrite.
2026-07-10 08:54:31 -04:00

56 lines
1.5 KiB
Go

package campaign
import (
"os"
"path/filepath"
"testing"
)
func TestSyncTemplate_CreatesWhenMissing(t *testing.T) {
f := newFakeListmonk(t)
lm := f.client()
dir := t.TempDir()
path := filepath.Join(dir, "campaign.html")
if err := os.WriteFile(path, []byte("<html>v1</html>"), 0o644); err != nil {
t.Fatal(err)
}
tpl, err := SyncTemplate(lm, "eec-campaigns default", path)
if err != nil {
t.Fatalf("SyncTemplate: %v", err)
}
if tpl.ID == 0 {
t.Errorf("expected a nonzero id, got %+v", tpl)
}
if len(f.templates) != 1 || f.templates[0].Body != "<html>v1</html>" {
t.Fatalf("expected 1 template with the file's content, got %+v", f.templates)
}
if !f.templates[0].IsDefault {
t.Error("expected the template to be created as the default")
}
}
func TestSyncTemplate_UpdatesWhenAlreadyExists(t *testing.T) {
f := newFakeListmonk(t)
f.templates = []fakeTemplate{{ID: 5, Name: "eec-campaigns default", Body: "<html>v1</html>", IsDefault: true}}
lm := f.client()
dir := t.TempDir()
path := filepath.Join(dir, "campaign.html")
if err := os.WriteFile(path, []byte("<html>v2</html>"), 0o644); err != nil {
t.Fatal(err)
}
tpl, err := SyncTemplate(lm, "eec-campaigns default", path)
if err != nil {
t.Fatalf("SyncTemplate: %v", err)
}
if tpl.ID != 5 {
t.Errorf("expected the existing template's id 5 to be reused, got %d", tpl.ID)
}
if len(f.templates) != 1 || f.templates[0].Body != "<html>v2</html>" {
t.Fatalf("expected the existing template's body updated in place, got %+v", f.templates)
}
}