From ffbe10f862dc879f087c62f0bf7c270d24486c1a Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 9 Oct 2025 15:43:57 +0800 Subject: [PATCH] Update template tests to align with actual template variables and improve consistency - Modified test data in template tests to include 'invitation_link' and 'expires_at' fields, ensuring alignment with the actual template structure. - Changed message type verification from "mail" to "email" for clarity and accuracy. - Enhanced comments for better understanding of test data relevance. --- messenger/template/render_test.go | 28 ++++++++++++----------- messenger/template/template.go | 10 ++++----- messenger/template/template_test.go | 35 ++++++++++++++++------------- test/utils.go | 10 +++++++++ 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/messenger/template/render_test.go b/messenger/template/render_test.go index 4465bf2a..6144296a 100644 --- a/messenger/template/render_test.go +++ b/messenger/template/render_test.go @@ -25,12 +25,13 @@ func TestTemplateRender(t *testing.T) { t.Fatalf("Failed to get template: %v", err) } - // Test data + // Test data - matching actual template variables data := types.TemplateData{ - "to": []string{"test@example.com"}, - "team_name": "Awesome Team", - "inviter_name": "Alice Johnson", - "invite_link": "https://example.com/invite/abc123", + "to": []string{"test@example.com"}, + "team_name": "Awesome Team", + "inviter_name": "Alice Johnson", + "invitation_link": "https://example.com/invite/abc123", + "expires_at": "2025-10-16 12:00:00 UTC", } // Test rendering @@ -73,12 +74,13 @@ func TestTemplateToMessage(t *testing.T) { t.Fatalf("Failed to get template: %v", err) } - // Test data + // Test data - matching actual template variables data := types.TemplateData{ - "to": []string{"test@example.com", "user@example.com"}, - "team_name": "Awesome Team", - "inviter_name": "Alice Johnson", - "invite_link": "https://example.com/invite/abc123", + "to": []string{"test@example.com", "user@example.com"}, + "team_name": "Awesome Team", + "inviter_name": "Alice Johnson", + "invitation_link": "https://example.com/invite/abc123", + "expires_at": "2025-10-16 12:00:00 UTC", } // Convert template to message @@ -87,9 +89,9 @@ func TestTemplateToMessage(t *testing.T) { t.Fatalf("Failed to convert template to message: %v", err) } - // Verify message properties - if message.Type != types.MessageType("mail") { - t.Errorf("Expected message type 'mail', got %s", message.Type) + // Verify message properties - email type, not "mail" + if message.Type != types.MessageTypeEmail { + t.Errorf("Expected message type 'email', got %s", message.Type) } if len(message.To) != 2 { t.Errorf("Expected 2 recipients, got %d", len(message.To)) diff --git a/messenger/template/template.go b/messenger/template/template.go index 3294665c..db36d0ea 100644 --- a/messenger/template/template.go +++ b/messenger/template/template.go @@ -246,13 +246,13 @@ func parseMailTemplate(content string) (subject, body, html string, err error) { return "", "", "", fmt.Errorf("failed to parse mail template HTML: %w", err) } - // Extract subject from tag - subject = strings.TrimSpace(doc.Find("Subject").Text()) + // Extract subject from or tag (HTML parsers normalize to lowercase) + subject = strings.TrimSpace(doc.Find("subject").Text()) - // Extract body content from tag (using custom tag to avoid HTML parser auto-conversion) - bodySelection := doc.Find("Content") + // Extract body content from tag (HTML parsers normalize to lowercase) + bodySelection := doc.Find("content") if bodySelection.Length() == 0 { - return "", "", "", fmt.Errorf("no tag found in mail template") + return "", "", "", fmt.Errorf("no tag found in mail template") } // Get the HTML content of the Content tag diff --git a/messenger/template/template_test.go b/messenger/template/template_test.go index 28cf8c15..f6b75bbc 100644 --- a/messenger/template/template_test.go +++ b/messenger/template/template_test.go @@ -73,11 +73,12 @@ func TestTemplate_Render(t *testing.T) { return } - // Test data + // Test data - matching actual template variables data := types.TemplateData{ - "team_name": "Awesome Team", - "inviter_name": "Alice Johnson", - "invite_link": "https://example.com/invite/abc123", + "team_name": "Awesome Team", + "inviter_name": "Alice Johnson", + "invitation_link": "https://example.com/invite/abc123", + "expires_at": "2025-10-16 12:00:00 UTC", } // Render template @@ -93,6 +94,7 @@ func TestTemplate_Render(t *testing.T) { assert.Contains(t, subject, "Awesome Team") assert.Contains(t, body, "Alice Johnson") assert.Contains(t, body, "https://example.com/invite/abc123") + assert.Contains(t, body, "2025-10-16 12:00:00 UTC") t.Logf("Rendered subject: %s", subject) t.Logf("Rendered body: %s", body) @@ -114,12 +116,13 @@ func TestTemplate_ToMessage(t *testing.T) { return } - // Test data with recipients + // Test data with recipients - matching actual template variables data := types.TemplateData{ - "to": []string{"test@example.com", "user@example.com"}, - "team_name": "Awesome Team", - "inviter_name": "Alice Johnson", - "invite_link": "https://example.com/invite/abc123", + "to": []string{"test@example.com", "user@example.com"}, + "team_name": "Awesome Team", + "inviter_name": "Alice Johnson", + "invitation_link": "https://example.com/invite/abc123", + "expires_at": "2025-10-16 12:00:00 UTC", } // Convert template to message @@ -128,7 +131,7 @@ func TestTemplate_ToMessage(t *testing.T) { // Verify message properties assert.NotNil(t, message) - assert.Equal(t, types.MessageType("mail"), message.Type) + assert.Equal(t, types.MessageTypeEmail, message.Type) // Changed from "mail" to MessageTypeEmail assert.NotEmpty(t, message.Subject) assert.NotEmpty(t, message.Body) assert.NotEmpty(t, message.HTML) @@ -140,6 +143,7 @@ func TestTemplate_ToMessage(t *testing.T) { assert.Contains(t, message.Subject, "Awesome Team") assert.Contains(t, message.Body, "Alice Johnson") assert.Contains(t, message.Body, "https://example.com/invite/abc123") + assert.Contains(t, message.Body, "2025-10-16 12:00:00 UTC") t.Logf("Generated message: Subject=%s, To=%v", message.Subject, message.To) } @@ -160,12 +164,13 @@ func TestTemplate_SMSTemplate(t *testing.T) { return } - // Test data + // Test data - matching actual template variables data := types.TemplateData{ - "to": []string{"+1234567890"}, - "team_name": "Awesome Team", - "inviter_name": "Alice Johnson", - "invite_link": "https://example.com/invite/abc123", + "to": []string{"+1234567890"}, + "team_name": "Awesome Team", + "inviter_name": "Alice Johnson", + "invitation_link": "https://example.com/invite/abc123", + "expires_at": "2025-10-16 12:00:00 UTC", } // Convert template to message diff --git a/test/utils.go b/test/utils.go index c3518650..86379764 100644 --- a/test/utils.go +++ b/test/utils.go @@ -228,6 +228,11 @@ var testSystemStores = map[string]string{ func loadSystemStores(t *testing.T, cfg config.Config) error { for id, path := range testSystemStores { + // Check if store already exists, skip if already loaded + if _, err := store.Get(id); err == nil { + continue + } + raw, err := data.Read(path) if err != nil { return err @@ -273,6 +278,11 @@ func replaceVars(jsonStr string, vars map[string]string) string { // loadSystemModels load system models for testing func loadSystemModels(t *testing.T, cfg config.Config) error { for id, path := range testSystemModels { + // Check if model already exists, skip if already loaded + if _, exists := model.Models[id]; exists { + continue + } + content, err := data.Read(path) if err != nil { return err