forked from project-chip/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genengine.test.js
341 lines (310 loc) · 11.4 KB
/
genengine.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
*
* Copyright (c) 2020 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @jest-environment node
*/
const path = require('path')
const genEngine = require('../src-electron/generator/generation-engine.js')
const args = require('../src-electron/util/args.js')
const env = require('../src-electron/util/env.js')
const dbApi = require('../src-electron/db/db-api.js')
const queryPackage = require('../src-electron/db/query-package.js')
const querySession = require('../src-electron/db/query-session.js')
const utilJs = require('../src-electron/util/util.js')
const zclLoader = require('../src-electron/zcl/zcl-loader.js')
const helperZap = require('../src-electron/generator/helper-zap.js')
const importJs = require('../src-electron/importexport/import.js')
const testUtil = require('./test-util.js')
let db
const templateCount = 14
const genTimeout = 8000
const testFile = path.join(__dirname, 'resource/generation-test-file-1.zap')
beforeAll(() => {
let file = env.sqliteTestFile('genengine')
return dbApi
.initDatabaseAndLoadSchema(file, env.schemaFile(), env.zapVersion())
.then((d) => {
db = d
env.logInfo('DB initialized.')
})
}, 5000)
afterAll(() => {
return dbApi.closeDatabase(db)
})
let templateContext
test(
'Basic gen template parsing and generation',
() =>
genEngine
.loadTemplates(db, testUtil.testZigbeeGenerationTemplates)
.then((context) => {
expect(context.crc).not.toBeNull()
expect(context.templateData).not.toBeNull()
expect(context.templateData.name).toEqual('Test templates')
expect(context.templateData.version).toEqual('test-v1')
expect(context.templateData.templates.length).toEqual(templateCount)
expect(context.packageId).not.toBeNull()
templateContext = context
}),
3000
)
test('Validate package loading', () =>
queryPackage
.getPackageByParent(templateContext.db, templateContext.packageId)
.then((packages) => {
templateContext.packages = packages
return templateContext
})
.then((context) => {
expect(context.packages.length).toBe(templateCount + 2) // One for helper and one for overridable
}))
test('Create session', () =>
querySession.createBlankSession(db).then((sessionId) => {
expect(sessionId).not.toBeNull()
templateContext.sessionId = sessionId
}))
test(
'Load ZCL stuff',
() => zclLoader.loadZcl(db, args.zclPropertiesFile),
5000
)
test('Initialize session packages', async () => {
let packages = await utilJs.initializeSessionPackage(
templateContext.db,
templateContext.sessionId
)
expect(packages.length).toBe(2)
})
test(
'Validate basic generation',
() =>
genEngine
.generate(
templateContext.db,
templateContext.sessionId,
templateContext.packageId
)
.then((genResult) => {
expect(genResult).not.toBeNull()
expect(genResult.partial).toBeFalsy()
expect(genResult.content).not.toBeNull()
let simpleTest = genResult.content['simple-test.out']
expect(simpleTest.startsWith('Test template file.')).toBeTruthy()
expect(simpleTest.includes('Strange type: bacnet_type_t')).toBeTruthy()
}),
genTimeout
)
test(
'Validate more complex generation',
() =>
genEngine
.generate(
templateContext.db,
templateContext.sessionId,
templateContext.packageId
)
.then((genResult) => {
expect(genResult).not.toBeNull()
expect(genResult.partial).toBeFalsy()
expect(genResult.content).not.toBeNull()
let simpleTest = genResult.content['simple-test.out']
expect(simpleTest.startsWith('Test template file.')).toBeTruthy()
expect(simpleTest.includes(helperZap.zap_header())).toBeTruthy()
expect(
simpleTest.includes(`SessionId: ${genResult.sessionId}`)
).toBeTruthy()
expect(
simpleTest.includes('Addon: This is example of test addon helper')
).toBeTruthy()
let zclId = genResult.content['zcl-test.out']
//expect(zclId).toEqual('random placeholder')
expect(
zclId.includes(
'// 207/208: label=>ZllStatus caption=>Enum of type ENUM8'
)
).toBeTruthy()
expect(zclId.includes('Label count: 208')).toBeTruthy()
expect(
zclId.includes(
'// 128/208: label=>MeteringBlockEnumerations caption=>Enum of type ENUM8'
)
).toBeTruthy()
expect(
zclId.includes('// struct: ReadReportingConfigurationAttributeRecord')
).toBeTruthy()
expect(zclId.includes('cluster: 0x0700 Price')).toBeTruthy()
expect(zclId.includes('cmd: 0x0A GetUserStatusResponse')).toBeTruthy()
expect(
zclId.includes('att: 0x0002 gps communication mode')
).toBeTruthy()
expect(
zclId.includes('First item\n// struct: BlockThreshold')
).toBeTruthy()
expect(
zclId.includes('// struct: WwahClusterStatusToUseTC\nLast item')
).toBeTruthy()
let accumulator = genResult.content['accumulator.out']
expect(accumulator.includes('Iteration: 19 out of 20')).toBeTruthy()
expect(accumulator.includes('Cumulative size: 16 / 206')).toBeTruthy()
expect(accumulator.includes('Cumulative size: 8 / 109')).toBeTruthy()
expect(accumulator.includes('Cumulative size: 0 / 206')).toBeTruthy()
let atomics = genResult.content['atomics.out']
expect(atomics.includes('C type: bacnet_type_t')).toBeTruthy()
// Now check for the override
expect(
atomics.includes('C type: security_key_type_override')
).toBeTruthy()
let zapCommand = genResult.content['zap-command.h']
expect(zapCommand).not.toBeNull()
expect(
zapCommand.includes(
'#define emberAfFillCommandGlobalReadAttributesResponse(clusterId,'
)
).toBeTruthy()
let zapPrint = genResult.content['zap-print.h']
expect(
zapPrint.includes(
'#define SILABS_PRINTCLUSTER_POWER_CONFIG_CLUSTER {ZCL_POWER_CONFIG_CLUSTER_ID, 0x0000, "Power Configuration" },'
)
).toBeTruthy()
let sdkExtension = genResult.content['sdk-extension.out']
expect(
sdkExtension.includes(
"// cluster: 0x0000 Basic, text extension: 'Extension to basic cluster'"
)
).toBeTruthy()
expect(
sdkExtension.includes(
"// cluster: 0x0002 Device Temperature Configuration, text extension: 'Extension to temperature config cluster'"
)
).toBeTruthy()
expect(
sdkExtension.includes(
"// attribute: 0x0000 / 0x0000 => ZCL version, extensions: '42', '99'"
)
).toBeTruthy()
expect(
sdkExtension.includes(
"// cluster: 0x0003 Identify, text extension: ''"
)
).toBeTruthy()
expect(
sdkExtension.includes(
"// command: 0x0000 / 0x00 => ResetToFactoryDefaults, test extension: '1'"
)
).toBeTruthy()
expect(
sdkExtension.includes(
"// device type: HA / 0x0006 => HA-remote // extension: 'path/to/remote.c'"
)
).toBeTruthy()
}),
genTimeout
)
test(
'Validate specific file generation',
() =>
genEngine
.generate(
templateContext.db,
templateContext.sessionId,
templateContext.packageId
)
.then((genResult) => {
expect(genResult).not.toBeNull()
expect(genResult.partial).toBeFalsy()
expect(genResult.content).not.toBeNull()
let zapId = genResult.content['zap-id.h']
//expect(zapId).toEqual('random placeholder')
expect(zapId.includes('// Definitions for cluster: Basic')).toBeTruthy()
expect(
zapId.includes('#define ZCL_GET_PROFILE_RESPONSE_COMMAND_ID (0x00)')
).toBeTruthy()
expect(
zapId.includes(
'// Client attributes for cluster: Fluoride Concentration Measurement'
)
).toBeTruthy()
expect(
zapId.includes('#define ZCL_NUMBER_OF_RESETS_ATTRIBUTE_ID (0x0000)')
).toBeTruthy()
let zapTypes = genResult.content['zap-type.h']
expect(
zapTypes.includes(
'ZCL_INT16U_ATTRIBUTE_TYPE = 0x21, // Unsigned 16-bit integer'
)
).toBeTruthy()
expect(zapTypes.includes('uint32_t snapshotCause')).toBeTruthy()
let zapCli = genResult.content['zap-cli.c']
expect(zapCli.includes('#include <stdlib.h>')).toBeTruthy()
let zapCommandParser = genResult.content['zap-command-parser.c']
expect(zapCommandParser).not.toBeNull()
expect(
zapCommandParser.includes(
'EmberAfStatus emberAfClusterSpecificCommandParse(EmberAfClusterCommand * cmd)'
)
).toBeTruthy()
}),
genTimeout
)
test(
'Test file import and generation',
() =>
importJs
.importDataFromFile(db, testFile)
.then((sessionId) =>
genEngine.generate(db, sessionId, templateContext.packageId)
)
.then((genResult) => {
expect(genResult).not.toBeNull()
expect(genResult.partial).toBeFalsy()
expect(genResult.content).not.toBeNull()
let zapCli = genResult.content['zap-cli.c']
expect(zapCli.includes('#include <stdlib.h>')).toBeTruthy()
//expect(zapCli.includes('void zclIdentifyIdCommand(sl_cli_command_arg_t *arguments);')).toBeTruthy()
//expect(zapCli.includes('SL_CLI_COMMAND(zclIdentifyIdCommand,')).toBeTruthy()
//expect(zapCli.includes('{ "id", &cli_cmd_zcl_identify_client_cluster_identify, false },')).toBeTruthy()
//expect(zapCli.includes('SL_CLI_COMMAND_GROUP(zcl_identify_client_cluster_command_table, "ZCL identify client cluster commands");')).toBeTruthy()
//expect(zapCli.includes('{ "identify", &cli_cmd_identify_client_group, false },')).toBeTruthy()
}),
genTimeout
)
test('Test content indexer - simple', () =>
genEngine.contentIndexer('Short example').then((preview) => {
expect(preview['1']).toBe('Short example\n')
}))
test('Test content indexer - line by line', () =>
genEngine
.contentIndexer('Short example\nwith three\nlines of text', 1)
.then((preview) => {
expect(preview['1']).toBe('Short example\n')
expect(preview['2']).toBe('with three\n')
expect(preview['3']).toBe('lines of text\n')
}))
test('Test content indexer - blocks', () => {
let content = ''
let i = 0
for (i = 0; i < 1000; i++) {
content = content.concat(`line ${i}\n`)
}
return genEngine.contentIndexer(content, 50).then((preview) => {
expect(preview['1'].startsWith('line 0')).toBeTruthy()
expect(preview['2'].startsWith('line 50')).toBeTruthy()
expect(preview['3'].startsWith('line 100')).toBeTruthy()
expect(preview['20'].startsWith('line 950')).toBeTruthy()
})
})