@encore.dev/sdk 2.0.0-beta.8 → 2.0.0-beta.9

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 (186) hide show
  1. package/README.md +54 -0
  2. package/app_meta.test.ts +25 -5
  3. package/app_meta.ts +43 -12
  4. package/config/config.test.ts +8 -12
  5. package/config/config.ts +16 -12
  6. package/config/secrets.ts +19 -10
  7. package/config/value.ts +13 -17
  8. package/cron/mod.test.ts +52 -0
  9. package/cron/mod.ts +9 -2
  10. package/dist/app_meta.d.ts +37 -12
  11. package/dist/app_meta.d.ts.map +1 -1
  12. package/dist/app_meta.js +1 -0
  13. package/dist/app_meta.js.map +1 -1
  14. package/dist/config/config.d.ts +16 -12
  15. package/dist/config/config.d.ts.map +1 -1
  16. package/dist/config/config.js +5 -2
  17. package/dist/config/config.js.map +1 -1
  18. package/dist/config/secrets.d.ts +19 -10
  19. package/dist/config/secrets.d.ts.map +1 -1
  20. package/dist/config/secrets.js +19 -10
  21. package/dist/config/secrets.js.map +1 -1
  22. package/dist/config/value.d.ts +7 -5
  23. package/dist/config/value.d.ts.map +1 -1
  24. package/dist/config/value.js +10 -7
  25. package/dist/config/value.js.map +1 -1
  26. package/dist/cron/mod.d.ts +5 -1
  27. package/dist/cron/mod.d.ts.map +1 -1
  28. package/dist/cron/mod.js +8 -1
  29. package/dist/cron/mod.js.map +1 -1
  30. package/dist/frontends/mod.d.ts +39 -0
  31. package/dist/frontends/mod.d.ts.map +1 -0
  32. package/dist/frontends/mod.js +47 -0
  33. package/dist/frontends/mod.js.map +1 -0
  34. package/dist/gateway/mod.d.ts +3 -2
  35. package/dist/gateway/mod.d.ts.map +1 -1
  36. package/dist/gateway/mod.js +3 -2
  37. package/dist/gateway/mod.js.map +1 -1
  38. package/dist/gateways/mod.d.ts +42 -0
  39. package/dist/gateways/mod.d.ts.map +1 -0
  40. package/dist/gateways/mod.js +55 -0
  41. package/dist/gateways/mod.js.map +1 -0
  42. package/dist/internal/fetch.d.ts +4 -0
  43. package/dist/internal/fetch.d.ts.map +1 -1
  44. package/dist/internal/fetch.js +12 -1
  45. package/dist/internal/fetch.js.map +1 -1
  46. package/dist/internal/otel/http.d.ts +11 -0
  47. package/dist/internal/otel/http.d.ts.map +1 -0
  48. package/dist/internal/otel/http.js +64 -0
  49. package/dist/internal/otel/http.js.map +1 -0
  50. package/dist/internal/otel/index.d.ts +1 -1
  51. package/dist/internal/otel/index.d.ts.map +1 -1
  52. package/dist/internal/otel/index.js +1 -1
  53. package/dist/internal/otel/index.js.map +1 -1
  54. package/dist/internal/otel/provider.d.ts +3 -0
  55. package/dist/internal/otel/provider.d.ts.map +1 -1
  56. package/dist/internal/otel/provider.js +3 -0
  57. package/dist/internal/otel/provider.js.map +1 -1
  58. package/dist/internal/otel/span.d.ts +11 -1
  59. package/dist/internal/otel/span.d.ts.map +1 -1
  60. package/dist/internal/otel/span.js +25 -2
  61. package/dist/internal/otel/span.js.map +1 -1
  62. package/dist/internal/reqfields.d.ts +26 -6
  63. package/dist/internal/reqfields.d.ts.map +1 -1
  64. package/dist/internal/reqfields.js +44 -7
  65. package/dist/internal/reqfields.js.map +1 -1
  66. package/dist/internal/runtime/napi/napi.cjs +36 -4
  67. package/dist/internal/runtime/napi/napi.d.cts +99 -4
  68. package/dist/internal/testing/carry.d.ts +49 -0
  69. package/dist/internal/testing/carry.d.ts.map +1 -0
  70. package/dist/internal/testing/carry.js +77 -0
  71. package/dist/internal/testing/carry.js.map +1 -0
  72. package/dist/internal/testing/scope.d.ts +34 -6
  73. package/dist/internal/testing/scope.d.ts.map +1 -1
  74. package/dist/internal/testing/scope.js +103 -5
  75. package/dist/internal/testing/scope.js.map +1 -1
  76. package/dist/internal/testing/steps.d.ts +55 -0
  77. package/dist/internal/testing/steps.d.ts.map +1 -0
  78. package/dist/internal/testing/steps.js +295 -0
  79. package/dist/internal/testing/steps.js.map +1 -0
  80. package/dist/internal/testing/telemetry.d.ts +305 -0
  81. package/dist/internal/testing/telemetry.d.ts.map +1 -0
  82. package/dist/internal/testing/telemetry.js +188 -0
  83. package/dist/internal/testing/telemetry.js.map +1 -0
  84. package/dist/mod.d.ts +1 -1
  85. package/dist/mod.d.ts.map +1 -1
  86. package/dist/mod.js.map +1 -1
  87. package/dist/pubsub/mod.d.ts +1 -1
  88. package/dist/pubsub/mod.d.ts.map +1 -1
  89. package/dist/pubsub/subscription.d.ts +6 -5
  90. package/dist/pubsub/subscription.d.ts.map +1 -1
  91. package/dist/pubsub/subscription.js.map +1 -1
  92. package/dist/pubsub/topic.d.ts +14 -8
  93. package/dist/pubsub/topic.d.ts.map +1 -1
  94. package/dist/pubsub/topic.js +36 -22
  95. package/dist/pubsub/topic.js.map +1 -1
  96. package/dist/reqctx/mod.d.ts +1 -18
  97. package/dist/reqctx/mod.d.ts.map +1 -1
  98. package/dist/reqctx/mod.js +1 -17
  99. package/dist/reqctx/mod.js.map +1 -1
  100. package/dist/services/index.d.ts +2 -1
  101. package/dist/services/index.d.ts.map +1 -1
  102. package/dist/services/index.js +2 -1
  103. package/dist/services/index.js.map +1 -1
  104. package/dist/storage/objects/bucket.d.ts +2 -1
  105. package/dist/storage/objects/bucket.d.ts.map +1 -1
  106. package/dist/storage/objects/bucket.js +2 -1
  107. package/dist/storage/objects/bucket.js.map +1 -1
  108. package/dist/storage/postgres/database.d.ts +5 -1
  109. package/dist/storage/postgres/database.d.ts.map +1 -1
  110. package/dist/storage/postgres/database.js +7 -3
  111. package/dist/storage/postgres/database.js.map +1 -1
  112. package/dist/storage/redis/commands.d.ts +5 -5
  113. package/dist/storage/redis/commands.d.ts.map +1 -1
  114. package/dist/storage/redis/commands.js +5 -5
  115. package/dist/storage/redis/commands.js.map +1 -1
  116. package/dist/testing/mod.d.ts +28 -5
  117. package/dist/testing/mod.d.ts.map +1 -1
  118. package/dist/testing/mod.js +52 -8
  119. package/dist/testing/mod.js.map +1 -1
  120. package/dist/testing/playwright.d.ts +93 -0
  121. package/dist/testing/playwright.d.ts.map +1 -0
  122. package/dist/testing/playwright.js +164 -0
  123. package/dist/testing/playwright.js.map +1 -0
  124. package/dist/testing/puppeteer.d.ts +36 -0
  125. package/dist/testing/puppeteer.d.ts.map +1 -0
  126. package/dist/testing/puppeteer.js +70 -0
  127. package/dist/testing/puppeteer.js.map +1 -0
  128. package/dist/testing/system.d.ts +181 -0
  129. package/dist/testing/system.d.ts.map +1 -0
  130. package/dist/testing/system.js +255 -0
  131. package/dist/testing/system.js.map +1 -0
  132. package/dist/tracing/http.d.ts +11 -0
  133. package/dist/tracing/http.d.ts.map +1 -0
  134. package/dist/tracing/http.js +34 -0
  135. package/dist/tracing/http.js.map +1 -0
  136. package/dist/tracing/mod.d.ts +1 -0
  137. package/dist/tracing/mod.d.ts.map +1 -1
  138. package/dist/tracing/mod.js +1 -0
  139. package/dist/tracing/mod.js.map +1 -1
  140. package/dist/tracing/node.d.ts +10 -0
  141. package/dist/tracing/node.d.ts.map +1 -0
  142. package/dist/tracing/node.js +142 -0
  143. package/dist/tracing/node.js.map +1 -0
  144. package/dist/tsconfig.build.tsbuildinfo +1 -1
  145. package/frontends/mod.test.ts +51 -0
  146. package/frontends/mod.ts +50 -0
  147. package/gateway/mod.ts +3 -2
  148. package/gateways/mod.ts +60 -0
  149. package/internal/fetch.ts +12 -1
  150. package/internal/otel/http.ts +91 -0
  151. package/internal/otel/index.ts +1 -1
  152. package/internal/otel/provider.ts +3 -0
  153. package/internal/otel/span.ts +25 -2
  154. package/internal/reqfields.ts +43 -10
  155. package/internal/runtime/napi/napi.cjs +36 -4
  156. package/internal/runtime/napi/napi.d.cts +99 -4
  157. package/internal/testing/carry.test.ts +242 -0
  158. package/internal/testing/carry.ts +101 -0
  159. package/internal/testing/scope.ts +106 -7
  160. package/internal/testing/steps.test.ts +152 -0
  161. package/internal/testing/steps.ts +342 -0
  162. package/internal/testing/telemetry.test.ts +210 -0
  163. package/internal/testing/telemetry.ts +466 -0
  164. package/mod.ts +1 -0
  165. package/package.json +26 -1
  166. package/pubsub/mod.ts +6 -1
  167. package/pubsub/pubsub.test.ts +1 -0
  168. package/pubsub/subscription.ts +6 -5
  169. package/pubsub/topic.ts +45 -21
  170. package/reqctx/mod.ts +1 -18
  171. package/reqctx/reqctx.test.ts +23 -0
  172. package/scripts/postprocess-docs.test.ts +11 -1
  173. package/services/index.ts +2 -1
  174. package/storage/objects/bucket.ts +2 -1
  175. package/storage/postgres/database.ts +7 -3
  176. package/storage/redis/commands.ts +5 -5
  177. package/testing/mod.ts +63 -7
  178. package/testing/playwright.ts +247 -0
  179. package/testing/puppeteer.ts +101 -0
  180. package/testing/system.test.ts +108 -0
  181. package/testing/system.ts +373 -0
  182. package/tracing/http.ts +49 -0
  183. package/tracing/mod.ts +1 -0
  184. package/tracing/native.test.ts +101 -4
  185. package/tracing/node.ts +176 -0
  186. package/tracing/tracing.test.ts +660 -24
