@@ -9,6 +9,63 @@ import {
99 type StepRunner ,
1010} from '@nordicsemiconductor/bdd-markdown'
1111import { Type } from '@sinclair/typebox'
12+ import { fingerprintGenerator } from '@hello.nrfcloud.com/proto/fingerprint'
13+ import { IMEI } from '@hello.nrfcloud.com/bdd-markdown-steps/random'
14+ import type { HttpAPIMock } from '@bifravst/http-api-mock/mock'
15+
16+ const getCurrentWeekNumber = ( ) : number => {
17+ const now = new Date ( )
18+ const firstOfJanuary = new Date ( now . getFullYear ( ) , 0 , 1 )
19+ return Math . ceil (
20+ ( ( now . getTime ( ) - firstOfJanuary . getTime ( ) ) / 86400000 +
21+ firstOfJanuary . getDay ( ) +
22+ 1 ) /
23+ 7 ,
24+ )
25+ }
26+
27+ const oobDeviceWithFingerprint = (
28+ httpApiMock : HttpAPIMock ,
29+ helloAPIBasePath : string ,
30+ ) =>
31+ regExpMatchedStep (
32+ {
33+ regExp :
34+ / ^ I h a v e t h e f i n g e r p r i n t f o r m y d e v i c e i n ` (?< storageName > [ ^ ` ] + ) ` $ / ,
35+ schema : Type . Object ( {
36+ storageName : Type . String ( { minLength : 1 } ) ,
37+ } ) ,
38+ } ,
39+ async ( { match : { storageName } , log : { progress } , context } ) => {
40+ const now = new Date ( )
41+ const fingerprint = fingerprintGenerator (
42+ parseInt (
43+ `${ ( now . getFullYear ( ) - 2000 ) . toString ( ) } ${ getCurrentWeekNumber ( ) } ` ,
44+ 10 ,
45+ ) ,
46+ ) ( )
47+ progress ( `Fingerprint: ${ fingerprint } ` )
48+ context [ storageName ] = fingerprint
49+ const deviceId = `oob-${ IMEI ( ) } `
50+ progress ( `DeviceID: ${ deviceId } ` )
51+
52+ await httpApiMock . response (
53+ `GET ${ helloAPIBasePath } /device?fingerprint=${ fingerprint } ` ,
54+ {
55+ status : 200 ,
56+ headers : new Headers ( {
57+ 'content-type' : 'application/json; charset=utf-8' ,
58+ } ) ,
59+ body : JSON . stringify ( {
60+ '@context' :
61+ 'https://github.com/hello-nrfcloud/proto/deviceIdentity' ,
62+ id : deviceId ,
63+ model : 'PCA20035+solar' ,
64+ } ) ,
65+ } ,
66+ )
67+ } ,
68+ )
1269
1370const publishDeviceMessage = ( iotData : IoTDataPlaneClient ) =>
1471 regExpMatchedStep (
@@ -40,6 +97,13 @@ const publishDeviceMessage = (iotData: IoTDataPlaneClient) =>
4097
4198export const steps = ( {
4299 iotData,
100+ httpApiMock,
101+ helloAPIBasePath,
43102} : {
44103 iotData : IoTDataPlaneClient
45- } ) : Array < StepRunner < Record < string , any > > > => [ publishDeviceMessage ( iotData ) ]
104+ httpApiMock : HttpAPIMock
105+ helloAPIBasePath : string
106+ } ) : Array < StepRunner < Record < string , any > > > => [
107+ publishDeviceMessage ( iotData ) ,
108+ oobDeviceWithFingerprint ( httpApiMock , helloAPIBasePath ) ,
109+ ]
0 commit comments