Go CLI that turns the markdown currency of this stack (Redmine notes, Discourse posts, briefing output) into typeset PDFs: two embedded typst templates (report with title page/TOC/headers, dense brief), front-matter (title/subtitle/author/date/classification/template), GFM tables, and bar charts rendered in pure Go (go-chart) from fenced chart data blocks. Engine is a prebuilt typst 0.15.1 container pinned by digest and run --network none; PDF bytes to stdout or -o. Exit codes 0/1/2. All dev in docker (dev.sh/Makefile); unit tests + golden typst fixtures plus a host-side smoke against the real engine container. Generated with Crush Assisted-by: Crush:glm-5.2
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package pdfinfo
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// fakePDF builds a minimal but structurally scannable PDF with a page-tree
|
|
// /Count and N /Type /Page objects.
|
|
func fakePDF(pages, declared int) []byte {
|
|
var objs []string
|
|
for i := 1; i <= pages; i++ {
|
|
objs = append(objs, fmt.Sprintf("%d 0 obj<</Type/Page/Parent 99 0 R>>endobj", i))
|
|
}
|
|
objs = append(objs, fmt.Sprintf("99 0 obj<</Type/Pages/Count %d/Kids[1 0 R]>>endobj", declared))
|
|
body := strings.Join(objs, "\n")
|
|
return []byte("%PDF-1.7\n" + body + "\n%%EOF")
|
|
}
|
|
|
|
func TestPagesCount(t *testing.T) {
|
|
for _, n := range []int{1, 2, 7, 23} {
|
|
got, err := Pages(fakePDF(n, n))
|
|
if err != nil {
|
|
t.Fatalf("n=%d: %v", n, err)
|
|
}
|
|
if got != n {
|
|
t.Fatalf("n=%d got %d", n, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPagesFallsBackToPageObjects(t *testing.T) {
|
|
// No /Pages /Count reachable: count /Type /Page objects instead.
|
|
pdf := []byte("%PDF-1.7\n1 0 obj<</Type/Page>>endobj\n2 0 obj<</Type/Page>>endobj\n%%EOF")
|
|
got, err := Pages(pdf)
|
|
if err != nil || got != 2 {
|
|
t.Fatalf("got=%d err=%v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestPagesRejectsNonPDF(t *testing.T) {
|
|
if _, err := Pages([]byte("hello")); err == nil {
|
|
t.Fatal("want error for non-PDF")
|
|
}
|
|
if _, err := Pages(nil); err == nil {
|
|
t.Fatal("want error for empty input")
|
|
}
|
|
}
|
|
|
|
func TestPagesDoesNotCountPagesType(t *testing.T) {
|
|
// "/Type /Pages" must not match the /Type /Page fallback.
|
|
pdf := []byte("%PDF-1.7\n1 0 obj<</Type/Page>>endobj\n2 0 obj<</Type/Pages/Count 5>>endobj\n%%EOF")
|
|
got, err := Pages(pdf)
|
|
if err != nil || got != 5 {
|
|
t.Fatalf("got=%d err=%v", got, err)
|
|
}
|
|
}
|