-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter_test.go
More file actions
706 lines (591 loc) · 16.5 KB
/
printer_test.go
File metadata and controls
706 lines (591 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package main
import (
"bytes"
"errors"
"os"
"testing"
)
// MockWritable for testing error conditions
type MockWritable struct {
WriteRawError error
OpenError error
CloseError error
WriteRawCalls [][]byte
OpenCallCount int
CloseCallCount int
}
func (m *MockWritable) WriteRaw(data []byte) error {
m.WriteRawCalls = append(m.WriteRawCalls, data)
return m.WriteRawError
}
func (m *MockWritable) Open() error {
m.OpenCallCount++
return m.OpenError
}
func (m *MockWritable) Close() error {
m.CloseCallCount++
return m.CloseError
}
func createMockPrinter() (*Printer, string) {
f, err := os.CreateTemp("/tmp/", "epsonproxytest")
if err != nil {
panic(err)
}
printer, err := NewPrinter(f.Name(), 576, UsbPath)
if err != nil {
panic(err)
}
printer.connection = &UsbWriter{
path: f.Name(),
writer: f,
}
printer.retryDelay = 0 // No delay for tests
return printer, f.Name()
}
func TestWithRetry_RetriesSuccessful(t *testing.T) {
printer, _ := createMockPrinter()
count := 0
numRetries := 3
fn := func() (*string, error) {
count++
return nil, errors.New("testing err")
}
withRetry(printer, numRetries, fn)
if count != numRetries {
t.Errorf("expected %d retries, got %d", numRetries, count)
}
}
func TestKickDrawer_Bytes(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
err := printer.KickDrawer()
if err != nil {
t.Fatalf("KickDrawer failed: %v", err)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
expected := []byte{0x1B, 0x70, 0, 25, 25}
if !bytes.Equal(data, expected) {
t.Errorf("KickDrawer sent wrong bytes. Got %v, expected %v", data, expected)
}
}
func TestKickDrawer_WriteRawFailure(t *testing.T) {
mock := &MockWritable{
WriteRawError: errors.New("write failed"),
}
printer := &Printer{
connection_string: "/test",
receipt_width: 576,
connection: mock,
retryDelay: 0,
}
err := printer.KickDrawer()
if err == nil {
t.Error("expected error when WriteRaw fails, got nil")
}
}
func TestCut_Bytes(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
err := printer.Cut()
if err != nil {
t.Fatalf("Cut failed: %v", err)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
expected := append(CUT_CMD, FEED_N_CMD(2)...)
if !bytes.Equal(data, expected) {
t.Errorf("Cut sent wrong bytes. Got %v, expected %v", data, expected)
}
}
// Custom mock for testing partial failures in Cut
type PartialFailMock struct {
callCount int
failAfter int
}
func (p *PartialFailMock) WriteRaw(data []byte) error {
p.callCount++
if p.callCount > p.failAfter {
return errors.New("write failed")
}
return nil
}
func (p *PartialFailMock) Open() error { return nil }
func (p *PartialFailMock) Close() error { return nil }
func TestCut_WriteRawFailure(t *testing.T) {
mock := &PartialFailMock{failAfter: 1} // Fail on second call (FEED)
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
err := printer.Cut()
if err == nil {
t.Error("expected error when WriteRaw fails on feed, got nil")
}
}
func TestPrintGraphics_NoCentering(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
// Use width >= receipt_width to avoid centering
width := 576 // Same as receipt width
height := 1
widthBytes := width / 8 // 72 bytes
data := make([]byte, widthBytes*height)
for i := range data {
data[i] = byte(i)
}
err := printer.PrintGraphics(data, width, height)
if err != nil {
t.Fatalf("PrintGraphics failed: %v", err)
}
fileData, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
// Verify header
if !bytes.HasPrefix(fileData, PRINT_RASTER_CMD) {
t.Errorf("PrintGraphics missing raster command prefix")
}
// Verify dimensions in bytes
offset := len(PRINT_RASTER_CMD)
xL := byte(widthBytes & 0xFF)
xH := byte((widthBytes >> 8) & 0xFF)
yL := byte(height & 0xFF)
yH := byte((height >> 8) & 0xff)
if fileData[offset] != xL || fileData[offset+1] != xH {
t.Errorf("width bytes incorrect. Got %d,%d expected %d,%d",
fileData[offset], fileData[offset+1], xL, xH)
}
if fileData[offset+2] != yL || fileData[offset+3] != yH {
t.Errorf("height bytes incorrect. Got %d,%d expected %d,%d",
fileData[offset+2], fileData[offset+3], yL, yH)
}
// Verify footer
expectedFooter := FEED_N_CMD(12)
if !bytes.HasSuffix(fileData, expectedFooter) {
t.Errorf("PrintGraphics missing feed command footer")
}
}
func TestPrintGraphics_WithCentering(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
// Use width < receipt_width to trigger centering
width := 64 // 8 bytes
height := 1
paperWidthBytes := printer.receipt_width / 8 // 72 bytes
widthBytes := width / 8 // 8 bytes
data := make([]byte, widthBytes*height)
for i := range data {
data[i] = byte(i + 1)
}
err := printer.PrintGraphics(data, width, height)
if err != nil {
t.Fatalf("PrintGraphics failed: %v", err)
}
fileData, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
// Verify dimensions reflect paper width, not image width
offset := len(PRINT_RASTER_CMD)
xL := byte(paperWidthBytes & 0xFF)
xH := byte((paperWidthBytes >> 8) & 0xFF)
if fileData[offset] != xL || fileData[offset+1] != xH {
t.Errorf("width bytes should be paper width. Got %d,%d expected %d,%d",
fileData[offset], fileData[offset+1], xL, xH)
}
// Calculate where raster data starts
rasterStart := offset + 4
paddingBytes := (paperWidthBytes - widthBytes) / 2 // (72-8)/2 = 32
// Verify padding before data (should be zeros)
for i := 0; i < paddingBytes; i++ {
if fileData[rasterStart+i] != 0 {
t.Errorf("padding byte %d should be 0, got %d", i, fileData[rasterStart+i])
}
}
// Verify actual data is centered
dataStart := rasterStart + paddingBytes
for i := 0; i < widthBytes; i++ {
if fileData[dataStart+i] != byte(i+1) {
t.Errorf("data byte %d incorrect. Got %d expected %d",
i, fileData[dataStart+i], byte(i+1))
}
}
}
func TestPrintGraphics_NonByteAlignedWidth(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
printer.receipt_width = 9
width := 9
height := 2
data := []byte{0x11, 0x22, 0x33, 0x44}
err := printer.PrintGraphics(data, width, height)
if err != nil {
t.Fatalf("PrintGraphics failed: %v", err)
}
fileData, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
offset := len(PRINT_RASTER_CMD)
if fileData[offset] != 2 || fileData[offset+1] != 0 {
t.Fatalf("expected width bytes to be 2,0 got %d,%d", fileData[offset], fileData[offset+1])
}
if fileData[offset+2] != 2 || fileData[offset+3] != 0 {
t.Fatalf("expected height bytes to be 2,0 got %d,%d", fileData[offset+2], fileData[offset+3])
}
rasterStart := offset + 4
rasterEnd := rasterStart + len(data)
if !bytes.Equal(fileData[rasterStart:rasterEnd], data) {
t.Fatalf("expected raster payload %v, got %v", data, fileData[rasterStart:rasterEnd])
}
}
func TestPrintGraphics_DataTooShort(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
width := 64
height := 10
// Provide less data than required
data := make([]byte, 10)
err := printer.PrintGraphics(data, width, height)
if err == nil {
t.Error("expected error when data is too short, got nil")
}
}
func TestPrintGraphics_WriteRawFailure(t *testing.T) {
mock := &MockWritable{
WriteRawError: errors.New("write failed"),
}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
width := 576
height := 1
data := make([]byte, width/8*height)
err := printer.PrintGraphics(data, width, height)
if err == nil {
t.Error("expected error when WriteRaw fails, got nil")
}
}
func TestCenter_SingleRow(t *testing.T) {
// 4-byte image, 8-byte paper, height 1
// Expected: 2 bytes padding left, 4 bytes data, 2 bytes padding right
data := []byte{1, 2, 3, 4}
w_b := 4
p_w_b := 8
h := 1
result, err := center(data, w_b, p_w_b, h)
if err != nil {
t.Fatalf("center failed: %v", err)
}
expected := []byte{0, 0, 1, 2, 3, 4, 0, 0}
if !bytes.Equal(result, expected) {
t.Errorf("center result incorrect. Got %v, expected %v", result, expected)
}
}
func TestCenter_MultiRow(t *testing.T) {
// 2-byte image, 6-byte paper, height 2
// Row 1: data[0:2], Row 2: data[2:4]
data := []byte{1, 2, 3, 4}
w_b := 2
p_w_b := 6
h := 2
result, err := center(data, w_b, p_w_b, h)
if err != nil {
t.Fatalf("center failed: %v", err)
}
// Expected: [0,0,1,2,0,0, 0,0,3,4,0,0]
expected := []byte{0, 0, 1, 2, 0, 0, 0, 0, 3, 4, 0, 0}
if !bytes.Equal(result, expected) {
t.Errorf("center result incorrect. Got %v, expected %v", result, expected)
}
}
func TestCenter_AsymmetricPadding(t *testing.T) {
// 3-byte image, 8-byte paper (diff is 5, so 2 left, 3 right)
data := []byte{1, 2, 3}
w_b := 3
p_w_b := 8
h := 1
result, err := center(data, w_b, p_w_b, h)
if err != nil {
t.Fatalf("center failed: %v", err)
}
// Expected: 2 bytes left padding, 3 bytes data, 3 bytes right padding
expected := []byte{0, 0, 1, 2, 3, 0, 0, 0}
if !bytes.Equal(result, expected) {
t.Errorf("center result incorrect. Got %v, expected %v", result, expected)
}
}
func TestCenter_DataTooShort(t *testing.T) {
data := []byte{1, 2} // Only 2 bytes
w_b := 4
p_w_b := 8
h := 1
_, err := center(data, w_b, p_w_b, h)
if err == nil {
t.Error("expected error when data is too short, got nil")
}
}
func TestCenter_OutOfBounds(t *testing.T) {
// Height 2 but only enough data for height 1
data := []byte{1, 2, 3, 4} // 4 bytes = height 1 with w_b=2
w_b := 2
p_w_b := 4
h := 3 // Request 3 rows, but only have data for 2
_, err := center(data, w_b, p_w_b, h)
if err == nil {
t.Error("expected error when row is out of bounds, got nil")
}
}
func TestCenter_ExactSize(t *testing.T) {
// Image width equals paper width, no padding needed
data := []byte{1, 2, 3, 4}
w_b := 4
p_w_b := 4
h := 1
result, err := center(data, w_b, p_w_b, h)
if err != nil {
t.Fatalf("center failed: %v", err)
}
// Expected: same as input, no padding
if !bytes.Equal(result, data) {
t.Errorf("center result incorrect. Got %v, expected %v", result, data)
}
}
func TestClose(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
err := printer.Close()
if err != nil {
t.Errorf("Close failed: %v", err)
}
}
func TestClose_WithNilConnection(t *testing.T) {
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: nil,
}
// Should not panic
err := printer.Close()
if err != nil {
t.Errorf("Close with nil connection should return nil, got: %v", err)
}
}
func TestClose_Failure(t *testing.T) {
mock := &MockWritable{
CloseError: errors.New("close failed"),
}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
err := printer.Close()
if err == nil {
t.Error("expected error when Close fails, got nil")
}
}
func TestWithRetry_ClosesConnectionOnFailure(t *testing.T) {
mock := &MockWritable{}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
fn := func() (any, error) {
return nil, errors.New("always fails")
}
withRetry(printer, 2, fn)
// Should have closed connection at least once
if mock.CloseCallCount == 0 {
t.Error("expected Close to be called on failure")
}
// Should have reopened connection
if mock.OpenCallCount == 0 {
t.Error("expected Open to be called on failure")
}
}
func TestWithRetry_CloseFailure(t *testing.T) {
mock := &MockWritable{
CloseError: errors.New("close failed"),
}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
fn := func() (any, error) {
return nil, errors.New("always fails")
}
// Should not panic even when Close fails
_, err := withRetry(printer, 2, fn)
if err == nil {
t.Error("expected error after retries")
}
// Should still attempt to close
if mock.CloseCallCount == 0 {
t.Error("expected Close to be attempted even on failure")
}
}
func TestWithRetry_OpenCalledOnEachRetry(t *testing.T) {
mock := &MockWritable{}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
fn := func() (any, error) {
return nil, errors.New("always fails")
}
withRetry(printer, 3, fn)
// Should have called Open 2 times (after failures 1 and 2, not after final failure)
if mock.OpenCallCount != 2 {
t.Errorf("expected Open to be called 2 times (after failures 1 and 2), got %d", mock.OpenCallCount)
}
}
func TestWithRetry_OpenCalledOnlyOnFailureRetries(t *testing.T) {
mock := &MockWritable{}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
callCount := 0
fn := func() (any, error) {
callCount++
if callCount < 3 {
return nil, errors.New("fails first 2 times")
}
return "success", nil
}
result, err := withRetry(printer, 5, fn)
if err != nil {
t.Errorf("expected success, got error: %v", err)
}
if result != "success" {
t.Errorf("expected 'success', got %v", result)
}
// Should have called Open 2 times (after failures 1 and 2, not after success)
if mock.OpenCallCount != 2 {
t.Errorf("expected Open to be called 2 times (after failures), got %d", mock.OpenCallCount)
}
}
func TestWithRetry_OpenFailureContinuesToNextRetry(t *testing.T) {
mock := &MockWritable{}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
fnCallCount := 0
fn := func() (any, error) {
fnCallCount++
return nil, errors.New("always fails")
}
withRetry(printer, 3, fn)
// Should have called Open 2 times (after failures 1 and 2, not after final failure)
if mock.OpenCallCount != 2 {
t.Errorf("expected Open to be called 2 times (after failures 1 and 2), got %d", mock.OpenCallCount)
}
// Should have called the function 3 times
if fnCallCount != 3 {
t.Errorf("expected fn to be called 3 times, got %d", fnCallCount)
}
}
func TestWithRetry_NoOpenOnFirstSuccess(t *testing.T) {
mock := &MockWritable{}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
fn := func() (any, error) {
return "immediate success", nil
}
result, err := withRetry(printer, 3, fn)
if err != nil {
t.Errorf("expected success, got error: %v", err)
}
if result != "immediate success" {
t.Errorf("expected 'immediate success', got %v", result)
}
// Should not call Open on immediate success
if mock.OpenCallCount != 0 {
t.Errorf("expected Open to be called 0 times on first success, got %d", mock.OpenCallCount)
}
// Should not call Close on immediate success
if mock.CloseCallCount != 0 {
t.Errorf("expected Close to be called 0 times on first success, got %d", mock.CloseCallCount)
}
}
func TestWithRetry_CloseAndOpenSequence(t *testing.T) {
mock := &MockWritable{}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
fn := func() (any, error) {
return nil, errors.New("always fails")
}
withRetry(printer, 2, fn)
// Verify sequence:
// - Attempt 1 fails: Close then Open
// - Attempt 2 fails: Close (no Open because it's the final attempt)
if mock.CloseCallCount != 2 {
t.Errorf("expected Close to be called 2 times (after each failure), got %d", mock.CloseCallCount)
}
if mock.OpenCallCount != 1 {
t.Errorf("expected Open to be called 1 time (after first failure only), got %d", mock.OpenCallCount)
}
}
func TestReset_Bytes(t *testing.T) {
printer, path := createMockPrinter()
defer os.Remove(path)
err := printer.Reset()
if err != nil {
t.Fatalf("Reset failed: %v", err)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
expected := RESET_CMD
if !bytes.Equal(data, expected) {
t.Errorf("Reset sent wrong bytes. Got %v, expected %v", data, expected)
}
}
func TestReset_WriteRawFailure(t *testing.T) {
mock := &MockWritable{
WriteRawError: errors.New("write failed"),
}
printer := &Printer{
connection_string: "/test",
retryDelay: 0,
receipt_width: 576,
connection: mock,
}
err := printer.Reset()
if err == nil {
t.Error("expected error when WriteRaw fails, got nil")
}
}