@matter/testing 0.17.9 → 0.18.0-alpha.0-20260812-4d7f2790e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/dist/cjs/chai.js +4 -4
  2. package/dist/cjs/chai.js.map +2 -2
  3. package/dist/cjs/chip/cert/cert-context.d.ts +115 -0
  4. package/dist/cjs/chip/cert/cert-context.d.ts.map +1 -0
  5. package/dist/cjs/chip/cert/cert-context.js +22 -0
  6. package/dist/cjs/chip/cert/cert-context.js.map +6 -0
  7. package/dist/cjs/chip/cert/cert-dsl.d.ts +48 -0
  8. package/dist/cjs/chip/cert/cert-dsl.d.ts.map +1 -0
  9. package/dist/cjs/chip/cert/cert-dsl.js +308 -0
  10. package/dist/cjs/chip/cert/cert-dsl.js.map +6 -0
  11. package/dist/cjs/chip/cert/cert-test.d.ts +46 -0
  12. package/dist/cjs/chip/cert/cert-test.d.ts.map +1 -0
  13. package/dist/cjs/chip/cert/cert-test.js +250 -0
  14. package/dist/cjs/chip/cert/cert-test.js.map +6 -0
  15. package/dist/cjs/chip/cert/chip-app-subject.d.ts +80 -0
  16. package/dist/cjs/chip/cert/chip-app-subject.d.ts.map +1 -0
  17. package/dist/cjs/chip/cert/chip-app-subject.js +337 -0
  18. package/dist/cjs/chip/cert/chip-app-subject.js.map +6 -0
  19. package/dist/cjs/chip/cert/controller-adapter.d.ts +119 -0
  20. package/dist/cjs/chip/cert/controller-adapter.d.ts.map +1 -0
  21. package/dist/cjs/chip/cert/controller-adapter.js +45 -0
  22. package/dist/cjs/chip/cert/controller-adapter.js.map +6 -0
  23. package/dist/cjs/chip/cert/device-config.d.ts +15 -0
  24. package/dist/cjs/chip/cert/device-config.d.ts.map +1 -0
  25. package/dist/cjs/chip/cert/device-config.js +43 -0
  26. package/dist/cjs/chip/cert/device-config.js.map +6 -0
  27. package/dist/cjs/chip/cert/evidence.d.ts +70 -0
  28. package/dist/cjs/chip/cert/evidence.d.ts.map +1 -0
  29. package/dist/cjs/chip/cert/evidence.js +128 -0
  30. package/dist/cjs/chip/cert/evidence.js.map +6 -0
  31. package/dist/cjs/chip/cert/log-follower.d.ts +96 -0
  32. package/dist/cjs/chip/cert/log-follower.d.ts.map +1 -0
  33. package/dist/cjs/chip/cert/log-follower.js +209 -0
  34. package/dist/cjs/chip/cert/log-follower.js.map +6 -0
  35. package/dist/cjs/chip/cert/matterjs-subject-registry.d.ts +22 -0
  36. package/dist/cjs/chip/cert/matterjs-subject-registry.d.ts.map +1 -0
  37. package/dist/cjs/chip/cert/matterjs-subject-registry.js +40 -0
  38. package/dist/cjs/chip/cert/matterjs-subject-registry.js.map +6 -0
  39. package/dist/cjs/chip/cert/prompt-driven-python-test.d.ts +40 -0
  40. package/dist/cjs/chip/cert/prompt-driven-python-test.d.ts.map +1 -0
  41. package/dist/cjs/chip/cert/prompt-driven-python-test.js +77 -0
  42. package/dist/cjs/chip/cert/prompt-driven-python-test.js.map +6 -0
  43. package/dist/cjs/chip/chip-bins.d.ts +132 -0
  44. package/dist/cjs/chip/chip-bins.d.ts.map +1 -0
  45. package/dist/cjs/chip/chip-bins.js +232 -0
  46. package/dist/cjs/chip/chip-bins.js.map +6 -0
  47. package/dist/cjs/chip/chip-test-common.d.ts.map +1 -1
  48. package/dist/cjs/chip/chip-test-common.js +1 -1
  49. package/dist/cjs/chip/chip-test-common.js.map +1 -1
  50. package/dist/cjs/chip/chip.d.ts.map +1 -1
  51. package/dist/cjs/chip/chip.js +1 -1
  52. package/dist/cjs/chip/chip.js.map +1 -1
  53. package/dist/cjs/chip/config.d.ts +26 -0
  54. package/dist/cjs/chip/config.d.ts.map +1 -1
  55. package/dist/cjs/chip/config.js +11 -3
  56. package/dist/cjs/chip/config.js.map +1 -1
  57. package/dist/cjs/chip/index.d.ts +26 -0
  58. package/dist/cjs/chip/index.d.ts.map +1 -1
  59. package/dist/cjs/chip/index.js +49 -0
  60. package/dist/cjs/chip/index.js.map +2 -2
  61. package/dist/cjs/chip/pics/file.d.ts +8 -0
  62. package/dist/cjs/chip/pics/file.d.ts.map +1 -1
  63. package/dist/cjs/chip/pics/file.js +4 -1
  64. package/dist/cjs/chip/pics/file.js.map +1 -1
  65. package/dist/cjs/chip/pics/source.d.ts +8 -5
  66. package/dist/cjs/chip/pics/source.d.ts.map +1 -1
  67. package/dist/cjs/chip/pics/source.js +25 -17
  68. package/dist/cjs/chip/pics/source.js.map +1 -1
  69. package/dist/cjs/chip/python-test.d.ts +12 -0
  70. package/dist/cjs/chip/python-test.d.ts.map +1 -1
  71. package/dist/cjs/chip/python-test.js +6 -4
  72. package/dist/cjs/chip/python-test.js.map +1 -1
  73. package/dist/cjs/chip/state.d.ts.map +1 -1
  74. package/dist/cjs/chip/state.js +24 -7
  75. package/dist/cjs/chip/state.js.map +1 -1
  76. package/dist/cjs/chip/yaml-test.js +1 -1
  77. package/dist/cjs/chip/yaml-test.js.map +1 -1
  78. package/dist/cjs/cli.d.ts.map +1 -1
  79. package/dist/cjs/cli.js +33 -2
  80. package/dist/cjs/cli.js.map +1 -1
  81. package/dist/cjs/device/backchannel.d.ts.map +1 -1
  82. package/dist/cjs/device/subject.d.ts.map +1 -1
  83. package/dist/cjs/device/test.d.ts.map +1 -1
  84. package/dist/cjs/docker/container.d.ts.map +1 -1
  85. package/dist/cjs/docker/docker.d.ts.map +1 -1
  86. package/dist/cjs/docker/edit.d.ts.map +1 -1
  87. package/dist/cjs/docker/errors.d.ts.map +1 -1
  88. package/dist/cjs/docker/network.d.ts.map +1 -1
  89. package/dist/cjs/docker/terminal.d.ts.map +1 -1
  90. package/dist/cjs/failure-reporter.d.ts.map +1 -1
  91. package/dist/cjs/global-declarations.d.js +0 -1
  92. package/dist/cjs/global-declarations.d.js.map +1 -1
  93. package/dist/cjs/global-declarations.d.ts +2 -2
  94. package/dist/cjs/global-definitions.js +17 -4
  95. package/dist/cjs/global-definitions.js.map +2 -2
  96. package/dist/cjs/index.d.js +2 -0
  97. package/dist/cjs/index.d.js.map +1 -1
  98. package/dist/cjs/index.d.ts +3 -0
  99. package/dist/cjs/index.js +2 -0
  100. package/dist/cjs/index.js.map +1 -1
  101. package/dist/cjs/mocha.d.ts.map +1 -1
  102. package/dist/cjs/mocha.js +5 -0
  103. package/dist/cjs/mocha.js.map +1 -1
  104. package/dist/cjs/mocharc.cjs +4 -3
  105. package/dist/cjs/options.d.ts.map +1 -1
  106. package/dist/cjs/print-report.js +1 -1
  107. package/dist/cjs/print-report.js.map +1 -1
  108. package/dist/cjs/test-descriptor.d.ts +1 -1
  109. package/dist/cjs/test-descriptor.d.ts.map +1 -1
  110. package/dist/cjs/text-diff.d.ts.map +1 -1
  111. package/dist/cjs/util/async.d.ts +30 -0
  112. package/dist/cjs/util/async.d.ts.map +1 -0
  113. package/dist/cjs/util/async.js +85 -0
  114. package/dist/cjs/util/async.js.map +6 -0
  115. package/dist/cjs/util/trace-unhandled.d.ts.map +1 -1
  116. package/dist/cjs/util/wtf.d.ts.map +1 -1
  117. package/dist/cjs/util/wtf.js +1 -1
  118. package/dist/cjs/util/wtf.js.map +1 -1
  119. package/dist/cjs/web.d.ts.map +1 -1
  120. package/dist/cjs/web.js +5 -1
  121. package/dist/cjs/web.js.map +1 -1
  122. package/dist/esm/chai.js +1 -1
  123. package/dist/esm/chai.js.map +1 -1
  124. package/dist/esm/chip/cert/cert-context.d.ts +115 -0
  125. package/dist/esm/chip/cert/cert-context.d.ts.map +1 -0
  126. package/dist/esm/chip/cert/cert-context.js +6 -0
  127. package/dist/esm/chip/cert/cert-context.js.map +6 -0
  128. package/dist/esm/chip/cert/cert-dsl.d.ts +48 -0
  129. package/dist/esm/chip/cert/cert-dsl.d.ts.map +1 -0
  130. package/dist/esm/chip/cert/cert-dsl.js +288 -0
  131. package/dist/esm/chip/cert/cert-dsl.js.map +6 -0
  132. package/dist/esm/chip/cert/cert-test.d.ts +46 -0
  133. package/dist/esm/chip/cert/cert-test.d.ts.map +1 -0
  134. package/dist/esm/chip/cert/cert-test.js +230 -0
  135. package/dist/esm/chip/cert/cert-test.js.map +6 -0
  136. package/dist/esm/chip/cert/chip-app-subject.d.ts +80 -0
  137. package/dist/esm/chip/cert/chip-app-subject.d.ts.map +1 -0
  138. package/dist/esm/chip/cert/chip-app-subject.js +317 -0
  139. package/dist/esm/chip/cert/chip-app-subject.js.map +6 -0
  140. package/dist/esm/chip/cert/controller-adapter.d.ts +119 -0
  141. package/dist/esm/chip/cert/controller-adapter.d.ts.map +1 -0
  142. package/dist/esm/chip/cert/controller-adapter.js +25 -0
  143. package/dist/esm/chip/cert/controller-adapter.js.map +6 -0
  144. package/dist/esm/chip/cert/device-config.d.ts +15 -0
  145. package/dist/esm/chip/cert/device-config.d.ts.map +1 -0
  146. package/dist/esm/chip/cert/device-config.js +23 -0
  147. package/dist/esm/chip/cert/device-config.js.map +6 -0
  148. package/dist/esm/chip/cert/evidence.d.ts +70 -0
  149. package/dist/esm/chip/cert/evidence.d.ts.map +1 -0
  150. package/dist/esm/chip/cert/evidence.js +108 -0
  151. package/dist/esm/chip/cert/evidence.js.map +6 -0
  152. package/dist/esm/chip/cert/log-follower.d.ts +96 -0
  153. package/dist/esm/chip/cert/log-follower.d.ts.map +1 -0
  154. package/dist/esm/chip/cert/log-follower.js +189 -0
  155. package/dist/esm/chip/cert/log-follower.js.map +6 -0
  156. package/dist/esm/chip/cert/matterjs-subject-registry.d.ts +22 -0
  157. package/dist/esm/chip/cert/matterjs-subject-registry.d.ts.map +1 -0
  158. package/dist/esm/chip/cert/matterjs-subject-registry.js +20 -0
  159. package/dist/esm/chip/cert/matterjs-subject-registry.js.map +6 -0
  160. package/dist/esm/chip/cert/prompt-driven-python-test.d.ts +40 -0
  161. package/dist/esm/chip/cert/prompt-driven-python-test.d.ts.map +1 -0
  162. package/dist/esm/chip/cert/prompt-driven-python-test.js +57 -0
  163. package/dist/esm/chip/cert/prompt-driven-python-test.js.map +6 -0
  164. package/dist/esm/chip/chip-bins.d.ts +132 -0
  165. package/dist/esm/chip/chip-bins.d.ts.map +1 -0
  166. package/dist/esm/chip/chip-bins.js +212 -0
  167. package/dist/esm/chip/chip-bins.js.map +6 -0
  168. package/dist/esm/chip/chip-test-common.d.ts.map +1 -1
  169. package/dist/esm/chip/chip-test-common.js +1 -1
  170. package/dist/esm/chip/chip-test-common.js.map +1 -1
  171. package/dist/esm/chip/chip.d.ts.map +1 -1
  172. package/dist/esm/chip/chip.js +1 -1
  173. package/dist/esm/chip/chip.js.map +1 -1
  174. package/dist/esm/chip/config.d.ts +26 -0
  175. package/dist/esm/chip/config.d.ts.map +1 -1
  176. package/dist/esm/chip/config.js +11 -3
  177. package/dist/esm/chip/config.js.map +1 -1
  178. package/dist/esm/chip/index.d.ts +26 -0
  179. package/dist/esm/chip/index.d.ts.map +1 -1
  180. package/dist/esm/chip/index.js +60 -0
  181. package/dist/esm/chip/index.js.map +1 -1
  182. package/dist/esm/chip/pics/file.d.ts +8 -0
  183. package/dist/esm/chip/pics/file.d.ts.map +1 -1
  184. package/dist/esm/chip/pics/file.js +4 -1
  185. package/dist/esm/chip/pics/file.js.map +1 -1
  186. package/dist/esm/chip/pics/source.d.ts +8 -5
  187. package/dist/esm/chip/pics/source.d.ts.map +1 -1
  188. package/dist/esm/chip/pics/source.js +24 -16
  189. package/dist/esm/chip/pics/source.js.map +1 -1
  190. package/dist/esm/chip/python-test.d.ts +12 -0
  191. package/dist/esm/chip/python-test.d.ts.map +1 -1
  192. package/dist/esm/chip/python-test.js +6 -4
  193. package/dist/esm/chip/python-test.js.map +1 -1
  194. package/dist/esm/chip/state.d.ts.map +1 -1
  195. package/dist/esm/chip/state.js +25 -8
  196. package/dist/esm/chip/state.js.map +1 -1
  197. package/dist/esm/chip/yaml-test.js +1 -1
  198. package/dist/esm/chip/yaml-test.js.map +1 -1
  199. package/dist/esm/cli.d.ts.map +1 -1
  200. package/dist/esm/cli.js +33 -2
  201. package/dist/esm/cli.js.map +1 -1
  202. package/dist/esm/device/backchannel.d.ts.map +1 -1
  203. package/dist/esm/device/subject.d.ts.map +1 -1
  204. package/dist/esm/device/test.d.ts.map +1 -1
  205. package/dist/esm/docker/container.d.ts.map +1 -1
  206. package/dist/esm/docker/docker.d.ts.map +1 -1
  207. package/dist/esm/docker/edit.d.ts.map +1 -1
  208. package/dist/esm/docker/errors.d.ts.map +1 -1
  209. package/dist/esm/docker/network.d.ts.map +1 -1
  210. package/dist/esm/docker/terminal.d.ts.map +1 -1
  211. package/dist/esm/failure-reporter.d.ts.map +1 -1
  212. package/dist/esm/global-declarations.d.js +0 -1
  213. package/dist/esm/global-declarations.d.js.map +1 -1
  214. package/dist/esm/global-declarations.d.ts +2 -2
  215. package/dist/esm/global-definitions.js +16 -3
  216. package/dist/esm/global-definitions.js.map +1 -1
  217. package/dist/esm/index.d.js +2 -0
  218. package/dist/esm/index.d.js.map +1 -1
  219. package/dist/esm/index.d.ts +3 -0
  220. package/dist/esm/index.js +2 -0
  221. package/dist/esm/index.js.map +1 -1
  222. package/dist/esm/mocha.d.ts.map +1 -1
  223. package/dist/esm/mocha.js +5 -0
  224. package/dist/esm/mocha.js.map +1 -1
  225. package/dist/esm/mocharc.cjs +4 -3
  226. package/dist/esm/options.d.ts.map +1 -1
  227. package/dist/esm/print-report.js +1 -1
  228. package/dist/esm/print-report.js.map +1 -1
  229. package/dist/esm/test-descriptor.d.ts +1 -1
  230. package/dist/esm/test-descriptor.d.ts.map +1 -1
  231. package/dist/esm/text-diff.d.ts.map +1 -1
  232. package/dist/esm/util/async.d.ts +30 -0
  233. package/dist/esm/util/async.d.ts.map +1 -0
  234. package/dist/esm/util/async.js +65 -0
  235. package/dist/esm/util/async.js.map +6 -0
  236. package/dist/esm/util/trace-unhandled.d.ts.map +1 -1
  237. package/dist/esm/util/wtf.d.ts.map +1 -1
  238. package/dist/esm/util/wtf.js +1 -1
  239. package/dist/esm/util/wtf.js.map +1 -1
  240. package/dist/esm/web.d.ts.map +1 -1
  241. package/dist/esm/web.js +5 -1
  242. package/dist/esm/web.js.map +1 -1
  243. package/package.json +9 -9
  244. package/src/chai.ts +3 -3
  245. package/src/chip/cert/cert-context.ts +126 -0
  246. package/src/chip/cert/cert-dsl.ts +451 -0
  247. package/src/chip/cert/cert-test.ts +360 -0
  248. package/src/chip/cert/chip-app-subject.ts +457 -0
  249. package/src/chip/cert/controller-adapter.ts +142 -0
  250. package/src/chip/cert/device-config.ts +33 -0
  251. package/src/chip/cert/evidence.ts +170 -0
  252. package/src/chip/cert/log-follower.ts +266 -0
  253. package/src/chip/cert/matterjs-subject-registry.ts +33 -0
  254. package/src/chip/cert/prompt-driven-python-test.ts +104 -0
  255. package/src/chip/chip-bins.ts +401 -0
  256. package/src/chip/chip-test-common.ts +3 -1
  257. package/src/chip/chip.ts +1 -1
  258. package/src/chip/config.ts +28 -1
  259. package/src/chip/index.ts +67 -0
  260. package/src/chip/pics/file.ts +8 -0
  261. package/src/chip/pics/source.ts +39 -19
  262. package/src/chip/python-test.ts +5 -4
  263. package/src/chip/state.ts +39 -8
  264. package/src/chip/yaml-test.ts +1 -1
  265. package/src/cli.ts +40 -2
  266. package/src/global-declarations.d.ts +2 -2
  267. package/src/global-definitions.ts +27 -3
  268. package/src/index.d.ts +3 -0
  269. package/src/index.js +1 -0
  270. package/src/mocha.ts +13 -0
  271. package/src/mocharc.cjs +4 -3
  272. package/src/print-report.ts +1 -1
  273. package/src/test-descriptor.ts +1 -1
  274. package/src/util/async.ts +83 -0
  275. package/src/util/wtf.ts +3 -1
  276. package/src/web.ts +5 -1
  277. package/test/util/AsyncTest.ts +68 -0