package/pubsub/topic.ts CHANGED
@@ -1,7 +1,13 @@
1
- import {activeCtxBytes} from '../internal/otel/index';
1
+ import {contextCtxBytes} from '../internal/otel/index';
2
2
  import {messageAttributes} from '../internal/reqfields';
3
3
  import * as runtime from '../internal/runtime/mod';
4
- import {currentScope, testMode, type Scope} from '../internal/testing/scope';
4
+ import {outgoingContext} from '../internal/testing/carry';
5
+ import {
6
+ currentScope,
7
+ systemTest,
8
+ testMode,
9
+ type Scope,
10
+ } from '../internal/testing/scope';
5
11
  import {encodePayload, rawPayloads} from './encoding';
6
12
  import {
7
13
  OrderedPublisher,
@@ -76,7 +82,9 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
76
82
  *
77
83
  * The message type cannot be inferred, so it must be given:
78
84
  *
79
- * const signups = Topic.named<SignupEvent>("signups");
85
+ * ```ts
86
+ * const signups = Topic.named<SignupEvent>("signups");
87
+ * ```
80
88
  *
81
89
  * The name must be a string literal, so Encore can resolve it statically.
82
90
  */
@@ -99,11 +107,12 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
99
107
  msg: Msg,
100
108
  attributes?: Record<string, string>,
101
109
  ): Promise<string> {
102
- const source = activeCtxBytes();
110
+ const ctx = outgoingContext();
111
+ const source = contextCtxBytes(ctx);
103
112
  const payload = encodePayload(msg);
104
113
  return this.sender
105
114
  .impl()
106
- .publish(payload, messageAttributes(attributes), source);
115
+ .publish(payload, messageAttributes(attributes, ctx), source);
107
116
  }
