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("v1"), 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 != "v1" { 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: "v1", IsDefault: true}} lm := f.client() dir := t.TempDir() path := filepath.Join(dir, "campaign.html") if err := os.WriteFile(path, []byte("v2"), 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 != "v2" { t.Fatalf("expected the existing template's body updated in place, got %+v", f.templates) } }