|
| 1 | +import { Device, DeviceEvent } from '../../src/endpoint/devices' |
| 2 | +import { |
| 3 | + VirtualDeviceCreateRequest, |
| 4 | + VirtualDeviceStandardCreateRequest, |
| 5 | + VirtualDevicesEndpoint, |
| 6 | +} from '../../src/endpoint/virtualdevices' |
| 7 | +import { BearerTokenAuthenticator } from '../../src/authenticator' |
| 8 | +import { EndpointClient } from '../../src/endpoint-client' |
| 9 | + |
| 10 | + |
| 11 | +const authenticator = new BearerTokenAuthenticator('00000000-0000-0000-0000-000000000000') |
| 12 | + |
| 13 | +describe('VirtualDevicesEndpoint', () => { |
| 14 | + afterEach(() => { |
| 15 | + jest.clearAllMocks() |
| 16 | + }) |
| 17 | + |
| 18 | + const postSpy = jest.spyOn(EndpointClient.prototype, 'post').mockImplementation() |
| 19 | + const getPagedItemsSpy = jest.spyOn(EndpointClient.prototype, 'getPagedItems').mockImplementation() |
| 20 | + |
| 21 | + const virtualDevicesEndpoint = new VirtualDevicesEndpoint({ authenticator }) |
| 22 | + |
| 23 | + const deviceList = [{ listed: 'device' }] as unknown as Device[] |
| 24 | + |
| 25 | + describe('list', () => { |
| 26 | + getPagedItemsSpy.mockResolvedValue(deviceList) |
| 27 | + |
| 28 | + it('works without options', async () => { |
| 29 | + expect(await virtualDevicesEndpoint.list()).toBe(deviceList) |
| 30 | + |
| 31 | + expect(getPagedItemsSpy).toHaveBeenCalledTimes(1) |
| 32 | + expect(getPagedItemsSpy).toHaveBeenCalledWith(undefined, {}) |
| 33 | + }) |
| 34 | + |
| 35 | + it('includes configured locationId', async () => { |
| 36 | + const devices = new VirtualDevicesEndpoint({ authenticator, locationId: 'configured-location-id' }) |
| 37 | + expect(await devices.list()).toBe(deviceList) |
| 38 | + |
| 39 | + expect(getPagedItemsSpy).toHaveBeenCalledTimes(1) |
| 40 | + expect(getPagedItemsSpy).toHaveBeenCalledWith(undefined, { locationId: 'configured-location-id' }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('include wanted locationId', async () => { |
| 44 | + expect(await virtualDevicesEndpoint.list({ locationId: 'wanted-locationId' })).toBe(deviceList) |
| 45 | + |
| 46 | + expect(getPagedItemsSpy).toHaveBeenCalledTimes(1) |
| 47 | + expect(getPagedItemsSpy).toHaveBeenCalledWith(undefined, { locationId: 'wanted-locationId' }) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe('create', () => { |
| 52 | + it('creates from device profile ID', async () => { |
| 53 | + const device = { new: 'device' } |
| 54 | + postSpy.mockResolvedValueOnce(device) |
| 55 | + |
| 56 | + const deviceCreate: VirtualDeviceCreateRequest = { |
| 57 | + owner: { |
| 58 | + ownerId: 'owner-id', |
| 59 | + ownerType: 'LOCATION', |
| 60 | + }, |
| 61 | + name: 'Living room light', |
| 62 | + roomId: 'room-id', |
| 63 | + deviceProfileId: 'profile-id', |
| 64 | + } |
| 65 | + |
| 66 | + const expectedData = deviceCreate |
| 67 | + |
| 68 | + expect(await virtualDevicesEndpoint.create(deviceCreate)).toBe(device) |
| 69 | + |
| 70 | + expect(postSpy).toHaveBeenCalledTimes(1) |
| 71 | + expect(postSpy).toHaveBeenCalledWith('', expectedData) |
| 72 | + }) |
| 73 | + |
| 74 | + it('creates from device profile definition', async () => { |
| 75 | + const device = { new: 'device' } |
| 76 | + postSpy.mockResolvedValueOnce(device) |
| 77 | + |
| 78 | + const deviceCreate: VirtualDeviceCreateRequest = { |
| 79 | + owner: { |
| 80 | + ownerId: 'owner-id', |
| 81 | + ownerType: 'LOCATION', |
| 82 | + }, |
| 83 | + name: 'Living room light', |
| 84 | + roomId: 'room-id', |
| 85 | + deviceProfile: { |
| 86 | + 'components': [ |
| 87 | + { |
| 88 | + 'id': 'main', |
| 89 | + 'capabilities': [ |
| 90 | + { |
| 91 | + 'id': 'switch', |
| 92 | + 'version': 1, |
| 93 | + }, |
| 94 | + ], |
| 95 | + 'categories': [], |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + const expectedData = deviceCreate |
| 102 | + |
| 103 | + expect(await virtualDevicesEndpoint.create(deviceCreate)).toBe(device) |
| 104 | + |
| 105 | + expect(postSpy).toHaveBeenCalledTimes(1) |
| 106 | + expect(postSpy).toHaveBeenCalledWith('', expectedData) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + describe('createStandard', () => { |
| 111 | + it('creates from prototype', async () => { |
| 112 | + const device = {new: 'device'} |
| 113 | + postSpy.mockResolvedValueOnce(device) |
| 114 | + |
| 115 | + const deviceCreate: VirtualDeviceStandardCreateRequest = { |
| 116 | + owner: { |
| 117 | + ownerId: 'owner-id', |
| 118 | + ownerType: 'LOCATION', |
| 119 | + }, |
| 120 | + name: 'Living room light', |
| 121 | + roomId: 'room-id', |
| 122 | + prototype: 'SWITCH', |
| 123 | + } |
| 124 | + |
| 125 | + const expectedData = deviceCreate |
| 126 | + |
| 127 | + expect(await virtualDevicesEndpoint.createStandard(deviceCreate)).toBe(device) |
| 128 | + |
| 129 | + expect(postSpy).toHaveBeenCalledTimes(1) |
| 130 | + expect(postSpy).toHaveBeenCalledWith('prototypes', expectedData) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + test('createEvents', async () => { |
| 135 | + const events: DeviceEvent[] = [{ component: 'main' } as DeviceEvent] |
| 136 | + postSpy.mockResolvedValueOnce([true]) |
| 137 | + |
| 138 | + expect(await virtualDevicesEndpoint.createEvents('device-id', events)).toStrictEqual([true]) |
| 139 | + |
| 140 | + expect(postSpy).toHaveBeenCalledTimes(1) |
| 141 | + expect(postSpy).toHaveBeenCalledWith('device-id/events', { deviceEvents: events }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments