dd-trace 6.24.0 → 6.26.0

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 (352) hide show
  1. package/README.md +3 -0
  2. package/ci/cypress/support.js +2 -1
  3. package/ci/init.js +2 -22
  4. package/ci/vitest-no-worker-init-node-setup.mjs +2 -1
  5. package/ci/vitest-no-worker-init-runner-setup.mjs +2 -1
  6. package/ci/vitest-no-worker-init-setup.mjs +729 -724
  7. package/ci/vitest-no-worker-init-v5-setup.mjs +2 -1
  8. package/ext/exporters.js +0 -1
  9. package/index.d.ts +105 -5
  10. package/initialize.mjs +5 -2
  11. package/package.json +41 -36
  12. package/packages/datadog-esbuild/index.js +16 -70
  13. package/packages/datadog-esbuild/src/log.js +2 -29
  14. package/packages/datadog-instrumentations/src/ai.js +6 -1
  15. package/packages/datadog-instrumentations/src/aws-lambda.js +88 -0
  16. package/packages/datadog-instrumentations/src/cucumber.js +5 -2
  17. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  18. package/packages/datadog-instrumentations/src/elevenlabs/activity.js +114 -0
  19. package/packages/datadog-instrumentations/src/elevenlabs/audio.js +104 -0
  20. package/packages/datadog-instrumentations/src/elevenlabs/index.js +288 -0
  21. package/packages/datadog-instrumentations/src/elevenlabs/session.js +444 -0
  22. package/packages/datadog-instrumentations/src/graphql.js +20 -17
  23. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +582 -0
  24. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  25. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +576 -0
  26. package/packages/datadog-instrumentations/src/helpers/bundler-utils.js +104 -0
  27. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  28. package/packages/datadog-instrumentations/src/helpers/hook.js +29 -1
  29. package/packages/datadog-instrumentations/src/helpers/hooks.js +3 -7
  30. package/packages/datadog-instrumentations/src/helpers/lambda-handler.js +189 -0
  31. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +8 -0
  32. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +26 -1
  33. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/async-mutex.js +41 -0
  34. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/elevenlabs.js +25 -0
  35. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql.js +0 -36
  36. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  37. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  38. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +61 -0
  39. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/openai-agents-realtime.js +23 -0
  40. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  41. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/trpc.js +148 -0
  42. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +80 -0
  43. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +23 -3
  44. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  45. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +59 -2
  46. package/packages/datadog-instrumentations/src/ioredis.js +101 -3
  47. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  48. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  49. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +7 -1
  50. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +41 -0
  51. package/packages/datadog-instrumentations/src/next.js +203 -26
  52. package/packages/datadog-instrumentations/src/openai-agents-realtime.js +149 -0
  53. package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +27 -0
  54. package/packages/datadog-instrumentations/src/openai-realtime/index.js +17 -6
  55. package/packages/datadog-instrumentations/src/openai-realtime/server-playback.js +887 -0
  56. package/packages/datadog-instrumentations/src/openai-realtime/session.js +150 -25
  57. package/packages/datadog-instrumentations/src/openai-realtime/turns.js +29 -2
  58. package/packages/datadog-instrumentations/src/otel-sdk-trace.js +11 -9
  59. package/packages/datadog-instrumentations/src/router.js +35 -12
  60. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  61. package/packages/datadog-instrumentations/src/webdriverio.js +12 -12
  62. package/packages/datadog-plugin-async-mutex/src/index.js +50 -0
  63. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  64. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  65. package/packages/datadog-plugin-aws-lambda/src/index.js +106 -0
  66. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  67. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  68. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  69. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  70. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  71. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  72. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +50 -13
  73. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  74. package/packages/datadog-plugin-elevenlabs/src/index.js +69 -0
  75. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  76. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  77. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +20 -7
  78. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +21 -52
  79. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +11 -2
  80. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +25 -4
  81. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  82. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  83. package/packages/datadog-plugin-http/src/client.js +34 -10
  84. package/packages/datadog-plugin-http2/src/client.js +40 -14
  85. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  86. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  87. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  88. package/packages/datadog-plugin-next/src/index.js +112 -14
  89. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  90. package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +5 -3
  91. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  92. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  93. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  94. package/packages/datadog-plugin-trpc/src/index.js +15 -0
  95. package/packages/datadog-plugin-trpc/src/procedure.js +48 -0
  96. package/packages/datadog-plugin-trpc/src/request.js +81 -0
  97. package/packages/datadog-plugin-undici/src/index.js +22 -7
  98. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  99. package/packages/datadog-plugin-ws/src/close.js +3 -5
  100. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  101. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  102. package/packages/datadog-plugin-ws/src/server.js +44 -13
  103. package/packages/datadog-turbopack/index.js +4 -28
  104. package/packages/datadog-turbopack/src/loader.js +14 -452
  105. package/packages/datadog-vite/index.js +537 -0
  106. package/packages/datadog-vite/src/log.js +5 -0
  107. package/packages/datadog-webpack/index.js +3 -55
  108. package/packages/datadog-webpack/src/log.js +2 -29
  109. package/packages/dd-trace/src/aiguard/evaluation.js +62 -1
  110. package/packages/dd-trace/src/aiguard/index.js +1 -1
  111. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  112. package/packages/dd-trace/src/aiguard/sdk.js +8 -2
  113. package/packages/dd-trace/src/aiguard/tags.js +2 -0
  114. package/packages/dd-trace/src/appsec/blocking/index.js +21 -1
  115. package/packages/dd-trace/src/appsec/channels.js +7 -0
  116. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  117. package/packages/dd-trace/src/appsec/graphql.js +89 -7
  118. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  119. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  120. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  121. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  122. package/packages/dd-trace/src/appsec/index.js +6 -0
  123. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  124. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  125. package/packages/dd-trace/src/appsec/rasp/index.js +8 -1
  126. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  127. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  128. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  129. package/packages/dd-trace/src/appsec/rasp/utils.js +33 -3
  130. package/packages/dd-trace/src/appsec/recommended.json +1358 -94
  131. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  132. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  133. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  134. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  135. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  136. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  137. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  138. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  139. package/packages/dd-trace/src/before-exit.js +32 -0
  140. package/packages/dd-trace/src/bootstrap.js +5 -3
  141. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +15 -2
  142. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +12 -34
  143. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +41 -13
  144. package/packages/dd-trace/src/ci-visibility/exporters/request.js +2 -3
  145. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +1 -2
  146. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +1 -1
  147. package/packages/dd-trace/src/ci-visibility/telemetry.js +6 -0
  148. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +1 -1
  149. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache-schema.js +9 -134
  150. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache.js +11 -156
  151. package/packages/dd-trace/src/config/config-types.d.ts +3 -0
  152. package/packages/dd-trace/src/config/defaults.js +5 -0
  153. package/packages/dd-trace/src/config/generated-config-types.d.ts +46 -4
  154. package/packages/dd-trace/src/config/index.js +63 -31
  155. package/packages/dd-trace/src/config/parsers.js +44 -0
  156. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  157. package/packages/dd-trace/src/config/remote_config.js +2 -1
  158. package/packages/dd-trace/src/config/supported-configurations.json +172 -10
  159. package/packages/dd-trace/src/constants.js +1 -0
  160. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  161. package/packages/dd-trace/src/datastreams/pathway.js +113 -28
  162. package/packages/dd-trace/src/datastreams/processor.js +5 -4
  163. package/packages/dd-trace/src/debugger/config.js +1 -1
  164. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  165. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  166. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  167. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  168. package/packages/dd-trace/src/debugger/index.js +2 -2
  169. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  170. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +6 -2
  171. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  172. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  173. package/packages/dd-trace/src/exporter.js +0 -11
  174. package/packages/dd-trace/src/exporters/common/request.js +81 -48
  175. package/packages/dd-trace/src/exporters/common/retry.js +12 -7
  176. package/packages/dd-trace/src/exporters/common/url.js +1 -1
  177. package/packages/dd-trace/src/git_metadata.js +2 -1
  178. package/packages/dd-trace/src/id.js +1 -1
  179. package/packages/dd-trace/src/index.js +6 -3
  180. package/packages/dd-trace/src/lambda/facade.js +75 -0
  181. package/packages/dd-trace/src/lambda/handler.js +3 -41
  182. package/packages/dd-trace/src/lambda/index.js +38 -15
  183. package/packages/dd-trace/src/lambda/runtime/patch.js +19 -3
  184. package/packages/dd-trace/src/lambda/wrap.js +44 -0
  185. package/packages/dd-trace/src/llmobs/constants/tags.js +2 -0
  186. package/packages/dd-trace/src/llmobs/experiments/experiment.js +10 -5
  187. package/packages/dd-trace/src/llmobs/plugins/elevenlabs.js +96 -0
  188. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +19 -6
  189. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +54 -3
  190. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +81 -1
  191. package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +38 -11
  192. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +10 -2
  193. package/packages/dd-trace/src/llmobs/sdk.js +19 -0
  194. package/packages/dd-trace/src/llmobs/span_processor.js +9 -0
  195. package/packages/dd-trace/src/llmobs/tagger.js +22 -0
  196. package/packages/dd-trace/src/log/channels.js +4 -0
  197. package/packages/dd-trace/src/log/writer.js +6 -1
  198. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +7 -0
  199. package/packages/dd-trace/src/openfeature/configuration_source.js +55 -27
  200. package/packages/dd-trace/src/openfeature/flag-selection.js +152 -0
  201. package/packages/dd-trace/src/openfeature/flagging_provider.js +19 -1
  202. package/packages/dd-trace/src/openfeature/writers/base.js +1 -3
  203. package/packages/dd-trace/src/opentelemetry/logs/index.js +11 -0
  204. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +1 -1
  205. package/packages/dd-trace/src/opentelemetry/metrics/index.js +7 -1
  206. package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +1 -1
  207. package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_exporter.js +4 -1
  208. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +19 -1
  209. package/packages/dd-trace/src/opentelemetry/span-ending-hook.js +5 -1
  210. package/packages/dd-trace/src/opentelemetry/span-helpers.js +6 -1
  211. package/packages/dd-trace/src/opentelemetry/span.js +2 -2
  212. package/packages/dd-trace/src/opentelemetry/startup-config.js +174 -0
  213. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +1 -1
  214. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +7 -1
  215. package/packages/dd-trace/src/opentracing/propagation/tracestate.js +77 -23
  216. package/packages/dd-trace/src/opentracing/tracer.js +24 -0
  217. package/packages/dd-trace/src/otel-thread-ctx.js +28 -31
  218. package/packages/dd-trace/src/plugin_manager.js +34 -7
  219. package/packages/dd-trace/src/plugins/ci_plugin.js +143 -17
  220. package/packages/dd-trace/src/plugins/index.js +8 -0
  221. package/packages/dd-trace/src/plugins/plugin.js +4 -2
  222. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  223. package/packages/dd-trace/src/plugins/util/ci.js +52 -1
  224. package/packages/dd-trace/src/plugins/util/git-cache.js +4 -4
  225. package/packages/dd-trace/src/plugins/util/git.js +263 -82
  226. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +80 -56
  227. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +12 -4
  228. package/packages/dd-trace/src/plugins/util/status-validator.js +23 -10
  229. package/packages/dd-trace/src/plugins/util/tags.js +6 -0
  230. package/packages/dd-trace/src/plugins/util/test.js +92 -40
  231. package/packages/dd-trace/src/plugins/util/url.js +18 -7
  232. package/packages/dd-trace/src/plugins/util/user-provided-git.js +6 -6
  233. package/packages/dd-trace/src/plugins/util/web.js +65 -10
  234. package/packages/dd-trace/src/process-tags/index.js +1 -1
  235. package/packages/dd-trace/src/profiler.js +74 -12
  236. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  237. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  238. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  239. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  240. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  241. package/packages/dd-trace/src/profiling/webspan-utils.js +6 -5
  242. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  243. package/packages/dd-trace/src/proxy.js +46 -54
  244. package/packages/dd-trace/src/span_processor.js +20 -0
  245. package/packages/dd-trace/src/startup-log.js +8 -6
  246. package/packages/dd-trace/src/telemetry/metrics.js +2 -1
  247. package/packages/dd-trace/src/tracer.js +0 -2
  248. package/packages/dd-trace/src/web-tags-cache.js +8 -9
  249. package/register.js +2 -1
  250. package/vendor/dist/@apm-js-collab/code-transformer/LICENSE +489 -0
  251. package/vendor/dist/@datadog/flagging-core/LICENSE +221 -0
  252. package/vendor/dist/@datadog/flagging-core/index.js +1 -0
  253. package/vendor/dist/@datadog/openfeature-node-server/LICENSE +21 -1
  254. package/vendor/dist/@datadog/openfeature-node-server/rules-based/LICENSE +3 -1
  255. package/vendor/dist/@datadog/sketches-js/LICENSE +263 -0
  256. package/vendor/dist/@datadog/source-map/LICENSE +1 -0
  257. package/vendor/dist/@isaacs/ttlcache/LICENSE +13 -0
  258. package/vendor/dist/@opentelemetry/core/LICENSE +220 -1
  259. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  260. package/vendor/dist/crypto-randomuuid/LICENSE +3 -0
  261. package/vendor/dist/escape-string-regexp/LICENSE +13 -0
  262. package/vendor/dist/esquery/LICENSE +2 -0
  263. package/vendor/dist/https-proxy-agent/LICENSE +80 -1
  264. package/vendor/dist/istanbul-lib-coverage/LICENSE +2 -0
  265. package/vendor/dist/jest-docblock/LICENSE +16 -0
  266. package/vendor/dist/jsonpath-plus/LICENSE +13 -0
  267. package/vendor/dist/limiter/LICENSE +2 -0
  268. package/vendor/dist/lodash.sortby/LICENSE +2 -0
  269. package/vendor/dist/lru-cache/LICENSE +13 -0
  270. package/vendor/dist/meriyah/LICENSE +13 -0
  271. package/vendor/dist/module-details-from-path/LICENSE +2 -0
  272. package/vendor/dist/mutexify/promise/LICENSE +28 -0
  273. package/vendor/dist/pprof-format/LICENSE +13 -0
  274. package/vendor/dist/pprof-format/index.js +1 -1
  275. package/vendor/dist/protobufjs/LICENSE +219 -0
  276. package/vendor/dist/protobufjs/minimal/LICENSE +219 -0
  277. package/vendor/dist/proxy-from-env/LICENSE +13 -0
  278. package/vendor/dist/retry/LICENSE +2 -0
  279. package/vendor/dist/rfdc/LICENSE +2 -0
  280. package/vendor/dist/semifies/LICENSE +3 -1
  281. package/vendor/dist/shell-quote/LICENSE +3 -1
  282. package/vendor/dist/source-map/LICENSE +1 -0
  283. package/vendor/dist/source-map/lib/util/LICENSE +1 -0
  284. package/vendor/dist/tlhunter-sorted-set/LICENSE +2 -0
  285. package/vendor/dist/ttl-set/LICENSE +28 -0
  286. package/vite.d.ts +7 -0
  287. package/vite.js +3 -0
  288. package/ci/diagnose.js +0 -2215
  289. package/ci/runbook.md +0 -299
  290. package/ci/test-optimization-validation/approval-artifacts.js +0 -175
  291. package/ci/test-optimization-validation/approval.js +0 -386
  292. package/ci/test-optimization-validation/artifact-id.js +0 -18
  293. package/ci/test-optimization-validation/blocker-category.js +0 -24
  294. package/ci/test-optimization-validation/bounded-json.js +0 -120
  295. package/ci/test-optimization-validation/ci-command-candidate.js +0 -77
  296. package/ci/test-optimization-validation/ci-discovery.js +0 -192
  297. package/ci/test-optimization-validation/ci-package-scripts.js +0 -169
  298. package/ci/test-optimization-validation/ci-remediation.js +0 -283
  299. package/ci/test-optimization-validation/cli.js +0 -954
  300. package/ci/test-optimization-validation/command-blocker.js +0 -377
  301. package/ci/test-optimization-validation/command-output-policy.js +0 -188
  302. package/ci/test-optimization-validation/command-runner.js +0 -649
  303. package/ci/test-optimization-validation/environment.js +0 -88
  304. package/ci/test-optimization-validation/executable-approval.js +0 -47
  305. package/ci/test-optimization-validation/executable.js +0 -244
  306. package/ci/test-optimization-validation/execution-lock.js +0 -70
  307. package/ci/test-optimization-validation/framework-adapters/cucumber-validation.json +0 -3
  308. package/ci/test-optimization-validation/framework-adapters/cucumber.js +0 -767
  309. package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -99
  310. package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -175
  311. package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -513
  312. package/ci/test-optimization-validation/generated-file-policy.js +0 -63
  313. package/ci/test-optimization-validation/generated-files.js +0 -425
  314. package/ci/test-optimization-validation/generated-test-contract.js +0 -284
  315. package/ci/test-optimization-validation/generated-verifier.js +0 -235
  316. package/ci/test-optimization-validation/literal-environment.js +0 -56
  317. package/ci/test-optimization-validation/literal-glob.js +0 -52
  318. package/ci/test-optimization-validation/manifest-loader.js +0 -116
  319. package/ci/test-optimization-validation/manifest-scaffold.js +0 -1276
  320. package/ci/test-optimization-validation/manifest-schema.js +0 -695
  321. package/ci/test-optimization-validation/offline-fixtures.js +0 -335
  322. package/ci/test-optimization-validation/offline-output.js +0 -406
  323. package/ci/test-optimization-validation/package-check.js +0 -97
  324. package/ci/test-optimization-validation/payload-normalizer.js +0 -72
  325. package/ci/test-optimization-validation/plan-writer.js +0 -541
  326. package/ci/test-optimization-validation/preflight-runner.js +0 -186
  327. package/ci/test-optimization-validation/project-build-artifact.js +0 -31
  328. package/ci/test-optimization-validation/redaction.js +0 -319
  329. package/ci/test-optimization-validation/report-writer.js +0 -666
  330. package/ci/test-optimization-validation/result-semantics.js +0 -103
  331. package/ci/test-optimization-validation/runner-command.js +0 -264
  332. package/ci/test-optimization-validation/runner-contract.js +0 -695
  333. package/ci/test-optimization-validation/safe-files.js +0 -202
  334. package/ci/test-optimization-validation/scenarios/auto-test-retries.js +0 -171
  335. package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -407
  336. package/ci/test-optimization-validation/scenarios/ci-wiring.js +0 -666
  337. package/ci/test-optimization-validation/scenarios/early-flake-detection.js +0 -142
  338. package/ci/test-optimization-validation/scenarios/helpers.js +0 -621
  339. package/ci/test-optimization-validation/scenarios/test-management.js +0 -219
  340. package/ci/test-optimization-validation/source-text.js +0 -84
  341. package/ci/test-optimization-validation/static-diagnosis.js +0 -166
  342. package/ci/test-optimization-validation/test-output.js +0 -120
  343. package/ci/test-optimization-validation/validation-blocker.js +0 -21
  344. package/ci/validate-test-optimization.js +0 -3
  345. package/packages/datadog-esbuild/src/resolver.js +0 -284
  346. package/packages/datadog-esbuild/src/utils.js +0 -318
  347. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
  348. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -156
  349. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -287
  350. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/payload-projection.js +0 -84
  351. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -368
  352. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/writer.js +0 -59
