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>
198 lines
7.6 KiB
Go
198 lines
7.6 KiB
Go
package modules_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"uberbringer/internal/pkg/escpos"
|
|
"uberbringer/internal/pkg/modules"
|
|
"uberbringer/internal/pkg/receipt"
|
|
)
|
|
|
|
// markdownBox lays out text as markdown inside a box, as a card does.
|
|
func markdownBox(text string) (receipt.Job, []modules.Link) {
|
|
body, links := modules.MarkdownBody(text)
|
|
if len(body) == 0 {
|
|
body = []receipt.Element{{Type: receipt.TypeText}}
|
|
}
|
|
return receipt.Job{Elements: []receipt.Element{{Type: receipt.TypeBox, Children: body}}}, links
|
|
}
|
|
|
|
// TestMarkdownGoldens previews testdata/markdown/*.md inside a box.
|
|
func TestMarkdownGoldens(t *testing.T) {
|
|
paths, err := filepath.Glob(filepath.Join("testdata", "markdown", "*.md"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(paths) == 0 {
|
|
t.Fatal("no texts in testdata/markdown")
|
|
}
|
|
for _, path := range paths {
|
|
name := strings.TrimSuffix(filepath.Base(path), ".md")
|
|
t.Run(name, func(t *testing.T) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(data)
|
|
job, links := markdownBox(text)
|
|
got := previewText(t, job)
|
|
for _, l := range links {
|
|
got += "link: " + l.Label + " -> " + l.URL + "\n"
|
|
}
|
|
checkGolden(t, filepath.Join("testdata", "markdown", name+".txt"), got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMarkdownInline(t *testing.T) {
|
|
tests := []struct{ in, want string }{
|
|
{"**/var** is at *94%* on nas01", "/var is at 94% on nas01"},
|
|
{"__bold__ and _it_ and ~~gone~~ and `code`", "bold and it and gone and code"},
|
|
{"snake_case_name and 2 * 3 * 4", "snake_case_name and 2 * 3 * 4"},
|
|
{"_a_ _b_", "a b"},
|
|
{`\*not italic\* and \_kept\_`, "*not italic* and _kept_"},
|
|
{"see [docs](https://example.com/docs) and [local](/x)", "see docs and local"},
|
|
{" and ", "[image: chart] and [image]"},
|
|
{"** not bold **", "** not bold **"},
|
|
// Code spans and bare URLs keep their marks.
|
|
{"run `__init__.py` and `**/*.go`", "run __init__.py and **/*.go"},
|
|
{"see https://host/__debug__/ or *now*", "see https://host/__debug__/ or now"},
|
|
{"`a` **b** `c_d_e` _f_", "a b c_d_e f"},
|
|
// A URL ends before the marks and punctuation that follow it.
|
|
{"**https://example.com/status**", "https://example.com/status"},
|
|
{"**Down: https://status.lan**", "Down: https://status.lan"},
|
|
{"*https://example.com*", "https://example.com"},
|
|
{"_https://example.com/x_", "https://example.com/x"},
|
|
{"~~https://old.example.com~~ moved", "https://old.example.com moved"},
|
|
{"Run https://ci.example.com/job/`nightly` now", "Run https://ci.example.com/job/nightly now"},
|
|
{"see https://example.com/a_(b) and (https://example.com/c).", "see https://example.com/a_(b) and (https://example.com/c)."},
|
|
{"Is it https://example.com?", "Is it https://example.com?"},
|
|
// Marks inside a URL stay part of it.
|
|
{"Home: https://example.com/~bob/__init__.py", "Home: https://example.com/~bob/__init__.py"},
|
|
{"https://example.com/~alice/_drafts_/post", "https://example.com/~alice/_drafts_/post"},
|
|
{"https://example.com/a*b*c", "https://example.com/a*b*c"},
|
|
{"Logs: https://kibana.lan/app/discover#/?_a=(index:'logs-*',query:(language:kuery,query:'host:*'))",
|
|
"Logs: https://kibana.lan/app/discover#/?_a=(index:'logs-*',query:(language:kuery,query:'host:*'))"},
|
|
{"\uE100 literal", " literal"},
|
|
}
|
|
for _, tt := range tests {
|
|
body, _ := modules.MarkdownBody(tt.in)
|
|
if len(body) != 1 || body[0].Text != tt.want {
|
|
t.Errorf("%q: %+v, want the text %q", tt.in, body, tt.want)
|
|
}
|
|
}
|
|
_, links := modules.MarkdownBody("[docs](https://example.com/docs) [local](/x) [mail](mailto:a@b)")
|
|
if len(links) != 1 || links[0] != (modules.Link{Label: "docs", URL: "https://example.com/docs"}) {
|
|
t.Errorf("links %+v, want only the https one", links)
|
|
}
|
|
}
|
|
|
|
func TestMarkdownLines(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want []receipt.Element
|
|
}{
|
|
{"# H1\n## H2\n### H3 ###", []receipt.Element{
|
|
{Type: "text", Text: "H1", Bold: true}, {Type: "text", Text: "H2", Bold: true}, {Type: "text", Text: "H3", Bold: true}}},
|
|
{"- a\n* b\n+ c", []receipt.Element{{Type: "list", Bullet: "dot", Items: []string{"a", "b", "c"}}}},
|
|
{"1. a\n2) b\n10. c", []receipt.Element{{Type: "list", Bullet: "number", Items: []string{"a", "b", "c"}}}},
|
|
{"- a\n1. b", []receipt.Element{
|
|
{Type: "list", Bullet: "dot", Items: []string{"a"}}, {Type: "list", Bullet: "number", Items: []string{"b"}}}},
|
|
{"> one\n> two", []receipt.Element{{Type: "text", Text: "│ one\n│ two"}}},
|
|
{"---\n***\n- - -", []receipt.Element{{Type: "rule"}, {Type: "rule"}, {Type: "rule"}}},
|
|
{"a\n\n\n\nb\n\n", []receipt.Element{{Type: "text", Text: "a"}, {Type: "feed"}, {Type: "text", Text: "b"}}},
|
|
{"\n\na", []receipt.Element{{Type: "text", Text: "a"}}},
|
|
{"plain\nlines", []receipt.Element{{Type: "text", Text: "plain\nlines"}}},
|
|
{"-not a list\n#not a heading", []receipt.Element{{Type: "text", Text: "-not a list\n#not a heading"}}},
|
|
}
|
|
for _, tt := range tests {
|
|
got, _ := modules.MarkdownBody(tt.in)
|
|
if !sameElements(got, tt.want) {
|
|
t.Errorf("%q:\n got %+v\nwant %+v", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
|
|
fence, _ := modules.MarkdownBody("```sh\n**kept** as written\n- not a list\n```")
|
|
if len(fence) != 1 || fence[0].Text != "**kept** as written\n- not a list" || fence[0].Wrap == nil || *fence[0].Wrap {
|
|
t.Errorf("fence: %+v", fence)
|
|
}
|
|
}
|
|
|
|
func sameElements(a, b []receipt.Element) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
x, y := a[i], b[i]
|
|
if x.Type != y.Type || x.Text != y.Text || x.Bold != y.Bold || x.Bullet != y.Bullet ||
|
|
strings.Join(x.Items, "|") != strings.Join(y.Items, "|") {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// FuzzMarkdown lays out any text as markdown in a box: it must never
|
|
// panic, always build a valid job, and every line it prints must fit the
|
|
// paper.
|
|
func FuzzMarkdown(f *testing.F) {
|
|
for _, seed := range []string{
|
|
"# H\n- a\n1. b\n> q\n---\n```\ncode\n```\n**b** _i_ [l](https://x/) ",
|
|
"```\nunclosed",
|
|
"- \n#\n>\n- [ ] task",
|
|
strings.Repeat("- item\n", 250),
|
|
"\\*\\_\\`\\\\",
|
|
"\uE02A literal private use",
|
|
"**https://example.com/status** ~~https://old.example.com~~ Run https://ci.example.com/job/`nightly` (https://example.com/c).",
|
|
} {
|
|
f.Add(seed)
|
|
}
|
|
profile, err := escpos.LookupProfile("tm-t88")
|
|
if err != nil {
|
|
f.Fatal(err)
|
|
}
|
|
renderer := receipt.NewRenderer(receipt.Options{
|
|
Profile: profile, CodePage: escpos.CodePage437, MaxPixels: 1000, MaxHeight: 1000,
|
|
}, noFetch{})
|
|
columns := profile.Columns(escpos.FontA)
|
|
env := modules.Env{Now: time.Date(2026, 11, 29, 21, 4, 5, 0, time.UTC), Location: time.UTC, TimeLayout: "02 Jan 15:04"}
|
|
f.Fuzz(func(t *testing.T, text string) {
|
|
modules.MarkdownBody(text)
|
|
for _, priority := range []int{1, 3, 5} {
|
|
n := modules.Notice{Title: "T", Message: text, Markdown: true, Priority: priority}
|
|
job, _ := modules.Card(n, modules.CardOptions{MaxLines: 400}, env)
|
|
if job.Elements[0].Type == receipt.TypeText {
|
|
t.Fatalf("priority %d: the card fell back to the plain receipt", priority)
|
|
}
|
|
p, err := renderer.Preview(context.Background(), job)
|
|
if err != nil {
|
|
t.Fatalf("priority %d: Preview: %v", priority, err)
|
|
}
|
|
for _, line := range p.Lines {
|
|
if line.Kind != escpos.LineText || smallFont(line) {
|
|
continue
|
|
}
|
|
if utf8.RuneCountInString(line.Text) > columns {
|
|
t.Fatalf("priority %d: line %q is longer than %d columns", priority, line.Text, columns)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// smallFont reports whether line prints in Font B, whose lines are longer.
|
|
func smallFont(line escpos.PreviewLine) bool {
|
|
for _, r := range line.Runs {
|
|
if r.Font == escpos.FontB {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|