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"}, {"![chart](https://example.com/c.png) and ![](x.png)", "[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/) ![a](u)", "```\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 }