mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
add another test for mismatch device type
This commit is contained in:
@@ -51,7 +51,7 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe('flashFirmware', () => {
|
||||
describe('normal flow', () => {
|
||||
describe('normal flow using app supplied firmware', () => {
|
||||
test('success', async () => {
|
||||
const metadata: FirmwareMetadata = {
|
||||
'metadata-version': '1.0.0',
|
||||
@@ -538,6 +538,94 @@ describe('flashFirmware', () => {
|
||||
await saga.end();
|
||||
});
|
||||
|
||||
test('connected device does not match firmware device', async () => {
|
||||
const metadata: FirmwareMetadata = {
|
||||
'metadata-version': '1.0.0',
|
||||
'device-id': HubType.MoveHub,
|
||||
'checksum-type': 'sum',
|
||||
'firmware-version': '1.2.3',
|
||||
'max-firmware-size': 1024,
|
||||
'mpy-abi-version': 5,
|
||||
'mpy-cross-options': ['-mno-unicode'],
|
||||
'user-mpy-offset': 100,
|
||||
};
|
||||
|
||||
const zip = new JSZip();
|
||||
zip.file('firmware-base.bin', new Uint8Array(64));
|
||||
zip.file('firmware.metadata.json', JSON.stringify(metadata));
|
||||
zip.file('main.py', 'print("test")');
|
||||
zip.file('ReadMe_OSS.txt', 'test');
|
||||
|
||||
jest.spyOn(window, 'fetch').mockResolvedValueOnce(
|
||||
new Response(await zip.generateAsync({ type: 'blob' })),
|
||||
);
|
||||
|
||||
const saga = new AsyncSaga(
|
||||
flashFirmware,
|
||||
{
|
||||
bootloader: { connection: BootloaderConnectionState.Disconnected },
|
||||
settings: { flashCurrentProgram: false },
|
||||
},
|
||||
{
|
||||
nextMessageId: createCountFunc(),
|
||||
},
|
||||
);
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction());
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
let action = await saga.take();
|
||||
expect(action).toEqual(connect());
|
||||
|
||||
saga.updateState({
|
||||
bootloader: { connection: BootloaderConnectionState.Connected },
|
||||
});
|
||||
saga.put(didConnect());
|
||||
|
||||
// then find out what kind of hub it is
|
||||
|
||||
action = await saga.take();
|
||||
expect(action).toEqual(infoRequest(0));
|
||||
|
||||
// connected hub type does not match firmware hub type
|
||||
saga.put(didRequest(0));
|
||||
saga.put(infoResponse(0x01000000, 0x08005000, 0x081f800, HubType.CityHub));
|
||||
|
||||
// then compile main.py to .mpy
|
||||
|
||||
action = await saga.take();
|
||||
expect(action).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"options": Array [
|
||||
"-mno-unicode",
|
||||
],
|
||||
"script": "print(\\"test\\")",
|
||||
"type": "mpy.action.compile",
|
||||
}
|
||||
`);
|
||||
|
||||
const mpySize = 20;
|
||||
const mpyBinaryData = new Uint8Array(mpySize);
|
||||
saga.put(didCompile(mpyBinaryData));
|
||||
|
||||
// should raise an error that we don't have any firmware for this hub
|
||||
|
||||
action = await saga.take();
|
||||
expect(action).toStrictEqual(
|
||||
didFailToFinish(FailToFinishReasonType.DeviceMismatch),
|
||||
);
|
||||
|
||||
// should request to disconnect after failure
|
||||
|
||||
action = await saga.take();
|
||||
expect(action).toEqual(disconnect());
|
||||
|
||||
await saga.end();
|
||||
});
|
||||
|
||||
test('unsupported device', async () => {
|
||||
const metadata: FirmwareMetadata = {
|
||||
'metadata-version': '1.0.0',
|
||||
|
||||
@@ -332,9 +332,8 @@ function* flashFirmware(action: FlashFirmwareFlashAction): Generator {
|
||||
({ firmware, deviceId } = yield* loadFirmware(data, program));
|
||||
|
||||
if (deviceId !== undefined && info.hubType !== deviceId) {
|
||||
throw Error(
|
||||
`Connected to ${info.hubType} but firmware is for ${deviceId}`,
|
||||
);
|
||||
yield* put(didFailToFinish(FailToFinishReasonType.DeviceMismatch));
|
||||
yield* disconnectAndCancel();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user