108
117
 
109
118
  /**
@@ -120,11 +129,12 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
120
129
  messages: readonly BatchMessage<Msg>[],
121
130
  ): Promise<string[]> {
122
131
  checkBatchSize(messages.length);
123
- const source = activeCtxBytes();
132
+ const ctx = outgoingContext();
133
+ const source = contextCtxBytes(ctx);
124
134
  const entries = messages.map(m => ({
125
135
  data: encodePayload(m.data),
126
136
  // Each message carries the Request Context fields, as one sent alone.
127
- attributes: messageAttributes(m.attributes),
137
+ attributes: messageAttributes(m.attributes, ctx),
128
138
  }));
129
139
  return publishBatch(this.sender.impl(), entries, source);
130
140
  }
@@ -139,13 +149,15 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
139
149
  * key in the order they were published. Each message is published with its
140
150
  * key:
141
151
  *
142
- * const userEvents = new OrderedTopic<UserEvent>("user-events", {
143
- * deliveryGuarantee: "at-least-once",
144
- * });
152
+ * ```ts
153
+ * const userEvents = new OrderedTopic<UserEvent>("user-events", {
154
+ * deliveryGuarantee: "at-least-once",
155
+ * });
145
156
  *
146
- * await userEvents.publish({ action: "login" }, "user-1"); // Delivered before the logout
147
- * await userEvents.publish({ action: "login" }, "user-2"); // Delivered at any time: a different key
148
- * await userEvents.publish({ action: "logout" }, "user-1"); // Delivered after the first login
157
+ * await userEvents.publish({ action: "login" }, "user-1"); // Delivered before the logout
158
+ * await userEvents.publish({ action: "login" }, "user-2"); // Delivered at any time: a different key
159
+ * await userEvents.publish({ action: "logout" }, "user-1"); // Delivered after the first login
160
+ * ```
149
161
  *
150
162
  * A message whose handler fails is retried before the next one with its key
151
163
  * is delivered, so depending on the retry policy a backlog can build up
@@ -187,7 +199,9 @@ export class OrderedTopic<Msg>
187
199
  /**
188
200
  * Reference an existing ordered topic by name. See `Topic.named`.
189
201
  *
190
- * const changes = OrderedTopic.named<ChangeEvent>("changes");
202
+ * ```ts
203
+ * const changes = OrderedTopic.named<ChangeEvent>("changes");
204
+ * ```
191
205
  */