@@ -1,425 +0,0 @@
1
- 'use strict'
2
-
3
- const fs = require('node:fs')
4
- const path = require('node:path')
5
-
6
- const {
7
- MAX_GENERATED_FILES,
8
- getGeneratedFileContentError,
9
- } = require('./generated-file-policy')
10
- const { createFileSafely, ensureSafeDirectory } = require('./safe-files')
11
-
12
- const RUNTIME_FILE_NAMESPACE = 'dd-test-optimization-validation'
13
- const createdGeneratedDirectories = new Map()
14
- const initializedCleanupStrategies = new WeakSet()
15
- const authorizedRuntimeCleanupFiles = new Map()
16
- const writtenGeneratedFiles = new Map()
17
-
18
- function writeGeneratedFiles (framework, scenario) {
19
- const strategy = framework.generatedTestStrategy
20
- if (!strategy || !['planned', 'verified'].includes(strategy.status)) {
21
- return []
22
- }
23
- if ((strategy.files || []).length > MAX_GENERATED_FILES) {
24
- throw new Error(`Generated validation strategy must contain at most ${MAX_GENERATED_FILES} files.`)
25
- }
26
- initializeRuntimeCleanupFiles(framework, strategy)
27
-
28
- const written = []
29
- const files = getScenarioFiles(strategy, scenario)
30
- try {
31
- for (const file of files) {
32
- const filename = validateGeneratedFilePath(framework, file.path)
33
- validateContentLines(file.contentLines, filename)
34
- const content = `${file.contentLines.join('\n')}\n`
35
- if (fs.existsSync(filename)) {
36
- if (fs.readFileSync(filename, 'utf8') === content) continue
37
- throw new Error(`Refusing to overwrite existing generated validation file with different content: ${filename}`)
38
- }
39
-
40
- const directory = path.dirname(filename)
41
- const missingDirectories = getMissingDirectories(framework.project.root, directory)
42
- ensureSafeDirectory(framework.project.root, directory, 'generated validation file directory', {
43
- allowRootSymlink: true,
44
- })
45
- for (const createdDirectory of missingDirectories) {
46
- createdGeneratedDirectories.set(
47
- createdDirectory,
48
- authorizePathForCleanup(framework.project.root, createdDirectory)
49
- )
50
- }
51
- validateGeneratedFilePath(framework, filename)
52
- createFileSafely(framework.project.root, filename, content, 'generated validation file')
53
- writtenGeneratedFiles.set(filename, authorizePathForCleanup(framework.project.root, filename))
54
- written.push(filename)
55
- }
56
- pinRuntimeCleanupParents(strategy)
57
- } catch (err) {
58
- cleanupPaths(written)
59
- cleanupCreatedDirectories(framework.project.root)
60
- forgetWrittenGeneratedFiles(written)
61
- throw err
62
- }
63
- return written
64
- }
65
-
66
- /**
67
- * Returns one scenario file plus adapter support files, or every file when no scenario is selected.
68
- *
69
- * @param {object} strategy generated strategy
70
- * @param {object|undefined} scenario selected generated scenario
71
- * @returns {object[]} generated files to write
72
- */
73
- function getScenarioFiles (strategy, scenario) {
74
- if (!scenario) return strategy.files || []
75
- const scenarioPaths = new Set((strategy.scenarios || []).map(candidate => {
76
- return path.resolve(candidate.testIdentities[0].file)
77
- }))
78
- const selectedPath = path.resolve(scenario.testIdentities[0].file)
79
- return (strategy.files || []).filter(file => {
80
- const filename = path.resolve(file.path)
81
- return filename === selectedPath || !scenarioPaths.has(filename)
82
- })
83
- }
84
-
85
- function cleanupGeneratedFiles (manifest, { keep = false } = {}) {
86
- if (keep) return { status: 'retained_by_request' }
87
-
88
- const outcome = {
89
- directoriesRemoved: 0,
90
- directoriesRetained: 0,
91
- filesRemoved: 0,
92
- filesRetained: 0,
93
- }
94
- if (manifest.frameworks) {
95
- for (const framework of manifest.frameworks) {
96
- const strategy = framework.generatedTestStrategy
97
- addCleanupOutcome(
98
- outcome,
99
- cleanupPaths(getSafeCleanupPaths(framework, strategy, { includeGeneratedFiles: true })),
100
- 'files'
101
- )
102
- addCleanupOutcome(outcome, cleanupCreatedDirectories(framework.project.root), 'directories')
103
- }
104
- }
105
- outcome.status = outcome.filesRetained > 0 || outcome.directoriesRetained > 0
106
- ? 'incomplete'
107
- : 'completed'
108
- return outcome
109
- }
110
-
111
- /**
112
- * Finds missing directories that the validator will create for one generated file.
113
- *
114
- * @param {string} root project root
115
- * @param {string} directory generated file directory
116
- * @returns {string[]} missing directories, from deepest to shallowest
117
- */
118
- function getMissingDirectories (root, directory) {
119
- const missing = []
120
- let current = path.resolve(directory)
121
- const resolvedRoot = path.resolve(root)
122
- while (current !== resolvedRoot && isPathInside(resolvedRoot, current) && !fs.existsSync(current)) {
123
- missing.push(current)
124
- current = path.dirname(current)
125
- }
126
- return missing
127
- }
128
-
129
- /**
130
- * Removes empty generated directories created by this validator process.
131
- *
132
- * @param {string} root project root
133
- */
134
- function cleanupCreatedDirectories (root) {
135
- const outcome = { removed: 0, retained: 0 }
136
- const resolvedRoot = path.resolve(root)
137
- const directories = [...createdGeneratedDirectories]
138
- .filter(([directory]) => isPathInside(resolvedRoot, directory))
139
- .sort(([left], [right]) => right.length - left.length)
140
-
141
- for (const [directory, authorization] of directories) {
142
- if (!isCleanupAuthorizationValid(directory, authorization)) {
143
- if (pathExists(directory)) outcome.retained++
144
- continue
145
- }
146
- try {
147
- fs.rmdirSync(directory)
148
- createdGeneratedDirectories.delete(directory)
149
- outcome.removed++
150
- } catch (error) {
151
- if (error.code === 'ENOENT') {
152
- createdGeneratedDirectories.delete(directory)
153
- } else {
154
- outcome.retained++
155
- }
156
- }
157
- }
158
- return outcome
159
- }
160
-
161
- function cleanupGeneratedRuntimeFiles (framework) {
162
- const strategy = framework.generatedTestStrategy
163
- if (!strategy) return
164
-
165
- initializeRuntimeCleanupFiles(framework, strategy)
166
- cleanupPaths(getSafeCleanupPaths(framework, strategy, { includeGeneratedFiles: false }))
167
- }
168
-
169
- function initializeRuntimeCleanupFiles (framework, strategy) {
170
- if (initializedCleanupStrategies.has(strategy)) return
171
-
172
- const generatedFiles = new Set((strategy.files || []).map(file => validateGeneratedFilePath(framework, file.path)))
173
- if (strategy.cleanupPaths) {
174
- for (const cleanupPath of strategy.cleanupPaths) {
175
- const filename = validateCleanupPath(framework, cleanupPath)
176
- if (generatedFiles.has(filename) || isDirectory(filename) || !isNamespacedRuntimeFile(filename)) continue
177
- if (fs.existsSync(filename)) {
178
- throw new Error(`Refusing to delete pre-existing generated validation runtime file: ${filename}`)
179
- }
180
- authorizedRuntimeCleanupFiles.set(filename, authorizePathForCleanup(framework.project.root, filename))
181
- }
182
- }
183
- initializedCleanupStrategies.add(strategy)
184
- }
185
-
186
- function getSafeCleanupPaths (framework, strategy, { includeGeneratedFiles }) {
187
- if (!strategy) return []
188
-
189
- const generatedFiles = new Set()
190
- if (strategy.files) {
191
- for (const file of strategy.files) {
192
- generatedFiles.add(validateGeneratedFilePath(framework, file.path))
193
- }
194
- }
195
-
196
- const cleanupPaths = []
197
- if (strategy.cleanupPaths) {
198
- for (const cleanupPath of strategy.cleanupPaths) {
199
- const filename = validateCleanupPath(framework, cleanupPath)
200
- if (generatedFiles.has(filename)) {
201
- if (includeGeneratedFiles && writtenGeneratedFiles.has(filename)) cleanupPaths.push(filename)
202
- continue
203
- }
204
-
205
- if (authorizedRuntimeCleanupFiles.has(filename)) {
206
- cleanupPaths.push(filename)
207
- }
208
- }
209
- }
210
-
211
- if (includeGeneratedFiles) {
212
- for (const filename of generatedFiles) {
213
- if (writtenGeneratedFiles.has(filename)) cleanupPaths.push(filename)
214
- }
215
- }
216
- return [...new Set(cleanupPaths)]
217
- }
218
-
219
- function cleanupPaths (cleanupPaths) {
220
- const outcome = { removed: 0, retained: 0 }
221
- for (const cleanupPath of cleanupPaths) {
222
- try {
223
- const authorization = writtenGeneratedFiles.get(cleanupPath) ||
224
- authorizedRuntimeCleanupFiles.get(cleanupPath)
225
- if (!authorization || !isCleanupAuthorizationValid(cleanupPath, authorization) || isDirectory(cleanupPath)) {
226
- if (pathExists(cleanupPath)) outcome.retained++
227
- continue
228
- }
229
- fs.rmSync(cleanupPath, { force: true })
230
- writtenGeneratedFiles.delete(cleanupPath)
231
- outcome.removed++
232
- } catch {
233
- if (pathExists(cleanupPath)) outcome.retained++
234
- }
235
- }
236
- return outcome
237
- }
238
-
239
- function addCleanupOutcome (target, outcome, kind) {
240
- target[`${kind}Removed`] += outcome.removed
241
- target[`${kind}Retained`] += outcome.retained
242
- }
243
-
244
- function pathExists (filename) {
245
- try {
246
- fs.lstatSync(filename)
247
- return true
248
- } catch {
249
- return false
250
- }
251
- }
252
-
253
- function forgetWrittenGeneratedFiles (filenames) {
254
- for (const filename of filenames) {
255
- writtenGeneratedFiles.delete(filename)
256
- }
257
- }
258
-
259
- function authorizePathForCleanup (root, filename) {
260
- const lexicalRoot = path.resolve(root)
261
- const physicalRoot = fs.realpathSync(lexicalRoot)
262
- const rootStat = fs.statSync(physicalRoot, { bigint: true })
263
- const authorization = {
264
- lexicalRoot,
265
- physicalRoot,
266
- rootDevice: rootStat.dev,
267
- rootInode: rootStat.ino,
268
- }
269
- pinCleanupParent(authorization, filename)
270
- pinCleanupTarget(authorization, filename)
271
- return authorization
272
- }
273
-
274
- function pinRuntimeCleanupParents (strategy) {
275
- if (strategy.cleanupPaths) {
276
- for (const cleanupPath of strategy.cleanupPaths) {
277
- const authorization = authorizedRuntimeCleanupFiles.get(path.resolve(cleanupPath))
278
- if (authorization && authorization.physicalParent === undefined) {
279
- pinCleanupParent(authorization, cleanupPath)
280
- }
281
- }
282
- }
283
- }
284
-
285
- function pinCleanupParent (authorization, filename) {
286
- try {
287
- const physicalParent = fs.realpathSync(path.dirname(filename))
288
- if (!isPathInside(authorization.physicalRoot, physicalParent)) return
289
- const parentStat = fs.statSync(physicalParent, { bigint: true })
290
- authorization.physicalParent = physicalParent
291
- authorization.parentDevice = parentStat.dev
292
- authorization.parentInode = parentStat.ino
293
- } catch {}
294
- }
295
-
296
- function pinCleanupTarget (authorization, filename) {
297
- try {
298
- const targetStat = fs.lstatSync(filename, { bigint: true })
299
- authorization.targetDevice = targetStat.dev
300
- authorization.targetInode = targetStat.ino
301
- } catch {}
302
- }
303
-
304
- function isCleanupAuthorizationValid (filename, authorization) {
305
- try {
306
- const currentPhysicalRoot = fs.realpathSync(authorization.lexicalRoot)
307
- const rootStat = fs.statSync(currentPhysicalRoot, { bigint: true })
308
- if (
309
- currentPhysicalRoot !== authorization.physicalRoot ||
310
- rootStat.dev !== authorization.rootDevice ||
311
- rootStat.ino !== authorization.rootInode ||
312
- authorization.physicalParent === undefined
313
- ) {
314
- return false
315
- }
316
- const physicalParent = fs.realpathSync(path.dirname(filename))
317
- const parentStat = fs.statSync(physicalParent, { bigint: true })
318
- if (physicalParent !== authorization.physicalParent ||
319
- parentStat.dev !== authorization.parentDevice || parentStat.ino !== authorization.parentInode) {
320
- return false
321
- }
322
-
323
- if (authorization.targetDevice !== undefined) {
324
- const targetStat = fs.lstatSync(filename, { bigint: true })
325
- if (targetStat.dev !== authorization.targetDevice || targetStat.ino !== authorization.targetInode) {
326
- return false
327
- }
328
- }
329
- return true
330
- } catch {
331
- return false
332
- }
333
- }
334
-
335
- function validateGeneratedFilePath (framework, filename) {
336
- return validatePathUnderProjectRoot(framework, filename, 'generated validation file')
337
- }
338
-
339
- function validateCleanupPath (framework, filename) {
340
- return validatePathUnderProjectRoot(framework, filename, 'generated validation cleanup')
341
- }
342
-
343
- function validatePathUnderProjectRoot (framework, filename, label) {
344
- const root = getProjectRoot(framework)
345
- const resolved = path.resolve(filename || '')
346
- if (!root || !isPathInside(root, resolved)) {
347
- throw new Error(`Refusing ${label} path outside project root: ${filename}`)
348
- }
349
- validatePhysicalPath(root, resolved, label)
350
- return resolved
351
- }
352
-
353
- /**
354
- * Verifies that an existing parent resolves inside the physical project root.
355
- *
356
- * @param {string} root project root
357
- * @param {string} filename candidate filename
358
- * @param {string} label customer-facing path label
359
- */
360
- function validatePhysicalPath (root, filename, label) {
361
- const parent = path.dirname(filename)
362
- let physicalRoot
363
- let physicalParent
364
- try {
365
- physicalRoot = fs.realpathSync(root)
366
- physicalParent = fs.realpathSync(parent)
367
- } catch (error) {
368
- if (error.code === 'ENOENT') return
369
- throw error
370
- }
371
-
372
- if (!isPathInside(physicalRoot, physicalParent)) {
373
- throw new Error(`Refusing ${label} path outside physical project root: ${filename}`)
374
- }
375
- try {
376
- if (fs.lstatSync(filename).isSymbolicLink()) {
377
- throw new Error(`Refusing ${label} symbolic-link target: ${filename}`)
378
- }
379
- } catch (error) {
380
- if (error.code !== 'ENOENT') throw error
381
- }
382
- }
383
-
384
- function getProjectRoot (framework) {
385
- const root = framework.project?.root
386
- return typeof root === 'string' && path.isAbsolute(root) ? path.resolve(root) : null
387
- }
388
-
389
- function isPathInside (root, filename) {
390
- const relative = path.relative(root, filename)
391
- return relative === '' || (relative && !relative.startsWith('..') && !path.isAbsolute(relative))
392
- }
393
-
394
- function validateContentLines (contentLines, filename) {
395
- if (!Array.isArray(contentLines) || contentLines.some(line => typeof line !== 'string')) {
396
- throw new Error(`Generated validation file contentLines must be an array of strings: ${filename}`)
397
- }
398
- const policyError = getGeneratedFileContentError(contentLines)
399
- if (policyError) {
400
- throw new Error(`Generated validation file ${policyError}: ${filename}`)
401
- }
402
- }
403
-
404
- function isNamespacedRuntimeFile (filename) {
405
- return path.basename(filename).includes(RUNTIME_FILE_NAMESPACE)
406
- }
407
-
408
- function isDirectory (filename) {
409
- try {
410
- return fs.statSync(filename).isDirectory()
411
- } catch {
412
- return false
413
- }
414
- }
415
-
416
- function findGeneratedScenario (framework, scenarioId) {
417
- return (framework.generatedTestStrategy?.scenarios || []).find(scenario => scenario.id === scenarioId)
418
- }
419
-
420
- module.exports = {
421
- cleanupGeneratedFiles,
422
- cleanupGeneratedRuntimeFiles,
423
- findGeneratedScenario,
424
- writeGeneratedFiles,
425
- }
@@ -1,284 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('node:path')
4
-
5
- const cucumberAdapter = require('./framework-adapters/cucumber')
6
- const cypressAdapter = require('./framework-adapters/cypress')
7
- const playwrightAdapter = require('./framework-adapters/playwright')
8
-
9
- const GENERATED_SCENARIOS = {
10
- 'basic-pass': {
11
- expectedWithoutDatadog: { exitCode: 0, observedTestCount: 1 },
12
- purpose: 'basic_reporting|efd_candidate',
13
- suiteName: 'dd-test-optimization-validation',
14
- testName: 'basic-pass',
15
- },
16
- 'atr-fail-once': {
17
- expectedWithoutDatadog: { exitCode: 1, observedTestCount: 1 },
18
- purpose: 'auto_test_retries_candidate',
19
- suiteName: 'dd-test-optimization-validation',
20
- testName: 'atr-fail-once',
21
- },
22
- 'test-management-target': {
23
- expectedWithoutDatadog: { exitCode: 0, observedTestCount: 1 },
24
- purpose: 'test_management_candidate',
25
- suiteName: 'dd-test-optimization-validation',
26
- testName: 'test-management-target',
27
- },
28
- }
29
-
30
- /**
31
- * Returns the retry-state path used by generated Node.js test scenarios.
32
- *
33
- * @param {string} framework test framework
34
- * @param {string} filename generated ATR test file
35
- */
36
- function getGeneratedRetryStatePath (framework, filename) {
37
- const namespace = `dd-test-optimization-validation-${framework}-`
38
- const namespaced = path.basename(filename).includes(namespace) ||
39
- path.basename(path.dirname(filename)).includes(namespace)
40
- const stateFilename = namespaced
41
- ? `.dd-test-optimization-validation-${framework}-atr-state`
42
- : '.dd-test-optimization-validation-atr-state'
43
- return path.join(path.dirname(filename), stateFilename)
44
- }
45
-
46
- /**
47
- * Returns validator-owned source for one generated scenario.
48
- *
49
- * @param {object} input generated source input
50
- * @param {string} input.framework test framework
51
- * @param {string} input.moduleSystem generated module system
52
- * @param {string} input.scenarioId generated scenario id
53
- * @param {string} [input.stateFile] persistent ATR state file
54
- */
55
- function getGeneratedTestContent ({ framework, moduleSystem, scenarioId, stateFile }) {
56
- const scenario = GENERATED_SCENARIOS[scenarioId]
57
- if (!scenario) throw new Error(`Unknown generated Test Optimization scenario: ${scenarioId}`)
58
- if (framework === 'cucumber') {
59
- return cucumberAdapter.getGeneratedTestContent({ testName: scenario.testName })
60
- }
61
- if (framework === 'cypress') {
62
- return cypressAdapter.getGeneratedTestContent({
63
- scenarioId,
64
- testName: scenario.testName,
65
- })
66
- }
67
- if (framework === 'playwright') {
68
- return playwrightAdapter.getGeneratedTestContent({
69
- moduleSystem,
70
- scenarioId,
71
- testName: scenario.testName,
72
- })
73
- }
74
-
75
- const imports = []
76
- if (framework === 'jest') {
77
- const requireCall = 'require'
78
- imports.push(moduleSystem === 'esm'
79
- ? "import { describe, expect, test } from '@jest/globals'"
80
- : `const jestGlobals = ${requireCall}('@jest/globals')`)
81
- }
82
- if (framework === 'vitest' && moduleSystem === 'esm') {
83
- imports.push("import { describe, expect, it } from 'vitest'")
84
- }
85
- if (framework === 'mocha') {
86
- imports.push(moduleSystem === 'esm'
87
- ? "import assert from 'node:assert/strict'"
88
- : "const assert = require('node:assert/strict')")
89
- }
90
- if (scenarioId === 'atr-fail-once') {
91
- if (moduleSystem === 'esm') {
92
- imports.push("import { existsSync, writeFileSync } from 'node:fs'")
93
- } else {
94
- imports.push("const fs = require('node:fs')")
95
- }
96
- }
97
-
98
- const commonJsJest = framework === 'jest' && moduleSystem === 'commonjs'
99
- const assertion = framework === 'mocha'
100
- ? 'assert.equal(1, 1)'
101
- : `${commonJsJest ? 'jestGlobals.' : ''}expect(true).toBe(true)`
102
- const testFunction = framework === 'jest' ? `${commonJsJest ? 'jestGlobals.' : ''}test` : 'it'
103
- const describeFunction = commonJsJest ? 'jestGlobals.describe' : 'describe'
104
- const body = scenarioId === 'atr-fail-once'
105
- ? getAtrBody({ moduleSystem, assertion, stateFile })
106
- : ` ${assertion}`
107
-
108
- return [
109
- ...imports,
110
- imports.length > 0 ? '' : undefined,
111
- `${describeFunction}('dd-test-optimization-validation', () => {`,
112
- ` ${testFunction}('${scenario.testName}', () => {`,
113
- body,
114
- ' })',
115
- '})',
116
- ].filter(line => line !== undefined).join('\n')
117
- }
118
-
119
- /**
120
- * Verifies that a runnable framework retained the validator-owned generated-test recipe.
121
- *
122
- * @param {object} framework manifest framework entry
123
- * @returns {string|undefined} contract error
124
- */
125
- function getGeneratedTestContractError (framework) {
126
- const strategy = framework.generatedTestStrategy
127
- if (
128
- !strategy ||
129
- !['planned', 'verified'].includes(strategy.status) ||
130
- !['cucumber', 'cypress', 'jest', 'mocha', 'playwright', 'vitest'].includes(framework.framework)
131
- ) {
132
- return
133
- }
134
- if (!['commonjs', 'esm'].includes(strategy.moduleSystem)) {
135
- return 'must retain generatedTestStrategy.moduleSystem as "commonjs" or "esm".'
136
- }
137
-
138
- const files = strategy.files || []
139
- const scenarios = strategy.scenarios || []
140
- const projectRoot = framework.project?.root
141
- if (typeof projectRoot !== 'string') return 'must declare a project root for generated tests.'
142
- if ((strategy.cleanupPaths || []).some(filename => typeof filename !== 'string')) {
143
- return 'must contain only string cleanup paths.'
144
- }
145
- const cleanupPaths = new Set((strategy.cleanupPaths || []).map(filename => path.normalize(filename)))
146
- const expectedFileCount = ['cucumber', 'playwright'].includes(framework.framework) ? 4 : 3
147
- if (files.length !== expectedFileCount || scenarios.length !== 3) {
148
- return 'must contain exactly one validator-owned file for each of basic-pass, atr-fail-once, and ' +
149
- `test-management-target${getAdditionalGeneratedFileDescription(framework.framework)}.`
150
- }
151
-
152
- const selectedFiles = new Set()
153
- for (const [scenarioId, definition] of Object.entries(GENERATED_SCENARIOS)) {
154
- const scenario = scenarios.find(entry => entry.id === scenarioId)
155
- if (!scenario) return `is missing the validator-owned ${scenarioId} scenario.`
156
- if (scenario.testIdentities?.length !== 1) {
157
- return `scenario ${scenarioId} must declare exactly one generated test identity.`
158
- }
159
-
160
- const filename = scenario.testIdentities[0].file
161
- if (typeof filename !== 'string') return `scenario ${scenarioId} must identify a generated test file.`
162
- if (!isPathInside(projectRoot, filename)) {
163
- return `scenario ${scenarioId} file must remain inside project root ${projectRoot}.`
164
- }
165
- const file = files.find(entry => {
166
- return typeof entry.path === 'string' && path.normalize(entry.path) === path.normalize(filename)
167
- })
168
- if (!file) return `scenario ${scenarioId} must identify exactly one file in generatedTestStrategy.files.`
169
- selectedFiles.add(path.normalize(filename))
170
- if (scenario.testIdentities[0].name !== definition.testName) {
171
- return `scenario ${scenarioId} must retain test name ${JSON.stringify(definition.testName)}.`
172
- }
173
-
174
- const stateFile = ['cucumber', 'cypress', 'playwright'].includes(framework.framework)
175
- ? undefined
176
- : getGeneratedRetryStatePath(framework.framework, filename)
177
- const expectedSource = getGeneratedTestContent({
178
- framework: framework.framework,
179
- moduleSystem: strategy.moduleSystem,
180
- scenarioId,
181
- stateFile,
182
- })
183
- if (file.contentLines?.join('\n') !== expectedSource) {
184
- return `scenario ${scenarioId} source differs from the validator-owned ${framework.framework} recipe. ` +
185
- 'Regenerate the manifest scaffold instead of rewriting temporary tests.'
186
- }
187
- if (!cleanupPaths.has(path.normalize(filename))) {
188
- return `scenario ${scenarioId} file must be included in generatedTestStrategy.cleanupPaths.`
189
- }
190
- }
191
-
192
- if (framework.framework === 'cucumber') {
193
- const stepsFile = cucumberAdapter.getGeneratedStepsPath(strategy.testDirectory)
194
- const steps = files.find(file => path.normalize(file.path) === path.normalize(stepsFile))
195
- if (steps?.contentLines?.join('\n') !== cucumberAdapter.getGeneratedStepsContent()) {
196
- return 'must retain the validator-owned Cucumber step definitions.'
197
- }
198
- if (!cleanupPaths.has(path.normalize(stepsFile))) {
199
- return 'must include the isolated Cucumber step definitions in cleanupPaths.'
200
- }
201
- } else if (framework.framework === 'playwright') {
202
- const configPath = playwrightAdapter.getGeneratedConfigPath(strategy.testDirectory)
203
- const config = files.find(file => path.normalize(file.path) === path.normalize(configPath))
204
- if (config?.contentLines?.join('\n') !== playwrightAdapter.getGeneratedConfigContent()) {
205
- return 'must retain the validator-owned isolated Playwright config.'
206
- }
207
- if (!cleanupPaths.has(path.normalize(configPath))) {
208
- return 'must include the isolated Playwright config in generatedTestStrategy.cleanupPaths.'
209
- }
210
- }
211
-
212
- const usesPersistentRetryState = !['cucumber', 'cypress', 'playwright'].includes(framework.framework)
213
- if (usesPersistentRetryState) {
214
- const atrScenario = scenarios.find(entry => entry.id === 'atr-fail-once')
215
- const stateFile = getGeneratedRetryStatePath(framework.framework, atrScenario.testIdentities[0].file)
216
- if (!cleanupPaths.has(path.normalize(stateFile))) {
217
- return `scenario atr-fail-once must clean up its persistent retry state file ${stateFile}.`
218
- }
219
- }
220
- const expectedCleanupPathCount = usesPersistentRetryState ? 4 : expectedFileCount
221
- if (selectedFiles.size !== 3 || cleanupPaths.size !== expectedCleanupPathCount) {
222
- if (framework.framework === 'cucumber') {
223
- return 'must use three distinct generated feature files and clean up those files plus the isolated Cucumber ' +
224
- 'step definitions.'
225
- }
226
- return usesPersistentRetryState
227
- ? 'must use three distinct generated test files and clean up exactly those files plus the persistent ATR ' +
228
- 'state file.'
229
- : 'must use three distinct generated test files and clean up exactly those files.'
230
- }
231
- }
232
-
233
- /**
234
- * Describes adapter-specific files in generated-contract errors.
235
- *
236
- * @param {string} framework framework name
237
- */
238
- function getAdditionalGeneratedFileDescription (framework) {
239
- if (framework === 'cucumber') return ' plus isolated Cucumber step definitions'
240
- if (framework === 'playwright') return ' plus one isolated Playwright config'
241
- return ''
242
- }
243
-
244
- /**
245
- * Checks whether a generated path is inside or equal to the declared project root.
246
- *
247
- * @param {string} root project root
248
- * @param {string} filename generated path
249
- */
250
- function isPathInside (root, filename) {
251
- const relative = path.relative(path.resolve(root), path.resolve(filename))
252
- return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative))
253
- }
254
-
255
- /**
256
- * Returns the persistent retry body used across independent test-runner processes.
257
- *
258
- * @param {object} input body input
259
- * @param {string} input.moduleSystem generated module system
260
- * @param {string} input.assertion passing assertion
261
- * @param {string} input.stateFile persistent ATR state file
262
- */
263
- function getAtrBody ({ moduleSystem, assertion, stateFile }) {
264
- if (typeof stateFile !== 'string' || !path.isAbsolute(stateFile)) {
265
- throw new Error('The validator-owned ATR recipe requires an absolute persistent state file path.')
266
- }
267
- const exists = moduleSystem === 'esm' ? 'existsSync' : 'fs.existsSync'
268
- const write = moduleSystem === 'esm' ? 'writeFileSync' : 'fs.writeFileSync'
269
- return [
270
- ` const stateFile = ${JSON.stringify(stateFile)}`,
271
- ` if (!${exists}(stateFile)) {`,
272
- ` ${write}(stateFile, 'failed-once')`,
273
- " throw new Error('dd-test-optimization-validation atr first failure')",
274
- ' }',
275
- ` ${assertion}`,
276
- ].join('\n')
277
- }
278
-
279
- module.exports = {
280
- GENERATED_SCENARIOS,
281
- getGeneratedRetryStatePath,
282
- getGeneratedTestContent,
283
- getGeneratedTestContractError,
284
- }