@executablemd/testing 0.3.1

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 (137) hide show
  1. package/LICENSE +21 -0
  2. package/esm/core/mod.js +55 -0
  3. package/esm/core/src/api.js +31 -0
  4. package/esm/core/src/collect.js +24 -0
  5. package/esm/core/src/component-api.js +47 -0
  6. package/esm/core/src/content-context.js +23 -0
  7. package/esm/core/src/deno-compiler.js +51 -0
  8. package/esm/core/src/errors.js +13 -0
  9. package/esm/core/src/eval-context.js +13 -0
  10. package/esm/core/src/eval-handler.js +79 -0
  11. package/esm/core/src/eval-interpolate.js +63 -0
  12. package/esm/core/src/eval-transform.js +378 -0
  13. package/esm/core/src/execute.js +378 -0
  14. package/esm/core/src/expand.js +988 -0
  15. package/esm/core/src/frontmatter.js +98 -0
  16. package/esm/core/src/heal.js +29 -0
  17. package/esm/core/src/interpolate.js +39 -0
  18. package/esm/core/src/modifiers/daemon.js +60 -0
  19. package/esm/core/src/modifiers/persist.js +35 -0
  20. package/esm/core/src/modifiers/timeout.js +55 -0
  21. package/esm/core/src/modifiers.js +113 -0
  22. package/esm/core/src/output/normalize.js +33 -0
  23. package/esm/core/src/output/terminal.js +20 -0
  24. package/esm/core/src/render.js +30 -0
  25. package/esm/core/src/replay-stream.js +58 -0
  26. package/esm/core/src/sample-api.js +45 -0
  27. package/esm/core/src/scanner.js +632 -0
  28. package/esm/core/src/temp-file-compiler.js +68 -0
  29. package/esm/core/src/types.js +7 -0
  30. package/esm/core/src/validate.js +82 -0
  31. package/esm/durable-streams/combinators.js +227 -0
  32. package/esm/durable-streams/context.js +13 -0
  33. package/esm/durable-streams/divergence.js +56 -0
  34. package/esm/durable-streams/each.js +193 -0
  35. package/esm/durable-streams/effect.js +273 -0
  36. package/esm/durable-streams/ephemeral.js +80 -0
  37. package/esm/durable-streams/errors.js +92 -0
  38. package/esm/durable-streams/http-stream.js +207 -0
  39. package/esm/durable-streams/mod.js +34 -0
  40. package/esm/durable-streams/operations.js +88 -0
  41. package/esm/durable-streams/replay-guard.js +87 -0
  42. package/esm/durable-streams/replay-index.js +132 -0
  43. package/esm/durable-streams/run.js +137 -0
  44. package/esm/durable-streams/serialize.js +65 -0
  45. package/esm/durable-streams/stream.js +56 -0
  46. package/esm/durable-streams/types.js +10 -0
  47. package/esm/package.json +3 -0
  48. package/esm/runtime/apis.js +237 -0
  49. package/esm/runtime/find-free-port.js +33 -0
  50. package/esm/runtime/mod.js +20 -0
  51. package/esm/testing/npm/src/_dnt.polyfills.js +1 -0
  52. package/esm/testing/npm/src/_dnt.shims.js +57 -0
  53. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/almost_equals.js +47 -0
  54. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/array_includes.js +65 -0
  55. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/assert.js +23 -0
  56. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/assertion_error.js +30 -0
  57. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/equal.js +194 -0
  58. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/equals.js +61 -0
  59. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/exists.js +27 -0
  60. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/fail.js +20 -0
  61. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/false.js +23 -0
  62. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/greater.js +29 -0
  63. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/greater_or_equal.js +29 -0
  64. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/instance_of.js +52 -0
  65. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/is_error.js +53 -0
  66. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/less.js +28 -0
  67. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/less_or_equal.js +29 -0
  68. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/match.js +26 -0
  69. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/mod.js +45 -0
  70. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_equals.js +39 -0
  71. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_instance_of.js +29 -0
  72. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_match.js +26 -0
  73. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_strict_equals.js +32 -0
  74. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/object_match.js +177 -0
  75. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/rejects.js +53 -0
  76. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/strict_equals.js +58 -0
  77. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/string_includes.js +26 -0
  78. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/throws.js +44 -0
  79. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/unimplemented.js +20 -0
  80. package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/unreachable.js +20 -0
  81. package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/build_message.js +116 -0
  82. package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/diff.js +277 -0
  83. package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/diff_str.js +181 -0
  84. package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/format.js +70 -0
  85. package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/styles.js +207 -0
  86. package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/types.js +3 -0
  87. package/esm/testing/npm/src/mod.js +27 -0
  88. package/esm/testing/npm/src/src/assertions.js +250 -0
  89. package/esm/testing/npm/src/src/handlers.js +245 -0
  90. package/esm/testing/npm/src/src/journal.js +152 -0
  91. package/esm/testing/npm/src/src/test-api.js +33 -0
  92. package/esm/testing/npm/src/src/use-testing.js +79 -0
  93. package/esm/testing/npm/src/src/vocabulary.js +111 -0
  94. package/package.json +35 -0
  95. package/types/_dnt.polyfills.d.ts +6 -0
  96. package/types/_dnt.shims.d.ts +1 -0
  97. package/types/deps/jsr.io/@std/assert/1.0.19/almost_equals.d.ts +29 -0
  98. package/types/deps/jsr.io/@std/assert/1.0.19/array_includes.d.ts +28 -0
  99. package/types/deps/jsr.io/@std/assert/1.0.19/assert.d.ts +16 -0
  100. package/types/deps/jsr.io/@std/assert/1.0.19/assertion_error.d.ts +25 -0
  101. package/types/deps/jsr.io/@std/assert/1.0.19/equal.d.ts +24 -0
  102. package/types/deps/jsr.io/@std/assert/1.0.19/equals.d.ts +36 -0
  103. package/types/deps/jsr.io/@std/assert/1.0.19/exists.d.ts +17 -0
  104. package/types/deps/jsr.io/@std/assert/1.0.19/fail.d.ts +14 -0
  105. package/types/deps/jsr.io/@std/assert/1.0.19/false.d.ts +18 -0
  106. package/types/deps/jsr.io/@std/assert/1.0.19/greater.d.ts +19 -0
  107. package/types/deps/jsr.io/@std/assert/1.0.19/greater_or_equal.d.ts +19 -0
  108. package/types/deps/jsr.io/@std/assert/1.0.19/instance_of.d.ts +22 -0
  109. package/types/deps/jsr.io/@std/assert/1.0.19/is_error.d.ts +24 -0
  110. package/types/deps/jsr.io/@std/assert/1.0.19/less.d.ts +18 -0
  111. package/types/deps/jsr.io/@std/assert/1.0.19/less_or_equal.d.ts +19 -0
  112. package/types/deps/jsr.io/@std/assert/1.0.19/match.d.ts +17 -0
  113. package/types/deps/jsr.io/@std/assert/1.0.19/mod.d.ts +43 -0
  114. package/types/deps/jsr.io/@std/assert/1.0.19/not_equals.d.ts +26 -0
  115. package/types/deps/jsr.io/@std/assert/1.0.19/not_instance_of.d.ts +19 -0
  116. package/types/deps/jsr.io/@std/assert/1.0.19/not_match.d.ts +17 -0
  117. package/types/deps/jsr.io/@std/assert/1.0.19/not_strict_equals.d.ts +22 -0
  118. package/types/deps/jsr.io/@std/assert/1.0.19/object_match.d.ts +28 -0
  119. package/types/deps/jsr.io/@std/assert/1.0.19/rejects.d.ts +41 -0
  120. package/types/deps/jsr.io/@std/assert/1.0.19/strict_equals.d.ts +23 -0
  121. package/types/deps/jsr.io/@std/assert/1.0.19/string_includes.d.ts +17 -0
  122. package/types/deps/jsr.io/@std/assert/1.0.19/throws.d.ts +44 -0
  123. package/types/deps/jsr.io/@std/assert/1.0.19/unimplemented.d.ts +14 -0
  124. package/types/deps/jsr.io/@std/assert/1.0.19/unreachable.d.ts +14 -0
  125. package/types/deps/jsr.io/@std/internal/1.0.14/build_message.d.ts +79 -0
  126. package/types/deps/jsr.io/@std/internal/1.0.14/diff.d.ts +139 -0
  127. package/types/deps/jsr.io/@std/internal/1.0.14/diff_str.d.ts +98 -0
  128. package/types/deps/jsr.io/@std/internal/1.0.14/format.d.ts +28 -0
  129. package/types/deps/jsr.io/@std/internal/1.0.14/styles.d.ts +158 -0
  130. package/types/deps/jsr.io/@std/internal/1.0.14/types.d.ts +24 -0
  131. package/types/mod.d.ts +29 -0
  132. package/types/src/assertions.d.ts +55 -0
  133. package/types/src/handlers.d.ts +18 -0
  134. package/types/src/journal.d.ts +29 -0
  135. package/types/src/test-api.d.ts +54 -0
  136. package/types/src/use-testing.d.ts +25 -0
  137. package/types/src/vocabulary.d.ts +34 -0