192
206
  static named<Msg>(name: string): OrderedTopic<Msg> {
193
207
  // Configured where it is declared, so it has no `cfg` of its own.
@@ -214,11 +228,17 @@ export class OrderedTopic<Msg>
214
228
  if (typeof orderingKey !== 'string' || orderingKey.length === 0) {
215
229
  throw new TypeError('orderingKey must be a non-empty string');
216
230
  }
217
- const source = activeCtxBytes();
231
+ const ctx = outgoingContext();
232
+ const source = contextCtxBytes(ctx);
218
233
  const payload = encodePayload(msg);
219
234
  return this.sender
220
235
  .impl()
221
- .publish(payload, messageAttributes(attributes), source, orderingKey);
236
+ .publish(
237
+ payload,
238
+ messageAttributes(attributes, ctx),
239
+ source,
240
+ orderingKey,
241
+ );
222
242
  }
223
243
 
224
244
  /**
@@ -245,11 +265,12 @@ export class OrderedTopic<Msg>
245
265
  );
246
266
  }
247
267
  });
248
- const source = activeCtxBytes();
268
+ const ctx = outgoingContext();
269
+ const source = contextCtxBytes(ctx);
249
270
  const entries = messages.map(m => ({
250
271
  data: encodePayload(m.data),
251
272
  // Each message carries the Request Context fields, as one sent alone.
252
- attributes: messageAttributes(m.attributes),
273
+ attributes: messageAttributes(m.attributes, ctx),
253
274
  orderingKey: m.orderingKey,
254
275
  }));
255
276
  return publishBatch(this.sender.impl(), entries, source);
@@ -266,7 +287,8 @@ type Publish = Pick<runtime.PubSubTopic, 'publish' | 'publishBatch'>;
266
287
  /**
267
288
  * Where a topic's messages go: to the runtime's topic, or, in a test, to the
268
289
  * test's record of what it published, sent nowhere, for it to read back with
269
- * `published` or hand to a subscription with `deliver`.
290
+ * `published` or hand to a subscription with `deliver`. A system test's are
291
+ * sent: the subscriptions handle them, in the services that declare them.
270
292
  */
