@encore.dev/sdk 2.0.0-beta.15 → 2.0.0-beta.17

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 (235) hide show
  1. package/README.md +111 -101
  2. package/app_meta.ts +56 -54
  3. package/config/config.ts +60 -57
  4. package/config/secrets.ts +45 -52
  5. package/config/value.ts +23 -26
  6. package/cron/mod.ts +28 -29
  7. package/dist/app_meta.d.ts +52 -50
  8. package/dist/app_meta.d.ts.map +1 -1
  9. package/dist/app_meta.js +2 -2
  10. package/dist/app_meta.js.map +1 -1
  11. package/dist/config/config.d.ts +58 -55
  12. package/dist/config/config.d.ts.map +1 -1
  13. package/dist/config/config.js +8 -9
  14. package/dist/config/config.js.map +1 -1
  15. package/dist/config/secrets.d.ts +45 -52
  16. package/dist/config/secrets.d.ts.map +1 -1
  17. package/dist/config/secrets.js +8 -9
  18. package/dist/config/secrets.js.map +1 -1
  19. package/dist/config/value.d.ts +20 -23
  20. package/dist/config/value.d.ts.map +1 -1
  21. package/dist/config/value.js +20 -23
  22. package/dist/config/value.js.map +1 -1
  23. package/dist/cron/mod.d.ts +26 -27
  24. package/dist/cron/mod.d.ts.map +1 -1
  25. package/dist/cron/mod.js +13 -14
  26. package/dist/cron/mod.js.map +1 -1
  27. package/dist/frontends/mod.d.ts +20 -19
  28. package/dist/frontends/mod.d.ts.map +1 -1
  29. package/dist/frontends/mod.js +20 -19
  30. package/dist/frontends/mod.js.map +1 -1
  31. package/dist/gateway/errors.d.ts +28 -30
  32. package/dist/gateway/errors.d.ts.map +1 -1
  33. package/dist/gateway/errors.js +29 -31
  34. package/dist/gateway/errors.js.map +1 -1
  35. package/dist/gateway/match.d.ts +54 -54
  36. package/dist/gateway/match.d.ts.map +1 -1
  37. package/dist/gateway/match.js +56 -56
  38. package/dist/gateway/match.js.map +1 -1
  39. package/dist/gateway/middleware.d.ts +62 -64
  40. package/dist/gateway/middleware.d.ts.map +1 -1
  41. package/dist/gateway/middleware.js +1 -1
  42. package/dist/gateway/middleware.js.map +1 -1
  43. package/dist/gateway/mod.d.ts +64 -67
  44. package/dist/gateway/mod.d.ts.map +1 -1
  45. package/dist/gateway/mod.js +72 -76
  46. package/dist/gateway/mod.js.map +1 -1
  47. package/dist/gateway/request.d.ts +138 -134
  48. package/dist/gateway/request.d.ts.map +1 -1
  49. package/dist/gateway/request.js +117 -108
  50. package/dist/gateway/request.js.map +1 -1
  51. package/dist/gateway/serve.d.ts +29 -34
  52. package/dist/gateway/serve.d.ts.map +1 -1
  53. package/dist/gateway/serve.js +51 -56
  54. package/dist/gateway/serve.js.map +1 -1
  55. package/dist/gateways/mod.d.ts +23 -21
  56. package/dist/gateways/mod.d.ts.map +1 -1
  57. package/dist/gateways/mod.js +25 -23
  58. package/dist/gateways/mod.js.map +1 -1
  59. package/dist/internal/runtime/napi/napi.cjs +1 -1
  60. package/dist/internal/runtime/napi/napi.d.cts +10 -3
  61. package/dist/internal/runtime/shutdown.d.ts +75 -57
  62. package/dist/internal/runtime/shutdown.d.ts.map +1 -1
  63. package/dist/internal/runtime/shutdown.js +88 -84
  64. package/dist/internal/runtime/shutdown.js.map +1 -1
  65. package/dist/log/logger.d.ts +43 -37
  66. package/dist/log/logger.d.ts.map +1 -1
  67. package/dist/log/logger.js +26 -25
  68. package/dist/log/logger.js.map +1 -1
  69. package/dist/metrics/mod.d.ts +133 -114
  70. package/dist/metrics/mod.d.ts.map +1 -1
  71. package/dist/metrics/mod.js +109 -101
  72. package/dist/metrics/mod.js.map +1 -1
  73. package/dist/otel.d.ts +6 -3
  74. package/dist/otel.d.ts.map +1 -1
  75. package/dist/otel.js +6 -3
  76. package/dist/otel.js.map +1 -1
  77. package/dist/pubsub/subscription.d.ts +88 -116
  78. package/dist/pubsub/subscription.d.ts.map +1 -1
  79. package/dist/pubsub/subscription.js +14 -17
  80. package/dist/pubsub/subscription.js.map +1 -1
  81. package/dist/pubsub/topic.d.ts +86 -113
  82. package/dist/pubsub/topic.d.ts.map +1 -1
  83. package/dist/pubsub/topic.js +68 -82
  84. package/dist/pubsub/topic.js.map +1 -1
  85. package/dist/reqctx/mod.d.ts +83 -86
  86. package/dist/reqctx/mod.d.ts.map +1 -1
  87. package/dist/reqctx/mod.js +39 -41
  88. package/dist/reqctx/mod.js.map +1 -1
  89. package/dist/services/index.d.ts +76 -75
  90. package/dist/services/index.d.ts.map +1 -1
  91. package/dist/services/index.js +73 -71
  92. package/dist/services/index.js.map +1 -1
  93. package/dist/shutdown/mod.d.ts +26 -25
  94. package/dist/shutdown/mod.d.ts.map +1 -1
  95. package/dist/shutdown/mod.js +26 -25
  96. package/dist/shutdown/mod.js.map +1 -1
  97. package/dist/storage/clickhouse/database.d.ts +113 -115
  98. package/dist/storage/clickhouse/database.d.ts.map +1 -1
  99. package/dist/storage/clickhouse/database.js +65 -79
  100. package/dist/storage/clickhouse/database.js.map +1 -1
  101. package/dist/storage/clickhouse/errors.d.ts +25 -26
  102. package/dist/storage/clickhouse/errors.d.ts.map +1 -1
  103. package/dist/storage/clickhouse/errors.js +15 -17
  104. package/dist/storage/clickhouse/errors.js.map +1 -1
  105. package/dist/storage/clickhouse/mod.d.ts +10 -10
  106. package/dist/storage/clickhouse/mod.js +10 -10
  107. package/dist/storage/objects/bucket.d.ts +196 -225
  108. package/dist/storage/objects/bucket.d.ts.map +1 -1
  109. package/dist/storage/objects/bucket.js +71 -84
  110. package/dist/storage/objects/bucket.js.map +1 -1
  111. package/dist/storage/objects/error.d.ts +4 -4
  112. package/dist/storage/objects/error.js +4 -4
  113. package/dist/storage/postgres/database.d.ts +124 -149
  114. package/dist/storage/postgres/database.d.ts.map +1 -1
  115. package/dist/storage/postgres/database.js +98 -135
  116. package/dist/storage/postgres/database.js.map +1 -1
  117. package/dist/storage/postgres/errors.d.ts +42 -48
  118. package/dist/storage/postgres/errors.d.ts.map +1 -1
  119. package/dist/storage/postgres/errors.js +44 -51
  120. package/dist/storage/postgres/errors.js.map +1 -1
  121. package/dist/storage/postgres/interval.d.ts +19 -19
  122. package/dist/storage/postgres/interval.d.ts.map +1 -1
  123. package/dist/storage/postgres/interval.js +21 -21
  124. package/dist/storage/postgres/interval.js.map +1 -1
  125. package/dist/storage/redis/bitfield.d.ts +48 -23
  126. package/dist/storage/redis/bitfield.d.ts.map +1 -1
  127. package/dist/storage/redis/bitfield.js +38 -19
  128. package/dist/storage/redis/bitfield.js.map +1 -1
  129. package/dist/storage/redis/command.d.ts +16 -12
  130. package/dist/storage/redis/command.d.ts.map +1 -1
  131. package/dist/storage/redis/command.js +16 -12
  132. package/dist/storage/redis/command.js.map +1 -1
  133. package/dist/storage/redis/commands.d.ts +99 -100
  134. package/dist/storage/redis/commands.d.ts.map +1 -1
  135. package/dist/storage/redis/commands.js +84 -85
  136. package/dist/storage/redis/commands.js.map +1 -1
  137. package/dist/storage/redis/database.d.ts +138 -122
  138. package/dist/storage/redis/database.d.ts.map +1 -1
  139. package/dist/storage/redis/database.js +120 -101
  140. package/dist/storage/redis/database.js.map +1 -1
  141. package/dist/storage/redis/mod.d.ts +52 -48
  142. package/dist/storage/redis/mod.d.ts.map +1 -1
  143. package/dist/storage/redis/mod.js +52 -48
  144. package/dist/storage/redis/mod.js.map +1 -1
  145. package/dist/storage/redis/options.d.ts +26 -24
  146. package/dist/storage/redis/options.d.ts.map +1 -1
  147. package/dist/storage/redis/options.js.map +1 -1
  148. package/dist/storage/redis/pubsub.d.ts +16 -16
  149. package/dist/storage/redis/pubsub.d.ts.map +1 -1
  150. package/dist/storage/redis/pubsub.js +14 -14
  151. package/dist/storage/redis/pubsub.js.map +1 -1
  152. package/dist/storage/redis/reply.d.ts +80 -72
  153. package/dist/storage/redis/reply.d.ts.map +1 -1
  154. package/dist/storage/redis/reply.js +53 -46
  155. package/dist/storage/redis/reply.js.map +1 -1
  156. package/dist/storage/redis/scan.d.ts +4 -4
  157. package/dist/storage/redis/scan.d.ts.map +1 -1
  158. package/dist/storage/redis/scan.js +6 -6
  159. package/dist/storage/redis/scan.js.map +1 -1
  160. package/dist/storage/redis/script.d.ts +28 -21
  161. package/dist/storage/redis/script.d.ts.map +1 -1
  162. package/dist/storage/redis/script.js +29 -22
  163. package/dist/storage/redis/script.js.map +1 -1
  164. package/dist/storage/redis/target.d.ts +17 -1
  165. package/dist/storage/redis/target.d.ts.map +1 -1
  166. package/dist/storage/redis/target.js +30 -1
  167. package/dist/storage/redis/target.js.map +1 -1
  168. package/dist/storage/redis/wire.d.ts +6 -6
  169. package/dist/storage/redis/wire.d.ts.map +1 -1
  170. package/dist/storage/redis/wire.js +6 -6
  171. package/dist/storage/redis/wire.js.map +1 -1
  172. package/dist/testing/mod.d.ts +83 -88
  173. package/dist/testing/mod.d.ts.map +1 -1
  174. package/dist/testing/mod.js +75 -81
  175. package/dist/testing/mod.js.map +1 -1
  176. package/dist/testing/playwright.d.ts +28 -32
  177. package/dist/testing/playwright.d.ts.map +1 -1
  178. package/dist/testing/playwright.js +22 -27
  179. package/dist/testing/playwright.js.map +1 -1
  180. package/dist/testing/puppeteer.d.ts +11 -12
  181. package/dist/testing/puppeteer.d.ts.map +1 -1
  182. package/dist/testing/puppeteer.js +17 -18
  183. package/dist/testing/puppeteer.js.map +1 -1
  184. package/dist/testing/system.d.ts +72 -90
  185. package/dist/testing/system.d.ts.map +1 -1
  186. package/dist/testing/system.js +90 -107
  187. package/dist/testing/system.js.map +1 -1
  188. package/dist/tsconfig.build.tsbuildinfo +1 -1
  189. package/frontends/mod.test.ts +6 -2
  190. package/frontends/mod.ts +20 -19
  191. package/gateway/errors.ts +30 -32
  192. package/gateway/match.ts +61 -57
  193. package/gateway/middleware.ts +62 -64
  194. package/gateway/mod.ts +73 -76
  195. package/gateway/request.ts +143 -140
  196. package/gateway/serve.ts +58 -63
  197. package/gateways/mod.ts +25 -23
  198. package/internal/runtime/napi/napi.cjs +1 -1
  199. package/internal/runtime/napi/napi.d.cts +10 -3
  200. package/internal/runtime/shutdown.ts +113 -101
  201. package/log/logger.test.ts +8 -0
  202. package/log/logger.ts +48 -45
  203. package/metrics/mod.ts +138 -122
  204. package/otel.ts +6 -3
  205. package/package.json +1 -1
  206. package/pubsub/subscription.ts +93 -123
  207. package/pubsub/topic.ts +96 -130
  208. package/reqctx/mod.ts +83 -86
  209. package/services/index.ts +78 -78
  210. package/shutdown/mod.ts +26 -25
  211. package/storage/clickhouse/database.ts +115 -129
  212. package/storage/clickhouse/errors.ts +27 -29
  213. package/storage/clickhouse/mod.ts +10 -10
  214. package/storage/objects/bucket.ts +209 -239
  215. package/storage/objects/error.ts +4 -4
  216. package/storage/postgres/database.ts +131 -164
  217. package/storage/postgres/errors.ts +45 -52
  218. package/storage/postgres/interval.ts +21 -21
  219. package/storage/redis/bitfield.ts +51 -26
  220. package/storage/redis/command.ts +16 -12
  221. package/storage/redis/commands.ts +99 -100
  222. package/storage/redis/database.ts +145 -123
  223. package/storage/redis/mod.ts +52 -48
  224. package/storage/redis/options.ts +26 -24
  225. package/storage/redis/pubsub.ts +14 -14
  226. package/storage/redis/redis.test.ts +17 -2
  227. package/storage/redis/reply.ts +85 -77
  228. package/storage/redis/scan.ts +6 -6
  229. package/storage/redis/script.ts +29 -22
  230. package/storage/redis/target.ts +34 -1
  231. package/storage/redis/wire.ts +8 -8
  232. package/testing/mod.ts +99 -105
  233. package/testing/playwright.ts +32 -37
  234. package/testing/puppeteer.ts +22 -23
  235. package/testing/system.ts +117 -133