@@ -0,0 +1,207 @@
1
+ // Copyright 2018-2026 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ // A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
4
+ // on npm.
5
+ // This code is vendored from `fmt/colors.ts`.
6
+ // deno-lint-ignore no-explicit-any
7
+ import * as dntShim from "../../../../../_dnt.shims.js";
8
+ const { Deno } = dntShim.dntGlobalThis;
9
+ const noColor = typeof Deno?.noColor === "boolean"
10
+ ? Deno.noColor
11
+ : false;
12
+ const enabled = !noColor;
13
+ function code(open, close) {
14
+ return {
15
+ open: `\x1b[${open.join(";")}m`,
16
+ close: `\x1b[${close}m`,
17
+ regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
18
+ };
19
+ }
20
+ function run(str, code) {
21
+ return enabled
22
+ ? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
23
+ : str;
24
+ }
25
+ /**
26
+ * Sets the style of text to be printed to bold.
27
+ *
28
+ * Disable by setting the `NO_COLOR` environmental variable.
29
+ *
30
+ * @param str Text to make bold
31
+ *
32
+ * @returns Bold text for printing
33
+ *
34
+ * @example Usage
35
+ * ```ts no-assert
36
+ * import { bold } from "@std/internal/styles";
37
+ *
38
+ * console.log(bold("Hello, world!")); // Prints "Hello, world!" in bold
39
+ * ```
40
+ */
41
+ export function bold(str) {
42
+ return run(str, code([1], 22));
43
+ }
44
+ /**
45
+ * Sets the color of text to be printed to red.
46
+ *
47
+ * Disable by setting the `NO_COLOR` environmental variable.
48
+ *
49
+ * @param str Text to make red
50
+ *
51
+ * @returns Red text for printing
52
+ *
53
+ * @example Usage
54
+ * ```ts no-assert
55
+ * import { red } from "@std/internal/styles";
56
+ *
57
+ * console.log(red("Hello, world!")); // Prints "Hello, world!" in red
58
+ * ```
59
+ */
60
+ export function red(str) {
61
+ return run(str, code([31], 39));
62
+ }
63
+ /**
64
+ * Sets the color of text to be printed to green.
65
+ *
66
+ * Disable by setting the `NO_COLOR` environmental variable.
67
+ *
68
+ * @param str Text to make green
69
+ *
70
+ * @returns Green text for print
71
+ *
72
+ * @example Usage
73
+ * ```ts no-assert
74
+ * import { green } from "@std/internal/styles";
75
+ *
76
+ * console.log(green("Hello, world!")); // Prints "Hello, world!" in green
77
+ * ```
78
+ */
79
+ export function green(str) {
80
+ return run(str, code([32], 39));
81
+ }
82
+ /**
83
+ * Sets the color of text to be printed to yellow.
84
+ *
85
+ * Disable by setting the `NO_COLOR` environmental variable.
86
+ *
87
+ * @param str Text to make yellow
88
+ *
89
+ * @returns Yellow text for print
90
+ *
91
+ * @example Usage
92
+ * ```ts no-assert
93
+ * import { yellow } from "@std/internal/styles";
94
+ *
95
+ * console.log(yellow("Hello, world!")); // Prints "Hello, world!" in yellow
96
+ * ```
97
+ */
98
+ export function yellow(str) {
99
+ return run(str, code([33], 39));
100
+ }
101
+ /**
102
+ * Sets the color of text to be printed to white.
103
+ *
104
+ * @param str Text to make white
105
+ *
106
+ * @returns White text for print
107
+ *
108
+ * @example Usage
109
+ * ```ts no-assert
110
+ * import { white } from "@std/internal/styles";
111
+ *
112
+ * console.log(white("Hello, world!")); // Prints "Hello, world!" in white
113
+ * ```
114
+ */
115
+ export function white(str) {
116
+ return run(str, code([37], 39));
117
+ }
118
+ /**
119
+ * Sets the color of text to be printed to gray.
120
+ *
121
+ * @param str Text to make gray
122
+ *
123
+ * @returns Gray text for print
124
+ *
125
+ * @example Usage
126
+ * ```ts no-assert
127
+ * import { gray } from "@std/internal/styles";
128
+ *
129
+ * console.log(gray("Hello, world!")); // Prints "Hello, world!" in gray
130
+ * ```
131
+ */
132
+ export function gray(str) {
133
+ return brightBlack(str);
134
+ }
135
+ /**
136
+ * Sets the color of text to be printed to bright-black.
137
+ *
138
+ * @param str Text to make bright-black
139
+ *
140
+ * @returns Bright-black text for print
141
+ *
142
+ * @example Usage
143
+ * ```ts no-assert
144
+ * import { brightBlack } from "@std/internal/styles";
145
+ *
146
+ * console.log(brightBlack("Hello, world!")); // Prints "Hello, world!" in bright-black
147
+ * ```
148
+ */
149
+ export function brightBlack(str) {
150
+ return run(str, code([90], 39));
151
+ }
152
+ /**
153
+ * Sets the background color of text to be printed to red.
154
+ *
155
+ * @param str Text to make its background red
156
+ *
157
+ * @returns Red background text for print
158
+ *
159
+ * @example Usage
160
+ * ```ts no-assert
161
+ * import { bgRed } from "@std/internal/styles";
162
+ *
163
+ * console.log(bgRed("Hello, world!")); // Prints "Hello, world!" with red background
164
+ * ```
165
+ */
166
+ export function bgRed(str) {
167
+ return run(str, code([41], 49));
168
+ }
169
+ /**
170
+ * Sets the background color of text to be printed to green.
171
+ *
172
+ * @param str Text to make its background green
173
+ *
174
+ * @returns Green background text for print
175
+ *
176
+ * @example Usage
177
+ * ```ts no-assert
178
+ * import { bgGreen } from "@std/internal/styles";
179
+ *
180
+ * console.log(bgGreen("Hello, world!")); // Prints "Hello, world!" with green background
181
+ * ```
182
+ */
183
+ export function bgGreen(str) {
184
+ return run(str, code([42], 49));
185
+ }
186
+ // https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
187
+ const ANSI_REGEXP = new RegExp([
188
+ "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
189
+ "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TXZcf-nq-uy=><~]))",
190
+ ].join("|"), "g");
191
+ /**
192
+ * Remove ANSI escape codes from the string.
193
+ *
194
+ * @param string Text to remove ANSI escape codes from
195
+ *
196
+ * @returns Text without ANSI escape codes
197
+ *
198
+ * @example Usage
199
+ * ```ts no-assert
200
+ * import { red, stripAnsiCode } from "@std/internal/styles";
201
+ *
202
+ * console.log(stripAnsiCode(red("Hello, world!"))); // Prints "Hello, world!"
203
+ * ```
204
+ */
205
+ export function stripAnsiCode(string) {
206
+ return string.replace(ANSI_REGEXP, "");
207
+ }
@@ -0,0 +1,3 @@
1
+ // Copyright 2018-2026 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @module
3
+ * Testing vocabulary for executable.md documents (specs/testing-spec.md).
4
+ *
5
+ * `<Testing>` activates testing mode for its expanded subtree, `<Test>`
6
+ * defines an atomic test, and the assertion components map to `@std/assert`.
7
+ *
8
+ * Composition with core execution:
9
+ *
10
+ * ```ts
11
+ * import { execute } from "@executablemd/core";
12
+ * import { useTesting } from "@executablemd/testing";
13
+ *
14
+ * const tests = yield* useTesting();
15
+ * const execution = yield* execute(options);
16
+ * const outcome = yield* execution; // Result<string>
17
+ * const results = yield* tests.results;
18
+ * ```
19
+ *
20
+ * Vocabulary registration (`installTestingVocabulary`) is distinct from
21
+ * testing-mode activation (`useTesting()` at the root, or a `<Testing>`
22
+ * element for a subtree).
23
+ */
24
+ import "./_dnt.polyfills.js";
25
+ export { Test, testing, record, results, TestFailureError } from "./src/test-api.js";
26
+ export { installTestingVocabulary } from "./src/vocabulary.js";
27
+ export { useTesting } from "./src/use-testing.js";
@@ -0,0 +1,250 @@
1
+ /**
2
+ * Assertion components (specs/testing-spec.md §Assertions).
3
+ *
4
+ * Each component maps to an `@std/assert` export with the same name and
5
+ * parameter names. Expression props evaluate LIVE against the merged binding
6
+ * environment — never through JSON serialization, which would destroy
7
+ * `RegExp`s, `undefined`, and object identity.
8
+ *
9
+ * The assertion runs on the raw values BEFORE any diagnostic formatting, so
10
+ * formatting arbitrary values (mutating or throwing getters/toJSON/toString)
11
+ * can never change the assertion outcome. Diagnostics are built afterwards
12
+ * under guarded fallback.
13
+ */
14
+ import { assert, assertEquals, assertExists, assertFalse, assertGreater, assertGreaterOrEqual, AssertionError, assertLess, assertLessOrEqual, assertMatch, assertNotEquals, assertNotMatch, assertNotStrictEquals, assertStrictEquals, assertStringIncludes, } from "../deps/jsr.io/@std/assert/1.0.19/mod.js";
15
+ import { DocumentOutput, env, renderSegments } from "@executablemd/core";
16
+ import { inTest, testing, verbose } from "./test-api.js";
17
+ function entry(name, kind, run) {
18
+ const allowsExpectedChildren = kind === "binary-eq" || kind === "string-includes";
19
+ return [name, { name, kind, run, allowsExpectedChildren }];
20
+ }
21
+ export const ASSERTIONS = new Map([
22
+ entry("Assert", "unary-truthy", (v) => assert(v.expr, v.msg)),
23
+ entry("AssertFalse", "unary-truthy", (v) => assertFalse(v.expr, v.msg)),
24
+ entry("AssertExists", "unary-exists", (v) => assertExists(v.actual, v.msg)),
25
+ entry("AssertEquals", "binary-eq", (v) => assertEquals(v.actual, v.expected, v.msg)),
26
+ entry("AssertNotEquals", "binary-eq", (v) => assertNotEquals(v.actual, v.expected, v.msg)),
27
+ entry("AssertStrictEquals", "binary-eq", (v) => assertStrictEquals(v.actual, v.expected, v.msg)),
28
+ entry("AssertNotStrictEquals", "binary-eq", (v) => assertNotStrictEquals(v.actual, v.expected, v.msg)),
29
+ entry("AssertStringIncludes", "string-includes", (v) => assertStringIncludes(coerceString(v.actual), coerceString(v.expected), v.msg)),
30
+ entry("AssertMatch", "match", (v) => assertMatch(coerceString(v.actual), requireRegExp(v.expected), v.msg)),
31
+ entry("AssertNotMatch", "match", (v) => assertNotMatch(coerceString(v.actual), requireRegExp(v.expected), v.msg)),
32
+ entry("AssertGreater", "numeric", (v) => assertGreater(v.actual, v.expected, v.msg)),
33
+ entry("AssertGreaterOrEqual", "numeric", (v) => assertGreaterOrEqual(v.actual, v.expected, v.msg)),
34
+ entry("AssertLess", "numeric", (v) => assertLess(v.actual, v.expected, v.msg)),
35
+ entry("AssertLessOrEqual", "numeric", (v) => assertLessOrEqual(v.actual, v.expected, v.msg)),
36
+ ]);
37
+ function coerceString(value) {
38
+ if (typeof value === "string") {
39
+ return value;
40
+ }
41
+ throw new AssertionError(`expected a string "actual"/"expected" value, got ${typeof value}`);
42
+ }
43
+ function requireRegExp(value) {
44
+ if (value instanceof RegExp) {
45
+ return value;
46
+ }
47
+ throw new AssertionError("match assertions require a RegExp through the expected prop — use expected={/pattern/}");
48
+ }
49
+ // ---------------------------------------------------------------------------
50
+ // Live expression evaluation
51
+ // ---------------------------------------------------------------------------
52
+ const IDENTIFIER_RE = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
53
+ function evaluateExpression(expression, values) {
54
+ const names = Object.keys(values).filter((name) => IDENTIFIER_RE.test(name));
55
+ const fn = new Function(...names, `return (${expression});`);
56
+ return fn(...names.map((name) => values[name]));
57
+ }
58
+ // ---------------------------------------------------------------------------
59
+ // Guarded value formatting — runs only AFTER the assertion outcome is fixed
60
+ // ---------------------------------------------------------------------------
61
+ function safeFormat(value) {
62
+ try {
63
+ if (typeof value === "string") {
64
+ return JSON.stringify(value);
65
+ }
66
+ if (value instanceof RegExp) {
67
+ return String(value);
68
+ }
69
+ const json = JSON.stringify(value);
70
+ if (json !== undefined) {
71
+ return json;
72
+ }
73
+ return String(value);
74
+ }
75
+ catch {
76
+ try {
77
+ return String(value);
78
+ }
79
+ catch {
80
+ return "<unformattable value>";
81
+ }
82
+ }
83
+ }
84
+ // ---------------------------------------------------------------------------
85
+ // Diagnostic-carrying assertion error
86
+ // ---------------------------------------------------------------------------
87
+ /**
88
+ * An assertion failure enriched with its Markdown diagnostic. Still an
89
+ * `AssertionError`, so containment and classification treat it as the
90
+ * original @std/assert failure.
91
+ */
92
+ export class AssertionDiagnostic extends AssertionError {
93
+ name = "AssertionDiagnostic";
94
+ diagnostic;
95
+ detail;
96
+ constructor(cause, diagnostic, detail) {
97
+ super(cause.message);
98
+ this.diagnostic = diagnostic;
99
+ this.detail = detail;
100
+ this.cause = cause;
101
+ }
102
+ }
103
+ // ---------------------------------------------------------------------------
104
+ // expandAssertion
105
+ // ---------------------------------------------------------------------------
106
+ const KIND_PROPS = {
107
+ "unary-truthy": { allowed: ["expr", "msg"], required: ["expr"] },
108
+ "unary-exists": { allowed: ["actual", "msg"], required: ["actual"] },
109
+ "binary-eq": { allowed: ["actual", "expected", "msg"], required: ["actual"] },
110
+ "string-includes": { allowed: ["actual", "expected", "msg"], required: ["actual"] },
111
+ match: { allowed: ["actual", "expected", "msg"], required: ["actual", "expected"] },
112
+ numeric: { allowed: ["actual", "expected", "msg"], required: ["actual", "expected"] },
113
+ };
114
+ function validationError(name, message) {
115
+ return { type: "error", message: `<${name}> ${message}`, source: name };
116
+ }
117
+ /**
118
+ * Expand one assertion component: validate props, resolve raw values, run the
119
+ * @std/assert function, then build the diagnostic. Returns diagnostic text on
120
+ * a visible pass; throws `AssertionDiagnostic` on failure.
121
+ */
122
+ export function* expandAssertion(assertion, invocation, ctx) {
123
+ const rules = KIND_PROPS[assertion.kind];
124
+ const supplied = [...Object.keys(invocation.props), ...Object.keys(invocation.expressions)];
125
+ for (const name of supplied) {
126
+ if (!rules.allowed.includes(name)) {
127
+ return [
128
+ validationError(assertion.name, `does not accept a "${name}" prop (allowed: ${rules.allowed.join(", ")}).`),
129
+ ];
130
+ }
131
+ }
132
+ const hasChildren = !invocation.selfClosing && invocation.children.length > 0;
133
+ if (hasChildren && !assertion.allowsExpectedChildren) {
134
+ return [validationError(assertion.name, "does not accept expected children.")];
135
+ }
136
+ if (hasChildren && supplied.includes("expected")) {
137
+ return [
138
+ validationError(assertion.name, 'accepts either an "expected" prop or expected children, not both.'),
139
+ ];
140
+ }
141
+ for (const name of rules.required) {
142
+ const suppliedByChildren = name === "expected" && hasChildren;
143
+ if (!supplied.includes(name) && !suppliedByChildren) {
144
+ return [validationError(assertion.name, `requires the "${name}" prop.`)];
145
+ }
146
+ }
147
+ if (assertion.kind === "binary-eq" || assertion.kind === "string-includes") {
148
+ if (!supplied.includes("expected") && !hasChildren) {
149
+ return [validationError(assertion.name, 'requires an "expected" prop or expected children.')];
150
+ }
151
+ }
152
+ // Resolve raw values: literal props as-is, expression props evaluated live
153
+ // against caller-projected bindings merged under the current environment
154
+ // (the same precedence core uses for projected children).
155
+ const currentEnv = yield* env;
156
+ const merged = {
157
+ ...(ctx.projectedEnv?.values ?? {}),
158
+ ...(currentEnv?.values ?? {}),
159
+ };
160
+ const resolved = {};
161
+ const resolutionOrder = [
162
+ "expr",
163
+ "actual",
164
+ "expected",
165
+ "msg",
166
+ ];
167
+ for (const name of resolutionOrder) {
168
+ if (name in invocation.expressions) {
169
+ try {
170
+ resolved[name] = evaluateExpression(invocation.expressions[name], merged);
171
+ }
172
+ catch (error) {
173
+ return [
174
+ validationError(assertion.name, `failed to evaluate the "${name}" expression: ${error instanceof Error ? error.message : String(error)}`),
175
+ ];
176
+ }
177
+ }
178
+ else if (name in invocation.props) {
179
+ resolved[name] = invocation.props[name];
180
+ }
181
+ }
182
+ // @std/assert takes msg as a string; a non-string is rejected by TYPE
183
+ // CHECK alone — formatting it here would run hostile toJSON/toString
184
+ // side effects before the assertion outcome is established.
185
+ if ("msg" in resolved && typeof resolved.msg !== "string") {
186
+ return [validationError(assertion.name, 'requires "msg" to be a string when supplied.')];
187
+ }
188
+ const values = {
189
+ expr: resolved.expr,
190
+ actual: resolved.actual,
191
+ expected: resolved.expected,
192
+ msg: typeof resolved.msg === "string" ? resolved.msg : undefined,
193
+ };
194
+ if (hasChildren) {
195
+ const expanded = yield* ctx.expand(invocation.children);
196
+ values.expected = renderSegments(expanded).replace(/\s+$/, "");
197
+ }
198
+ // Run the assertion on the raw values — the outcome is fixed before any
199
+ // diagnostic formatting can observe (or mutate) them.
200
+ let failure;
201
+ try {
202
+ assertion.run(values);
203
+ }
204
+ catch (error) {
205
+ failure = error instanceof Error ? error : new Error(String(error));
206
+ }
207
+ const detail = {};
208
+ if (assertion.kind === "unary-truthy") {
209
+ detail.actual = safeFormat(values.expr);
210
+ }
211
+ else {
212
+ detail.actual = safeFormat(values.actual);
213
+ if (assertion.kind !== "unary-exists") {
214
+ detail.expected = safeFormat(values.expected);
215
+ }
216
+ }
217
+ if (failure) {
218
+ const diagnostic = buildDiagnostic(assertion.name, "failed", values.msg, detail, failure);
219
+ const visible = (yield* testing) || (yield* verbose);
220
+ const inTestScope = yield* inTest;
221
+ if (visible && !inTestScope) {
222
+ // Outside a test the throw below aborts expansion before the segment
223
+ // could render — emit the diagnostic directly so it reaches the output.
224
+ yield* DocumentOutput.operations.output(diagnostic);
225
+ }
226
+ throw new AssertionDiagnostic(failure, diagnostic, detail);
227
+ }
228
+ const visible = (yield* testing) || (yield* verbose);
229
+ if (!visible) {
230
+ return [];
231
+ }
232
+ return [{ type: "text", content: buildDiagnostic(assertion.name, "passed", values.msg, detail) }];
233
+ }
234
+ function buildDiagnostic(name, outcome, msg, detail, failure) {
235
+ const icon = outcome === "passed" ? "✅" : "❌";
236
+ const lines = [`> ${icon} **${name}** ${outcome}${msg ? ` — ${msg}` : ""}`];
237
+ if (detail.actual !== undefined) {
238
+ lines.push(`> actual: ${detail.actual}`);
239
+ }
240
+ if (detail.expected !== undefined) {
241
+ lines.push(`> expected: ${detail.expected}`);
242
+ }
243
+ if (failure) {
244
+ const message = failure.message.split("\n")[0];
245
+ if (message) {
246
+ lines.push(`> ${message}`);
247
+ }
248
+ }
249
+ return `\n${lines.join("\n")}\n`;
250
+ }