Add box, rule and kv blocks, text folding and receipt preview
Security / Vulnerability check (push) Successful in 50s
Test / Lint and test (push) Successful in 1m59s

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>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-11-30 00:49:45 -06:00
1 parent 9fdea32b4f
commit 4562f0cebb
109 files changed
+6383 -487

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
package receipt_test
import (
"errors"
"strings"
"testing"
"uberbringer/internal/pkg/receipt"
)
func problems(t *testing.T, job receipt.Job) []string {
t.Helper()
err := job.Validate()
if err == nil {
return nil
}
var ve *receipt.ValidationError
if !errors.As(err, &ve) {
t.Fatalf("err = %v, want *ValidationError", err)
}
return ve.Problems
}
func TestValidateBlocks(t *testing.T) {
text := receipt.Element{Type: "text", Text: "x"}
box := func(children ...receipt.Element) receipt.Element {
return receipt.Element{Type: "box", Children: children}
}
tests := []struct {
name string
el receipt.Element
want []string // the complete list of problems
}{
{"qr inside a box", box(text, text, text, receipt.Element{Type: "qr", Data: "x"}),
[]string{"elements[0].children[3] (qr): not allowed inside a box; place it after the box"}},
{"every type that cannot go in a box", box(
receipt.Element{Type: "box", Text: "x"}, receipt.Element{Type: "image", URL: "https://x/y"},
receipt.Element{Type: "raw", Hex: "1B"}, receipt.Element{Type: "cut"},
receipt.Element{Type: "barcode", Symbology: "ean8", Data: "9638507"}),
[]string{
"elements[0].children[0] (box): not allowed inside a box; place it after the box",
"elements[0].children[1] (image): not allowed inside a box; place it after the box",
"elements[0].children[2] (raw): not allowed inside a box; place it after the box",
"elements[0].children[3] (cut): not allowed inside a box; place it after the box",
"elements[0].children[4] (barcode): not allowed inside a box; place it after the box",
}},
{"unknown child type", box(receipt.Element{Type: "list"}),
[]string{`elements[0].children[0] (list): unknown type "list" (inside a box, want text, feed, rule or kv)`}},
{"child without a type", box(receipt.Element{}),
[]string{"elements[0].children[0] (): type is required"}},
{"divider style", box(text, receipt.Element{Type: "rule", Style: "dark"}),
[]string{"elements[0].children[1] (rule): inside a box, style must be single or double"}},
{"text and children", receipt.Element{Type: "box", Text: "x", Children: []receipt.Element{text}},
[]string{"elements[0] (box): set text or children, not both"}},
{"no body", receipt.Element{Type: "box", Title: "x"},
[]string{"elements[0] (box): text or children is required"}},
{"pair without a key", receipt.Element{Type: "kv", Pairs: []receipt.Pair{{Key: "a"}, {Key: "b"}, {Value: "c"}}},
[]string{"elements[0] (kv): pairs[2]: key is required"}},
{"field that does not apply to a block", receipt.Element{Type: "rule", Pairs: []receipt.Pair{{Key: "a"}}, Data: "d"},
[]string{`elements[0] (rule): field "data" does not apply to rule`, `elements[0] (rule): field "pairs" does not apply to rule`}},
{"block field on an old type", receipt.Element{Type: "text", Text: "x", Title: "t", Children: []receipt.Element{text}},
[]string{`elements[0] (text): field "children" does not apply to text`, `elements[0] (text): field "title" does not apply to text`}},
{"inherited fields", box(receipt.Element{Type: "text", Text: "x", Size: 2, Font: "b"}),
[]string{"elements[0].children[0] (text): size is inherited from the box", "elements[0].children[0] (text): font is inherited from the box"}},
{"newline false inside a box", box(receipt.Element{Type: "text", Text: "x", Newline: boolPtr(false)}),
[]string{"elements[0].children[0] (text): newline:false is not allowed inside a box"}},
{"columns on a child", box(receipt.Element{Type: "kv", Pairs: []receipt.Pair{{Key: "a"}}, Columns: 10}),
[]string{`elements[0].children[0] (kv): field "columns" does not apply to kv inside a box`}},
{"child problems and box problems together", receipt.Element{Type: "box", Padding: intPtr(4), Children: []receipt.Element{{Type: "feed", Lines: 300}}},
[]string{"elements[0] (box): padding must be 0-3", "elements[0].children[0] (feed): lines must be 1-255"}},
{"box size", receipt.Element{Type: "box", Text: "x", Size: 4}, []string{"elements[0] (box): size must be 1-3"}},
{"box style", receipt.Element{Type: "box", Text: "x", Style: "triple"},
[]string{"elements[0] (box): style must be single, double, double-h, double-v or ascii"}},
{"title style", receipt.Element{Type: "box", Text: "x", TitleStyle: "boxed"}, []string{"elements[0] (box): title_style must be inline or capped"}},
{"box columns", receipt.Element{Type: "box", Text: "x", Columns: -1}, []string{"elements[0] (box): columns must be 1-255"}},
{"box underline", receipt.Element{Type: "box", Text: "x", Underline: 3}, []string{"elements[0] (box): underline must be 0, 1 or 2"}},
{"box font", receipt.Element{Type: "box", Text: "x", Font: "c"}, []string{"elements[0] (box): font must be a or b"}},
{"rule style", receipt.Element{Type: "rule", Style: "wavy"},
[]string{"elements[0] (rule): style must be single, double, light, medium, dark, solid, dashed, dotted or ascii"}},
{"rule align", receipt.Element{Type: "rule", Align: "middle"}, []string{"elements[0] (rule): align must be left, center or right"}},
{"leader", receipt.Element{Type: "kv", Leader: "*", Pairs: []receipt.Pair{{Key: "a"}}},
[]string{`elements[0] (kv): leader must be ".", "·", " ", "_", "-" or "─"`}},
{"no pairs", receipt.Element{Type: "kv"}, []string{"elements[0] (kv): pairs: at least one pair is required"}},
{"too many pairs", receipt.Element{Type: "kv", Pairs: manyPairs(101)}, []string{"elements[0] (kv): pairs: 101 pairs, limit is 100"}},
{"kv size", receipt.Element{Type: "kv", Size: 9, Pairs: []receipt.Pair{{Key: "a"}}}, []string{"elements[0] (kv): size must be 1-3"}},
{"old types stay lenient", receipt.Element{Type: "feed", Text: "ignored", Bold: true}, nil},
{"a full box", receipt.Element{
Type: "box", Style: "double-v", Title: "t", TitleStyle: "capped", Footer: "f", Padding: intPtr(0),
Fit: true, Columns: 30, Size: 2, Font: "b", Align: "right", Bold: true, Underline: 2, Invert: true, Children: []receipt.Element{
{Type: "text", Text: "x", Bold: true, Underline: 1, Invert: true, Align: "center", Wrap: boolPtr(false), Newline: boolPtr(true)},
{Type: "feed", Lines: 2},
{Type: "rule", Style: "double", Text: "l", Align: "left", Bold: true},
{Type: "kv", Leader: "─", Pairs: []receipt.Pair{{Key: "k", Value: "v", Bold: true}}},
}}, nil},
{"a full rule", receipt.Element{Type: "rule", Style: "dashed", Text: "l", Align: "right", Bold: true, Columns: 255, Size: 3, Font: "a"}, nil},
{"a full kv", receipt.Element{Type: "kv", Leader: "space", Columns: 1, Size: 1, Font: "B", Pairs: manyPairs(100)}, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := problems(t, receipt.Job{Elements: []receipt.Element{tt.el}})
if strings.Join(got, "\n") != strings.Join(tt.want, "\n") {
t.Errorf("problems:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(tt.want, "\n"))
}
})
}
}
func TestValidateCountsChildren(t *testing.T) {
children := make([]receipt.Element, receipt.MaxElements)
for i := range children {
children[i] = receipt.Element{Type: "feed"}
}
got := problems(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Children: children}}})
want := "elements: 501 elements including 500 box children, limit is 500"
if len(got) != 1 || got[0] != want {
t.Errorf("problems = %q, want %q", got, want)
}
if got := problems(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Children: children[:499]}}}); got != nil {
t.Errorf("500 elements with children: %q", got)
}
}
func TestValidateNestingIsOneLevel(t *testing.T) {
inner := receipt.Element{Type: "box", Children: []receipt.Element{{Type: "qr"}}}
got := problems(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Children: []receipt.Element{inner}}}})
// The inner box is refused as a whole; its own children are not looked at.
want := "elements[0].children[0] (box): not allowed inside a box; place it after the box"
if len(got) != 1 || got[0] != want {
t.Errorf("problems = %q, want %q", got, want)
}
}