1
+ -- Copyright 2025 SmartThings
2
+ --
3
+ -- Licensed under the Apache License, Version 2.0 (the "License");
4
+ -- you may not use this file except in compliance with the License.
5
+ -- You may obtain a copy of the License at
6
+ --
7
+ -- http://www.apache.org/licenses/LICENSE-2.0
8
+ --
9
+ -- Unless required by applicable law or agreed to in writing, software
10
+ -- distributed under the License is distributed on an "AS IS" BASIS,
11
+ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ -- See the License for the specific language governing permissions and
13
+ -- limitations under the License.
14
+
1
15
local test = require " integration_test"
2
16
test .add_package_capability (" cubeAction.yml" )
3
17
test .add_package_capability (" cubeFace.yml" )
4
18
local capabilities = require " st.capabilities"
5
19
local cubeAction = capabilities [" stse.cubeAction" ]
6
20
local cubeFace = capabilities [" stse.cubeFace" ]
7
21
8
- local t_utils = require " integration_test.utils"
9
22
local clusters = require " st.matter.clusters"
10
-
11
- -- used in unit testing, since device.profile.id and args.old_st_store.profile.id are always the same
12
- -- and this is to avoid the crash of the test case that occurs when try_update_metadata is performed in the device_init stage.
13
- local TEST_CONFIGURE = " __test_configure"
23
+ local dkjson = require " dkjson"
24
+ local t_utils = require " integration_test.utils"
25
+ local utils = require " st.utils"
14
26
15
27
-- mock the actual device1
16
28
local mock_device = test .mock_device .build_test_matter_device (
@@ -146,16 +158,22 @@ local CLUSTER_SUBSCRIBE_LIST ={
146
158
}
147
159
148
160
local function test_init ()
149
- local opts = { persist = true }
150
- mock_device :set_field (TEST_CONFIGURE , true , opts )
151
-
152
161
local subscribe_request = CLUSTER_SUBSCRIBE_LIST [1 ]:subscribe (mock_device )
153
162
for i , clus in ipairs (CLUSTER_SUBSCRIBE_LIST ) do
154
163
if i > 1 then
155
164
subscribe_request :merge (clus :subscribe (mock_device ))
156
165
end
157
166
end
158
167
test .socket .matter :__expect_send ({mock_device .id , subscribe_request })
168
+ test .socket .device_lifecycle :__queue_receive ({ mock_device .id , " added" })
169
+ test .socket .matter :__expect_send ({mock_device .id , subscribe_request })
170
+ test .socket .device_lifecycle :__queue_receive ({ mock_device .id , " doConfigure" })
171
+ mock_device :expect_metadata_update ({ profile = " cube-t1-pro" })
172
+ mock_device :expect_metadata_update ({ provisioning_state = " PROVISIONED" })
173
+ local device_info_copy = utils .deep_copy (mock_device .raw_st_data )
174
+ device_info_copy .profile .id = " matter-thing"
175
+ local device_info_json = dkjson .encode (device_info_copy )
176
+ test .socket .device_lifecycle :__queue_receive ({ mock_device .id , " infoChanged" , device_info_json })
159
177
test .mock_device .add_test_device (mock_device )
160
178
test .socket .capability :__expect_send (
161
179
mock_device :generate_test_message (" main" , cubeAction .cubeAction ({value = " flipToSide1" }))
@@ -168,16 +186,22 @@ end
168
186
test .set_test_init_function (test_init )
169
187
170
188
local function test_init_exhausted ()
171
- local opts = { persist = true }
172
- mock_device_exhausted :set_field (TEST_CONFIGURE , true , opts )
173
-
174
189
local subscribe_request = CLUSTER_SUBSCRIBE_LIST [1 ]:subscribe (mock_device_exhausted )
175
190
for i , clus in ipairs (CLUSTER_SUBSCRIBE_LIST ) do
176
191
if i > 1 then
177
192
subscribe_request :merge (clus :subscribe (mock_device_exhausted ))
178
193
end
179
194
end
180
195
test .socket .matter :__expect_send ({mock_device_exhausted .id , subscribe_request })
196
+ test .socket .device_lifecycle :__queue_receive ({ mock_device_exhausted .id , " added" })
197
+ test .socket .matter :__expect_send ({mock_device_exhausted .id , subscribe_request })
198
+ test .socket .device_lifecycle :__queue_receive ({ mock_device_exhausted .id , " doConfigure" })
199
+ mock_device_exhausted :expect_metadata_update ({ profile = " cube-t1-pro" })
200
+ mock_device_exhausted :expect_metadata_update ({ provisioning_state = " PROVISIONED" })
201
+ local device_info_copy = utils .deep_copy (mock_device_exhausted .raw_st_data )
202
+ device_info_copy .profile .id = " matter-thing"
203
+ local device_info_json = dkjson .encode (device_info_copy )
204
+ test .socket .device_lifecycle :__queue_receive ({ mock_device_exhausted .id , " infoChanged" , device_info_json })
181
205
test .mock_device .add_test_device (mock_device_exhausted )
182
206
test .socket .capability :__expect_send (
183
207
mock_device_exhausted :generate_test_message (" main" , cubeAction .cubeAction ({value = " flipToSide1" }))
@@ -188,22 +212,13 @@ local function test_init_exhausted()
188
212
end
189
213
190
214
test .register_coroutine_test (
191
- " Handle single press sequence when changing the device_lifecycle " ,
215
+ " Handle single press sequence" ,
192
216
function ()
193
- test .socket .device_lifecycle :__queue_receive ({ mock_device .id , " added" })
194
- test .mock_devices_api ._expected_device_updates [mock_device .device_id ] = " 00000000-1111-2222-3333-000000000001"
195
- test .mock_devices_api ._expected_device_updates [1 ] = {device_id = " 00000000-1111-2222-3333-000000000001" }
196
- test .mock_devices_api ._expected_device_updates [1 ].metadata = {deviceId = " 00000000-1111-2222-3333-000000000001" , profileReference = " cube-t1-pro" }
197
-
198
- test .socket .device_lifecycle :__queue_receive (mock_device :generate_info_changed ({value = " face1Up" }))
199
- -- let the driver run
200
- test .wait_for_events ()
201
-
202
217
test .socket .matter :__queue_receive (
203
218
{
204
219
mock_device .id ,
205
220
clusters .Switch .events .InitialPress :build_test_event_report (
206
- mock_device , 2 , {new_position = 1 } -- move to position 1?
221
+ mock_device , 2 , {new_position = 1 }
207
222
)
208
223
}
209
224
)
@@ -236,20 +251,11 @@ test.register_coroutine_test(
236
251
test .register_coroutine_test (
237
252
" Handle single press sequence in case of exhausted endpoint" ,
238
253
function ()
239
- test .socket .device_lifecycle :__queue_receive ({ mock_device_exhausted .id , " added" })
240
- test .mock_devices_api ._expected_device_updates [mock_device_exhausted .device_id ] = " 00000000-1111-2222-3333-000000000003"
241
- test .mock_devices_api ._expected_device_updates [1 ] = {device_id = " 00000000-1111-2222-3333-000000000003" }
242
- test .mock_devices_api ._expected_device_updates [1 ].metadata = {deviceId = " 00000000-1111-2222-3333-000000000003" , profileReference = " cube-t1-pro" }
243
-
244
- test .socket .device_lifecycle :__queue_receive (mock_device_exhausted :generate_info_changed ({value = " face1Up" }))
245
- -- let the driver run
246
- test .wait_for_events ()
247
-
248
254
test .socket .matter :__queue_receive (
249
255
{
250
256
mock_device_exhausted .id ,
251
257
clusters .Switch .events .InitialPress :build_test_event_report (
252
- mock_device_exhausted , 250 , {new_position = 1 } -- move to position 1?
258
+ mock_device_exhausted , 250 , {new_position = 1 }
253
259
)
254
260
}
255
261
)
@@ -265,6 +271,14 @@ test.register_coroutine_test(
265
271
{ test_init = test_init_exhausted }
266
272
)
267
273
274
+ test .register_coroutine_test (
275
+ " Test driver switched event" ,
276
+ function ()
277
+ test .socket .device_lifecycle :__queue_receive ({ mock_device .id , " driverSwitched" })
278
+ mock_device :expect_metadata_update ({ profile = " cube-t1-pro" })
279
+ end
280
+ )
281
+
268
282
-- run the tests
269
283
test .run_registered_tests ()
270
284
0 commit comments