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.
This commit is contained in:
Max 2025-10-09 15:43:57 +08:00
parent 03ecf96b51
commit ffbe10f862
4 changed files with 50 additions and 33 deletions

View file

@ -25,12 +25,13 @@ func TestTemplateRender(t *testing.T) {
t.Fatalf("Failed to get template: %v", err) t.Fatalf("Failed to get template: %v", err)
} }
// Test data // Test data - matching actual template variables
data := types.TemplateData{ data := types.TemplateData{
"to": []string{"test@example.com"}, "to": []string{"test@example.com"},
"team_name": "Awesome Team", "team_name": "Awesome Team",
"inviter_name": "Alice Johnson", "inviter_name": "Alice Johnson",
"invite_link": "https://example.com/invite/abc123", "invitation_link": "https://example.com/invite/abc123",
"expires_at": "2025-10-16 12:00:00 UTC",
} }
// Test rendering // Test rendering
@ -73,12 +74,13 @@ func TestTemplateToMessage(t *testing.T) {
t.Fatalf("Failed to get template: %v", err) t.Fatalf("Failed to get template: %v", err)
} }
// Test data // Test data - matching actual template variables
data := types.TemplateData{ data := types.TemplateData{
"to": []string{"test@example.com", "user@example.com"}, "to": []string{"test@example.com", "user@example.com"},
"team_name": "Awesome Team", "team_name": "Awesome Team",
"inviter_name": "Alice Johnson", "inviter_name": "Alice Johnson",
"invite_link": "https://example.com/invite/abc123", "invitation_link": "https://example.com/invite/abc123",
"expires_at": "2025-10-16 12:00:00 UTC",
} }
// Convert template to message // Convert template to message
@ -87,9 +89,9 @@ func TestTemplateToMessage(t *testing.T) {
t.Fatalf("Failed to convert template to message: %v", err) t.Fatalf("Failed to convert template to message: %v", err)
} }
// Verify message properties // Verify message properties - email type, not "mail"
if message.Type != types.MessageType("mail") { if message.Type != types.MessageTypeEmail {
t.Errorf("Expected message type 'mail', got %s", message.Type) t.Errorf("Expected message type 'email', got %s", message.Type)
} }
if len(message.To) != 2 { if len(message.To) != 2 {
t.Errorf("Expected 2 recipients, got %d", len(message.To)) t.Errorf("Expected 2 recipients, got %d", len(message.To))

View file

@ -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) return "", "", "", fmt.Errorf("failed to parse mail template HTML: %w", err)
} }
// Extract subject from <Subject> tag // Extract subject from <Subject> or <subject> tag (HTML parsers normalize to lowercase)
subject = strings.TrimSpace(doc.Find("Subject").Text()) subject = strings.TrimSpace(doc.Find("subject").Text())
// Extract body content from <Content> tag (using custom tag to avoid HTML parser auto-conversion) // Extract body content from <content> tag (HTML parsers normalize to lowercase)
bodySelection := doc.Find("Content") bodySelection := doc.Find("content")
if bodySelection.Length() == 0 { if bodySelection.Length() == 0 {
return "", "", "", fmt.Errorf("no <Content> tag found in mail template") return "", "", "", fmt.Errorf("no <content> tag found in mail template")
} }
// Get the HTML content of the Content tag // Get the HTML content of the Content tag

View file

@ -73,11 +73,12 @@ func TestTemplate_Render(t *testing.T) {
return return
} }
// Test data // Test data - matching actual template variables
data := types.TemplateData{ data := types.TemplateData{
"team_name": "Awesome Team", "team_name": "Awesome Team",
"inviter_name": "Alice Johnson", "inviter_name": "Alice Johnson",
"invite_link": "https://example.com/invite/abc123", "invitation_link": "https://example.com/invite/abc123",
"expires_at": "2025-10-16 12:00:00 UTC",
} }
// Render template // Render template
@ -93,6 +94,7 @@ func TestTemplate_Render(t *testing.T) {
assert.Contains(t, subject, "Awesome Team") assert.Contains(t, subject, "Awesome Team")
assert.Contains(t, body, "Alice Johnson") assert.Contains(t, body, "Alice Johnson")
assert.Contains(t, body, "https://example.com/invite/abc123") 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 subject: %s", subject)
t.Logf("Rendered body: %s", body) t.Logf("Rendered body: %s", body)
@ -114,12 +116,13 @@ func TestTemplate_ToMessage(t *testing.T) {
return return
} }
// Test data with recipients // Test data with recipients - matching actual template variables
data := types.TemplateData{ data := types.TemplateData{
"to": []string{"test@example.com", "user@example.com"}, "to": []string{"test@example.com", "user@example.com"},
"team_name": "Awesome Team", "team_name": "Awesome Team",
"inviter_name": "Alice Johnson", "inviter_name": "Alice Johnson",
"invite_link": "https://example.com/invite/abc123", "invitation_link": "https://example.com/invite/abc123",
"expires_at": "2025-10-16 12:00:00 UTC",
} }
// Convert template to message // Convert template to message
@ -128,7 +131,7 @@ func TestTemplate_ToMessage(t *testing.T) {
// Verify message properties // Verify message properties
assert.NotNil(t, message) 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.Subject)
assert.NotEmpty(t, message.Body) assert.NotEmpty(t, message.Body)
assert.NotEmpty(t, message.HTML) 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.Subject, "Awesome Team")
assert.Contains(t, message.Body, "Alice Johnson") assert.Contains(t, message.Body, "Alice Johnson")
assert.Contains(t, message.Body, "https://example.com/invite/abc123") 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) t.Logf("Generated message: Subject=%s, To=%v", message.Subject, message.To)
} }
@ -160,12 +164,13 @@ func TestTemplate_SMSTemplate(t *testing.T) {
return return
} }
// Test data // Test data - matching actual template variables
data := types.TemplateData{ data := types.TemplateData{
"to": []string{"+1234567890"}, "to": []string{"+1234567890"},
"team_name": "Awesome Team", "team_name": "Awesome Team",
"inviter_name": "Alice Johnson", "inviter_name": "Alice Johnson",
"invite_link": "https://example.com/invite/abc123", "invitation_link": "https://example.com/invite/abc123",
"expires_at": "2025-10-16 12:00:00 UTC",
} }
// Convert template to message // Convert template to message

View file

@ -228,6 +228,11 @@ var testSystemStores = map[string]string{
func loadSystemStores(t *testing.T, cfg config.Config) error { func loadSystemStores(t *testing.T, cfg config.Config) error {
for id, path := range testSystemStores { 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) raw, err := data.Read(path)
if err != nil { if err != nil {
return err return err
@ -273,6 +278,11 @@ func replaceVars(jsonStr string, vars map[string]string) string {
// loadSystemModels load system models for testing // loadSystemModels load system models for testing
func loadSystemModels(t *testing.T, cfg config.Config) error { func loadSystemModels(t *testing.T, cfg config.Config) error {
for id, path := range testSystemModels { 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) content, err := data.Read(path)
if err != nil { if err != nil {
return err return err