@@ -52,10 +52,43 @@ export class PoolTarget implements Target {
52
52
  runtime.RedisDatabase,
53
53
  string | null
54
54
  >();
55
+ /** A reading target's native handles, by the database's own each was
56
+ * made from. */
57
+ private readonly readers = new WeakMap<
58
+ runtime.RedisDatabase,
59
+ runtime.RedisDatabase
60
+ >();
55
61
 
56
- constructor(readonly name: string) {}
62
+ /**
63
+ * @param readerOf The target a reading one reads through. Unset, the
64
+ * target reads and writes.
65
+ * @param primary Whether a reading target reads from the primary.
66
+ */
67
+ constructor(
68
+ readonly name: string,
69
+ private readonly readerOf?: PoolTarget,
70
+ private readonly primary = false,
71
+ ) {}
72
+
73
+ /**
74
+ * A target on the database for a handle that only reads: from the primary
75
+ * if `primary`, and otherwise from a read replica where the database has a
76
+ * read-only pool on one. The runtime refuses its commands that write.
77
+ */
78
+ reader(primary: boolean): PoolTarget {
79
+ return new PoolTarget(this.name, this, primary);
80
+ }
57
81
 
58
82
  database(): runtime.RedisDatabase {
83
+ if (this.readerOf) {
84
+ const own = this.readerOf.database();
85
+ let reader = this.readers.get(own);
86
+ if (!reader) {
87
+ reader = own.reader(this.primary);
88
+ this.readers.set(own, reader);
89
+ }
90
+ return reader;
91
+ }
59
92
  return testDatabase(this.name) ?? (this.impl ??= openDatabase(this.name));
60
93
  }
61
94
 
@@ -8,10 +8,10 @@
8
8
  * @module
9
9
  */
10
10
 
11
- /** A value sent to Redis as an argument: text, bytes, or a number. */
11
+ /** A value that can be passed to Redis as an argument: text, bytes, or a number. */
12
12
  export type RedisArg = string | number | bigint | Buffer | Uint8Array;
13
13
 
14
- /** A key and its value, or a field and its value, in the order given. */
14
+ /** Key-value or field-value pairs passed as a plain object or a `ReadonlyMap`. */
15
15
  export type Pairs<V> = Record<string, V> | ReadonlyMap<string, V>;
16
16
 
17
17
  /** One argument of a command. */
@@ -122,8 +122,8 @@ export function toArg(value: unknown, operation: string): Buffer {
122
122
  return Buffer.from(String(value), 'ascii');
123
123
  }
124
124
  throw new TypeError(
125
- `redis: ${operation}: cannot send ${describe(value)} as an argument: ` +
126
- `give a string, a number, a bigint, or a Buffer`,
125
+ `redis: ${operation}: cannot send ${describe(value)} as an argument; ` +
126
+ `expected a string, number, bigint, or Buffer`,
127
127
  );
128
128
  }
