CI / Lint, test and scan (push) Successful in 1m41s
Restructure the daemon to follow the project's Go coding standards and
expand its features.
Layout and tooling
- cmd/uberbringer/main.go is now thin; all logic lives under internal/.
- Module path is git.anomalous.dev/57_Wolve/uberbringer, go 1.26, every
dependency upgraded, vendor/ removed.
- staticcheck, govulncheck and goimports pinned as go tools; add a
Makefile, CI workflow, goreleaser v2 config and Alpine Dockerfile.
Features
- Background print queue with job status (GET /api/v1/jobs/:id), send
retries with backoff, and graceful shutdown.
- New {"elements": [...]} job format on POST /api/v1/print. Breaking:
replaces {"data": [...]}, and the response is now JSON 202.
- In-house ESC/POS library replacing justinmichaelvieira/escpos: sizes
1-8, Font B, CODE39/CODE128, QR, banded raster images, DLE EOT status.
- UTF-8 text transcoded to CP437 (configurable) with word-aware wrapping.
- Images from URL or base64, scaled and dithered; URL fetches refuse
internal addresses (SSRF guard).
- Fiber v3 HTTP server, TypeID (UUIDv7) job and request IDs, /healthz
with live printer status, `secret` and `token` subcommands.
Fixes
- Jobs are validated before anything is sent; a failed printer dial no
longer panics.
- Request logs never contain tokens; Sentry no longer receives the
Authorization header or print contents.
- Rate limiting is per client behind nginx (X-Real-IP).
- install.sh writes the [printer] section, runs the service as a
dedicated user and uses a current nginx TLS config.
config.toml is no longer tracked; start from configs/config.example.toml.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package receipt
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestWrap(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
text string
|
|
first, width int
|
|
want []string
|
|
}{
|
|
{
|
|
name: "fits", text: "hello world", first: 48, width: 48,
|
|
want: []string{"hello world"},
|
|
},
|
|
{
|
|
name: "breaks at word boundary", text: "the quick brown fox", first: 10, width: 10,
|
|
want: []string{"the quick", "brown fox"},
|
|
},
|
|
{
|
|
name: "never splits a word that fits on a line",
|
|
text: "Thank you for shopping with us today", first: 12, width: 12,
|
|
want: []string{"Thank you", "for shopping", "with us", "today"},
|
|
},
|
|
{
|
|
name: "hard splits only words longer than a line", text: "see https://example.com/very/long ok", first: 12, width: 12,
|
|
want: []string{"see", "https://exam", "ple.com/very", "/long ok"},
|
|
},
|
|
{
|
|
name: "runs of spaces kept inside a line", text: "a b", first: 10, width: 10,
|
|
want: []string{"a b"},
|
|
},
|
|
{
|
|
name: "spaces at a break are dropped", text: "aaaa b", first: 5, width: 5,
|
|
want: []string{"aaaa", "b"},
|
|
},
|
|
{
|
|
name: "paragraphs wrap independently", text: "one two\n\nthree", first: 5, width: 5,
|
|
want: []string{"one", "two", "", "three"},
|
|
},
|
|
{
|
|
name: "indent kept on first line only", text: " indented text here", first: 10, width: 10,
|
|
want: []string{" indented", "text here"},
|
|
},
|
|
{
|
|
name: "inline continuation fits", text: " world", first: 6, width: 10,
|
|
want: []string{" world"},
|
|
},
|
|
{
|
|
name: "inline continuation breaks before the word", text: "world", first: 3, width: 10,
|
|
want: []string{"", "world"},
|
|
},
|
|
{
|
|
name: "inline continuation keeps filling then wraps", text: " big wide world", first: 8, width: 10,
|
|
want: []string{" big", "wide world"},
|
|
},
|
|
{
|
|
name: "trailing space kept for inline use", text: "Total: ", first: 48, width: 48,
|
|
want: []string{"Total: "},
|
|
},
|
|
{
|
|
name: "empty", text: "", first: 10, width: 10,
|
|
want: []string{""},
|
|
},
|
|
{
|
|
name: "unicode counted as runes", text: "Grüße an Übermorgen", first: 10, width: 10,
|
|
want: []string{"Grüße an", "Übermorgen"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := wrap(tt.text, tt.first, tt.width)
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("wrap(%q, %d, %d) = %q, want %q", tt.text, tt.first, tt.width, got, tt.want)
|
|
}
|
|
for i, line := range got {
|
|
limit := tt.width
|
|
if i == 0 {
|
|
limit = tt.first
|
|
}
|
|
if n := len([]rune(line)); n > limit {
|
|
t.Errorf("line %d %q has %d runes, limit %d", i, line, n, limit)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpandTabs(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
start int
|
|
want string
|
|
}{
|
|
{"a\tb", 0, "a b"},
|
|
{"abcdefgh\tx", 0, "abcdefgh x"},
|
|
{"\tx", 3, " x"},
|
|
{"a\n\tb", 5, "a\n b"},
|
|
{"none", 0, "none"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := expandTabs(tt.in, tt.start); got != tt.want {
|
|
t.Errorf("expandTabs(%q, %d) = %q, want %q", tt.in, tt.start, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSanitize(t *testing.T) {
|
|
if got := sanitize("a\x1b@b\r\n\tc\x00"); got != "a@b\n\tc" {
|
|
t.Errorf("sanitize = %q", got)
|
|
}
|
|
}
|