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") } }