|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { BoardClient } from './client'; |
| 5 | +import { type Tick } from './board'; |
| 6 | +import { EventDispatcher } from '../../events'; |
| 7 | +import { type ResponseStream } from '../../gen/robot/v1/robot_pb_service'; |
| 8 | +import { RobotClient } from '../../robot'; |
| 9 | +vi.mock('../../robot'); |
| 10 | +import { StreamTicksResponse } from '../../gen/component/board/v1/board_pb'; |
| 11 | + |
| 12 | +import { BoardServiceClient } from '../../gen/component/board/v1/board_pb_service'; |
| 13 | +vi.mock('../../gen/component/board/v1/board_pb_service'); |
| 14 | + |
| 15 | +let board: BoardClient; |
| 16 | + |
| 17 | +export class TestResponseStream<T> extends EventDispatcher { |
| 18 | + private stream: ResponseStream<any>; |
| 19 | + |
| 20 | + constructor(stream: ResponseStream<any>) { |
| 21 | + super(); |
| 22 | + this.stream = stream; |
| 23 | + } |
| 24 | + |
| 25 | + override on( |
| 26 | + type: string, |
| 27 | + handler: (message: any) => void |
| 28 | + ): ResponseStream<T> { |
| 29 | + super.on(type, handler); |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + cancel(): void { |
| 34 | + this.listeners = {}; |
| 35 | + this.stream.cancel(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +let tickStream: ResponseStream<StreamTicksResponse>; |
| 40 | +let testTickStream: TestResponseStream<StreamTicksResponse> | undefined; |
| 41 | + |
| 42 | +const tickStreamMock = (): |
| 43 | + | TestResponseStream<StreamTicksResponse> |
| 44 | + | undefined => { |
| 45 | + return testTickStream; |
| 46 | +}; |
| 47 | + |
| 48 | +beforeEach(() => { |
| 49 | + testTickStream = new TestResponseStream(tickStream); |
| 50 | + RobotClient.prototype.createServiceClient = vi |
| 51 | + .fn() |
| 52 | + .mockImplementation(() => new BoardServiceClient('board')); |
| 53 | + |
| 54 | + BoardServiceClient.prototype.streamTicks = vi |
| 55 | + .fn() |
| 56 | + .mockImplementation(tickStreamMock); |
| 57 | + board = new BoardClient(new RobotClient('host'), 'test-board'); |
| 58 | +}); |
| 59 | + |
| 60 | +afterEach(() => { |
| 61 | + testTickStream = undefined; |
| 62 | +}); |
| 63 | + |
| 64 | +describe('streamTicks tests', () => { |
| 65 | + it('streamTicks', () => { |
| 66 | + const ticks: Tick[] = []; |
| 67 | + board.streamTicks(['1', '2'], ticks); |
| 68 | + |
| 69 | + const response1 = new StreamTicksResponse(); |
| 70 | + response1.setPinName('1'); |
| 71 | + response1.setHigh(true); |
| 72 | + response1.setTime(1000); |
| 73 | + testTickStream?.emit('data', response1); |
| 74 | + |
| 75 | + const response2 = new StreamTicksResponse(); |
| 76 | + response2.setPinName('2'); |
| 77 | + response2.setHigh(false); |
| 78 | + response2.setTime(2000); |
| 79 | + testTickStream?.emit('data', response2); |
| 80 | + |
| 81 | + expect(ticks.length).toEqual(2); |
| 82 | + |
| 83 | + const tick1: Tick = ticks[0]!; |
| 84 | + expect(tick1.pinName).toEqual('1'); |
| 85 | + expect(tick1.high).toBe(true); |
| 86 | + expect(tick1.time).toEqual(1000); |
| 87 | + |
| 88 | + const tick2: Tick = ticks[1]!; |
| 89 | + expect(tick2.pinName).toEqual('2'); |
| 90 | + expect(tick2.high).toBe(false); |
| 91 | + expect(tick2.time).toEqual(2000); |
| 92 | + }); |
| 93 | + |
| 94 | + it('end streamTicks with an error', () => { |
| 95 | + const error = { code: 1, details: 'test', metadata: undefined }; |
| 96 | + |
| 97 | + const ticks: Tick[] = []; |
| 98 | + const promise1 = board.streamTicks(['1', '2'], ticks); |
| 99 | + |
| 100 | + testTickStream?.emit('end', undefined); |
| 101 | + expect(promise1).rejects.toStrictEqual({ |
| 102 | + message: 'Stream ended without a status code', |
| 103 | + }); |
| 104 | + |
| 105 | + const promise2 = board.streamTicks(['1', '2'], ticks); |
| 106 | + testTickStream?.emit('end', error); |
| 107 | + expect(promise2).rejects.toStrictEqual({ |
| 108 | + code: 1, |
| 109 | + message: 'test', |
| 110 | + metadata: undefined, |
| 111 | + }); |
| 112 | + |
| 113 | + const promise3 = board.streamTicks(['1', '2'], ticks); |
| 114 | + testTickStream?.emit('status', error); |
| 115 | + expect(promise3).rejects.toStrictEqual({ |
| 116 | + code: 1, |
| 117 | + message: 'test', |
| 118 | + metadata: undefined, |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments