package receipt_test import ( "bytes" "context" "errors" "testing" "uberbringer/internal/pkg/escpos" "uberbringer/internal/pkg/receipt" ) // cutFeedRenderer is a TM-T20 renderer that feeds feed lines before each // cut and fails a job over maxLines. func cutFeedRenderer(t *testing.T, feed, maxLines int) *receipt.Renderer { t.Helper() return receipt.NewRenderer(receipt.Options{ Profile: tmT20(t), CodePage: escpos.CodePage437, MaxPixels: 1000, MaxHeight: 1000, MaxLines: maxLines, CutFeed: feed, }, &fakeFetcher{}) } // CutFeed feeds blank lines before every cut: the one added at the end of // each copy and an explicit cut element alike. Zero feeds nothing. func TestRenderCutFeed(t *testing.T) { job := receipt.Job{Copies: 2, Elements: []receipt.Element{ {Type: "text", Text: "first"}, {Type: "cut", Mode: "full"}, {Type: "text", Text: "second"}, }} feedThenPartial := append([]byte{0x1B, 'd', 3}, partialCut...) feedThenFull := []byte{0x1B, 'd', 3, 0x1D, 0x56, 65, 0} out, err := cutFeedRenderer(t, 3, 0).Render(context.Background(), job) if err != nil { t.Fatal(err) } if n := bytes.Count(out, feedThenPartial); n != 2 { t.Errorf("%d fed partial cuts, want one per copy", n) } if n := bytes.Count(out, feedThenFull); n != 2 { t.Errorf("%d fed full cuts, want one per copy", n) } if !bytes.HasSuffix(out, feedThenPartial) { t.Errorf("the receipt ends % X, want the feed and the cut", out[max(len(out)-8, 0):]) } out, err = cutFeedRenderer(t, 0, 0).Render(context.Background(), job) if err != nil { t.Fatal(err) } if bytes.Contains(out, []byte{0x1B, 'd'}) || !bytes.HasSuffix(out, partialCut) { t.Errorf("CutFeed 0 fed paper: % X", out[max(len(out)-8, 0):]) } } // The preview shows the fed lines as blank lines before the cut, and they // count toward the line limit. func TestPreviewAndLimitCountCutFeed(t *testing.T) { job := receipt.Job{Elements: []receipt.Element{{Type: "text", Text: "one line"}}} p, err := cutFeedRenderer(t, 3, 0).Preview(context.Background(), job) if err != nil { t.Fatal(err) } var kinds []string for _, l := range p.Lines { kinds = append(kinds, l.Kind+":"+l.Text) } if len(p.Lines) != 5 || p.Lines[0].Text != "one line" || p.Lines[1].Text != "" || p.Lines[3].Text != "" || p.Lines[4].Kind != escpos.LineCut { t.Errorf("preview lines %q, want the text, three blank lines and the cut", kinds) } _, err = cutFeedRenderer(t, 3, 3).Render(context.Background(), job) var limit *receipt.LineLimitError if !errors.As(err, &limit) || limit.Lines != 4 { t.Errorf("err = %v, want 4 lines over the limit of 3", err) } }