129
129
 
@@ -213,12 +213,12 @@ export function encodeList(items: readonly string[]): Buffer {
213
213
  }
214
214
 
215
215
  /**
216
- * The error a command the server rejected fails with.
216
+ * Error returned when the Redis server rejects a command.
217
217
  */
218
218
  export class ServerError extends Error {
219
219
  /**
220
- * The server's message, as Redis spells it:
221
- * `WRONGTYPE Operation against a key holding the wrong kind of value`.
220
+ * The raw error message returned by Redis (for example,
221
+ * `WRONGTYPE Operation against a key holding the wrong kind of value`).
222
222
  */
223
223
  readonly serverMessage: string;
224
224
 
@@ -233,7 +233,7 @@ export class ServerError extends Error {
233
233
  Object.setPrototypeOf(this, ServerError.prototype);
234
234
  }
235
235
 
236
- /** The error's code: the first word of its message, `WRONGTYPE`. */
236
+ /** The Redis error code: the first word of `serverMessage` (for example, `WRONGTYPE`). */
237
237
  get code(): string {
238
238
  const space = this.serverMessage.indexOf(' ');
239
239
  return space < 0 ? this.serverMessage : this.serverMessage.slice(0, space);
package/testing/mod.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Tests of an Encore app. Each test gets infrastructure of its own —
3
- * databases, buckets, Redis databases and topics no other test sees — with
4
- * nothing to set up but one line of runner config:
2
+ * Testing helpers for Encore apps. Every test receives isolated infrastructure
3
+ * — databases, buckets, Redis databases, and Pub/Sub topics invisible to other
4
+ * tests — with a single line of runner configuration:
5
5
  *
6
6
  * | Runner | Config |
7
7
  * |-----------|---------------------------------------------------------------------|
@@ -10,17 +10,17 @@
10
10
  * | bun | `bunfig.toml`: `[test] preload = ["@encore.dev/sdk/testing/bun"]` |
11
11
  * | node:test | `import "@encore.dev/sdk/testing/node"` at the top of each test file |
12
12
  *
13
- * The tests run with `encore test`, or with the runner itself (`vitest`,
14
- * `bun test`, an editor's test button): Encore finds the app the tests are in,
15
- * and starts its daemon if it is not running. `ENCORE_TEST=0` turns this off,
16
- * and `ENCORE_TEST=1` turns it on for a runner Encore does not recognize.
13
+ * Run tests with `encore test` or directly via your test runner (`vitest`,
14
+ * `bun test`, or your editor's test button): Encore detects the app and starts
15
+ * its daemon automatically if needed. Set `ENCORE_TEST=0` to disable this, or
16
+ * `ENCORE_TEST=1` to enable it for an unrecognized runner.
17
17
  *
18
- * Nothing runs by itself in a test: what a test publishes is recorded, for it
19
- * to read back with {@link published} and hand to a subscription with
20
- * {@link deliver}.
18
+ * In unit tests, background work does not run automatically: messages published
19
+ * to Pub/Sub topics are captured so you can inspect them with {@link published}
20
+ * and trigger subscription handlers explicitly with {@link deliver}.
21
21
  *
22
- * The tests of a system test suite, a directory with an
23
- * `encore.systest.json`, run against the whole app instead: see
22
+ * For end-to-end tests in a system test suite (a directory with an
23
+ * `encore.systest.json`), tests run against the full app instead — see
24
24
  * {@link system}.
25
25
  *
26
26
  * @example
@@ -58,28 +58,29 @@ import {
58
58
  export type {Hooks} from '../internal/testing/scope';
59
59
  export * as system from './system';
60
60
 
61
- /** The options of {@link setup} and {@link scoped}. */
61
+ /** Options for {@link setup} and {@link scoped}. */
62
62
  export interface ScopeOptions {
63
63
  /**
64
- * What each test starts from: a {@link Fixture}, or `null` for the
65
- * resources' initial state, whatever {@link useFixture} says.
64
+ * Initial resource state for each test: a {@link Fixture}, or `null` to start
65
+ * from freshly provisioned empty resources regardless of {@link useFixture}.
66
66
  */
67
67
  fixture?: Fixture | null;
68
68
  }
69
69
 
70
- /** The hooks {@link setup} was given, for {@link useFixture}. */
70
+ /** Runner hooks registered via {@link setup}, used by {@link useFixture}. */
71
71
  let registered: Hooks | undefined;
72
72
 
73
73
  /**
74
- * Registers per-test infrastructure with a runner's hooks: each test begins
75
- * with resources of its own, released after it. The per-runner entry points
76
- * (`@encore.dev/sdk/testing/vitest` and the others) call this for you.
74
+ * Registers per-test infrastructure lifecycle hooks with a test runner so each
75
+ * test starts with isolated resources that are released when it finishes. The
76
+ * runner adapters (`@encore.dev/sdk/testing/vitest`, etc.) call this
77
+ * automatically.
77
78
  *
78
- * Once it has, a resource used outside any test — in a `beforeAll`, or at
79
- * module level — throws: no test would see what it does. Set up what each
80
- * test needs in `beforeEach`, or in a {@link fixture}.
79
+ * Once registered, accessing infrastructure outside a test (for example in
80
+ * `beforeAll` or at module top level) throws because no test would see those
81
+ * writes. Seed shared test data in `beforeEach` or with a {@link fixture}.
81
82
  *
82
- * Outside test mode it registers nothing, so a test file still runs.
83
+ * Outside test mode, this is a no-op so test files can still be imported.
83
84
  */
84
85
  export function setup(hooks: Hooks, options?: ScopeOptions): void {
85
86
  registered = hooks;
@@ -91,19 +92,20 @@ export function setup(hooks: Hooks, options?: ScopeOptions): void {
91
92
  }
92
93
 
93
94
  /**
94
- * Wraps a test body to run in a scope of its own: for concurrent tests,
95
- * which the runner's hooks cannot tell apart, or a runner without hooks.
95
+ * Wraps a test function in its own isolated resource scope. Use this for
96
+ * concurrent tests (which runner lifecycle hooks cannot distinguish) or runners
97
+ * without hooks.
96
98
  *
97
99
  * ```ts
98
100
  * test.concurrent("x", scoped(async () => { ... }));
99
101
  * ```
100
102
  *
101
- * The test starts from `options.fixture` if given, else from the fixture
102
- * {@link useFixture} chose for it, where the runner hands the test its
103
- * context (vitest, node:test). Outside test mode it runs `fn` as it is.
103
+ * Starts from `options.fixture` if specified, otherwise from any fixture
104
+ * configured via {@link useFixture} (when the runner supplies a test context,
105
+ * as in Vitest and `node:test`). Outside test mode, runs `fn` directly.
104
106
  *
105
- * A system test wrapped in it says how it ended in any runner, and prints
106
- * what it caused in the app when it fails: see {@link system}.
107
+ * Wrapping a system test in `scoped` also reports its pass/fail outcome in any
108
+ * runner and prints app telemetry when it fails (see {@link system}).
107
109
  */
108
110
  export function scoped<A extends unknown[], R>(
109
111
  fn: (...args: A) => Promise<R> | R,
@@ -135,20 +137,21 @@ export function scoped<A extends unknown[], R>(
135
137
  }
136
138
 
137
139
  /**
138
- * What the current test has published to `topic`, in the order it was
139
- * published: by the test itself, and by the code it called. Nothing handles
140
- * it otherwise: hand it to a subscription with {@link deliver}.
140
+ * Returns all messages published to `topic` during the current test (whether
141
+ * published directly by the test or by code it called), in publish order.
142
+ * Messages are not delivered automatically in unit tests; pass the subscription
143
+ * to {@link deliver} to invoke its handler.
141
144
  *
142
- * Each message's `data` is what was published: decoded from JSON, or the
143
- * bytes published as bytes.
145
+ * Each message's `data` is JSON-decoded unless it was published as raw bytes.
144
146
  *
145
- * Not in a system test, where what a test publishes is sent.
147
+ * Not available in system tests, where published messages are delivered to
148
+ * running services automatically.
146
149
  */
147
150
  export function published<T>(topic: Topic<T> | OrderedTopic<T>): Message<T>[] {
148
151
  if (systemTest !== null) {
149
152
  throw new Error(
150
- 'what a system test published is sent, and is not kept by the test: read what ' +
151
- 'the app did with it instead, once it has `settled`',
153
+ 'published() is not available in system tests because messages are delivered ' +
154
+ 'to running services: await system.settled() and assert on the resulting state instead',
152
155
  );
153
156
  }
154
157
  const scope = requireScope();
@@ -166,32 +169,30 @@ export function published<T>(topic: Topic<T> | OrderedTopic<T>): Message<T>[] {
166
169
  }
167
170
 
168
171
  /**
169
- * Runs `subscription`'s handler on each message the current test — or the
170
- * code it called — published to its topic that the subscription has not
171
- * handled yet, one at a time, in the order they were published, as part of
172
- * the test. Resolves to how many it handled.
172
+ * Invokes `subscription`'s handler sequentially for every matching undelivered
173
+ * message published to its topic during the current test, returning the number
174
+ * of messages handled.
173
175
  *
174
- * As in a real delivery, the handler gets a message only if the
175
- * subscription's filter matches it, decoded as the subscription says, with
176
- * the Request Context fields it was published with, and `deliveryAttempt` 1
177
- * the first time. What the handler publishes to the topic in turn is handed
178
- * over in the same call.
176
+ * Just like in production, messages are filtered by the subscription's filter,
177
+ * decoded using its encoding settings, and delivered with their propagated
178
+ * Request Context fields (starting at `deliveryAttempt: 1`). Messages published
179
+ * back to the same topic by the handler are also delivered during the same call.
179
180
  *
180
- * It stops at the first message the handler fails, and rejects with its
181
- * error. That message stays unhandled: the next call hands it over again,
182
- * its `deliveryAttempt` one more. Retry policies, ack deadlines and dead
183
- * letters play no part.
181
+ * Stops on the first handler error and rejects with it. The failed message
182
+ * remains pending and will be retried on the next `deliver()` call with an
183
+ * incremented `deliveryAttempt`. Retry policies, ack deadlines, and dead-letter
184
+ * queues do not apply.
184
185
  *
185
- * Not in a system test, where the subscriptions run on their own: wait for
186
- * them with `system.settled()`.
186
+ * Not available in system tests, where subscriptions run automatically — use
187
+ * `system.settled()` instead.
187
188
  */
188
189
  export async function deliver<T>(
189
190
  subscription: Subscription<T>,
190
191
  ): Promise<number> {
191
192
  if (systemTest !== null) {
192
193
  throw new Error(
193
- 'subscriptions run on their own in a system test, in the services that declare ' +
194
- 'them: wait for them to have handled what was published with `system.settled()`',
194
+ 'deliver() is not available in system tests because subscriptions run automatically ' +
195
+ 'in their declaring services: await system.settled() instead',
195
196
  );
196
197
  }
197
198
  const scope = requireScope();
@@ -203,69 +204,68 @@ export async function deliver<T>(
203
204
  }
204
205
 
205
206
  /**
206
- * Gives `secret` a value for the current test, in place of the app's. No
207
- * other test sees it, and it is gone when the test ends.
207
+ * Overrides `secret` with `value` for the current test only. Other tests do
208
+ * not see the override, and it is cleared when the test ends.
208
209
  *
209
- * Not in a system test, whose services read their own values: give the
210
- * secret its value for tests in `secrets.test.toml` instead.
210
+ * Not available in system tests, where services read their own secrets — set
211
+ * test secret values in `secrets.test.toml` instead.
211
212
  */
212
213
  export function setSecret(secret: AnySecret, value: string | Uint8Array): void {
213
214
  if (systemTest !== null) {
214
215
  throw new Error(
215
- "a system test can't give a secret a value of its own: the services it calls read " +
216
- "theirs, not the test's. Give the secret its value for tests in secrets.test.toml",
216
+ 'setSecret() is not available in system tests because running services read their ' +
217
+ 'own secrets: configure test secret values in secrets.test.toml instead',
217
218
  );
218
219
  }
219
220
  requireScope().native.setSecret(secret.name, Buffer.from(value));
220
221
  }
221
222
 
222
223
  /**
223
- * Gives `config` a value for the current test, in place of the app's: what
224
- * calling the config returns in the test from then on. No other test sees
225
- * it, and it is gone when the test ends. Throws if the value does not
226
- * satisfy the config's schema.
224
+ * Overrides `config` with `value` for the current test only. Other tests do
225
+ * not see the override, and it is cleared when the test ends. Throws if
226
+ * `value` fails schema validation.
227
227
  *
228
- * Not in a system test, whose services read their own values: give the
229
- * config its value for tests in its `test` config file instead.
228
+ * Not available in system tests, where services read their own config files —
229
+ * set test config values in `<name>.test.config.toml` instead.
230
230
  */
231
231
  export function setConfig<T>(config: Config<T>, value: T): void {
232
232
  if (systemTest !== null) {
233
233
  throw new Error(
234
- "a system test can't give a config a value of its own: the services it calls read " +
235
- "theirs, not the test's. Give the config its value for tests in its `test` config " +
236
- 'file (<name>.test.config.toml)',
234
+ 'setConfig() is not available in system tests because running services read their ' +
235
+ 'own config files: configure test values in <name>.test.config.toml instead',
237
236
  );
238
237
  }
239
238
  setTestValue(requireScope(), config, value);
240
239
  }
241
240
 
242
241
  /**
243
- * A saved state for tests to start from, in place of the app's freshly
244
- * provisioned one: the rows a setup inserted, say. Declare one with
242
+ * A reusable snapshot of resource state (such as seeded database rows) that
243
+ * tests can start from instead of empty resources. Create one with
245
244
  * {@link fixture}.
246
245
  */
247
246
  export interface Fixture {
248
- /** The fixture's name, which identifies it across the app's test files. */
247
+ /** Unique name identifying this fixture across all test files in the app. */
249
248
  readonly name: string;
250
- /** The fixture it extends: its setup starts from what that one saved. */
249
+ /** Optional parent fixture whose snapshot is loaded before this fixture's setup runs. */
251
250
  readonly parent?: Fixture;
252
251
  /**
253
- * Makes the current test start from the fixture, building it first if no
254
- * test of the run has yet. Call it before the test first uses a resource:
255
- * a test that has used one already cannot change what it started from.
252
+ * Loads this fixture into the current test, building it first if it has not
253
+ * been built yet in this test run. Must be called before the test accesses
254
+ * any resource.
256
255
  */
257
256
  load(): Promise<void>;
258
257
  }
259
258
 
260
- /** The options of {@link fixture}. */
259
+ /** Options for {@link fixture}. */
261
260
  export interface FixtureOptions {
262
- /** A fixture to build on: the setup starts from what it saved. */
261
+ /** Parent fixture to build on; this fixture's setup starts from its saved state. */
263
262
  extends?: Fixture;
264
263
  }
265
264
 
266
265
  /**
267
- * Declares a fixture: a state `setup` leaves the app's resources in,
268
- * built once per test run and copied for each test that starts from it.
266
+ * Declares a reusable {@link Fixture}: runs `setup` once per test run, saves
267
+ * the resulting state of all touched databases, buckets, and Redis databases,
268
+ * and gives each test that loads it an isolated copy.
269
269
  *
270
270
  * ```ts
271
271
  * const seeded = fixture("users.seeded", async () => {
@@ -274,15 +274,9 @@ export interface FixtureOptions {
274
274
  * useFixture(seeded);
275
275
  * ```
276
276
  *
277
- * The setup is ordinary app code: its resources are the fixture's while it
278
- * runs. It runs the first time a test loads the fixture, however many test
279
- * files and workers load it. What it leaves in each database, bucket and
280
- * Redis database it touched is saved; each test that loads the fixture gets
281
- * a copy of its own. Other kinds of resource start each test from their
282
- * initial state.
283
- *
284
- * The name identifies the fixture across the app's test files: two fixtures
285
- * of one name are one fixture, built by whichever is loaded first.
277
+ * `setup` runs normal application code on the first load across all test files
278
+ * and workers. Other resource types start from their initial empty state.
279
+ * Fixtures with the same `name` refer to the same snapshot across files.
286
280
  */
287
281
  export function fixture(
288
282
  name: string,
@@ -299,18 +293,18 @@ export function fixture(
299
293
  }
300
294
 
301
295
  /**
302
- * Makes `f` what every test after this call in the file — or, called in a
303
- * `describe` block, in the block — starts from; `null` for the resources'
304
- * initial state. A {@link scoped} test starts from it too, unless told
305
- * otherwise. Needs the runner adapter (or {@link setup}), whose hooks it
306
- * adds to. Outside test mode it does nothing.
296
+ * Sets `f` as the default starting fixture for subsequent tests in the current
297
+ * file or `describe` block (pass `null` to reset to freshly provisioned empty
298
+ * resources). Also applies to {@link scoped} tests unless overridden.
299
+ *
300
+ * Requires a test runner adapter (or {@link setup}). No-op outside test mode.
307
301
  */
308
302
  export function useFixture(f: Fixture | null): void {
309
303
  if (!testMode) return;
310
304
  if (registered === undefined) {
311
305
  throw new Error(
312
- 'useFixture needs the runner adapter for your test runner, such as ' +
313
- "@encore.dev/sdk/testing/vitest in vitest's setupFiles",
306
+ 'useFixture() requires a test runner adapter (e.g. add ' +
307
+ '"@encore.dev/sdk/testing/vitest" to Vitest setupFiles)',
314
308
  );
315
309
  }
316
310
  registered.beforeEach((...args: unknown[]) => {
@@ -321,29 +315,29 @@ export function useFixture(f: Fixture | null): void {
321
315
  });
322
316
  }
323
317
 
324
- /** Each fixture's setup. */
318
+ /** Maps each fixture handle to its setup function. */
325
319
  const setups = new WeakMap<Fixture, () => Promise<void> | void>();
326
320
 
327
- /** The fixture `useFixture` chose for the test of a runner's context. */
321
+ /** Maps runner test contexts to the fixture selected via `useFixture`. */
328
322
  const chosenFor = new WeakMap<object, Fixture | null>();
329
323
 
330
324
  function chosen(ctx: unknown): Fixture | null | undefined {
331
325
  return isContext(ctx) ? chosenFor.get(ctx) : undefined;
332
326
  }
333
327
 
334
- /** Makes `scope`'s test start from `f`, or from nothing for `null`. */
328
+ /** Applies fixture `f` (or clears the fixture when `null`) on `scope`. */
335
329
  async function choose(scope: Scope, f: Fixture | null): Promise<void> {
336
330
  if (f === null) scope.native.useFixture(null);
337
331
  else await load(f, scope);
338
332
  }
339
333
 
340
- /** Loads the fixture `f` into `scope`, building it first if need be. */
334
+ /** Ensures fixture `f` is built and applies it to `scope`. */
341
335
  async function load(f: Fixture, scope: Scope): Promise<void> {
342
336
  await ensureBuilt(f);
343
337
  scope.native.useFixture(f.name);
344
338
  }
345
339
 
346
- /** Each fixture's build, as far as this process has asked. */
340
+ /** Tracks in-flight or completed fixture builds in this process. */
347
341
  const builds = new WeakMap<Fixture, Promise<void>>();
348
342
 
349
343
  function ensureBuilt(f: Fixture): Promise<void> {
@@ -355,12 +349,12 @@ function ensureBuilt(f: Fixture): Promise<void> {
355
349
  return built;
356
350
  }
357
351
 
358
- /** Builds the fixture `f`, unless this process or another has already. */
352
+ /** Builds fixture `f` if no process has built it yet in this test run. */
359
353
  async function build(f: Fixture): Promise<void> {
360
354
  if (f.parent !== undefined) await ensureBuilt(f.parent);
361
355
  const scopes = requireScopes();
362
356
  const failed = (reason: string, cause?: unknown) =>
363
- new Error(`cannot load the fixture ${JSON.stringify(f.name)}: ${reason}`, {
357
+ new Error(`failed to load fixture ${JSON.stringify(f.name)}: ${reason}`, {
364
358
  cause,
365
359
  });
366
360
  let native;
@@ -377,7 +371,7 @@ async function build(f: Fixture): Promise<void> {
377
371
  await runIn(scope, async () => setups.get(f)!());
378
372
  } catch (err) {
379
373
  const error = failed(
380
- `its setup failed: ${err instanceof Error ? err.message : String(err)}`,
374
+ `setup failed: ${err instanceof Error ? err.message : String(err)}`,
381
375
  err,
382
376
  );
383
377
  scopes.failFixture(f.name, error.message);
@@ -1,18 +1,15 @@
1
1
  /**
2
- * A system test's browser, driven with Playwright.
2
+ * Playwright adapter for driving Encore's managed system-test browser.
3
3
  *
4
- * {@link connect} connects Playwright to the browser Encore runs for the
5
- * current test (see `system.browser` in `@encore.dev/sdk/testing`): a page
6
- * reaches the app where `encore run` has it, in the test session; its
7
- * requests are the test's; and what it shows is recorded. Each call the
8
- * test makes on a page or a locator is reported as it is written, with
9
- * where it is, so that the recording and the test's telemetry say which
10
- * statement had the browser do what.
4
+ * {@link connect} attaches Playwright to the Chromium instance Encore starts
5
+ * for the current test (see `system.browser` in `@encore.dev/sdk/testing`).
6
+ * Requests from pages route to the test session under your test's isolated
7
+ * data, pages are recorded, and every page or locator call is automatically
8
+ * tracked with its call site in test telemetry and recordings.
11
9
  *
12
- * Playwright is the test's own, passed in: `playwright` or
13
- * `playwright-core`. It drives Encore's browser over the DevTools Protocol
14
- * (`connectOverCDP`), so it needs no browser of its own installed, and it
15
- * is Chromium that is driven.
10
+ * Pass your own `playwright` or `playwright-core` `chromium` module; because
11
+ * Playwright connects over CDP (`connectOverCDP`), no separate Playwright
12
+ * browser download is required.
16
13
  *
17
14
  * @example
18
15
  * // With any test runner:
@@ -28,7 +25,7 @@
28
25
  * });
29
26
  *
30
27
  * @example
31
- * // With Playwright Test, which is its own runner:
28
+ * // With Playwright Test:
32
29
  * import {test as base, expect} from "@playwright/test";
33
30
  * import {extend} from "@encore.dev/sdk/testing/playwright";
34
31
  *
@@ -48,7 +45,7 @@ import {Steps, wrap} from '../internal/testing/steps';
48
45
  import {setup} from './mod';
49
46
  import {browser as testBrowser, type BrowserOptions} from './system';
50
47
 
51
- /** Playwright's `chromium`, as much of it as {@link connect} uses. */
48
+ /** Minimal subset of Playwright's `chromium` required by {@link connect}. */
52
49
  export interface Chromium<B> {
53
50
  connectOverCDP(
54
51
  endpointURL: string,
@@ -56,13 +53,13 @@ export interface Chromium<B> {
56
53
  ): Promise<B>;
57
54
  }
58
55
 
59
- /** The options of {@link connect}. */
56
+ /** Options for {@link connect}. */
60
57
  export interface ConnectOptions extends BrowserOptions {
61
- /** More of Playwright's own options for `connectOverCDP`, such as `slowMo`. */
58
+ /** Additional Playwright options passed to `connectOverCDP` (e.g. `slowMo`). */
62
59
  connect?: Record<string, unknown>;
63
60
  }
64
61
 
65
- /** What {@link connect} uses of a Playwright browser. */
62
+ /** Minimal subset of a connected Playwright browser used internally. */
66
63
  interface Connected {
67
64
  newBrowserCDPSession(): Promise<{
68
65
  send(method: string, params?: Record<string, unknown>): Promise<unknown>;
@@ -70,9 +67,9 @@ interface Connected {
70
67
  }
71
68
 
72
69
  /**
73
- * Connects `chromium` to the current test's browser, starting it if the
74
- * test has none yet, and returns Playwright's `Browser`. The browser ends
75
- * with the test: there is nothing to close.
70
+ * Connects Playwright's `chromium` to the current test's Encore browser
71
+ * (starting it if needed) and returns the Playwright `Browser`. The browser is
72
+ * automatically closed when the test ends.
76
73
  */
77
74
  export async function connect<B>(
78
75
  chromium: Chromium<B>,
@@ -91,31 +88,30 @@ export async function connect<B>(
91
88
  }
92
89
 
93
90
  /**
94
- * The URL a page of the current test's browser reaches the gateway or
95
- * frontend `name` at: the one `encore run` has it at, which the frontend is
96
- * written against.
91
+ * Returns the URL a page in the current test's browser uses to reach gateway
92
+ * or frontend `name` (matching the local `encore run` origin).
97
93
  */
98
94
  export async function url(name: string): Promise<string> {
99
95
  return (await testBrowser()).url(name);
100
96
  }
101
97
 
102
- /** What {@link extend} adds to Playwright Test's options. */
98
+ /** Extra Playwright Test options added by {@link extend}. */
103
99
  export interface Options {
104
100
  /**
105
- * The frontend whose address is the tests' `baseURL`, by name: `goto("/")`
106
- * then opens it. Without it the `baseURL` is the config's.
101
+ * Name of the frontend to use as `baseURL` so `page.goto("/")` opens it.
102
+ * Falls back to Playwright's configured `baseURL` when unset.
107
103
  */
108
104
  frontend: string | undefined;
109
- /** How each test wants its browser: see `system.browser`. */
105
+ /** Encore browser options for each test (see `system.browser`). */
110
106
  encoreBrowser: BrowserOptions;
111
107
  }
112
108
 
113
- /** Playwright Test's `test`, as much of it as {@link extend} uses. */
109
+ /** Minimal subset of Playwright Test's `test` object required by {@link extend}. */
114
110
  interface Extendable {
115
111
  extend(fixtures: Record<string, unknown>): unknown;
116
112
  }
117
113
 
118
- /** A test, as Playwright Test describes it to a fixture. */
114
+ /** Minimal subset of Playwright Test's `TestInfo` used internally. */
119
115
  interface TestInfo {
120
116
  titlePath: string[];
121
117
  status?: string;
@@ -128,8 +124,7 @@ interface TestInfo {
128
124
 
129
125
  type Hook = (ctx: {name: string; passed?: boolean}) => unknown;
130
126
 
131
- /** The hooks that begin and end a test's scope, kept for the fixture to
132
- * call: Playwright Test has no hooks of its own outside a test file. */
127
+ /** Captures Encore's per-test scope hooks for Playwright Test fixtures. */
133
128
  let hooks: {before: Hook; after: Hook} | undefined;
134
129
 
135
130
  function scopeHooks(): {before: Hook; after: Hook} | undefined {
@@ -150,13 +145,13 @@ function scopeHooks(): {before: Hook; after: Hook} | undefined {
150
145
  }
151
146
 
152
147
  /**
153
- * Playwright Test's `test`, made a system test's: each test runs as a test
154
- * of the suite, as under any runner's adapter, and its `context` and `page`
155
- * are of the browser Encore runs for it. Playwright launches no browser.
148
+ * Extends Playwright Test's `test` so each test runs in an Encore system test
149
+ * scope with `context` and `page` connected to Encore's managed browser
150
+ * (Playwright does not launch its own browser).
156
151
  *
157
- * It adds two options, set with `test.use`: {@link Options.frontend} and
158
- * {@link Options.encoreBrowser}. A test that failed has its recording
159
- * attached to its report.
152
+ * Adds {@link Options.frontend} and {@link Options.encoreBrowser} for use with
153
+ * `test.use(...)`, and attaches the HTML recording to Playwright's report when
154
+ * a test fails.
160
155
  *
161
156
  * ```ts
162
157
  * const test = extend(base);