[cirque] Add tests for re-registration for ICD devices (#36335)

* Update Linux app main

* [cirque] Add tests for re-registration for ICD devices

* Fix test

---------

Co-authored-by: Andrei Litvin <andy314@gmail.com>
diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp
index a2e341e..d18c310 100644
--- a/examples/platform/linux/AppMain.cpp
+++ b/examples/platform/linux/AppMain.cpp
@@ -96,6 +96,9 @@
 #if CHIP_DEVICE_CONFIG_ENABLE_DEVICE_ENERGY_MANAGEMENT_TRIGGER
 #include <app/clusters/device-energy-management-server/DeviceEnergyManagementTestEventTriggerHandler.h>
 #endif
+#if CHIP_CONFIG_ENABLE_ICD_SERVER
+#include <app/icd/server/ICDManager.h>
+#endif
 #include <app/TestEventTriggerDelegate.h>
 
 #include <signal.h>
@@ -593,6 +596,9 @@
     static DeviceEnergyManagementTestEventTriggerHandler sDeviceEnergyManagementTestEventTriggerHandler;
     sTestEventTriggerDelegate.AddHandler(&sDeviceEnergyManagementTestEventTriggerHandler);
 #endif
+#if CHIP_CONFIG_ENABLE_ICD_SERVER
+    sTestEventTriggerDelegate.AddHandler(&Server::GetInstance().GetICDManager());
+#endif
 
     initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
 
diff --git a/examples/platform/linux/BUILD.gn b/examples/platform/linux/BUILD.gn
index 336534a..30583ce 100644
--- a/examples/platform/linux/BUILD.gn
+++ b/examples/platform/linux/BUILD.gn
@@ -15,6 +15,7 @@
 import("//build_overrides/chip.gni")
 import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni")
 import("${chip_root}/src/app/common_flags.gni")
+import("${chip_root}/src/app/icd/icd.gni")
 import("${chip_root}/src/lib/core/core.gni")
 import("${chip_root}/src/lib/lib.gni")
 import("${chip_root}/src/tracing/tracing_args.gni")
@@ -133,6 +134,9 @@
       "${chip_root}/src/tracing",
     ]
   }
+  if (chip_enable_icd_server) {
+    deps += [ "${chip_root}/src/app/icd/server:manager" ]
+  }
 
   defines += [
     "CHIP_DEVICE_CONFIG_ENABLE_SMOKE_CO_TRIGGER=${chip_enable_smoke_co_trigger}",
diff --git a/scripts/tests/cirque_tests.sh b/scripts/tests/cirque_tests.sh
index b7dcdbc..1a591bd 100755
--- a/scripts/tests/cirque_tests.sh
+++ b/scripts/tests/cirque_tests.sh
@@ -48,7 +48,7 @@
     "FailsafeTest"
     "MobileDeviceTest"
     "CommissioningTest"
-    "IcdWaitForActiveTest"
+    "IcdDeviceTest"
     "SplitCommissioningTest"
     "CommissioningFailureTest"
     "CommissioningFailureOnReportTest"
diff --git a/src/controller/python/test/test_scripts/base.py b/src/controller/python/test/test_scripts/base.py
index dc07e15..b2c6ee2 100644
--- a/src/controller/python/test/test_scripts/base.py
+++ b/src/controller/python/test/test_scripts/base.py
@@ -1159,7 +1159,7 @@
             return False
 
     async def TestTriggerTestEventHandler(self, nodeid, enable_key, event_trigger):
-        self.logger.info("Test trigger test event handler for device = %08x", nodeid)
+        self.logger.info("Test trigger test event handler for device = %08x trigger = %016x", nodeid, event_trigger)
         try:
             await self.devCtrl.SendCommand(nodeid, 0, Clusters.GeneralDiagnostics.Commands.TestEventTrigger(enableKey=enable_key, eventTrigger=event_trigger))
             return True
@@ -1167,10 +1167,10 @@
             self.logger.exception("Failed to trigger test event handler {}".format(ex))
             return False
 
-    async def TestWaitForActive(self, nodeid):
+    async def TestWaitForActive(self, nodeid, stayActiveDurationMs=30000):
         self.logger.info("Test wait for device = %08x", nodeid)
         try:
-            await self.devCtrl.WaitForActive(nodeid)
+            await self.devCtrl.WaitForActive(nodeid, stayActiveDurationMs=stayActiveDurationMs)
             return True
         except Exception as ex:
             self.logger.exception("Failed to wait for active. {}".format(ex))
diff --git a/src/controller/python/test/test_scripts/icd_wait_for_device_test.py b/src/controller/python/test/test_scripts/icd_device_test.py
similarity index 83%
rename from src/controller/python/test/test_scripts/icd_wait_for_device_test.py
rename to src/controller/python/test/test_scripts/icd_device_test.py
index e28a76e..e93ee5d 100755
--- a/src/controller/python/test/test_scripts/icd_wait_for_device_test.py
+++ b/src/controller/python/test/test_scripts/icd_device_test.py
@@ -35,11 +35,15 @@
 
 
 async def waitForActiveAndTriggerCheckIn(test, nodeid):
-    coro = test.TestWaitForActive(nodeid=nodeid)
-    await test.TestTriggerTestEventHandler(nodeid, bytes.fromhex("00112233445566778899aabbccddeeff"), 0x0046 << 48)
+    coro = test.TestWaitForActive(nodeid=nodeid, stayActiveDurationMs=10)
     return await coro
 
 
+async def invalidateHalfCounterValuesAndWaitForCheckIn(test, nodeid, testEventKey):
+    await test.TestTriggerTestEventHandler(nodeid, bytes.fromhex(testEventKey), 0x0046_0000_0000_0003)
+    return await waitForActiveAndTriggerCheckIn(test, nodeid)
+
+
 async def main():
     optParser = OptionParser()
     optParser.add_option(
@@ -108,6 +112,15 @@
         help="Discovery type of commissioning. (0: networkOnly 1: networkOnlyWithoutPASEAutoRetry 2: All<Ble & Network>)",
         metavar="<discovery-type>"
     )
+    optParser.add_option(
+        "--test-event-key",
+        action="store",
+        dest="testEventKey",
+        default="00112233445566778899aabbccddeeff",
+        type=str,
+        help="Enable key of Test event trigger.",
+        metavar="<test-event-key>"
+    )
 
     (options, remainingArgs) = optParser.parse_args(sys.argv[1:])
 
@@ -125,9 +138,14 @@
                                            nodeid=options.nodeid),
               "Failed to finish key exchange")
     logger.info("Commissioning completed")
