@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,170 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2026 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { mkdir, writeFile } from "node:fs/promises";
8
+ import { join } from "node:path";
9
+ import type { CertStepDefinition, CheckRecord, DeviceExitInfo, StepRecorder, StepVerdict } from "./cert-context.js";
10
+ import type { LogLine } from "./log-follower.js";
11
+
12
+ // StepRecorder.check() needs this shape too, so cert-context.ts is CheckRecord's canonical home;
13
+ // re-exported here so callers of this module don't need a second import.
14
+ export type { CheckRecord };
15
+
16
+ /**
17
+ * A single cert-test step's recorded outcome and evidence.
18
+ */
19
+ export interface StepRecord {
20
+ step: number | string;
21
+ text: string;
22
+ expected?: string;
23
+ checks: CheckRecord[];
24
+ verdict: StepVerdict;
25
+ skipReason?: string;
26
+ }
27
+
28
+ /**
29
+ * The evidence bundle for one cert-test run, written to `result.json` by {@link EvidenceRecorder.flush}.
30
+ */
31
+ export interface RunRecord {
32
+ tc: string;
33
+ plan: string;
34
+ run: {
35
+ timestamp: string;
36
+ controller: string;
37
+ device: string;
38
+ matterJsCommit: string;
39
+ chipRef?: string;
40
+ };
41
+ steps: StepRecord[];
42
+ verdict: "pass" | "fail" | "skipped";
43
+ deviceExit?: { code: number | null; signal?: string };
44
+ }
45
+
46
+ // Colons aren't valid in a Windows path segment; CI here targets macOS/Linux, but a dash-for-colon
47
+ // directory name still reads as a timestamp on any filesystem, so there's no reason to special-case.
48
+ function sanitizeTimestampForPath(timestamp: string): string {
49
+ return timestamp.replace(/:/g, "-");
50
+ }
51
+
52
+ /**
53
+ * Collects a {@link CertTest} run's per-step evidence and writes it to disk as `result.json` plus one
54
+ * `<name>.log` per {@link attachLog} call.
55
+ *
56
+ * `endStep` always receives the full {@link CertStepDefinition}, not just a verdict — that's what
57
+ * lets a step the engine never reached (PICS-skipped, or trailing a prior abort, so `beginStep` was
58
+ * never called for it) still become a complete {@link StepRecord}: its `text`/`expected` come from
59
+ * the definition `endStep` was given, and its `checks` default to empty.
60
+ */
61
+ export class EvidenceRecorder implements StepRecorder {
62
+ #outDir: string;
63
+ #meta: RunRecord["run"] & { tc: string; plan: string };
64
+ #steps = new Array<StepRecord>();
65
+ #logs = new Map<string, LogLine[]>();
66
+ #current?: { def: CertStepDefinition; checks: CheckRecord[] };
67
+ #deviceExit?: { code: number | null; signal?: string };
68
+
69
+ constructor(outDir: string, meta: RunRecord["run"] & { tc: string; plan: string }) {
70
+ this.#outDir = outDir;
71
+ this.#meta = meta;
72
+ }
73
+
74
+ beginStep(step: CertStepDefinition): void {
75
+ this.#current = { def: step, checks: new Array<CheckRecord>() };
76
+ }
77
+
78
+ check(record: CheckRecord): void {
79
+ if (!this.#current) {
80
+ throw new Error("EvidenceRecorder.check() called without an active step; call beginStep() first");
81
+ }
82
+ this.#current.checks.push(record);
83
+ }
84
+
85
+ endStep(step: CertStepDefinition, verdict: StepVerdict, skipReason?: string): CheckRecord[] {
86
+ // No active step is fine (skipped/aborted steps never begin), but ending a *different*
87
+ // step than the active one would silently discard the active step's checks.
88
+ if (this.#current !== undefined && this.#current.def !== step) {
89
+ throw new Error(
90
+ `EvidenceRecorder.endStep(${step.number}) called while step ${this.#current.def.number} is active`,
91
+ );
92
+ }
93
+ const checks = this.#current?.checks ?? new Array<CheckRecord>();
94
+ this.#current = undefined;
95
+
96
+ this.#steps.push({
97
+ step: step.number,
98
+ text: step.text,
99
+ expected: step.expected,
100
+ checks,
101
+ verdict,
102
+ skipReason,
103
+ });
104
+
105
+ return checks;
106
+ }
107
+
108
+ /**
109
+ * Records that a device's backing process/container exited during the run. A device crash fails
110
+ * the run regardless of how far its steps got (see {@link RunRecord.deviceExit}).
111
+ */
112
+ deviceExited(info: DeviceExitInfo): void {
113
+ this.#deviceExit = { code: info.code, signal: info.signal ?? undefined };
114
+ }
115
+
116
+ /**
117
+ * Attaches a raw log dump under `<name>.log`, e.g. `attachLog("controller", ...)` or
118
+ * `attachLog("device-app1", ...)`.
119
+ */
120
+ attachLog(name: string, lines: LogLine[]): void {
121
+ this.#logs.set(name, lines);
122
+ }
123
+
124
+ async flush(): Promise<string> {
125
+ const dirName = `${sanitizeTimestampForPath(this.#meta.timestamp)}-${this.#meta.tc}`;
126
+ const dir = join(this.#outDir, dirName);
127
+ await mkdir(dir, { recursive: true });
128
+
129
+ const record: RunRecord = {
130
+ tc: this.#meta.tc,
131
+ plan: this.#meta.plan,
132
+ run: {
133
+ timestamp: this.#meta.timestamp,
134
+ controller: this.#meta.controller,
135
+ device: this.#meta.device,
136
+ matterJsCommit: this.#meta.matterJsCommit,
137
+ chipRef: this.#meta.chipRef,
138
+ },
139
+ steps: this.#steps,
140
+ verdict: this.#verdict(),
141
+ deviceExit: this.#deviceExit,
142
+ };
143
+
144
+ await writeFile(join(dir, "result.json"), JSON.stringify(record, null, 4));
145
+
146
+ for (const [name, lines] of this.#logs) {
147
+ await writeFile(join(dir, `${name}.log`), lines.map(line => line.text).join("\n"));
148
+ }
149
+
150
+ return dir;
151
+ }
152
+
153
+ /**
154
+ * A run where every step was skipped (or that had no steps at all — a malformed definition) is
155
+ * neither a pass nor a fail: nothing was actually exercised, so reporting `"pass"` would overstate
156
+ * what the run proved. Only a run with at least one non-skipped step can pass.
157
+ */
158
+ #verdict(): "pass" | "fail" | "skipped" {
159
+ if (this.#deviceExit) {
160
+ return "fail";
161
+ }
162
+ if (this.#steps.some(step => step.verdict === "fail" || step.verdict === "aborted")) {
163
+ return "fail";
164
+ }
165
+ if (this.#steps.some(step => step.verdict === "pass")) {
166
+ return "pass";
167
+ }
168
+ return "skipped";
169
+ }
170
+ }
@@ -0,0 +1,266 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2026 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { delay } from "../../util/async.js";
8
+ import { deansify } from "../../util/text.js";
9
+ import type { LogSource } from "./cert-context.js";
10
+
11
+ const DEFAULT_TIMEOUT_MS = 30_000;
12
+
13
+ /**
14
+ * A single buffered log line, with the cursor position it occupies in the follower's buffer.
15
+ */
16
+ export interface LogLine {
17
+ index: number;
18
+ at: Date;
19
+ text: string;
20
+ /** Set on a line added via {@link LogFollower.annotate} — never matched by {@link LogFollower.expect}. */
21
+ synthetic?: true;
22
+ }
23
+
24
+ export type LogExpectResult =
25
+ { verdict: "pass"; matched: LogLine; pattern: string } | { verdict: "unverified"; reason: "no-pattern-for-flavor" };
26
+
27
+ /**
28
+ * Per-implementation-family patterns for a {@link LogFollower.expect} call. A step supplies
29
+ * whichever of these it has evidence for; the active device or controller's flavor selects one.
30
+ */
31
+ export interface LogExpectPatterns {
32
+ chip?: RegExp;
33
+ matterjs?: RegExp;
34
+ }
35
+
36
+ export interface LogExpectOptions {
37
+ flavor: string;
38
+ timeoutMs?: number;
39
+ from?: number;
40
+ }
41
+
42
+ /**
43
+ * Thrown by {@link LogFollower.expect} when no buffered or newly-arrived line matches before the
44
+ * deadline.
45
+ */
46
+ export class CertLogTimeoutError extends Error {
47
+ constructor(
48
+ readonly pattern: string,
49
+ readonly from: number,
50
+ readonly tail: LogLine[],
51
+ ) {
52
+ super(
53
+ `Timed out waiting for ${pattern} (from index ${from}); last lines: ` +
54
+ tail
55
+ .slice(-20)
56
+ .map(line => line.text)
57
+ .join("\n"),
58
+ );
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Thrown by {@link LogFollower.expect} when the follower closes (or its source runs out) while the
64
+ * expectation is still pending and unmatched.
65
+ */
66
+ export class CertLogClosedError extends Error {
67
+ constructor(name: string) {
68
+ super(`Log follower "${name}" closed before a pending expect() matched`);
69
+ }
70
+ }
71
+
72
+ // Cert plans express expectations per implementation family, not per concrete DeviceFlavor
73
+ // ("chip-docker"/"chip-local" both speak for "chip"); this maps a flavor to the bucket that
74
+ // carries its pattern.
75
+ function patternFor(patterns: LogExpectPatterns, flavor: string): RegExp | undefined {
76
+ if (flavor.startsWith("chip")) {
77
+ return patterns.chip;
78
+ }
79
+ if (flavor === "matterjs") {
80
+ return patterns.matterjs;
81
+ }
82
+ return undefined;
83
+ }
84
+
85
+ // A caller-supplied /g or /y pattern keeps lastIndex between calls; reused as-is across the
86
+ // repeated buffer scans in expect()'s wait loop, that state silently skips or misattributes
87
+ // matches. Stripping once yields a pattern expect() can test() any number of times safely.
88
+ function matchableCopy(pattern: RegExp): RegExp {
89
+ return new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ""));
90
+ }
91
+
92
+ /**
93
+ * Buffers every line from an `AsyncIterable<string>` log source and lets cert-test steps assert
94
+ * that a matching line appears within a cursor window, without depending on any particular
95
+ * implementation's log format directly (a step supplies a chip-flavored and/or matterjs-flavored
96
+ * pattern; {@link expect} picks the one for the device or controller actually under test).
97
+ *
98
+ * Also implements {@link LogSource} so a follower can itself be consumed by another follower or
99
+ * assigned to a {@link CertDevice}/{@link ControllerAdapter}'s `log` field.
100
+ */
101
+ export class LogFollower implements LogSource {
102
+ readonly name: string;
103
+
104
+ #lines = new Array<LogLine>();
105
+ #lastMark = 0;
106
+ #closed = false;
107
+ #closeError?: Error;
108
+ #waiters = new Array<() => void>();
109
+ #resolveCloseSignal!: () => void;
110
+ #closeSignal: Promise<void>;
111
+ #pump: Promise<void>;
112
+
113
+ constructor(source: AsyncIterable<string>, name: string) {
114
+ this.name = name;
115
+ this.#closeSignal = new Promise<void>(resolve => {
116
+ this.#resolveCloseSignal = resolve;
117
+ });
118
+ this.#pump = this.#consume(source);
119
+ }
120
+
121
+ /**
122
+ * Appends a synthetic line (e.g. a step-boundary banner) to the buffer at the current position,
123
+ * indexed like a real line but flagged so {@link expect} skips it. Lets a caller mark the
124
+ * chronologically right position in the evidence `.log` output without that marker ever
125
+ * satisfying a step's own pattern.
126
+ */
127
+ annotate(text: string): void {
128
+ this.#lines.push({ index: this.#lines.length, at: new Date(), text, synthetic: true });
129
+ this.#wake();
130
+ }
131
+
132
+ /**
133
+ * Sets the default cursor a subsequent {@link expect} call scans from when it doesn't specify
134
+ * `from` itself. Typically called once at the start of a step.
135
+ */
136
+ mark(): number {
137
+ this.#lastMark = this.#lines.length;
138
+ return this.#lastMark;
139
+ }
140
+
141
+ /**
142
+ * Every line seen so far, in arrival order. A fresh copy on each access — callers cannot
143
+ * mutate the follower's own buffer through it.
144
+ */
145
+ get lines(): LogLine[] {
146
+ return [...this.#lines];
147
+ }
148
+
149
+ /**
150
+ * Waits for a line at or after the cursor (`options.from`, default: the last {@link mark})
151
+ * matching the pattern for `options.flavor`. Resolves `unverified` immediately if `patterns`
152
+ * has no entry for that flavor. Throws {@link CertLogTimeoutError} if the deadline passes
153
+ * first, or {@link CertLogClosedError} (or the source's own error) if the follower closes
154
+ * first.
155
+ */
156
+ async expect(patterns: LogExpectPatterns, options: LogExpectOptions): Promise<LogExpectResult> {
157
+ const original = patternFor(patterns, options.flavor);
158
+ if (!original) {
159
+ return { verdict: "unverified", reason: "no-pattern-for-flavor" };
160
+ }
161
+
162
+ const pattern = matchableCopy(original);
163
+ const from = options.from ?? this.#lastMark;
164
+ const timeout = delay(options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
165
+
166
+ try {
167
+ for (;;) {
168
+ const matched = this.#firstMatchFrom(pattern, from);
169
+ if (matched) {
170
+ return { verdict: "pass", matched, pattern: String(original) };
171
+ }
172
+
173
+ if (this.#closeError) {
174
+ throw this.#closeError;
175
+ }
176
+ if (this.#closed) {
177
+ throw new CertLogClosedError(this.name);
178
+ }
179
+
180
+ const outcome = await Promise.race([this.#waitForWake(), timeout.promise]);
181
+ if (outcome === "timeout") {
182
+ throw new CertLogTimeoutError(String(original), from, this.#lines.slice(from));
183
+ }
184
+ }
185
+ } finally {
186
+ timeout.cancel();
187
+ }
188
+ }
189
+
190
+ follow(): AsyncIterable<string> {
191
+ return this.#replay();
192
+ }
193
+
194
+ async close(): Promise<void> {
195
+ this.#resolveCloseSignal();
196
+ await this.#pump;
197
+ }
198
+
199
+ #firstMatchFrom(pattern: RegExp, from: number): LogLine | undefined {
200
+ for (let i = from; i < this.#lines.length; i++) {
201
+ if (this.#lines[i].synthetic) {
202
+ continue;
203
+ }
204
+ if (pattern.test(this.#lines[i].text)) {
205
+ return this.#lines[i];
206
+ }
207
+ }
208
+ return undefined;
209
+ }
210
+
211
+ #waitForWake(): Promise<"activity"> {
212
+ return new Promise(resolve => this.#waiters.push(() => resolve("activity")));
213
+ }
214
+
215
+ #wake(): void {
216
+ const waiters = this.#waiters;
217
+ this.#waiters = new Array<() => void>();
218
+ for (const waiter of waiters) {
219
+ waiter();
220
+ }
221
+ }
222
+
223
+ async *#replay(): AsyncGenerator<string> {
224
+ let index = 0;
225
+ for (;;) {
226
+ while (index < this.#lines.length) {
227
+ yield this.#lines[index++].text;
228
+ }
229
+ if (this.#closed) {
230
+ return;
231
+ }
232
+ await new Promise<void>(resolve => this.#waiters.push(resolve));
233
+ }
234
+ }
235
+
236
+ // #closeSignal is a single reused promise, so close() ends the loop immediately without
237
+ // forcing the source's iterator closed (which could hang on a source parked mid-await).
238
+ // Never rethrows: failures land on #closeError for expect() to surface, so #pump always
239
+ // resolves and close() has nothing to catch.
240
+ async #consume(source: AsyncIterable<string>): Promise<void> {
241
+ const iterator = source[Symbol.asyncIterator]();
242
+ try {
243
+ for (;;) {
244
+ const outcome = await Promise.race([
245
+ iterator.next().then(result => ({ closed: false as const, result })),
246
+ this.#closeSignal.then(() => ({ closed: true as const })),
247
+ ]);
248
+
249
+ if (outcome.closed || outcome.result.done) {
250
+ return;
251
+ }
252
+
253
+ // chip binaries colorize log output even when stdout isn't a TTY; a trailing ANSI reset
254
+ // sequence after the visible text breaks any pattern anchored on end-of-line ($).
255
+ this.#lines.push({ index: this.#lines.length, at: new Date(), text: deansify(outcome.result.value) });
256
+ this.#wake();
257
+ }
258
+ } catch (e) {
259
+ this.#closeError = e instanceof Error ? e : new Error(String(e));
260
+ console.warn(`Log follower "${this.name}" lost its source:`, this.#closeError);
261
+ } finally {
262
+ this.#closed = true;
263
+ this.#wake();
264
+ }
265
+ }
266
+ }
@@ -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 type { CertDeviceFactory } from "./cert-context.js";
8
+
9
+ const subjects = new Map<string, CertDeviceFactory>();
10
+
11
+ /**
12
+ * Registers a {@link CertDeviceFactory} for the "matterjs" {@link DeviceFlavor}, keyed by
13
+ * chip-test-header app name.
14
+ *
15
+ * Kept separate from {@link Chip.subjectFor}'s app registry: that one is for the existing py/yaml
16
+ * harness's plain `Subject.Factory`s, which don't carry {@link CertDevice}'s extra fields
17
+ * (`log`/`flavor`/`exit`). Registering the same app name in both places is fine — they're
18
+ * independent maps — but a cert factory needs the matter.js `Logger` sink that only
19
+ * `support/chip-testing` (not this package) is allowed to depend on, so it registers here instead.
20
+ */
21
+ export function registerMatterJsCertSubject(app: string, factory: CertDeviceFactory): void {
22
+ if (subjects.has(app)) {
23
+ throw new Error(`A matterjs cert subject is already registered for app "${app}"`);
24
+ }
25
+ subjects.set(app, factory);
26
+ }
27
+
28
+ /**
29
+ * Looks up a factory registered via {@link registerMatterJsCertSubject}.
30
+ */
31
+ export function matterJsCertSubjectFor(app: string): CertDeviceFactory | undefined {
32
+ return subjects.get(app);
33
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2026 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import type { Subject } from "../../device/subject.js";
8
+ import type { Container } from "../../docker/container.js";
9
+ import { Terminal } from "../../docker/terminal.js";
10
+ import type { TestFileDescriptor } from "../../test-descriptor.js";
11
+ import { parseStep } from "../chip-test-common.js";
12
+ import { FIFO_PATH } from "../container-command-pipe.js";
13
+ import { createCommand, PythonTest, spiffy } from "../python-test.js";
14
+ import type { CertStepContext } from "./cert-context.js";
15
+
16
+ /**
17
+ * Reacts to one line of a python-wrapped CHIP test script's own stdout. `pattern` identifies a prompt
18
+ * line — see `MatterBaseTest.wait_for_user_input` in `connectedhomeip`'s
19
+ * `matter/testing/matter_testing.py`, which logs the prompt then blocks on a bare `input()` read — and
20
+ * `action` reacts to it (e.g. drives a {@link CertStepContext} controller through a commissioning
21
+ * attempt) and returns the text {@link PromptDrivenPythonTest} writes to the script's stdin to unblock
22
+ * it.
23
+ */
24
+ export interface PromptHandler {
25
+ pattern: RegExp;
26
+ action: (cx: CertStepContext, promptText: string) => Promise<string>;
27
+ }
28
+
29
+ /**
30
+ * Runs a CHIP python test script that drives its own multi-party scenario and prompts for out-of-band
31
+ * action instead of letting the harness commission/act on its behalf — see `TC_SC_3_5.py`'s
32
+ * `wait_for_user_input` calls, one per CASE-handshake attempt it wants a real commissioner to make.
33
+ *
34
+ * Reuses {@link PythonTest}'s command construction ({@link createCommand}, so the same argument/PICS
35
+ * rules apply) but keeps the container exec's stdin open and answers matching prompt lines through
36
+ * `handlers` instead of running fully unattended. Commissioning happens interactively, driven by
37
+ * `handlers` reacting to prompts, so there is no subject to pre-pair — {@link initializeSubject} is a
38
+ * no-op.
39
+ */
40
+ export class PromptDrivenPythonTest extends PythonTest {
41
+ #handlers: PromptHandler[];
42
+ #cx: CertStepContext;
43
+
44
+ constructor(descriptor: TestFileDescriptor, container: Container, handlers: PromptHandler[], cx: CertStepContext) {
45
+ super(descriptor, container);
46
+ this.#handlers = handlers;
47
+ this.#cx = cx;
48
+ }
49
+
50
+ override async initializeSubject(_subject: Subject): Promise<void> {}
51
+
52
+ override async invoke(
53
+ subject: Subject,
54
+ step: (title: string) => void,
55
+ args: string[],
56
+ uncommissioned: boolean,
57
+ ): Promise<void> {
58
+ // CHIP's python runner refuses to start unless the --app-pipe fifo exists; no
59
+ // harness-managed app subject creates it in a prompt-driven run. Non-destructive so an
60
+ // already-attached classic-harness pipe is left alone.
61
+ await this.container.exec(["bash", "-c", `test -p ${FIFO_PATH} || mkfifo ${FIFO_PATH}`]);
62
+
63
+ const terminal = await this.container.exec(
64
+ await createCommand(this.descriptor, this.container, subject, args, uncommissioned),
65
+ Terminal.Line,
66
+ { cwd: "/tmp", stdin: true },
67
+ );
68
+
69
+ let passed = false;
70
+ try {
71
+ for await (let line of terminal) {
72
+ line = parseStep(line, step);
73
+
74
+ const handler = this.#handlers.find(h => h.pattern.test(line));
75
+ if (handler) {
76
+ const answer = await handler.action(this.#cx, line);
77
+ await terminal.write(answer);
78
+ }
79
+
80
+ if (line.indexOf("Final result: PASS") !== -1) {
81
+ // Old format
82
+ passed = true;
83
+ } else if (line.match(/\[Test\]\s+\S+\s+PASS$/)) {
84
+ // New format
85
+ passed = true;
86
+ }
87
+
88
+ MockLogger.injectExternalMessage("CHIP", spiffy(line));
89
+ }
90
+ } catch (e) {
91
+ // A handler that threw (e.g. an unexpected commissioning outcome) leaves the script still
92
+ // blocked on its own `input()` read; closing forces that read to fail instead of hanging
93
+ // the run for the full mocha timeout.
94
+ await terminal.close().catch(closeError => {
95
+ console.warn("Error closing prompt-driven python test terminal:", closeError);
96
+ });
97
+ throw e;
98
+ }
99
+
100
+ if (!passed) {
101
+ throw new Error("Python test exited without error but did not indicate successful test");
102
+ }
103
+ }
104
+ }