Skip to content

Commit

Permalink
feat(fota): allow ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Sep 22, 2024
1 parent 5fe095c commit ff6d71c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
46 changes: 46 additions & 0 deletions hello/FOTAJob.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { validateWithTypeBox } from 'validator/validateWithTypeBox.js'
import { UpgradePath } from './FOTAJob.js'

const v = validateWithTypeBox(UpgradePath)

void describe('UpgradePath', () => {
void it('should accept an upgrade path with plain versions', () =>
assert.equal(
hasError(
v({
'2.0.0': 'APP*1e29dfa3*v2.0.1',
'2.0.1': 'APP*cd5412d9*v2.0.2',
}),
),
false,
))

void describe('support semver ranges', () => {
for (const range of [
'<', // Less than
'<=', // Less than or equal to
'>', // Greater than
'>=', // Greater than or equal to
]) {
void it(range, () =>
assert.equal(
hasError(
v({
[`${range}2.0.0`]: 'APP*1e29dfa3*v2.0.1',
}),
),
false,
),
)
}
})

void describe('reject invalid upgrade paths', () => {
void it('should reject an upgrade path with an invalid version', () =>
assert.equal(hasError(v({ foo: 'bar' } as any)), true))
})
})

const hasError = (result: ReturnType<typeof v>) => 'errors' in result
14 changes: 9 additions & 5 deletions hello/FOTAJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ export enum FOTAJobTarget {
}

export const UpgradePath = Type.Record(
Type.RegExp(/[0-9]+\.[0-9]+\.[0-9]+/, {
title: 'Version',
description: 'The version the bundle is targeting',
}),
Type.RegExp(
new RegExp(`^(${['<', '<=', '>', '>='].join('|')})?[0-9]+.[0-9]+.[0-9]+$`),
{
title: 'Version',
description:
'The version the bundle is targeting. Supports ranges (e.g >=1.0.0).',
},
),
Type.RegExp(
/^(APP|MODEM|BOOT|SOFTDEVICE|BOOTLOADER|MDM_FULL)\*[0-9a-zA-Z]{8}\*.*$/,
{
title: 'Bundle ID',
description: 'The nRF Cloud firmware bundle ID',
},
),
{ minProperties: 1 },
{ minProperties: 1, additionalProperties: false },
)

export const FOTAJob = Type.Object(
Expand Down

0 comments on commit ff6d71c

Please sign in to comment.