+
     logger.info("Testing wait for active")
     FailIfNot(await waitForActiveAndTriggerCheckIn(test, nodeid=options.nodeid), "Failed to test wait for active")
-    logger.info('Successfully handled wait-for-active')
+    logger.info("Successfully handled wait-for-active")
+
+    logger.info("Testing InvalidateHalfCounterValues for refresh key")
+    FailIfNot(await invalidateHalfCounterValuesAndWaitForCheckIn(test, nodeid=options.nodeid, testEventKey=options.testEventKey), "Failed to test wait for active")
+    logger.info("Successfully handled key refresh")
 
     timeoutTicker.stop()
 
diff --git a/src/test_driver/linux-cirque/IcdWaitForActiveTest.py b/src/test_driver/linux-cirque/IcdDeviceTest.py
similarity index 93%
rename from src/test_driver/linux-cirque/IcdWaitForActiveTest.py
rename to src/test_driver/linux-cirque/IcdDeviceTest.py
index bb2c1cb..b478689 100755
--- a/src/test_driver/linux-cirque/IcdWaitForActiveTest.py
+++ b/src/test_driver/linux-cirque/IcdDeviceTest.py
@@ -43,6 +43,8 @@
 TEST_DISCOVERY_TYPE = [0, 1, 2]
 MATTER_DEVELOPMENT_PAA_ROOT_CERTS = "credentials/development/paa-root-certs"
 
+TEST_EVENT_KEY_HEX = "00112233445566778899aabbccddeeff"
+
 DEVICE_CONFIG = {
     'device0': {
         'type': 'MobileDevice',
@@ -88,8 +90,8 @@
             self.execute_device_cmd(
                 server,
                 ("CHIPCirqueDaemon.py -- run gdb -batch -return-child-result -q -ex \"set pagination off\" "
-                 "-ex run -ex \"thread apply all bt\" --args {} --thread --discriminator {}").format(
-                    os.path.join(CHIP_REPO, "out/debug/lit_icd/lit-icd-app"), TEST_DISCRIMINATOR))
+                 "-ex run -ex \"thread apply all bt\" --args {} --thread --discriminator {} --enable-key {}").format(
+                    os.path.join(CHIP_REPO, "out/debug/lit_icd/lit-icd-app"), TEST_DISCRIMINATOR, TEST_EVENT_KEY_HEX))
 
         self.reset_thread_devices(server_ids)
 
@@ -103,10 +105,10 @@
             CHIP_REPO, "out/debug/linux_x64_gcc/controller/python/chip_repl-0.0-py3-none-any.whl")))
 
         command = ("gdb -batch -return-child-result -q -ex run -ex \"thread apply all bt\" "
-                   "--args python3 {} -t 300 -a {} --paa-trust-store-path {}").format(
+                   "--args python3 {} -t 300 -a {} --paa-trust-store-path {} --test-event-key {}").format(
             os.path.join(
-                CHIP_REPO, "src/controller/python/test/test_scripts/icd_wait_for_device_test.py"), ethernet_ip,
-            os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS))
+                CHIP_REPO, "src/controller/python/test/test_scripts/icd_device_test.py"), ethernet_ip,
+            os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS), TEST_EVENT_KEY_HEX)
         ret = self.execute_device_cmd(req_device_id, command)
 
         self.assertEqual(ret['return_code'], '0',