@@ -0,0 +1,457 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2026 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { ChildProcess, spawn } from "node:child_process";
8
+ import { mkdtemp, rm } from "node:fs/promises";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import { env, platform as hostPlatform } from "node:process";
12
+ import type { Subject } from "../../device/subject.js";
13
+ import type { Container } from "../../docker/container.js";
14
+ import { Docker } from "../../docker/docker.js";
15
+ import { NonZeroExitError } from "../../docker/errors.js";
16
+ import { Terminal } from "../../docker/terminal.js";
17
+ import { Volume } from "../../docker/volume.js";
18
+ import { delay, LineQueue } from "../../util/async.js";
19
+ import { asyncLinesOf } from "../../util/text.js";
20
+ import { CERT_BINS_PLATFORM, prepareChipBins, resolveChipBinsSource } from "../chip-bins.js";
21
+ import { HARNESS_DBUS_CONTAINER } from "../config.js";
22
+ import { PicsUnavailableError, type PicsFile } from "../pics/file.js";
23
+ import type { CertDevice, CertDeviceFactory, DeviceExitInfo, DeviceFlavor } from "./cert-context.js";
24
+ import { LogFollower } from "./log-follower.js";
25
+
26
+ // ChipDockerDevice requires the harness dbus sidecar rather than starting its own dbus/mdns pair:
27
+ // two system dbus daemons and mdns responders bound to the same `matter.js-mdns` volume would
28
+ // fight over the same `/run/dbus` socket once the app container becomes reachable.
29
+ export { HARNESS_DBUS_CONTAINER };
30
+
31
+ const DEFAULT_DISCRIMINATOR = 3840;
32
+ const DEFAULT_PASSCODE = 20202021;
33
+ const STOP_TIMEOUT_MS = 5_000;
34
+
35
+ // Without these, a chip binary logs only its terse progress categories (EM/IN/SC) — the structured
36
+ // `CHIP:DMG: ReadRequestMessage = { AttributePathIB = ... }` decode dumps cert-test log checks match
37
+ // against (see TC-IDM-2.1's AGENTS.md section) never appear at all, on any platform build.
38
+ const TRACE_ARGS = ["--trace_log", "1", "--trace_decode", "1"];
39
+
40
+ function defaultCommissioning(): Subject.CommissioningParameters {
41
+ return {
42
+ kind: "on-network",
43
+ passcode: DEFAULT_PASSCODE,
44
+ discriminator: DEFAULT_DISCRIMINATOR,
45
+
46
+ // A real onboarding QR code needs the spec's base38 payload encoder, which lives in
47
+ // matter.js and is out of reach here per the packages/testing dependency invariant.
48
+ // ControllerAdapter.CommissioningTarget treats qrPairingCode as optional and commissions
49
+ // via discriminator/passcode instead.
50
+ qrPairingCode: "",
51
+ };
52
+ }
53
+
54
+ function throwUnsupported(flavor: DeviceFlavor, capability: string): never {
55
+ throw new Error(`${flavor} subjects do not support ${capability}; commission fresh per test instead`);
56
+ }
57
+
58
+ interface ExitDeferred {
59
+ promise: Promise<DeviceExitInfo>;
60
+ resolve: (info: DeviceExitInfo) => void;
61
+ }
62
+
63
+ function createExitDeferred(): ExitDeferred {
64
+ let resolve!: (info: DeviceExitInfo) => void;
65
+ const promise = new Promise<DeviceExitInfo>(r => {
66
+ resolve = r;
67
+ });
68
+ return { promise, resolve };
69
+ }
70
+
71
+ /**
72
+ * Resolve the directory `chip-local` subjects spawn `chip-<app>-app` binaries from. When
73
+ * `MATTER_CHIP_BINS_SOURCE=cert-bins`, this extracts (if not already cached — see
74
+ * {@link prepareChipBins}) the official `connectedhomeip/chip-cert-bins` image and returns its own
75
+ * directory, ignoring `MATTER_CERT_APP_DIR` entirely; otherwise it requires `MATTER_CERT_APP_DIR` as
76
+ * before (a directory the user built/populated themselves). `cert-dsl.ts`'s
77
+ * `chipLocalMarkerRevision()` calls this too, so a cert-bins-sourced run's evidence `chipRef` comes
78
+ * from the same directory without separate wiring.
79
+ */
80
+ export async function resolveChipLocalAppDir(): Promise<string> {
81
+ if (resolveChipBinsSource() === "cert-bins") {
82
+ // chip-local spawns the binary directly on the host, with no container in between —
83
+ // unlike the classic-test bind-mount (chip/state.ts), there's no container platform to
84
+ // match, but the host OS itself must be able to run the extracted Linux ELF binaries, which
85
+ // macOS cannot (exec format error), regardless of arch.
86
+ if (hostPlatform !== "linux") {
87
+ throw new Error(
88
+ `MATTER_CHIP_BINS_SOURCE=cert-bins selects Linux/${CERT_BINS_PLATFORM} binaries that chip-local ` +
89
+ `spawns directly on the host; this host is "${hostPlatform}", which cannot run them. Use a ` +
90
+ "Linux host or CI runner for chip-local with cert-bins, or unset MATTER_CHIP_BINS_SOURCE and " +
91
+ "point MATTER_CERT_APP_DIR at binaries built for this platform.",
92
+ );
93
+ }
94
+ return (await prepareChipBins()).dir;
95
+ }
96
+
97
+ const dir = env.MATTER_CERT_APP_DIR;
98
+ if (!dir) {
99
+ throw new Error("MATTER_CERT_APP_DIR is not set; ChipLocalSubject needs it to find chip-<app>-app binaries");
100
+ }
101
+ return dir;
102
+ }
103
+
104
+ /**
105
+ * A chip-app device spawned as a local process. Binary path, KVS storage, and process lifecycle
106
+ * are entirely local to this instance; nothing here touches Docker.
107
+ */
108
+ class ChipLocalDevice implements CertDevice {
109
+ readonly flavor: DeviceFlavor = "chip-local";
110
+ readonly id: string;
111
+ readonly app: string;
112
+ readonly commissioning: Subject.CommissioningParameters;
113
+ readonly log: LogFollower;
114
+
115
+ #appArgs: string[];
116
+ #hub = new LineQueue();
117
+ #storageDir?: string;
118
+ #child?: ChildProcess;
119
+ #exit: ExitDeferred = createExitDeferred();
120
+ #pumps = new Array<Promise<void>>();
121
+
122
+ constructor(app: string, domain: string, options?: Subject.Options) {
123
+ this.app = app;
124
+ this.id = domain;
125
+ this.commissioning = defaultCommissioning();
126
+ this.log = new LogFollower(this.#hub.follow(), domain);
127
+ this.#appArgs = options?.appArgs ?? [];
128
+ }
129
+
130
+ get pics(): PicsFile {
131
+ throw new PicsUnavailableError("No active PICS file for this device");
132
+ }
133
+
134
+ get exit(): Promise<DeviceExitInfo> {
135
+ return this.#exit.promise;
136
+ }
137
+
138
+ async initialize(): Promise<void> {
139
+ await resolveChipLocalAppDir();
140
+ this.#storageDir ??= await mkdtemp(join(tmpdir(), "matter-cert-local-"));
141
+ }
142
+
143
+ async start(): Promise<void> {
144
+ if (this.#child) {
145
+ return;
146
+ }
147
+
148
+ const dir = await resolveChipLocalAppDir();
149
+ this.#storageDir ??= await mkdtemp(join(tmpdir(), "matter-cert-local-"));
150
+
151
+ const binPath = join(dir, `chip-${this.app}-app`);
152
+ const kvsPath = join(this.#storageDir, "chip_kvs");
153
+
154
+ const args = [
155
+ "--discriminator",
156
+ String(this.commissioning.discriminator),
157
+ "--passcode",
158
+ String(this.commissioning.passcode),
159
+ "--KVS",
160
+ kvsPath,
161
+ ...TRACE_ARGS,
162
+ ...this.#appArgs,
163
+ ];
164
+
165
+ const child = spawn(binPath, args, { stdio: ["ignore", "pipe", "pipe"] });
166
+ const { stdout, stderr } = child;
167
+ if (!stdout || !stderr) {
168
+ throw new Error("Spawned process has no stdout/stderr streams");
169
+ }
170
+
171
+ this.#child = child;
172
+ this.#exit = createExitDeferred();
173
+ this.#pumps = [this.#hub.pump(asyncLinesOf(stdout)), this.#hub.pump(asyncLinesOf(stderr))];
174
+
175
+ child.once("exit", (code, signal) => {
176
+ this.#exit.resolve({ code, signal });
177
+ });
178
+
179
+ child.once("error", error => {
180
+ // spawn() failures (e.g. ENOENT) surface via "error", not "exit" — the exit promise
181
+ // still needs to settle so a caller awaiting it doesn't hang.
182
+ this.#exit.resolve({ code: null, signal: null });
183
+ console.warn(`Error running ${binPath}:`, error);
184
+ });
185
+ }
186
+
187
+ async stop(): Promise<void> {
188
+ const child = this.#child;
189
+ if (!child) {
190
+ return;
191
+ }
192
+
193
+ child.kill("SIGTERM");
194
+
195
+ const timer = delay(STOP_TIMEOUT_MS);
196
+ try {
197
+ const outcome = await Promise.race([this.#exit.promise.then((): "exited" => "exited"), timer.promise]);
198
+ if (outcome === "timeout") {
199
+ child.kill("SIGKILL");
200
+ await this.#exit.promise;
201
+ }
202
+ } finally {
203
+ timer.cancel();
204
+ this.#child = undefined;
205
+ }
206
+ }
207
+
208
+ async close(): Promise<void> {
209
+ await this.stop();
210
+
211
+ const results = await Promise.allSettled(this.#pumps);
212
+ for (const result of results) {
213
+ if (result.status === "rejected") {
214
+ console.warn("Error reading device output:", result.reason);
215
+ }
216
+ }
217
+ this.#hub.close();
218
+
219
+ if (this.#storageDir) {
220
+ await rm(this.#storageDir, { recursive: true, force: true });
221
+ this.#storageDir = undefined;
222
+ }
223
+ }
224
+
225
+ async snapshot(): Promise<{}> {
226
+ throwUnsupported(this.flavor, "snapshot/restore");
227
+ }
228
+
229
+ async restore(): Promise<void> {
230
+ throwUnsupported(this.flavor, "snapshot/restore");
231
+ }
232
+
233
+ async backchannel(): Promise<void> {
234
+ throwUnsupported(this.flavor, "backchannel simulation");
235
+ }
236
+ }
237
+
238
+ export function chipImageBase(): string {
239
+ return env.MATTER_CERT_CHIP_IMAGE_BASE || "ghcr.io/matter-js/chip";
240
+ }
241
+
242
+ function mdnsVolumeName(): string {
243
+ return env.MATTER_MDNS_VOLUME || "matter.js-mdns";
244
+ }
245
+
246
+ /**
247
+ * The subset of `Docker.compose()`'s return value that {@link ChipDockerDevice} needs, extracted so
248
+ * tests can substitute a fake without a running Docker daemon.
249
+ */
250
+ export interface CompositionHandle {
251
+ add(
252
+ config: Partial<Container.Configuration> & {
253
+ name: string;
254
+ recreate?: boolean;
255
+ },
256
+ ): Promise<Container>;
257
+ close(): Promise<void>;
258
+ }
259
+
260
+ /**
261
+ * The subset of {@link Docker} (plus volume setup) that {@link ChipDockerDevice} needs, extracted so
262
+ * tests can substitute a fake without a running Docker daemon.
263
+ */
264
+ export interface DockerHandle {
265
+ ensureVolume(name: string): Promise<void>;
266
+ compose(name: string, config?: Partial<Container.Configuration>): CompositionHandle;
267
+ containerStatus(name: string): Promise<{ isRunning: boolean } | undefined>;
268
+ }
269
+
270
+ function realDockerHandle(): DockerHandle {
271
+ const docker = new Docker();
272
+ return {
273
+ async ensureVolume(name) {
274
+ await Volume(docker, name).open();
275
+ },
276
+ compose(name, config) {
277
+ return docker.compose(name, config);
278
+ },
279
+ containerStatus(name) {
280
+ return docker.containerStatus(name);
281
+ },
282
+ };
283
+ }
284
+
285
+ /**
286
+ * A chip-app device run in Docker. Runs the app-specific `ghcr.io/matter-js/chip-<app>` image (its
287
+ * own `ENTRYPOINT` already runs the right binary) as a single container, reusing the CHIP harness's
288
+ * dbus/mdns sidecars (`chip/state.ts`'s `configureContainer`) instead of starting a duplicate pair —
289
+ * see {@link HARNESS_DBUS_CONTAINER}.
290
+ */
291
+ export class ChipDockerDevice implements CertDevice {
292
+ readonly flavor: DeviceFlavor = "chip-docker";
293
+ readonly id: string;
294
+ readonly app: string;
295
+ readonly commissioning: Subject.CommissioningParameters;
296
+ readonly log: LogFollower;
297
+
298
+ #appArgs: string[];
299
+ #hub = new LineQueue();
300
+ #docker: DockerHandle;
301
+ #composition?: CompositionHandle;
302
+ #container?: Container;
303
+ #exit: ExitDeferred = createExitDeferred();
304
+ #pump?: Promise<void>;
305
+
306
+ constructor(app: string, domain: string, options?: Subject.Options, docker: DockerHandle = realDockerHandle()) {
307
+ this.app = app;
308
+ this.id = domain;
309
+ this.commissioning = defaultCommissioning();
310
+ this.log = new LogFollower(this.#hub.follow(), domain);
311
+ this.#appArgs = options?.appArgs ?? [];
312
+ this.#docker = docker;
313
+ }
314
+
315
+ get pics(): PicsFile {
316
+ throw new PicsUnavailableError("No active PICS file for this device");
317
+ }
318
+
319
+ get exit(): Promise<DeviceExitInfo> {
320
+ return this.#exit.promise;
321
+ }
322
+
323
+ async initialize(): Promise<void> {}
324
+
325
+ async start(): Promise<void> {
326
+ if (this.#container) {
327
+ return;
328
+ }
329
+
330
+ const dbusStatus = await this.#docker.containerStatus(HARNESS_DBUS_CONTAINER);
331
+ if (!dbusStatus?.isRunning) {
332
+ throw new Error(
333
+ `Cannot start chip-docker cert subject "${this.id}": harness dbus container ` +
334
+ `"${HARNESS_DBUS_CONTAINER}" is not running. chip-docker subjects reuse the CHIP harness's ` +
335
+ "dbus/mdns sidecars (chip/state.ts's configureContainer) instead of starting their own; " +
336
+ "start the harness (e.g. via certTest(), which installs State.initialize() as a beforeRun " +
337
+ "hook) before starting this subject.",
338
+ );
339
+ }
340
+
341
+ const platform = env.MATTER_CHIP_PLATFORM || "linux/amd64";
342
+ const appImage = `${chipImageBase()}-${this.app}:latest`;
343
+ const volumeName = mdnsVolumeName();
344
+
345
+ await this.#docker.ensureVolume(volumeName);
346
+
347
+ const composition = this.#docker.compose(`cert-${this.app}-${this.id}`, {
348
+ platform,
349
+ binds: { [volumeName]: "/run/dbus" },
350
+ network: "host",
351
+ autoRemove: true,
352
+ });
353
+
354
+ const args = [
355
+ "--discriminator",
356
+ String(this.commissioning.discriminator),
357
+ "--passcode",
358
+ String(this.commissioning.passcode),
359
+ ...TRACE_ARGS,
360
+ ...this.#appArgs,
361
+ ];
362
+
363
+ const container = await composition.add({
364
+ name: "app",
365
+ image: appImage,
366
+ recreate: true,
367
+ binds: { [volumeName]: "/run/dbus" },
368
+ command: args,
369
+ });
370
+
371
+ this.#composition = composition;
372
+ this.#container = container;
373
+ this.#exit = createExitDeferred();
374
+
375
+ // Deliberately not awaited — it runs for the container's whole lifetime and only settles
376
+ // #exit, which stop()/close() await separately. Its own try/catch means nothing is swallowed.
377
+ // Must stay ahead of anything that can throw below: #container/#exit are already installed,
378
+ // so a start() that fails later still leaves stop() an #exit that settles.
379
+ void this.#trackExit(container);
380
+
381
+ // Attaching immediately after the container starts still risks losing whatever it printed
382
+ // in that gap — Docker doesn't let us attach before start. Acceptable for now; Task 6 smoke-
383
+ // tests this flavor end to end.
384
+ const terminal = await container.attach(Terminal.Line);
385
+ this.#pump = this.#hub.pump(terminal);
386
+ }
387
+
388
+ async #trackExit(container: Container): Promise<void> {
389
+ try {
390
+ await container.wait();
391
+ this.#exit.resolve({ code: 0, signal: null });
392
+ } catch (e) {
393
+ if (e instanceof NonZeroExitError) {
394
+ this.#exit.resolve({ code: e.code, signal: null });
395
+ } else {
396
+ console.warn(`Error waiting for cert device container ${this.id}:`, e);
397
+ this.#exit.resolve({ code: null, signal: null });
398
+ }
399
+ }
400
+ }
401
+
402
+ async stop(): Promise<void> {
403
+ const container = this.#container;
404
+ if (!container) {
405
+ return;
406
+ }
407
+
408
+ try {
409
+ await container.kill();
410
+ } catch {
411
+ // Already stopped/removed (e.g. crashed) — nothing left to stop.
412
+ }
413
+ await this.#exit.promise;
414
+
415
+ await this.#composition?.close();
416
+ this.#composition = undefined;
417
+ this.#container = undefined;
418
+ }
419
+
420
+ async close(): Promise<void> {
421
+ await this.stop();
422
+
423
+ if (this.#pump) {
424
+ await this.#pump.catch(e => console.warn("Error reading device output:", e));
425
+ this.#pump = undefined;
426
+ }
427
+ this.#hub.close();
428
+ }
429
+
430
+ async snapshot(): Promise<{}> {
431
+ throwUnsupported(this.flavor, "snapshot/restore");
432
+ }
433
+
434
+ async restore(): Promise<void> {
435
+ throwUnsupported(this.flavor, "snapshot/restore");
436
+ }
437
+
438
+ async backchannel(): Promise<void> {
439
+ throwUnsupported(this.flavor, "backchannel simulation");
440
+ }
441
+ }
442
+
443
+ /**
444
+ * Spawns `${MATTER_CERT_APP_DIR}/chip-<app>-app` as a local child process for cert tests.
445
+ */
446
+ export function ChipLocalSubject(app: string): CertDeviceFactory {
447
+ return (domain: string, options?: Subject.Options) => new ChipLocalDevice(app, domain, options);
448
+ }
449
+
450
+ /**
451
+ * Runs a chip-<app> device in a Docker container. Requires the CHIP harness's dbus/mdns sidecars
452
+ * (started by `certTest()` via `State.initialize()`) to already be running — see
453
+ * {@link HARNESS_DBUS_CONTAINER}.
454
+ */
455
+ export function ChipDockerSubject(app: string): CertDeviceFactory {
456
+ return (domain: string, options?: Subject.Options) => new ChipDockerDevice(app, domain, options);
457
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2026 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import type { LogSource } from "./cert-context.js";
8
+ import type { LogFollower } from "./log-follower.js";
9
+
10
+ export type { LogSource };
11
+
12
+ /**
13
+ * Opaque reference to a node commissioned through a {@link ControllerAdapter}.
14
+ *
15
+ * Adapters mint and interpret their own refs; a step never constructs or parses one, it only passes
16
+ * back what {@link ControllerAdapter.commission} returned.
17
+ */
18
+ export type CertNodeRef = string;
19
+
20
+ /**
21
+ * Commissioning parameters a {@link ControllerAdapter} needs to pair a node.
22
+ *
23
+ * Structurally compatible with {@link Subject.CommissioningParameters} so a step can pass
24
+ * `subject.commissioning` directly for a device's original setup code.
25
+ *
26
+ * Either `manualPairingCode` or both `passcode`/`discriminator` must be present. An enhanced
27
+ * commissioning window (`CertNodeApi.openCommissioningWindow({enhanced: true})`) generates a fresh
28
+ * random discriminator/passcode pair that only the returned `manualPairingCode` carries — a step
29
+ * commissioning through that window has no other way to obtain them.
30
+ */
31
+ export interface CommissioningTarget {
32
+ passcode?: number;
33
+ discriminator?: number;
34
+ qrPairingCode?: string;
35
+ manualPairingCode?: string;
36
+ }
37
+
38
+ /**
39
+ * An attribute (or, for {@link CertNodeApi.readAttribute}, wildcard attribute path).
40
+ *
41
+ * An absent field is a wildcard for that path segment. `readAttribute` supports wildcards
42
+ * (TC-IDM-2.1); other operations require a concrete path.
43
+ */
44
+ export interface AttributePathSpec {
45
+ endpoint?: number;
46
+ cluster?: number;
47
+ attribute?: number;
48
+ }
49
+
50
+ export interface SubscribeOptions {
51
+ minIntervalFloorSeconds: number;
52
+ maxIntervalCeilingSeconds: number;
53
+ onUpdate?: (value: unknown) => void;
54
+ }
55
+
56
+ export interface ReadAttributeOptions {
57
+ /**
58
+ * Whether the read is fabric-filtered (Matter Core § 8.9.2's `FabricFiltered` flag; default
59
+ * true, the interaction-model default). Set false to read across all fabrics — a
60
+ * fabric-scoped attribute like OperationalCredentials.fabrics otherwise returns only the
61
+ * reading controller's own entry, useless for a multi-controller TC that must see fabrics it
62
+ * didn't itself create.
63
+ */
64
+ fabricFiltered?: boolean;
65
+ }
66
+
67
+ /**
68
+ * Controller-side view of a single commissioned node.
69
+ */
70
+ export interface CertNodeApi {
71
+ invoke(cluster: string | number, command: string, args?: object, endpoint?: number): Promise<unknown>;
72
+ readAttribute(path: AttributePathSpec, options?: ReadAttributeOptions): Promise<unknown>;
73
+ writeAttribute(path: AttributePathSpec, value: unknown): Promise<void>;
74
+ subscribe(path: AttributePathSpec, opts: SubscribeOptions): Promise<unknown>;
75
+ openCommissioningWindow(opts: {
76
+ timeout: number;
77
+ enhanced: boolean;
78
+ }): Promise<{ manualPairingCode?: string; qrPairingCode?: string }>;
79
+ decommission(): Promise<void>;
80
+ /**
81
+ * The operational mDNS instance name (`<compressed-fabric-id>-<node-id>._matter._tcp.local`) this node
82
+ * advertises on the fabric it was commissioned onto — the same value matter.js's own advertiser computes
83
+ * via `getOperationalDeviceQname` (`@matter/protocol`). A network check (see
84
+ * `support/chip-testing/src/cert/mdns-check.ts`) uses this to attribute an operational SRV record to this
85
+ * specific node rather than to whatever else is advertising `_matter._tcp` on the network.
86
+ */
87
+ operationalMdnsInstanceName(): Promise<string>;
88
+ }
89
+
90
+ /**
91
+ * A controller identity participating in a cert test (e.g. "dut", "th_cr2").
92
+ *
93
+ * Pure interface: no matter.js type ever crosses this boundary, only plain string/number addressing
94
+ * and plain data. Implementations wrap a real controller stack (see
95
+ * `support/chip-testing/src/cert/InProcessControllerAdapter.ts`) but this package must stay free of
96
+ * that dependency.
97
+ */
98
+ export interface ControllerAdapter {
99
+ id: string;
100
+ start(): Promise<void>;
101
+ close(): Promise<void>;
102
+ commission(target: CommissioningTarget): Promise<CertNodeRef>;
103
+ node(ref: CertNodeRef): CertNodeApi;
104
+ log: LogFollower;
105
+ }
106
+
107
+ /**
108
+ * Builds a {@link ControllerAdapter} for the given id (e.g. "dut", "th_cr2").
109
+ */
110
+ export type ControllerAdapterFactory = (id: string) => ControllerAdapter;
111
+
112
+ let activeFactory: ControllerAdapterFactory | undefined;
113
+
114
+ /**
115
+ * Registers the {@link ControllerAdapterFactory} cert-test wiring uses to construct controllers.
116
+ *
117
+ * `packages/testing` cannot construct a real controller itself (that needs matter.js, which this
118
+ * package must stay free of — see the repo's dependency invariant); `support/chip-testing/src/cert`
119
+ * registers its `InProcessControllerAdapter` here at load time instead. There is exactly one slot:
120
+ * re-registration throws, since a silent overwrite would swap controller implementations under any
121
+ * cert test already declared.
122
+ */
123
+ export function registerControllerAdapterFactory(factory: ControllerAdapterFactory): void {
124
+ if (activeFactory) {
125
+ throw new Error("A ControllerAdapter factory is already registered; only one is supported per process");
126
+ }
127
+ activeFactory = factory;
128
+ }
129
+
130
+ /**
131
+ * Constructs a {@link ControllerAdapter} via the factory registered with
132
+ * {@link registerControllerAdapterFactory}.
133
+ */
134
+ export function createControllerAdapter(id: string): ControllerAdapter {
135
+ if (!activeFactory) {
136
+ throw new Error(
137
+ "No ControllerAdapter factory registered; a consumer (e.g. support/chip-testing/src/cert/index.ts) " +
138
+ "must call registerControllerAdapterFactory() before running a cert test",
139
+ );
140
+ }
141
+ return activeFactory(id);
142
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2026 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { env } from "node:process";
8
+ import type { DeviceFlavor } from "./cert-context.js";
9
+
10
+ function isDeviceFlavor(value: string): value is DeviceFlavor {
11
+ return value === "chip-docker" || value === "chip-local" || value === "matterjs";
12
+ }
13
+
14
+ /**
15
+ * Resolve which device implementation cert tests run against, from `MATTER_CERT_DEVICE`.
16
+ *
17
+ * Unset defaults to `matterjs`, the only flavor that works with no configuration at all:
18
+ * `chip-local` needs `MATTER_CERT_APP_DIR`/`MATTER_CHIP_BINS_SOURCE`, and `chip-docker` has no
19
+ * published per-app images yet, so either would guarantee a failing default run.
20
+ */
21
+ export function resolveDeviceFlavor(): DeviceFlavor {
22
+ const value = env.MATTER_CERT_DEVICE;
23
+
24
+ if (value === undefined || value === "") {
25
+ return "matterjs";
26
+ }
27
+
28
+ if (isDeviceFlavor(value)) {
29
+ return value;
30
+ }
31
+
32
+ throw new Error(`Unknown MATTER_CERT_DEVICE "${value}" (expected "chip-docker", "chip-local", or "matterjs")`);
33
+ }