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>
121 lines
4.6 KiB
Go
121 lines
4.6 KiB
Go
package receipt
|
||
|
||
import (
|
||
"reflect"
|
||
"strings"
|
||
"testing"
|
||
|
||
"uberbringer/internal/pkg/escpos"
|
||
)
|
||
|
||
func TestNormalize(t *testing.T) {
|
||
cp437, cp850, cp858, cp1252 := escpos.CodePage437, escpos.CodePage850, escpos.CodePage858, escpos.CodePage1252
|
||
tests := []struct {
|
||
name string
|
||
cp escpos.CodePage
|
||
in string
|
||
want string
|
||
unknown int
|
||
}{
|
||
{"decomposed é becomes one rune", cp437, "cafe\u0301", "café", 0},
|
||
{"decomposed ü too", cp850, "u\u0308ber", "über", 0},
|
||
{"smart quotes", cp437, "“double” „low” ‘single’", `"double" "low" 'single'`, 0},
|
||
{"dashes", cp437, "a–b—c", "a-b--c", 0},
|
||
{"ellipsis", cp437, "wait…", "wait...", 0},
|
||
{"bullet", cp437, "• item", "· item", 0},
|
||
{"arrows", cp437, "a → b ← c", "a -> b <- c", 0},
|
||
{"check marks", cp437, "✓ ✔", "√ √", 0},
|
||
{"check marks without √", cp850, "✓ ✔", "x x", 0},
|
||
{"times", cp437, "3×", "3x", 0},
|
||
{"times kept where it prints", cp850, "3×", "3×", 0},
|
||
{"euro", cp437, "5 €", "5 EUR", 0},
|
||
{"euro kept on cp858", cp858, "5 €", "5 €", 0},
|
||
{"trademark", cp437, "Brand™", "BrandTM", 0},
|
||
{"cp1252 keeps its own typography", cp1252, "“q” – — … • € ™", "“q” – — … • € ™", 0},
|
||
{"one emoji", cp437, "a 🎉 b", "a ? b", 1},
|
||
{"skin tone", cp437, "👍🏽", "?", 1},
|
||
{"zero-width joiner sequence", cp437, "👨\u200D👩\u200D👧 family", "? family", 1},
|
||
{"flags pair up", cp437, "🇩🇪🇫🇷", "??", 2},
|
||
{"tag sequence", cp437, "🏴\U000E0067\U000E0062\U000E0065\U000E006E\U000E0067\U000E007F!", "?!", 1},
|
||
{"variation selector after an emoji", cp437, "⚠\uFE0F careful", "? careful", 1},
|
||
{"keycap", cp437, "1\uFE0F\u20E3 first", "1 first", 0},
|
||
{"VS16 after a printable character", cp437, "x\uFE0F", "x", 0},
|
||
{"zero-width characters dropped", cp437, "a\u200Bb\u200Cc\u200Dd", "abcd", 0},
|
||
{"other missing characters", cp437, "漢字 ¤", "?? ?", 3},
|
||
{"controls stripped", cp437, "a\x1b@b\r\n\tc\x00", "a@b\n\tc", 0},
|
||
{"two emoji are two", cp437, "🎉🎉", "??", 2},
|
||
}
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
got, unknown := normalize(tt.in, tt.cp)
|
||
if got != tt.want || unknown != tt.unknown {
|
||
t.Errorf("normalize(%q, %s) = %q, %d; want %q, %d", tt.in, tt.cp, got, unknown, tt.want, tt.unknown)
|
||
}
|
||
for _, r := range got {
|
||
if r != '\n' && r != '\t' && !tt.cp.CanEncode(r) {
|
||
t.Errorf("result %q has %q, which %s cannot print", got, r, tt.cp)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestNormalizeLeavesPrintableTextAlone(t *testing.T) {
|
||
samples := map[escpos.CodePage]string{
|
||
escpos.CodePage437: "┌─┐│└┘ ╔═╗ ░▒▓█ £ é ü Ω ·√° \u00A0 plain ASCII ~!@#",
|
||
escpos.CodePage850: "╔═╗ ┌─┐ ░▒▓ × © ® Ø ß £ àéîõü \u00A0",
|
||
escpos.CodePage858: "€ ╔═╗ ß",
|
||
escpos.CodePage1252: "“q” ‘q’ – — … • € ™ × é",
|
||
}
|
||
for cp, s := range samples {
|
||
if got, unknown := normalize(s, cp); got != s || unknown != 0 {
|
||
t.Errorf("%s: normalize(%q) = %q, %d", cp, s, got, unknown)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestStripEmoji(t *testing.T) {
|
||
tests := []struct{ in, want string }{
|
||
{"plain text", "plain text"},
|
||
{"Party 🎉", "Party"},
|
||
{"🎉 Party", "Party"},
|
||
{"Backup 🎉 done", "Backup done"},
|
||
{"Backup 🎉 done", "Backup done"},
|
||
{"a🎉b", "ab"},
|
||
{"Done 🎉\nNext 🎉 line", "Done\nNext line"},
|
||
{"👍🏽 skin tone", "skin tone"},
|
||
{"👨\u200D👩\u200D👧 family", "family"},
|
||
{"🇬🇧 flag", "flag"},
|
||
{"🏴\U000E0067\U000E0062\U000E0065\U000E006E\U000E0067\U000E007F tag sequence", "tag sequence"},
|
||
{"⚠\uFE0F careful ❤\uFE0F", "careful"},
|
||
{"1\uFE0F\u20E3 keycap", "1 keycap"},
|
||
{"zero\u200Bwidth", "zerowidth"},
|
||
// Symbols a code page prints or folds stay.
|
||
{"■ → ← ✓ ✔ ⌐ √ ° ·", "■ → ← ✓ ✔ ⌐ √ ° ·"},
|
||
{"box ┌─┐ ░▒▓█", "box ┌─┐ ░▒▓█"},
|
||
{"✔\uFE0F checked", "✔ checked"},
|
||
{"", ""},
|
||
{"🎉", ""},
|
||
}
|
||
for _, tt := range tests {
|
||
if got := StripEmoji(tt.in); got != tt.want {
|
||
t.Errorf("StripEmoji(%q) = %q, want %q", tt.in, got, tt.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestNormalizeRunsBeforeWrap(t *testing.T) {
|
||
// The ellipsis takes three columns, so "word…" no longer fits in 6.
|
||
text, _ := normalize("ab word…", escpos.CodePage437)
|
||
if got := wrap(text, 6, 6); !reflect.DeepEqual(got, []string{"ab", "word..", "."}) {
|
||
t.Errorf("wrap = %q", got)
|
||
}
|
||
line, _ := normalizeLine("a\nb\tc", escpos.CodePage437)
|
||
if line != "a b c" {
|
||
t.Errorf("normalizeLine = %q", line)
|
||
}
|
||
if strings.ContainsAny(line, "\n\t") {
|
||
t.Error("normalizeLine kept a newline or tab")
|
||
}
|
||
}
|