Files
uberbringer/internal/pkg/receipt/validate_test.go
T
57_WolveandClaude Opus 5.5 1335ed0687
Security / Vulnerability check (push) Successful in 41s
Test / Lint and test (push) Successful in 1m57s
Add the notify and ntfy modules and scoped tokens
Breaking: a job with a raw element prints only with a token issued with
uberbringer token --raw. Every other token, every existing one included,
gets 403 token_scope for it, with the raw elements in details, and keeps
every other route.

The notify module prints a notification card from a title, message,
priority and tags, with topic, click, up to three actions, image,
markdown and time as options; it decodes strictly and refuses a payload
over its length caps. The ntfy module takes one ntfy message as ntfy
sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does
not know, cuts text over the caps, and answers 204 for other events and
binary bodies. Both print every message plain: one message cannot tell
the first of a sequence from a replacement, since a publisher may choose
the sequence ID. internal/pkg/ntfy holds ntfy's message types.

Both print the same card. The priority picks the form: a dark URGENT
rule and a double box, a double box headed HIGH, a single box with the
title in its border, or a rule with no box. Tags print as markers in
front of the title, the rest on a Tags row; links print as QR codes with
their labels; attached PNG, JPEG, GIF and WebP images print after the
card. A line-level markdown subset is laid out without a dependency,
keeping marks inside code spans and URLs, and emoji are removed after a
few typed ones become markers. A card that cannot be laid out prints as
a plain receipt, and the server logs the fault once at ERROR and reports
it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the
markers table is a setting with the user's own keys, also settable as
UBERBRINGER_NTFY_MARKERS=name=value,...

uberbringer token --modules issues a token limited to those modules'
print and preview routes, job status and the module list; each route in
the table says which tokens it takes, and a scope claim that could read
as absent makes the token invalid. image elements take optional, which
also shrinks a too-wide image to the paper; a QR code that could be too
wide for the paper prints with smaller modules; box takes title_bold.
Submit takes a Meta whose source shows in job status and, with a copy
of the request ID, in every worker log line of the job.

The four ntfy cards passed on the TM-T88IV, posted as the client recipe
posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md
describes the modules, the card and the client recipe, with tokens read
from a header file; docs/swagger.json is regenerated.

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

182 lines
12 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"}},
{"title_bold on another type", receipt.Element{Type: "rule", TitleBold: true},
[]string{`elements[0] (rule): field "title_bold" does not apply to rule`}},
{"title_bold on an old type", receipt.Element{Type: "text", Text: "x", TitleBold: true},
[]string{`elements[0] (text): field "title_bold" does not apply to text`}},
{"optional on another type", receipt.Element{Type: "qr", Data: "x", Optional: true},
[]string{`elements[0] (qr): field "optional" does not apply to qr`}},
{"optional on a block", receipt.Element{Type: "kv", Pairs: []receipt.Pair{{Key: "a"}}, Optional: true},
[]string{`elements[0] (kv): field "optional" does not apply to kv`}},
{"optional inside a box", box(receipt.Element{Type: "text", Text: "x", Optional: true}),
[]string{`elements[0].children[0] (text): field "optional" does not apply to text inside a box`}},
{"an optional image", receipt.Element{Type: "image", URL: "https://x/y.png", Optional: true, Width: 384, Align: "center"}, nil},
{"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", TitleBold: true, 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)
}
}