271
293
  class Sender {
272
294
  /** The runtime's topic, made on first use in test mode. */
@@ -277,8 +299,10 @@ class Sender {
277
299
  }
278
300
 
279
301
  impl(): Publish {
280
- const scope = currentScope();
281
- if (scope !== null) return recorder(scope, this.name);
302
+ if (systemTest === null) {
303
+ const scope = currentScope();
304
+ if (scope !== null) return recorder(scope, this.name);
305
+ }
282
306
  return (this.topic ??= runtime.RT.pubsubTopic(this.name));
283
307
  }
284
308
  }
package/reqctx/mod.ts CHANGED
@@ -249,24 +249,7 @@ export function inject(headers: reqfields.HeaderSink, ctx?: Context): void {
249
249
  reqfields.toHeaders(headers, ctx ?? activeContext());
250
250
  }
251
251
 
252
- /**
253
- * An OpenTelemetry propagator that extracts the fields a request carries into
254
- * its context, and injects none. Compose it into the app's global propagator,
255
- * so a server instrumented for tracing has them:
256
- *
257
- * ```ts
258
- * propagation.setGlobalPropagator(
259
- * new CompositePropagator({
260
- * propagators: [new W3CTraceContextPropagator(), new reqctx.Propagator()],
261
- * }),
262
- * );
263
- * ```
264
- *
265
- * It never injects, so an instrumented client never sends fields to a third
266
- * party: send them to the app's services with {@link inject}.
267
- */
268
- export const Propagator = reqfields.Propagator;
269
- export type Propagator = reqfields.Propagator;
252
+ export {Propagator} from '../internal/reqfields';
270
253
 
271
254
  function messageOf(err: unknown): string {
272
255
  return err instanceof Error ? err.message : String(err);
@@ -190,4 +190,27 @@ describe('the runtime config', () => {
190
190
  reqfields.setDeclaredLookup(() => null);
191
191
  }
192
192
  });
193
+
194
+ test('lists the test a request is made for, which is `encore.test.id`', () => {
195
+ // As under test, where Encore declares the field itself.
196
+ reqfields.setDeclaredLookup(name =>
197
+ name === 'encore-test' ? {pubsub: true, trace: true} : null,
198
+ );
199
+ try {
200
+ const of = (value: string) =>
201
+ reqfields.spanAttributes(
202
+ reqfields.withFields({'encore-test': value}, ROOT_CONTEXT),
203
+ );
204
+ // The id as it is, not its JSON as another field's is.
205
+ expect(of(wire('00f067aa0ba902b7'))).toEqual({
206
+ 'encore.test.id': '00f067aa0ba902b7',
207
+ });
208
+ // What is no JSON string is no test's id.
209
+ expect(of(wire(7))).toEqual({});
210
+ expect(of(wire({id: 'x'}))).toEqual({});
211
+ expect(of(Buffer.from('not json').toString('base64url'))).toEqual({});
212
+ } finally {
213
+ reqfields.setDeclaredLookup(() => null);
214
+ }
215
+ });
193
216
  });
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  copyFileSync,
3
+ cpSync,
3
4
  mkdirSync,
4
5
  mkdtempSync,
5
6
  readFileSync,
