kereru-forms/backend/internal/pdf/generator_test.go

77 lines
2.1 KiB
Go

package pdf
import (
"bytes"
"strings"
"testing"
"time"
"kereru.forms/internal/database"
)
func formFixture() *database.Form {
now := time.Now()
desc := "A short description"
intro := "Welcome to the survey"
email := "hello@example.com"
placeholder := "Type here"
multiline := true
return &database.Form{
ID: "11111111-1111-1111-1111-111111111111",
FormGroupID: "22222222-2222-2222-2222-222222222222",
Version: 3,
Title: "Kererū feedback",
Description: &desc,
Introduction: &intro,
IsActive: true,
ContactEmail: &email,
CreatedAt: now,
UpdatedAt: now,
Questions: []database.Question{
{ID: "1", Key: "name", Label: "Your name", Type: "text", Required: true, Placeholder: &placeholder},
{ID: "2", Key: "notes", Label: "Notes", Type: "text", Multiline: multiline},
{ID: "3", Key: "happy", Label: "Happy?", Type: "boolean"},
{ID: "4", Key: "score", Label: "Score", Type: "number", MinValue: floatPtr(1), MaxValue: floatPtr(5)},
{ID: "5", Key: "colour", Label: "Colour", Type: "select", Options: []database.QuestionOption{
{Value: "red", Label: "Red"},
{Value: "blue", Label: "Blue"},
}},
{ID: "6", Key: "when", Label: "When", Type: "date", Required: true},
},
}
}
func floatPtr(f float64) *float64 { return &f }
func TestGenerate(t *testing.T) {
data, err := Generate(formFixture())
if err != nil {
t.Fatalf("Generate: %v", err)
}
if len(data) < 500 {
t.Fatalf("pdf is suspiciously small: %d bytes", len(data))
}
if !bytes.HasPrefix(data, []byte("%PDF")) {
t.Fatal("output does not start with %PDF")
}
if !bytes.Contains(data, []byte("%%EOF")) {
t.Fatal("output does not contain the EOF marker")
}
}
func TestSanitize(t *testing.T) {
got := sanitize("Kererū — “quoted” …")
if strings.ContainsAny(got, "ū—") {
t.Fatalf("sanitize left unsupported characters: %q", got)
}
if got != `Kereru - "quoted" ...` {
t.Fatalf("sanitize = %q", got)
}
}
func TestGenerateNil(t *testing.T) {
if _, err := Generate(nil); err == nil {
t.Fatal("expected error for nil form")
}
}