The list block prints items with a dot, dash, number or checkbox bullet,
wrapping a long item with a hanging indent; a checkbox list ticks its
done items as [√], or [x] on code pages without √. It stands alone or
inside a box, and its items, bullet and done fields carry their enums
and limits as jsonschema tags.
internal/pkg/modules holds modules that build ordinary jobs from their
own payloads: a registry by name, an Env with the clock, time zone and
time layout, strict payload decoding that reports every problem, ErrSkip
and PayloadError. The note module prints a boxed note with an optional
checklist and a time stamp in the footer.
Three routes join the route table: POST /api/v1/print/{module} and
POST /api/v1/preview/{module}, which take ?copies and a reserved
?printer, and GET /api/v1/modules. Print and the module route share one
submit path, and preview shares one too. New error codes:
invalid_payload, invalid_query, unknown_printer, module_not_found and
module_failed; a module that builds an invalid receipt is logged and
reported to Sentry once. [format] gains timezone and time_layout, the
binary embeds the time zone database, and uberbringer preview takes
--module.
The note receipt passed on the TM-T88IV, and its bytes are pinned in
TestPaperReceipts. docs/api.md describes the list block, the module
routes and the note payload; docs/swagger.json is regenerated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
11 KiB
Go
170 lines
11 KiB
Go
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: "table"}),
|
|
[]string{`elements[0].children[0] (table): unknown type "table" (inside a box, want text, feed, rule, kv or list)`}},
|
|
{"unknown type", receipt.Element{Type: "table"},
|
|
[]string{`elements[0] (table): unknown type "table" (want text, feed, qr, barcode, image, raw, cut, box, rule, kv or list)`}},
|
|
{"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"}},
|
|
{"no items", receipt.Element{Type: "list"}, []string{"elements[0] (list): items: at least one item is required"}},
|
|
{"too many items", receipt.Element{Type: "list", Items: manyItems(201)}, []string{"elements[0] (list): items: 201 items, limit is 200"}},
|
|
{"empty item", receipt.Element{Type: "list", Items: []string{"a", ""}}, []string{"elements[0] (list): items[1]: must not be empty"}},
|
|
{"bullet", receipt.Element{Type: "list", Bullet: "star", Items: []string{"a"}},
|
|
[]string{"elements[0] (list): bullet must be dot, dash, number or checkbox"}},
|
|
{"done without checkboxes", receipt.Element{Type: "list", Bullet: "number", Items: []string{"a"}, Done: []int{0}},
|
|
[]string{`elements[0] (list): done: only a list with bullet "checkbox" has done items`}},
|
|
{"done out of range and repeated", receipt.Element{Type: "list", Bullet: "checkbox", Items: []string{"a", "b"}, Done: []int{1, 2, -1, 1}},
|
|
[]string{
|
|
"elements[0] (list): done[1]: index 2 is out of range",
|
|
"elements[0] (list): done[2]: index -1 is out of range",
|
|
"elements[0] (list): done[3]: index 1 is repeated",
|
|
}},
|
|
{"list size and columns", receipt.Element{Type: "list", Size: 4, Columns: 300, Items: []string{"a"}},
|
|
[]string{"elements[0] (list): size must be 1-3"}},
|
|
{"list columns", receipt.Element{Type: "list", Columns: 300, Items: []string{"a"}},
|
|
[]string{"elements[0] (list): columns must be 1-255"}},
|
|
{"field that does not apply to a list", receipt.Element{Type: "list", Items: []string{"a"}, Bold: true, Leader: "."},
|
|
[]string{`elements[0] (list): field "bold" does not apply to list`, `elements[0] (list): field "leader" does not apply to list`}},
|
|
{"list fields on an old type", receipt.Element{Type: "text", Text: "x", Items: []string{"a"}, Done: []int{0}},
|
|
[]string{`elements[0] (text): field "items" does not apply to text`, `elements[0] (text): field "done" does not apply to text`}},
|
|
{"list fields inside a box", box(receipt.Element{Type: "list", Items: []string{"a"}, Columns: 10, Font: "b"}),
|
|
[]string{"elements[0].children[0] (list): font is inherited from the box", `elements[0].children[0] (list): field "columns" does not apply to list inside a box`}},
|
|
{"list problems inside a box", box(receipt.Element{Type: "list", Bullet: "checkbox", Items: []string{"a"}, Done: []int{3}}),
|
|
[]string{"elements[0].children[0] (list): done[0]: index 3 is out of range"}},
|
|
|
|
{"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},
|
|
{"a full list", receipt.Element{Type: "list", Bullet: "Checkbox", Columns: 1, Size: 3, Font: "b", Items: manyItems(200), Done: []int{0, 199}}, nil},
|
|
{"a list in a box", box(receipt.Element{Type: "list", Bullet: "checkbox", Items: []string{"a", "b"}, Done: []int{1}}), 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 manyItems(n int) []string {
|
|
items := make([]string, n)
|
|
for i := range items {
|
|
items[i] = "item"
|
|
}
|
|
return items
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|