@@ -22,11 +23,17 @@ describe('postprocess-docs', () => {
22
23
  mkdirSync(sdkScripts, {recursive: true});
23
24
  mkdirSync(docs, {recursive: true});
24
25
  copyFileSync(script, join(sdkScripts, 'postprocess-docs.mjs'));
26
+ cpSync(new URL('./pages/', import.meta.url), join(sdkScripts, 'pages'), {
27
+ recursive: true,
28
+ });
25
29
  writeFileSync(
26
30
  join(docs, 'tracing.mod.mdx'),
27
31
  existing ? '---\nlang: ts\n---\n# Tracing\n' : '# Tracing\n',
28
32
  );
29
- writeFileSync(join(docs, 'other.mdx'), '[Tracing](tracing.mod.mdx)\n');
33
+ writeFileSync(
34
+ join(docs, 'other.mdx'),
35
+ '[Tracing](tracing.mod.mdx)\n[CronJob](cron.mod.mdx#cronjob)\n',
36
+ );
30
37
 
31
38
  try {
32
39
  const result = Bun.spawnSync({
@@ -40,6 +47,9 @@ describe('postprocess-docs', () => {
40
47
  expect(readFileSync(join(docs, 'other.mdx'), 'utf8')).toContain(
41
48
  '[Tracing](/ts/runtime/tracing)',
42
49
  );
50
+ expect(readFileSync(join(docs, 'other.mdx'), 'utf8')).toContain(
51
+ '[CronJob](/ts/runtime/cron#cronjob)',
52
+ );
43
53
  } finally {
44
54
  rmSync(root, {recursive: true, force: true});
45
55
  }
package/services/index.ts CHANGED
@@ -90,7 +90,8 @@
90
90
  * No service runs under `encore test`. Every service still has a base URL
91
91
  * there, but a call to it is answered with `501 Not Implemented` and an error
92
92
  * saying which service and path the test called: mock the call in the test
93
- * instead.
93
+ * instead. A system test's calls do reach the services, which start when
94
+ * first called (`system` of `@encore.dev/sdk/testing`).
94
95
  *
95
96
  * @module
96
97
  */
@@ -172,7 +172,8 @@ export class Bucket
172
172
  if (!testMode) this.own = runtime.RT.bucket(name);
173
173
  }
174
174
 
175
- /** The native bucket: in a test, the test's instance of it. */
175
+ /** The native bucket: in a test, the test's instance of it.
176
+ * @internal */
176
177
  get impl(): runtime.Bucket {
177
178
  return (
178
179
  currentScope()?.bucket(this.bucketName) ??
@@ -75,7 +75,8 @@ export interface ExecResult {
75
75
  /** Base class containing shared query functionality */
76
76
  class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
77
77
  /** The native handle, or how to find it for each call: a database's is
78
- * the current test's instance of it, in test mode. */
78
+ * the current test's instance of it, in test mode.
79
+ * @internal */
79
80
  constructor(private readonly handle: I | (() => I)) {}
80
81
 
81
82
  /** The native handle statements run on. */
@@ -365,6 +366,7 @@ export interface ReadOptions {
365
366
  * untrusted code, the read-only pool's database role is what refuses writes.
366
367
  */
367
368
  export class DatabaseRef extends Queryable<runtime.SQLDatabase> {
369
+ /** @internal */
368
370
  constructor(impl: runtime.SQLDatabase | (() => runtime.SQLDatabase)) {
369
371
  super(impl);
370
372
  }
@@ -388,7 +390,7 @@ export class DatabaseRef extends Queryable<runtime.SQLDatabase> {
388
390
  * See {@link Database.acquire}.
389
391
  */
390
392
  async acquire(): Promise<Connection> {
391
- return new Connection(await native(this.impl.acquire()));
393
+ return new Connection(await native(this.impl.acquire(activeCtxBytes())));
392
394
  }
393
395
 
394
396
  /**
@@ -481,7 +483,7 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
481
483
  * @returns a new connection to the database
482
484
  */
483
485
  async acquire(): Promise<Connection> {
484
- const impl = await native(this.impl.acquire());
486
+ const impl = await native(this.impl.acquire(activeCtxBytes()));
485
487
  return new Connection(impl);
486
488
  }
487
489
 
@@ -538,6 +540,7 @@ export class Transaction
538
540
  {
539
541
  private done: boolean = false;
540
542
 
543
+ /** @internal */
541
544
  constructor(impl: runtime.Transaction) {
542
545
  super(impl);
543
546
  }
@@ -578,6 +581,7 @@ export class Connection
578
581
  extends Queryable<runtime.SQLConn>
579
582
  implements AsyncDisposable
580
583
  {
584
+ /** @internal */
581
585
  constructor(impl: runtime.SQLConn) {
582
586
  super(impl);
583
587
  }
@@ -613,14 +613,14 @@ const list = {
613
613
  cmd('BLPOP').keys(keyList(keys)).arg(timeout),
614
614
  as.popped,
615
615
  ),
616
- /** {@link ListCommands.blpop}, from the end of the list. */
616
+ /** `blpop`, from the end of the list. */
617
617
  brpop: writeBlocking(
618
618
  (keys: string | string[], timeout: number) =>
619
619
  cmd('BRPOP').keys(keyList(keys)).arg(timeout),
620
620
  as.popped,
621
621
  ),
622
622
  /**
623
- * {@link ListCommands.lmove}, waiting up to `timeout` seconds for `source`
623
+ * `lmove`, waiting up to `timeout` seconds for `source`
624
624
  * to have an element: 0 waits for ever. A nil reply once the timeout passes.
625
625
  */
626
626
  blmove: writeBlocking(
@@ -906,7 +906,7 @@ const sortedSet = {
906
906
  cmd('BZPOPMIN').keys(keyList(keys)).arg(timeout),
907
907
  as.poppedScored,
908
908
  ),
909
- /** {@link SortedSetCommands.bzpopmin}, the highest scored member. */
909
+ /** `bzpopmin`, the highest scored member. */
910
910
  bzpopmax: writeBlocking(
911
911
  (keys: string | string[], timeout: number) =>
912
912
  cmd('BZPOPMAX').keys(keyList(keys)).arg(timeout),
@@ -1186,7 +1186,7 @@ const stream = {
1186
1186
  ),
1187
1187
 
1188
1188
  /**
1189
- * {@link StreamCommands.xread}, waiting up to `blockMs` milliseconds for an
1189
+ * `xread`, waiting up to `blockMs` milliseconds for an
1190
1190
  * entry if there is none yet: 0 waits for ever. With a `count`, at most that
1191
1191
  * many per stream. A nil reply once the wait passes.
1192
1192
  */
@@ -1202,7 +1202,7 @@ const stream = {
1202
1202
  as.reply,
1203
1203
  ),
1204
1204
  /**
1205
- * {@link StreamCommands.xreadGroup}, waiting up to `blockMs` milliseconds
1205
+ * `xreadGroup`, waiting up to `blockMs` milliseconds
1206
1206
  * for an entry if there is none yet: 0 waits for ever. A nil reply once the
1207
1207
  * wait passes.
1208
1208
  */
package/testing/mod.ts CHANGED
@@ -19,6 +19,10 @@
19
19
  * to read back with {@link published} and hand to a subscription with
20
20
  * {@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
24
+ * {@link system}.
25
+ *
22
26
  * @example
23
27
  * import {deliver, published} from "@encore.dev/sdk/testing";
24
28
  *
@@ -45,12 +49,14 @@ import {
45
49
  runIn,
46
50
  Scope,
47
51
  setupHooks,
52
+ systemTest,
48
53
  testMode,
49
54
  testName,
50
55
  type Hooks,
51
56
  } from '../internal/testing/scope';
52
57
 
53
58
  export type {Hooks} from '../internal/testing/scope';
59
+ export * as system from './system';
54
60
 
55
61
  /** The options of {@link setup} and {@link scoped}. */
56
62
  export interface ScopeOptions {
@@ -88,11 +94,16 @@ export function setup(hooks: Hooks, options?: ScopeOptions): void {
88
94
  * Wraps a test body to run in a scope of its own: for concurrent tests,
89
95
  * which the runner's hooks cannot tell apart, or a runner without hooks.
90
96
  *
91
- * test.concurrent("x", scoped(async () => { ... }));
97
+ * ```ts
98
+ * test.concurrent("x", scoped(async () => { ... }));
99
+ * ```
92
100
  *
93
101
  * The test starts from `options.fixture` if given, else from the fixture
94
102
  * {@link useFixture} chose for it, where the runner hands the test its
95
103
  * context (vitest, node:test). Outside test mode it runs `fn` as it is.
104
+ *
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}.
96
107
  */
97
108
  export function scoped<A extends unknown[], R>(
98
109
  fn: (...args: A) => Promise<R> | R,
@@ -103,15 +114,22 @@ export function scoped<A extends unknown[], R>(
103
114
  return async (...args: A): Promise<R> => {
104
115
  const scope = begin(testName(args[0]));
105
116
  if (scope === null) return fn(...args);
117
+ let failed = true;
106
118
  try {
107
- return await runIn(scope, async () => {
119
+ const result = await runIn(scope, async () => {
108
120
  const fixture =
109
121
  options?.fixture !== undefined ? options.fixture : chosen(args[0]);
110
122
  if (fixture !== undefined) await choose(scope, fixture);
111
123
  return fn(...args);
112
124
  });
125
+ failed = false;
126
+ return result;
113
127
  } finally {
114
- scope.end();
128
+ try {
129
+ await scope.ended(failed ? 'failed' : 'passed');
130
+ } finally {
131
+ scope.end();
132
+ }
115
133
  }
116
134
  };
117
135
  }
@@ -123,8 +141,16 @@ export function scoped<A extends unknown[], R>(
123
141
  *
124
142
  * Each message's `data` is what was published: decoded from JSON, or the
125
143
  * bytes published as bytes.
144
+ *
145
+ * Not in a system test, where what a test publishes is sent.
126
146
  */
127
147
  export function published<T>(topic: Topic<T> | OrderedTopic<T>): Message<T>[] {
148
+ if (systemTest !== null) {
149
+ 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`',
152
+ );
153
+ }
128
154
  const scope = requireScope();
129
155
  return scope.native.published(topic.name).map(msg => ({
130
156
  data: (scope.raw.has(msg.id)
@@ -155,10 +181,19 @@ export function published<T>(topic: Topic<T> | OrderedTopic<T>): Message<T>[] {
155
181
  * error. That message stays unhandled: the next call hands it over again,
156
182
  * its `deliveryAttempt` one more. Retry policies, ack deadlines and dead
157
183
  * letters play no part.
184
+ *
185
+ * Not in a system test, where the subscriptions run on their own: wait for
186
+ * them with `system.settled()`.
158
187
  */
159
188
  export async function deliver<T>(
160
189
  subscription: Subscription<T>,
161
190
  ): Promise<number> {
191
+ if (systemTest !== null) {
192
+ 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()`',
195
+ );
196
+ }
162
197
  const scope = requireScope();
163
198
  return Subscription.deliverIn(
164
199
  subscription as Subscription<unknown>,
@@ -170,8 +205,17 @@ export async function deliver<T>(
170
205
  /**
171
206
  * Gives `secret` a value for the current test, in place of the app's. No
172
207
  * other test sees it, and it is gone when the test ends.
208
+ *
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.
173
211
  */
174
212
  export function setSecret(secret: AnySecret, value: string | Uint8Array): void {
213
+ if (systemTest !== null) {
214
+ 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",
217
+ );
218
+ }
175
219
  requireScope().native.setSecret(secret.name, Buffer.from(value));
176
220
  }
177
221
 
@@ -180,8 +224,18 @@ export function setSecret(secret: AnySecret, value: string | Uint8Array): void {
180
224
  * calling the config returns in the test from then on. No other test sees
181
225
  * it, and it is gone when the test ends. Throws if the value does not
182
226
  * satisfy the config's schema.
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.
183
230
  */
184
231
  export function setConfig<T>(config: Config<T>, value: T): void {
232
+ if (systemTest !== null) {
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)',
237
+ );
238
+ }
185
239
  setTestValue(requireScope(), config, value);
186
240
  }
187
241
 
@@ -213,10 +267,12 @@ export interface FixtureOptions {
213
267
  * Declares a fixture: a state `setup` leaves the app's resources in,
214
268
  * built once per test run and copied for each test that starts from it.
215
269
  *
216
- * const seeded = fixture("users.seeded", async () => {
217
- * await db.exec`INSERT INTO users (name) VALUES ('ada'), ('grace')`;
218
- * });
219
- * useFixture(seeded);
270
+ * ```ts
271
+ * const seeded = fixture("users.seeded", async () => {
272
+ * await db.exec`INSERT INTO users (name) VALUES ('ada'), ('grace')`;
273
+ * });
274
+ * useFixture(seeded);
275
+ * ```
220
276
  *
221
277
  * The setup is ordinary app code: its resources are the fixture's while it
222
278
  * runs. It runs the first time a test loads the fixture, however many test