|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { Device, usb } from 'usb'; |
| 3 | +import USBAdapter from '../src'; |
| 4 | +import { promisify } from 'util'; |
2 | 5 |
|
3 | 6 | describe('should', () => { |
4 | | - it('exported', () => { |
5 | | - expect(1).toEqual(1) |
6 | | - }) |
7 | | -}) |
| 7 | + it('Validate findPrinter devices', () => { |
| 8 | + const printers = USBAdapter.findPrinter(); |
| 9 | + if (printers.length > 0) { |
| 10 | + // Validate printer iface class for all received printers |
| 11 | + for (const printer of printers) { |
| 12 | + if (printer.configDescriptor !== undefined) { |
| 13 | + let hasPrinterClass = false; |
| 14 | + exit_loop: |
| 15 | + for (const iface of printer.configDescriptor?.interfaces) { |
| 16 | + for (const conf of iface) { |
| 17 | + if (conf.bInterfaceClass === 0x07) { |
| 18 | + hasPrinterClass = true; |
| 19 | + break exit_loop; |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + expect(hasPrinterClass).to.be.true; |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + it('Validate adapter listeners', async () => { |
| 30 | + const printer = new USBAdapter(); |
| 31 | + |
| 32 | + const handler = vi.fn(); |
| 33 | + printer.addListener('close', handler); |
| 34 | + |
| 35 | + const openDevice = promisify(printer.open).bind(printer); |
| 36 | + await openDevice(); |
| 37 | + const closeDevice = promisify(printer.close).bind(printer); |
| 38 | + await closeDevice(); |
| 39 | + |
| 40 | + expect(handler).toBeCalledTimes(1); |
| 41 | + }); |
| 42 | + |
| 43 | + it('Validate usb listeners', async () => { |
| 44 | + let listenerCount = usb.listenerCount('detach'); |
| 45 | + const printer = new USBAdapter(); |
| 46 | + let listenerCountAfter = usb.listenerCount('detach'); |
| 47 | + // Save last recognized listener function for validation |
| 48 | + const libraryListenerFn = usb.listeners('detach')[listenerCountAfter - 1]; |
| 49 | + expect(listenerCountAfter).to.be.greaterThan(listenerCount); |
| 50 | + |
| 51 | + // Create custom listener and validate if it still exists after device close |
| 52 | + listenerCount = listenerCountAfter; |
| 53 | + function listenerExec() {} |
| 54 | + usb.on('detach', listenerExec); |
| 55 | + listenerCountAfter = usb.listenerCount('detach'); |
| 56 | + expect(listenerCountAfter).to.be.greaterThan(listenerCount); |
| 57 | + |
| 58 | + const openDevice = promisify(printer.open).bind(printer); |
| 59 | + await openDevice(); |
| 60 | + const closeDevice = promisify(printer.close).bind(printer); |
| 61 | + await closeDevice(); // Only the internal listener should be removed |
| 62 | + |
| 63 | + expect(usb.listenerCount('detach')).to.be.lessThan(listenerCountAfter); |
| 64 | + let libListenerRemoved = true, customListenerStillExist = false; |
| 65 | + for (const listenerFn of usb.listeners('detach')) { |
| 66 | + if (listenerFn === libraryListenerFn) { |
| 67 | + libListenerRemoved = false; |
| 68 | + } else if (listenerFn === listenerExec) { |
| 69 | + customListenerStillExist = true; |
| 70 | + } |
| 71 | + } |
| 72 | + expect(libListenerRemoved).to.be.true; |
| 73 | + expect(customListenerStillExist).to.be.true; |
| 74 | + }); |
| 75 | +}); |
0 commit comments