summaryrefslogtreecommitdiff
path: root/internal/reports/pdf_test.go
diff options
context:
space:
mode:
authorT <t@tjp.lol>2025-08-04 09:49:52 -0600
committerT <t@tjp.lol>2025-08-04 15:15:18 -0600
commit56e0af3b41742876b471332aeb943a5a2ca8dfbf (patch)
treeef75f4900107ef28977823eabd11ec3014cd40ba /internal/reports/pdf_test.go
parent4c29dfee9be26996ce548e2edf0328422df598d0 (diff)
Generate invoice PDFs
Diffstat (limited to 'internal/reports/pdf_test.go')
-rw-r--r--internal/reports/pdf_test.go122
1 files changed, 122 insertions, 0 deletions
diff --git a/internal/reports/pdf_test.go b/internal/reports/pdf_test.go
new file mode 100644
index 0000000..e1c4020
--- /dev/null
+++ b/internal/reports/pdf_test.go
@@ -0,0 +1,122 @@
+package reports
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+ "punchcard/templates"
+ "testing"
+ "time"
+)
+
+// Helper function for tests
+func mustParseDate(dateStr string) time.Time {
+ t, err := time.Parse("2006-01-02", dateStr)
+ if err != nil {
+ panic(err)
+ }
+ return t
+}
+
+func TestTypstTemplateCompilation(t *testing.T) {
+ // Check if Typst is installed
+ if err := checkTypstInstalled(); err != nil {
+ t.Skip("Typst is not installed, skipping template compilation test")
+ }
+
+ // Create temporary directory
+ tempDir, err := os.MkdirTemp("", "punchcard-test")
+ if err != nil {
+ t.Fatalf("Failed to create temp directory: %v", err)
+ }
+ defer os.RemoveAll(tempDir)
+
+ // Copy test data to temp directory
+ testDataPath := filepath.Join("testdata", "invoice_test_data.json")
+ testData, err := os.ReadFile(testDataPath)
+ if err != nil {
+ t.Fatalf("Failed to read test data: %v", err)
+ }
+
+ dataFile := filepath.Join(tempDir, "data.json")
+ if err := os.WriteFile(dataFile, testData, 0644); err != nil {
+ t.Fatalf("Failed to write test data file: %v", err)
+ }
+
+ // Write Typst template to temp directory
+ typstFile := filepath.Join(tempDir, "invoice.typ")
+ if err := os.WriteFile(typstFile, []byte(templates.InvoiceTemplate), 0644); err != nil {
+ t.Fatalf("Failed to write Typst template: %v", err)
+ }
+
+ // Compile with Typst
+ outputPDF := filepath.Join(tempDir, "test-invoice.pdf")
+ cmd := exec.Command("typst", "compile", typstFile, outputPDF)
+ cmd.Dir = tempDir
+
+ if output, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("Typst compilation failed: %v\nOutput: %s", err, string(output))
+ }
+
+ // Verify PDF was created
+ if _, err := os.Stat(outputPDF); os.IsNotExist(err) {
+ t.Fatalf("PDF file was not created")
+ }
+
+ t.Logf("Successfully compiled Typst template to PDF")
+}
+
+func TestGenerateInvoicePDF(t *testing.T) {
+ // Check if Typst is installed
+ if err := checkTypstInstalled(); err != nil {
+ t.Skip("Typst is not installed, skipping PDF generation test")
+ }
+
+ // Create test invoice data
+ invoiceData := &InvoiceData{
+ ClientName: "Test Client Co.",
+ ProjectName: "Test Project",
+ DateRange: DateRange{
+ Start: mustParseDate("2025-07-01"),
+ End: mustParseDate("2025-07-31"),
+ },
+ LineItems: []LineItem{
+ {
+ Description: "Software development",
+ Hours: 8.5,
+ Rate: 150.0,
+ Amount: 1275.0,
+ },
+ {
+ Description: "Code review",
+ Hours: 1.5,
+ Rate: 150.0,
+ Amount: 225.0,
+ },
+ },
+ TotalHours: 10.0,
+ TotalAmount: 1500.0,
+ GeneratedDate: mustParseDate("2025-08-04"),
+ }
+
+ // Create temporary output file
+ tempDir, err := os.MkdirTemp("", "punchcard-pdf-test")
+ if err != nil {
+ t.Fatalf("Failed to create temp directory: %v", err)
+ }
+ defer os.RemoveAll(tempDir)
+
+ outputPath := filepath.Join(tempDir, "test-invoice.pdf")
+
+ // Generate PDF
+ if err := GenerateInvoicePDF(invoiceData, outputPath); err != nil {
+ t.Fatalf("Failed to generate invoice PDF: %v", err)
+ }
+
+ // Verify PDF was created
+ if _, err := os.Stat(outputPath); os.IsNotExist(err) {
+ t.Fatalf("PDF file was not created at %s", outputPath)
+ }
+
+ t.Logf("Successfully generated invoice PDF at %s", outputPath)
+}