Files
uberbringer/internal/pkg/receipt/golden_test.go
T
57_WolveandClaude Opus 5.5 4562f0cebb
Security / Vulnerability check (push) Successful in 50s
Test / Lint and test (push) Successful in 1m59s
Add box, rule and kv blocks, text folding and receipt preview
Phase 1 of docs/plans/formatters.md: single and double CP437 boxes from
one /print element, and a way to see a receipt without using paper.

- receipt: box, rule and kv elements, laid out when the job renders from
  the printer profile, size and font (never fixed widths). Boxes take a
  title (inline or capped), a footer, padding, fit and columns, and a
  body of text or children (text, feed, kv, and rules as dividers joined
  to the borders). Glyphs a code page lacks fall back per set: double-h
  and double-v to double, then single, then ascii; a missing junction
  switches line weight first.
- Validation names nested elements (elements[0].children[3] (qr)),
  checks fields per block type, allows one level of children and counts
  them toward the 500-element limit. Block fields on the older types are
  problems, as they were unknown fields before.
- Text is composed to NFC and characters the code page lacks fold to
  look-alikes (curly quotes, dashes, ellipsis, arrows, check marks, euro,
  trademark), as the owner approved on 2026-11-29. An emoji sequence
  prints as one '?'. Encodable text is untouched.
- escpos: CodePage.CanEncode and Decode, and Interpret, which reads a
  command stream back as the printer would print it. It takes the
  profile so that it can follow the printer's own wrapping and
  alignment; raw spans stay opaque.
- POST /api/v1/preview and `uberbringer preview` show the lines a job
  prints, as text or JSON with styles, without fetching images.
- Config: [format] max_lines (default 2000), refused with 422
  too_many_lines on preview; printer.box_line_spacing (default 0).

Byte compatibility: testdata/compat fixtures were rendered by 9b91109
before the renderer changed, and still match it and the new renderer.
examples/blocks.json was printed on the TM-T88IV; the owner judged the
border gaps at the default spacing fine (2026-11-30), so
box_line_spacing stays 0, and TestPaperReceipts pins its bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 00:49:45 -06:00

153 lines
3.9 KiB
Go

package receipt_test
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"uberbringer/internal/pkg/escpos"
"uberbringer/internal/pkg/receipt"
)
var update = flag.Bool("update", false, "rewrite testdata/blocks/*.txt from the current renderer")
// goldenCodePages are the code pages every block golden is rendered in, to
// cover the glyph fallbacks.
var goldenCodePages = []escpos.CodePage{escpos.CodePage437, escpos.CodePage850, escpos.CodePage1252}
// TestBlockGoldens previews each testdata/blocks/*.json job on a TM-T88 in
// every golden code page and compares the lines with NAME.CODEPAGE.txt.
// Run with -update to rewrite the .txt files, then review the diff.
func TestBlockGoldens(t *testing.T) {
paths, err := filepath.Glob(filepath.Join("testdata", "blocks", "*.json"))
if err != nil {
t.Fatal(err)
}
if len(paths) == 0 {
t.Fatal("no jobs in testdata/blocks")
}
for _, path := range paths {
job := readJob(t, path)
for _, cp := range goldenCodePages {
name := strings.TrimSuffix(filepath.Base(path), ".json") + "." + cp.String()
t.Run(name, func(t *testing.T) {
got := previewText(t, newPreviewer(t, "tm-t88", cp), job)
golden := filepath.Join("testdata", "blocks", name+".txt")
if *update {
if err := os.WriteFile(golden, []byte(got), 0o644); err != nil {
t.Fatal(err)
}
return
}
want, err := os.ReadFile(golden)
if err != nil {
t.Fatalf("%v (run go test -run TestBlockGoldens -update)", err)
}
if got != string(want) {
t.Errorf("preview differs from %s:\n%s", golden, diffLines(string(want), got))
}
})
}
}
}
// TestExampleJobs keeps the jobs in examples/ valid: each previews on a
// TM-T88 in CP437 with nothing printed as '?'.
func TestExampleJobs(t *testing.T) {
paths, err := filepath.Glob(filepath.Join("..", "..", "..", "examples", "*.json"))
if err != nil {
t.Fatal(err)
}
if len(paths) == 0 {
t.Fatal("no jobs in examples/")
}
for _, path := range paths {
t.Run(filepath.Base(path), func(t *testing.T) {
p, err := newPreviewer(t, "tm-t88", escpos.CodePage437).Preview(context.Background(), readJob(t, path))
if err != nil {
t.Fatal(err)
}
if len(p.Warnings) > 0 {
t.Errorf("warnings: %q", p.Warnings)
}
})
}
}
func readJob(t *testing.T, path string) receipt.Job {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var job receipt.Job
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
if err := dec.Decode(&job); err != nil {
t.Fatalf("%s: %v", path, err)
}
return job
}
func newPreviewer(t *testing.T, model string, cp escpos.CodePage) *receipt.Renderer {
t.Helper()
profile, err := escpos.LookupProfile(model)
if err != nil {
t.Fatal(err)
}
return receipt.NewRenderer(receipt.Options{
Profile: profile,
CodePage: cp,
MaxPixels: 10_000_000,
MaxHeight: 4000,
MaxLines: 2000,
}, &fakeFetcher{})
}
// previewText is the preview of job as text: its lines, then its warnings.
func previewText(t *testing.T, r *receipt.Renderer, job receipt.Job) string {
t.Helper()
p, err := r.Preview(context.Background(), job)
if err != nil {
t.Fatalf("Preview: %v", err)
}
var b strings.Builder
for _, l := range p.Lines {
b.WriteString(l.Text)
b.WriteByte('\n')
}
if len(p.Warnings) > 0 {
b.WriteString("=== warnings\n")
for _, w := range p.Warnings {
b.WriteString(w)
b.WriteByte('\n')
}
}
return b.String()
}
// diffLines shows the lines of got that differ from want.
func diffLines(want, got string) string {
w, g := strings.Split(want, "\n"), strings.Split(got, "\n")
var b strings.Builder
for i := range max(len(w), len(g)) {
var wl, gl string
if i < len(w) {
wl = w[i]
}
if i < len(g) {
gl = g[i]
}
if wl != gl {
fmt.Fprintf(&b, "line %d:\n want |%s|\n got |%s|\n", i+1, wl, gl)
}
}
return b.String()
}