Files
bamort/backend/pdfrender/pagination_utils_test.go
T
Frank 59fe69d35d refactor: Unify PDF pagination system and rename templates
BREAKING CHANGE: Template names changed from page1_stats.html to page_1.html

## Phase 1: Unified Pagination Function
- Implemented PaginateMultiList() to replace PaginateSkills(), PaginateSpells(), and PaginatePage2PlayLists()
- Single metadata-driven function handles all list types (skills, weapons, spells, equipment)
- Properly handles filters (learned/unlearned/language) via template metadata
- Shares list trackers by ListType+Filter combination to avoid duplication
- Added comprehensive tests for all edge cases

## Phase 2: Template Naming Convention
- Renamed templates to be data-agnostic:
  - page1_stats.html -> page_1.html
  - page1.2_stats.html -> page_1.2.html
  - page2_play.html -> page_2.html
  - page2.2_play.html -> page_2.2.html
  - page3_spell.html -> page_3.html
  - page3.2_spell.html -> page_3.2.html
  - page4_equip.html -> page_4.html
- Updated GenerateContinuationTemplateName() for new naming (page_1.html -> page_1.2.html)
- Updated ExtractBaseTemplateName() to handle new format
- Updated all test files and source files with new template names

## Phase 3: Simplified RenderPageWithContinuations
- Removed hardcoded switch statements based on template names
- Replaced with generic dataMap and unified pagination call
- Extracted populatePageDataFromDistribution() to handle data mapping
- Template type detection now driven by metadata, not hardcoded names

## Benefits
-  Extensibility: Add new templates without code changes
-  Maintainability: One pagination algorithm instead of three
-  Clarity: Template names reflect page numbers, not content types
-  Flexibility: Templates can mix any data types
-  All 40+ tests passing

## Technical Details
- Added SkillsColumn3 and SkillsColumn4 fields to PageData for continuation pages
- Template metadata loaded from HTML comments drives pagination behavior
- Backward compatibility maintained for old template references in comments
2025-12-21 22:07:46 +01:00

105 lines
2.3 KiB
Go

package pdfrender
import "testing"
func TestGenerateContinuationTemplateName(t *testing.T) {
tests := []struct {
name string
template string
pageNum int
expected string
}{
{
name: "First page returns original",
template: "page_1.html",
pageNum: 1,
expected: "page_1.html",
},
{
name: "Second page gets continuation name",
template: "page_1.html",
pageNum: 2,
expected: "page_1.2.html",
},
{
name: "Third page uses same continuation template (.2)",
template: "page_2.html",
pageNum: 3,
expected: "page_2.2.html",
},
{
name: "All continuation pages use .2 template",
template: "page_1.html",
pageNum: 10,
expected: "page_1.2.html",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GenerateContinuationTemplateName(tt.template, tt.pageNum)
if result != tt.expected {
t.Errorf("Expected '%s', got '%s'", tt.expected, result)
}
})
}
}
func TestExtractBaseTemplateName(t *testing.T) {
tests := []struct {
name string
template string
expected string
}{
{
name: "Base template returns itself",
template: "page_1.html",
expected: "page_1.html",
},
{
name: "Continuation template returns base",
template: "page_1.2.html",
expected: "page_1.html",
},
{
name: "Multiple digit continuation",
template: "page_1.10.html",
expected: "page_1.html",
},
{
name: "Different page type",
template: "page_2.3.html",
expected: "page_2.html",
},
{
name: "Template without underscore",
template: "simple.html",
expected: "simple.html",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractBaseTemplateName(tt.template)
if result != tt.expected {
t.Errorf("Expected '%s', got '%s'", tt.expected, result)
}
})
}
}
func TestContinuationRoundTrip(t *testing.T) {
// Test that generating a continuation name and extracting the base works correctly
original := "page_1.html"
for pageNum := 1; pageNum <= 5; pageNum++ {
continuation := GenerateContinuationTemplateName(original, pageNum)
extracted := ExtractBaseTemplateName(continuation)
if extracted != original {
t.Errorf("Round trip failed for page %d: original '%s', continuation '%s', extracted '%s'",
pageNum, original, continuation, extracted)
}
}
}