2025-12-18 22:59:33 +01:00
|
|
|
package pdfrender
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLoadTemplate_Success(t *testing.T) {
|
|
|
|
|
// Arrange
|
|
|
|
|
loader := NewTemplateLoader("../templates/Default_A4_Quer")
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
err := loader.LoadTemplates()
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that templates were loaded
|
|
|
|
|
if loader.templates == nil {
|
|
|
|
|
t.Error("Expected templates to be loaded, got nil")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRenderTemplate_BasicData(t *testing.T) {
|
|
|
|
|
// Arrange
|
|
|
|
|
loader := NewTemplateLoader("../templates/Default_A4_Quer")
|
|
|
|
|
err := loader.LoadTemplates()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to load templates: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := &PageData{
|
|
|
|
|
Character: CharacterInfo{
|
|
|
|
|
Name: "Test Character",
|
|
|
|
|
Grade: 5,
|
|
|
|
|
},
|
|
|
|
|
Attributes: AttributeValues{
|
|
|
|
|
St: 80,
|
|
|
|
|
Gs: 70,
|
|
|
|
|
},
|
|
|
|
|
Meta: PageMeta{
|
|
|
|
|
Date: "18.12.2025",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-12-21 22:07:46 +01:00
|
|
|
html, err := loader.RenderTemplate("page_1.html", data)
|
2025-12-18 22:59:33 +01:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if html == "" {
|
|
|
|
|
t.Error("Expected non-empty HTML, got empty string")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that template variables were replaced
|
|
|
|
|
if !strings.Contains(html, "Test Character") {
|
|
|
|
|
t.Error("Expected HTML to contain 'Test Character'")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(html, "18.12.2025") {
|
|
|
|
|
t.Error("Expected HTML to contain date '18.12.2025'")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetTemplateMetadata(t *testing.T) {
|
|
|
|
|
// Arrange
|
|
|
|
|
loader := NewTemplateLoader("../templates/Default_A4_Quer")
|
|
|
|
|
err := loader.LoadTemplates()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to load templates: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-12-21 22:07:46 +01:00
|
|
|
metadata := loader.GetTemplateMetadata("page_3.html")
|
2025-12-18 22:59:33 +01:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
if len(metadata) == 0 {
|
|
|
|
|
t.Fatal("Expected metadata blocks, got none")
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 22:32:43 +01:00
|
|
|
// Read actual template to get expected MAX values
|
|
|
|
|
templateSet := DefaultA4QuerTemplateSet()
|
|
|
|
|
var page3Template *TemplateWithMeta
|
|
|
|
|
for i := range templateSet.Templates {
|
2025-12-21 22:07:46 +01:00
|
|
|
if templateSet.Templates[i].Metadata.Name == "page_3.html" {
|
2025-12-19 22:32:43 +01:00
|
|
|
page3Template = &templateSet.Templates[i]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if page3Template == nil {
|
2025-12-21 22:07:46 +01:00
|
|
|
t.Fatal("page_3.html template not found")
|
2025-12-19 22:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get expected values from template
|
|
|
|
|
var expectedLeftMax, expectedRightMax int
|
|
|
|
|
for i := range page3Template.Metadata.Blocks {
|
2025-12-22 23:20:49 +01:00
|
|
|
if page3Template.Metadata.Blocks[i].Name == "spells_column1" {
|
2025-12-19 22:32:43 +01:00
|
|
|
expectedLeftMax = page3Template.Metadata.Blocks[i].MaxItems
|
2025-12-22 23:20:49 +01:00
|
|
|
} else if page3Template.Metadata.Blocks[i].Name == "spells_column2" {
|
2025-12-19 22:32:43 +01:00
|
|
|
expectedRightMax = page3Template.Metadata.Blocks[i].MaxItems
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 23:20:49 +01:00
|
|
|
// Check for spells_column1 block
|
|
|
|
|
leftBlock := GetBlockByName(metadata, "spells_column1")
|
2025-12-18 22:59:33 +01:00
|
|
|
if leftBlock == nil {
|
2025-12-22 23:20:49 +01:00
|
|
|
t.Error("Expected to find 'spells_column1' block")
|
2025-12-18 22:59:33 +01:00
|
|
|
} else {
|
2025-12-19 22:32:43 +01:00
|
|
|
if leftBlock.MaxItems != expectedLeftMax {
|
2025-12-22 23:20:49 +01:00
|
|
|
t.Errorf("Expected spells_column1 max %d (from template), got %d", expectedLeftMax, leftBlock.MaxItems)
|
2025-12-18 22:59:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 23:20:49 +01:00
|
|
|
// Check for spells_column2 block
|
|
|
|
|
rightBlock := GetBlockByName(metadata, "spells_column2")
|
2025-12-18 22:59:33 +01:00
|
|
|
if rightBlock == nil {
|
2025-12-22 23:20:49 +01:00
|
|
|
t.Error("Expected to find 'spells_column2' block")
|
2025-12-18 22:59:33 +01:00
|
|
|
} else {
|
2025-12-19 22:32:43 +01:00
|
|
|
if rightBlock.MaxItems != expectedRightMax {
|
2025-12-22 23:20:49 +01:00
|
|
|
t.Errorf("Expected spells_column2 max %d (from template), got %d", expectedRightMax, rightBlock.MaxItems)
|
2025-12-18 22:59:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRenderTemplate_WithSkills(t *testing.T) {
|
|
|
|
|
// Arrange
|
|
|
|
|
loader := NewTemplateLoader("../templates/Default_A4_Quer")
|
|
|
|
|
err := loader.LoadTemplates()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to load templates: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := &PageData{
|
|
|
|
|
Character: CharacterInfo{
|
|
|
|
|
Name: "Test",
|
|
|
|
|
},
|
2025-12-19 08:24:32 +01:00
|
|
|
SkillsColumn1: []SkillViewModel{
|
|
|
|
|
{Name: "Schwimmen", Value: 10, PracticePoints: 2},
|
|
|
|
|
},
|
|
|
|
|
SkillsColumn2: []SkillViewModel{
|
2025-12-18 22:59:33 +01:00
|
|
|
{Name: "Klettern", Value: 8, PracticePoints: 3},
|
|
|
|
|
},
|
|
|
|
|
Meta: PageMeta{
|
|
|
|
|
Date: "18.12.2025",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-12-21 22:07:46 +01:00
|
|
|
html, err := loader.RenderTemplate("page_1.html", data)
|
2025-12-18 22:59:33 +01:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that skills were rendered
|
|
|
|
|
if !strings.Contains(html, "Schwimmen") {
|
|
|
|
|
t.Error("Expected HTML to contain 'Schwimmen'")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(html, "Klettern") {
|
|
|
|
|
t.Error("Expected HTML to contain 'Klettern'")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLoadTemplate_InvalidPath(t *testing.T) {
|
|
|
|
|
// Arrange
|
|
|
|
|
loader := NewTemplateLoader("/invalid/path")
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
err := loader.LoadTemplates()
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Error("Expected error for invalid path, got nil")
|
|
|
|
|
}
|
|
|
|
|
}
|