usb_layout_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package service
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. )
  7. func TestBuildUSBTicketLayoutPrintsTopToBottom(t *testing.T) {
  8. data := usbTicketData{
  9. lotName: "KTI AIRPORT",
  10. channelCode: "IN11",
  11. plateNo: "2BC1875",
  12. entryTime: "30-07-2026 11:14:59",
  13. slipNo: "54600",
  14. ticketNo: "0123456789abcdef0123456789abcdef",
  15. }
  16. ops := buildUSBTicketLayout(data)
  17. texts := usbTextOperations(ops)
  18. want := []string{
  19. "Parking Ticket\n",
  20. "KTI AIRPORT\n",
  21. strings.Repeat("-", 42) + "\n",
  22. "\n",
  23. "PARK AT YOUR OWN RISK\n",
  24. "KTI AIRPORT A-General Car\n",
  25. "In-Time::30-07-2026 11:14:59\n",
  26. "InGate::IN11\n",
  27. "Slip No::54600\n",
  28. "Veh No::2BC1875\n",
  29. "SCAN AND PAY WITH NEW\n",
  30. "THE CANADIA BANK APP\n",
  31. }
  32. if !reflect.DeepEqual(texts, want) {
  33. t.Fatalf("USB text order = %#v, want %#v", texts, want)
  34. }
  35. if countText(ops, strings.Repeat("-", 42)+"\n") != 1 {
  36. t.Fatalf("expected one 42-character divider, got %d", countText(ops, strings.Repeat("-", 42)+"\n"))
  37. }
  38. qr, ok := findUSBQRCode(ops)
  39. if !ok {
  40. t.Fatal("expected a QR-code operation")
  41. }
  42. if qr.qrData != data.ticketNo || qr.qrWidth != 12 {
  43. t.Fatalf("QR operation = (%q, %d), want (%q, 12)", qr.qrData, qr.qrWidth, data.ticketNo)
  44. }
  45. qrIndex := findUSBQRCodeIndex(ops)
  46. paymentIndex := findTextIndex(ops, "SCAN AND PAY WITH NEW\n")
  47. plateIndex := findTextIndex(ops, "Veh No::2BC1875\n")
  48. cutIndex := findUSBOpKindIndex(ops, usbOpCut)
  49. if qrIndex < 0 || paymentIndex < 0 || plateIndex < 0 || cutIndex < 0 || !(plateIndex < qrIndex && qrIndex < paymentIndex && paymentIndex < cutIndex) {
  50. t.Fatalf("expected plate before QR before payment before cut, got plate=%d QR=%d payment=%d cut=%d", plateIndex, qrIndex, paymentIndex, cutIndex)
  51. }
  52. }
  53. func TestBuildUSBTicketLayoutOmitsOptionalLines(t *testing.T) {
  54. ops := buildUSBTicketLayout(usbTicketData{
  55. lotName: "KTI AIRPORT",
  56. entryTime: "30-07-2026 11:14:59",
  57. slipNo: "54600",
  58. ticketNo: "ticket-no",
  59. })
  60. texts := usbTextOperations(ops)
  61. for _, text := range texts {
  62. if strings.HasPrefix(text, "InGate::") || strings.HasPrefix(text, "Veh No::") {
  63. t.Fatalf("optional line should be omitted, got %q", text)
  64. }
  65. }
  66. }
  67. func TestBuildUSBTicketLayoutUsesDoubleWidthAroundTitleAndPlate(t *testing.T) {
  68. ops := buildUSBTicketLayout(usbTicketData{
  69. lotName: "KTI AIRPORT",
  70. plateNo: "2BC1875",
  71. entryTime: "30-07-2026 11:14:59",
  72. slipNo: "54600",
  73. ticketNo: "ticket-no",
  74. })
  75. titleIndex := findTextIndex(ops, "Parking Ticket\n")
  76. plateIndex := findTextIndex(ops, "Veh No::2BC1875\n")
  77. if titleIndex < 2 || plateIndex < 2 {
  78. t.Fatalf("title or plate operation is missing")
  79. }
  80. if !bytesEqual(ops[titleIndex-2].data, []byte{GS, '!', 0x10}) || !bytesEqual(ops[titleIndex+1].data, []byte{GS, '!', 0x00}) {
  81. t.Fatalf("title is not bracketed by GS ! double-width/reset operations")
  82. }
  83. if !bytesEqual(ops[titleIndex-1].data, BoldOnCmd) || !bytesEqual(ops[titleIndex+2].data, BoldOffCmd) {
  84. t.Fatalf("title is not bracketed by bold on/off operations")
  85. }
  86. if !bytesEqual(ops[plateIndex-2].data, []byte{GS, '!', 0x10}) || !bytesEqual(ops[plateIndex+1].data, []byte{GS, '!', 0x00}) {
  87. t.Fatalf("plate is not bracketed by GS ! double-width/reset operations")
  88. }
  89. if !bytesEqual(ops[plateIndex-1].data, BoldOnCmd) || !bytesEqual(ops[plateIndex+2].data, BoldOffCmd) {
  90. t.Fatalf("plate is not bracketed by bold on/off operations")
  91. }
  92. }
  93. func TestBuildUSBTicketLayoutUsesNormalTextDirection(t *testing.T) {
  94. ops := buildUSBTicketLayout(usbTicketData{
  95. lotName: "Lot#1",
  96. plateNo: "2BC1875",
  97. entryTime: "03-08-2026 10:00:00",
  98. slipNo: "1",
  99. ticketNo: "ticket-no",
  100. })
  101. directionOff := findUSBWriteDataIndices(ops, []byte{ESC, '{', 0})
  102. directionOn := findUSBWriteDataIndex(ops, []byte{ESC, '{', 1})
  103. qrIndex := findUSBQRCodeIndex(ops)
  104. paymentIndex := findTextIndex(ops, "SCAN AND PAY WITH NEW\n")
  105. cutIndex := findUSBOpKindIndex(ops, usbOpCut)
  106. if len(directionOff) < 2 || directionOn >= 0 || qrIndex < 0 || paymentIndex < 0 || cutIndex < 0 {
  107. t.Fatalf("missing normal direction/print operations: off=%d on=%d qr=%d payment=%d cut=%d", len(directionOff), directionOn, qrIndex, paymentIndex, cutIndex)
  108. }
  109. if !(directionOff[0] < qrIndex && qrIndex < directionOff[1] && directionOff[1] < paymentIndex && paymentIndex < cutIndex) {
  110. t.Fatalf("normal direction must precede text and be reapplied after QR: off=%v qr=%d payment=%d cut=%d", directionOff, qrIndex, paymentIndex, cutIndex)
  111. }
  112. }
  113. func TestBuildUSBTicketLayoutFeedsPastCutterBeforeCut(t *testing.T) {
  114. ops := buildUSBTicketLayout(usbTicketData{ticketNo: "ticket-no"})
  115. if len(ops) < 2 || ops[len(ops)-2].kind != usbOpFeed || ops[len(ops)-2].count != 8 || ops[len(ops)-1].kind != usbOpCut {
  116. t.Fatalf("ticket must end with an 8-line feed and cut, got %#v", ops[len(ops)-2:])
  117. }
  118. }
  119. func TestExecuteUSBLayoutStopsOnShortWrite(t *testing.T) {
  120. device := &recordingUSBDevice{shortWriteAt: 1}
  121. ops := []usbPrintOp{
  122. {kind: usbOpWrite, data: []byte("first\n")},
  123. {kind: usbOpWrite, data: []byte("second\n")},
  124. {kind: usbOpQRCode, qrData: "ticket-no", qrWidth: 8},
  125. }
  126. if err := executeUSBLayout(device, ops); err == nil {
  127. t.Fatal("expected short-write error")
  128. }
  129. if len(device.writes) != 1 {
  130. t.Fatalf("operations after short write executed: %d writes", len(device.writes))
  131. }
  132. }
  133. func TestExecuteUSBLayoutReturnsQRCodeError(t *testing.T) {
  134. device := &recordingUSBDevice{qrOK: false}
  135. ops := []usbPrintOp{{kind: usbOpQRCode, qrData: "ticket-no", qrWidth: 8}}
  136. if err := executeUSBLayout(device, ops); err == nil {
  137. t.Fatal("expected QR-code error")
  138. }
  139. }
  140. type recordingUSBDevice struct {
  141. writes [][]byte
  142. shortWriteAt int
  143. qrOK bool
  144. }
  145. func (d *recordingUSBDevice) Write(data []byte) int {
  146. d.writes = append(d.writes, append([]byte(nil), data...))
  147. if len(d.writes) == d.shortWriteAt {
  148. return len(data) - 1
  149. }
  150. return len(data)
  151. }
  152. func (d *recordingUSBDevice) FeedLines(int) {}
  153. func (d *recordingUSBDevice) QRCode(string, int) bool { return d.qrOK }
  154. func (d *recordingUSBDevice) FullCut() {}
  155. func usbTextOperations(ops []usbPrintOp) []string {
  156. var texts []string
  157. for _, op := range ops {
  158. if op.kind == usbOpWrite && len(op.data) > 0 && op.data[len(op.data)-1] == '\n' {
  159. texts = append(texts, string(op.data))
  160. }
  161. }
  162. return texts
  163. }
  164. func countText(ops []usbPrintOp, want string) int {
  165. count := 0
  166. for _, text := range usbTextOperations(ops) {
  167. if text == want {
  168. count++
  169. }
  170. }
  171. return count
  172. }
  173. func findTextIndex(ops []usbPrintOp, want string) int {
  174. for i, op := range ops {
  175. if op.kind == usbOpWrite && string(op.data) == want {
  176. return i
  177. }
  178. }
  179. return -1
  180. }
  181. func findUSBWriteDataIndex(ops []usbPrintOp, want []byte) int {
  182. for i, op := range ops {
  183. if op.kind == usbOpWrite && bytesEqual(op.data, want) {
  184. return i
  185. }
  186. }
  187. return -1
  188. }
  189. func findUSBWriteDataIndices(ops []usbPrintOp, want []byte) []int {
  190. var indices []int
  191. for i, op := range ops {
  192. if op.kind == usbOpWrite && bytesEqual(op.data, want) {
  193. indices = append(indices, i)
  194. }
  195. }
  196. return indices
  197. }
  198. func findUSBQRCode(ops []usbPrintOp) (usbPrintOp, bool) {
  199. for _, op := range ops {
  200. if op.kind == usbOpQRCode {
  201. return op, true
  202. }
  203. }
  204. return usbPrintOp{}, false
  205. }
  206. func findUSBQRCodeIndex(ops []usbPrintOp) int {
  207. return findUSBOpKindIndex(ops, usbOpQRCode)
  208. }
  209. func findUSBOpKindIndex(ops []usbPrintOp, kind usbPrintOpKind) int {
  210. for i, op := range ops {
  211. if op.kind == kind {
  212. return i
  213. }
  214. }
  215. return -1
  216. }
  217. func bytesEqual(a, b []byte) bool { return reflect.DeepEqual(a, b) }