-
-
Notifications
You must be signed in to change notification settings - Fork 1
Use zc95's serial no. info in fw >=2.0 #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+200
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
tests/unit/device/protocol/zc95/zc95DeviceFactory.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import { describe, it, expect, beforeEach } from 'vitest'; | ||
| import { mock, MockProxy } from 'vitest-mock-extended'; | ||
| import Zc95DeviceFactory from '../../../../../src/device/protocol/zc95/zc95DeviceFactory.js'; | ||
| import Settings from '../../../../../src/settings/settings.js'; | ||
| import KnownDevice from '../../../../../src/settings/knownDevice.js'; | ||
| import DeviceNameGenerator from '../../../../../src/device/deviceNameGenerator.js'; | ||
| import DateFactory from '../../../../../src/factory/dateFactory.js'; | ||
| import EventEmitterFactory from '../../../../../src/factory/eventEmitterFactory.js'; | ||
| import Logger from '../../../../../src/logging/Logger.js'; | ||
| import Zc95Protocol from '../../../../../src/device/protocol/zc95/zc95Protocol.js'; | ||
| import DeviceBidirectionalTransport from '../../../../../src/device/transport/deviceBidirectionalTransport.js'; | ||
| import MessageResponseHandler from '../../../../../src/device/protocol/messageResponseHandler.js'; | ||
| import Zc95MessageFactory, { | ||
| GetPatternsMsg, | ||
| PatternsMsgResponse, | ||
| VersionMsgResponse, | ||
| } from '../../../../../src/device/protocol/zc95/zc95MessageFactory.js'; | ||
| import { MsgAndResponseIdentifier } from '../../../../../src/device/protocol/zc95/zc95Protocol.js'; | ||
| import { DeviceId } from '../../../../../src/device/deviceId.js'; | ||
|
|
||
| describe('Zc95DeviceFactory', () => { | ||
| let settings: MockProxy<Settings>; | ||
| let nameGenerator: MockProxy<DeviceNameGenerator>; | ||
| let eventEmitterFactory: EventEmitterFactory; | ||
| let dateFactory: DateFactory; | ||
| let logger: MockProxy<Logger>; | ||
| let mockProtocol: MockProxy<Zc95Protocol>; | ||
| let mockTransport: MockProxy<DeviceBidirectionalTransport>; | ||
| let mockMsgFactory: MockProxy<Zc95MessageFactory>; | ||
| let mockMsgHandler: MockProxy<MessageResponseHandler<Zc95Protocol>>; | ||
|
|
||
| const fakeGetPatternsMsg = {} as MsgAndResponseIdentifier<GetPatternsMsg, PatternsMsgResponse>; | ||
| const patternsResponse: PatternsMsgResponse = { | ||
| Type: 'PatternList', | ||
| MsgId: 1, | ||
| Result: 'OK', | ||
| Patterns: [{ Type: 'PatternDetail', Id: 0, Name: 'Pattern A' }], | ||
| }; | ||
|
|
||
| const transportDeviceId = DeviceId.create('transport-device-id'); | ||
| const provider = 'usb'; | ||
|
|
||
| function baseVersionDetails(overrides: Partial<VersionMsgResponse> = {}): VersionMsgResponse { | ||
| return { | ||
| Type: 'VersionDetails', | ||
| MsgId: 1, | ||
| Result: 'OK', | ||
| ZC95: '2.0.0', | ||
| WsMajor: 1, | ||
| WsMinor: 0, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function createFactory(): Zc95DeviceFactory { | ||
| return new Zc95DeviceFactory(dateFactory, eventEmitterFactory, settings, nameGenerator, logger); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| settings = mock<Settings>(); | ||
| nameGenerator = mock<DeviceNameGenerator>(); | ||
| eventEmitterFactory = new EventEmitterFactory(); | ||
| dateFactory = new DateFactory(); | ||
| logger = mock<Logger>(); | ||
| mockProtocol = mock<Zc95Protocol>(); | ||
| mockTransport = mock<DeviceBidirectionalTransport>(); | ||
| mockMsgFactory = mock<Zc95MessageFactory>(); | ||
| mockMsgHandler = mock<MessageResponseHandler<Zc95Protocol>>(); | ||
|
|
||
| mockMsgFactory.createGetPatterns.mockReturnValue(fakeGetPatternsMsg); | ||
| mockMsgHandler.send.mockResolvedValue(patternsResponse); | ||
| nameGenerator.generateName.mockReturnValue('Generated Name'); | ||
| settings.getKnownDeviceById.mockReturnValue(undefined); | ||
| }); | ||
|
|
||
| it('uses the transport device id and does not persist a known device when no SerialNo is provided (fw <2.0)', async () => { | ||
| const factory = createFactory(); | ||
|
|
||
| const device = await factory.create( | ||
| transportDeviceId, | ||
| baseVersionDetails({ SerialNo: undefined }), | ||
| mockProtocol, | ||
| mockTransport, | ||
| mockMsgFactory, | ||
| mockMsgHandler, | ||
| provider, | ||
| ); | ||
|
|
||
| expect(device.getDeviceId).toStrictEqual(transportDeviceId); | ||
| expect(device.getDeviceName).toStrictEqual('Generated Name'); | ||
| expect(settings.addKnownDevice).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('derives a deterministic device id from SerialNo and persists it as a known device when SerialNo is provided', async () => { | ||
| const factory = createFactory(); | ||
| const expectedDeviceId = DeviceId.create('ZC95-SERIAL-123'); | ||
|
|
||
| const device = await factory.create( | ||
| transportDeviceId, | ||
| baseVersionDetails({ SerialNo: 'ZC95-SERIAL-123' }), | ||
| mockProtocol, | ||
| mockTransport, | ||
| mockMsgFactory, | ||
| mockMsgHandler, | ||
| provider, | ||
| ); | ||
|
|
||
| expect(device.getDeviceId).toStrictEqual(expectedDeviceId); | ||
| expect(device.getDeviceId).not.toStrictEqual(transportDeviceId); | ||
| expect(device.getDeviceName).toStrictEqual('Generated Name'); | ||
| expect(settings.addKnownDevice).toHaveBeenCalledTimes(1); | ||
|
|
||
| const persisted = settings.addKnownDevice.mock.calls[0][0]; | ||
| expect(persisted.id).toStrictEqual(expectedDeviceId); | ||
| expect(persisted.type).toStrictEqual('zc95'); | ||
| expect(persisted.source).toStrictEqual(provider); | ||
| }); | ||
|
|
||
| it('reuses an already known device (id and name) when the derived serial-based id is already known', async () => { | ||
| const existingKnownDevice = new KnownDevice( | ||
| DeviceId.create('ZC95-SERIAL-123'), | ||
| 'Existing Device Name', | ||
| 'zc95', | ||
| provider, | ||
| ); | ||
| settings.getKnownDeviceById.mockReturnValue(existingKnownDevice); | ||
|
|
||
| const factory = createFactory(); | ||
|
|
||
| const device = await factory.create( | ||
| transportDeviceId, | ||
| baseVersionDetails({ SerialNo: 'ZC95-SERIAL-123' }), | ||
| mockProtocol, | ||
| mockTransport, | ||
| mockMsgFactory, | ||
| mockMsgHandler, | ||
| provider, | ||
| ); | ||
|
|
||
| expect(device.getDeviceId).toStrictEqual(existingKnownDevice.id); | ||
| expect(device.getDeviceName).toStrictEqual('Existing Device Name'); | ||
| expect(nameGenerator.generateName).not.toHaveBeenCalled(); | ||
| expect(settings.addKnownDevice).toHaveBeenCalledWith(existingKnownDevice); | ||
| }); | ||
|
|
||
| it('throws and does not create a device when retrieving the pattern list fails', async () => { | ||
| mockMsgHandler.send.mockRejectedValue(new Error('timeout')); | ||
|
|
||
| const factory = createFactory(); | ||
|
|
||
| await expect( | ||
| factory.create( | ||
| transportDeviceId, | ||
| baseVersionDetails({ SerialNo: undefined }), | ||
| mockProtocol, | ||
| mockTransport, | ||
| mockMsgFactory, | ||
| mockMsgHandler, | ||
| provider, | ||
| ), | ||
| ).rejects.toThrow('timeout'); | ||
|
|
||
| expect(settings.addKnownDevice).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.