@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.
- package/README.md +54 -0
- package/app_meta.test.ts +25 -5
- package/app_meta.ts +43 -12
- package/config/config.test.ts +8 -12
- package/config/config.ts +16 -12
- package/config/secrets.ts +19 -10
- package/config/value.ts +13 -17
- package/cron/mod.test.ts +52 -0
- package/cron/mod.ts +9 -2
- package/dist/app_meta.d.ts +37 -12
- package/dist/app_meta.d.ts.map +1 -1
- package/dist/app_meta.js +1 -0
- package/dist/app_meta.js.map +1 -1
- package/dist/config/config.d.ts +16 -12
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +5 -2
- package/dist/config/config.js.map +1 -1
- package/dist/config/secrets.d.ts +19 -10
- package/dist/config/secrets.d.ts.map +1 -1
- package/dist/config/secrets.js +19 -10
- package/dist/config/secrets.js.map +1 -1
- package/dist/config/value.d.ts +7 -5
- package/dist/config/value.d.ts.map +1 -1
- package/dist/config/value.js +10 -7
- package/dist/config/value.js.map +1 -1
- package/dist/cron/mod.d.ts +5 -1
- package/dist/cron/mod.d.ts.map +1 -1
- package/dist/cron/mod.js +8 -1
- package/dist/cron/mod.js.map +1 -1
- package/dist/frontends/mod.d.ts +39 -0
- package/dist/frontends/mod.d.ts.map +1 -0
- package/dist/frontends/mod.js +47 -0
- package/dist/frontends/mod.js.map +1 -0
- package/dist/gateway/mod.d.ts +3 -2
- package/dist/gateway/mod.d.ts.map +1 -1
- package/dist/gateway/mod.js +3 -2
- package/dist/gateway/mod.js.map +1 -1
- package/dist/gateways/mod.d.ts +42 -0
- package/dist/gateways/mod.d.ts.map +1 -0
- package/dist/gateways/mod.js +55 -0
- package/dist/gateways/mod.js.map +1 -0
- package/dist/internal/fetch.d.ts +4 -0
- package/dist/internal/fetch.d.ts.map +1 -1
- package/dist/internal/fetch.js +12 -1
- package/dist/internal/fetch.js.map +1 -1
- package/dist/internal/otel/http.d.ts +11 -0
- package/dist/internal/otel/http.d.ts.map +1 -0
- package/dist/internal/otel/http.js +64 -0
- package/dist/internal/otel/http.js.map +1 -0
- package/dist/internal/otel/index.d.ts +1 -1
- package/dist/internal/otel/index.d.ts.map +1 -1
- package/dist/internal/otel/index.js +1 -1
- package/dist/internal/otel/index.js.map +1 -1
- package/dist/internal/otel/provider.d.ts +3 -0
- package/dist/internal/otel/provider.d.ts.map +1 -1
- package/dist/internal/otel/provider.js +3 -0
- package/dist/internal/otel/provider.js.map +1 -1
- package/dist/internal/otel/span.d.ts +11 -1
- package/dist/internal/otel/span.d.ts.map +1 -1
- package/dist/internal/otel/span.js +25 -2
- package/dist/internal/otel/span.js.map +1 -1
- package/dist/internal/reqfields.d.ts +26 -6
- package/dist/internal/reqfields.d.ts.map +1 -1
- package/dist/internal/reqfields.js +44 -7
- package/dist/internal/reqfields.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +36 -4
- package/dist/internal/runtime/napi/napi.d.cts +99 -4
- package/dist/internal/testing/carry.d.ts +49 -0
- package/dist/internal/testing/carry.d.ts.map +1 -0
- package/dist/internal/testing/carry.js +77 -0
- package/dist/internal/testing/carry.js.map +1 -0
- package/dist/internal/testing/scope.d.ts +34 -6
- package/dist/internal/testing/scope.d.ts.map +1 -1
- package/dist/internal/testing/scope.js +103 -5
- package/dist/internal/testing/scope.js.map +1 -1
- package/dist/internal/testing/steps.d.ts +55 -0
- package/dist/internal/testing/steps.d.ts.map +1 -0
- package/dist/internal/testing/steps.js +295 -0
- package/dist/internal/testing/steps.js.map +1 -0
- package/dist/internal/testing/telemetry.d.ts +305 -0
- package/dist/internal/testing/telemetry.d.ts.map +1 -0
- package/dist/internal/testing/telemetry.js +188 -0
- package/dist/internal/testing/telemetry.js.map +1 -0
- package/dist/mod.d.ts +1 -1
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js.map +1 -1
- package/dist/pubsub/mod.d.ts +1 -1
- package/dist/pubsub/mod.d.ts.map +1 -1
- package/dist/pubsub/subscription.d.ts +6 -5
- package/dist/pubsub/subscription.d.ts.map +1 -1
- package/dist/pubsub/subscription.js.map +1 -1
- package/dist/pubsub/topic.d.ts +14 -8
- package/dist/pubsub/topic.d.ts.map +1 -1
- package/dist/pubsub/topic.js +36 -22
- package/dist/pubsub/topic.js.map +1 -1
- package/dist/reqctx/mod.d.ts +1 -18
- package/dist/reqctx/mod.d.ts.map +1 -1
- package/dist/reqctx/mod.js +1 -17
- package/dist/reqctx/mod.js.map +1 -1
- package/dist/services/index.d.ts +2 -1
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +2 -1
- package/dist/services/index.js.map +1 -1
- package/dist/storage/objects/bucket.d.ts +2 -1
- package/dist/storage/objects/bucket.d.ts.map +1 -1
- package/dist/storage/objects/bucket.js +2 -1
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/postgres/database.d.ts +5 -1
- package/dist/storage/postgres/database.d.ts.map +1 -1
- package/dist/storage/postgres/database.js +7 -3
- package/dist/storage/postgres/database.js.map +1 -1
- package/dist/storage/redis/commands.d.ts +5 -5
- package/dist/storage/redis/commands.d.ts.map +1 -1
- package/dist/storage/redis/commands.js +5 -5
- package/dist/storage/redis/commands.js.map +1 -1
- package/dist/testing/mod.d.ts +28 -5
- package/dist/testing/mod.d.ts.map +1 -1
- package/dist/testing/mod.js +52 -8
- package/dist/testing/mod.js.map +1 -1
- package/dist/testing/playwright.d.ts +93 -0
- package/dist/testing/playwright.d.ts.map +1 -0
- package/dist/testing/playwright.js +164 -0
- package/dist/testing/playwright.js.map +1 -0
- package/dist/testing/puppeteer.d.ts +36 -0
- package/dist/testing/puppeteer.d.ts.map +1 -0
- package/dist/testing/puppeteer.js +70 -0
- package/dist/testing/puppeteer.js.map +1 -0
- package/dist/testing/system.d.ts +181 -0
- package/dist/testing/system.d.ts.map +1 -0
- package/dist/testing/system.js +255 -0
- package/dist/testing/system.js.map +1 -0
- package/dist/tracing/http.d.ts +11 -0
- package/dist/tracing/http.d.ts.map +1 -0
- package/dist/tracing/http.js +34 -0
- package/dist/tracing/http.js.map +1 -0
- package/dist/tracing/mod.d.ts +1 -0
- package/dist/tracing/mod.d.ts.map +1 -1
- package/dist/tracing/mod.js +1 -0
- package/dist/tracing/mod.js.map +1 -1
- package/dist/tracing/node.d.ts +10 -0
- package/dist/tracing/node.d.ts.map +1 -0
- package/dist/tracing/node.js +142 -0
- package/dist/tracing/node.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/frontends/mod.test.ts +51 -0
- package/frontends/mod.ts +50 -0
- package/gateway/mod.ts +3 -2
- package/gateways/mod.ts +60 -0
- package/internal/fetch.ts +12 -1
- package/internal/otel/http.ts +91 -0
- package/internal/otel/index.ts +1 -1
- package/internal/otel/provider.ts +3 -0
- package/internal/otel/span.ts +25 -2
- package/internal/reqfields.ts +43 -10
- package/internal/runtime/napi/napi.cjs +36 -4
- package/internal/runtime/napi/napi.d.cts +99 -4
- package/internal/testing/carry.test.ts +242 -0
- package/internal/testing/carry.ts +101 -0
- package/internal/testing/scope.ts +106 -7
- package/internal/testing/steps.test.ts +152 -0
- package/internal/testing/steps.ts +342 -0
- package/internal/testing/telemetry.test.ts +210 -0
- package/internal/testing/telemetry.ts +466 -0
- package/mod.ts +1 -0
- package/package.json +26 -1
- package/pubsub/mod.ts +6 -1
- package/pubsub/pubsub.test.ts +1 -0
- package/pubsub/subscription.ts +6 -5
- package/pubsub/topic.ts +45 -21
- package/reqctx/mod.ts +1 -18
- package/reqctx/reqctx.test.ts +23 -0
- package/scripts/postprocess-docs.test.ts +11 -1
- package/services/index.ts +2 -1
- package/storage/objects/bucket.ts +2 -1
- package/storage/postgres/database.ts +7 -3
- package/storage/redis/commands.ts +5 -5
- package/testing/mod.ts +63 -7
- package/testing/playwright.ts +247 -0
- package/testing/puppeteer.ts +101 -0
- package/testing/system.test.ts +108 -0
- package/testing/system.ts +373 -0
- package/tracing/http.ts +49 -0
- package/tracing/mod.ts +1 -0
- package/tracing/native.test.ts +101 -4
- package/tracing/node.ts +176 -0
- package/tracing/tracing.test.ts +660 -24
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {afterEach, describe, expect, test} from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
createTraceState,
|
|
4
|
+
ROOT_CONTEXT,
|
|
5
|
+
trace,
|
|
6
|
+
TraceFlags,
|
|
7
|
+
} from '@opentelemetry/api';
|
|
8
|
+
import {recordOrigin, wrapFetch} from '../fetch';
|
|
9
|
+
import {withContext} from '../otel/context';
|
|
10
|
+
import {contextCtxBytes} from '../otel/span';
|
|
11
|
+
import * as reqfields from '../reqfields';
|
|
12
|
+
import {
|
|
13
|
+
outgoingContext,
|
|
14
|
+
setCarriedLookup,
|
|
15
|
+
withCarried,
|
|
16
|
+
type Carried,
|
|
17
|
+
} from './carry';
|
|
18
|
+
|
|
19
|
+
const wire = (value: unknown) =>
|
|
20
|
+
Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
21
|
+
|
|
22
|
+
const TEST_TRACE = '0af7651916cd43dd8448eb211c80319c';
|
|
23
|
+
const TEST_SPAN = 'b7ad6b7169203331';
|
|
24
|
+
|
|
25
|
+
/** What the test `00f067aa0ba902b7` carries, as the core gives it. */
|
|
26
|
+
const carried: Carried = {
|
|
27
|
+
fieldName: 'encore-test',
|
|
28
|
+
field: wire('00f067aa0ba902b7'),
|
|
29
|
+
traceparent: `00-${TEST_TRACE}-${TEST_SPAN}-01`,
|
|
30
|
+
tracestate: 'encore=rc',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** A request's context: a span of its own, and the test it is made for. */
|
|
34
|
+
const requestContext = trace.setSpanContext(
|
|
35
|
+
reqfields.withFields(
|
|
36
|
+
{'encore-test': wire('another-test'), plan: wire('pro')},
|
|
37
|
+
ROOT_CONTEXT,
|
|
38
|
+
),
|
|
39
|
+
{
|
|
40
|
+
traceId: '1bf7651916cd43dd8448eb211c80319d',
|
|
41
|
+
spanId: 'c8ad6b7169203332',
|
|
42
|
+
traceFlags: TraceFlags.SAMPLED,
|
|
43
|
+
traceState: createTraceState('vendor=1'),
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
afterEach(() => setCarriedLookup(undefined));
|
|
48
|
+
|
|
49
|
+
describe('withCarried', () => {
|
|
50
|
+
test('adds the test to a context that has none', () => {
|
|
51
|
+
const ctx = withCarried(ROOT_CONTEXT, carried);
|
|
52
|
+
expect(reqfields.current(ctx)).toEqual({'encore-test': carried.field});
|
|
53
|
+
const span = trace.getSpanContext(ctx)!;
|
|
54
|
+
expect(span.traceId).toBe(TEST_TRACE);
|
|
55
|
+
expect(span.spanId).toBe(TEST_SPAN);
|
|
56
|
+
expect(span.traceFlags).toBe(TraceFlags.SAMPLED);
|
|
57
|
+
expect(span.traceState?.serialize()).toBe('encore=rc');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('is what the runtime is handed of a publish made for the test', () => {
|
|
61
|
+
// The test's span, for the publish to be under, then the test's field,
|
|
62
|
+
// for the span the runtime records of it.
|
|
63
|
+
const bytes = contextCtxBytes(withCarried(ROOT_CONTEXT, carried))!;
|
|
64
|
+
expect(bytes.subarray(0, 25).toString('hex')).toBe(
|
|
65
|
+
`${TEST_TRACE}${TEST_SPAN}01`,
|
|
66
|
+
);
|
|
67
|
+
expect(bytes.subarray(25).toString('latin1')).toBe(carried.field);
|
|
68
|
+
// Where the process records no spans, the field alone.
|
|
69
|
+
const spanless = contextCtxBytes(
|
|
70
|
+
withCarried(ROOT_CONTEXT, {...carried, traceparent: null}),
|
|
71
|
+
)!;
|
|
72
|
+
expect(spanless.subarray(0, 25)).toEqual(Buffer.alloc(25));
|
|
73
|
+
expect(spanless.subarray(25).toString('latin1')).toBe(carried.field);
|
|
74
|
+
// A context of no test hands over its span, or nothing.
|
|
75
|
+
expect(contextCtxBytes(ROOT_CONTEXT)).toBeUndefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('keeps the fields a context has beside it', () => {
|
|
79
|
+
const ctx = withCarried(
|
|
80
|
+
reqfields.withFields({plan: wire('pro')}, ROOT_CONTEXT),
|
|
81
|
+
carried,
|
|
82
|
+
);
|
|
83
|
+
expect(reqfields.current(ctx)).toEqual({
|
|
84
|
+
plan: wire('pro'),
|
|
85
|
+
'encore-test': carried.field,
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('keeps the test and the span a context already has', () => {
|
|
90
|
+
const ctx = withCarried(requestContext, carried);
|
|
91
|
+
expect(ctx).toBe(requestContext);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('adds each where only the other is there', () => {
|
|
95
|
+
const spanOnly = trace.setSpanContext(
|
|
96
|
+
ROOT_CONTEXT,
|
|
97
|
+
trace.getSpanContext(requestContext)!,
|
|
98
|
+
);
|
|
99
|
+
const withField = withCarried(spanOnly, carried);
|
|
100
|
+
expect(reqfields.current(withField)['encore-test']).toBe(carried.field);
|
|
101
|
+
expect(trace.getSpanContext(withField)!.spanId).toBe('c8ad6b7169203332');
|
|
102
|
+
|
|
103
|
+
const fieldOnly = reqfields.withFields(
|
|
104
|
+
{'encore-test': wire('another-test')},
|
|
105
|
+
ROOT_CONTEXT,
|
|
106
|
+
);
|
|
107
|
+
const withSpan = withCarried(fieldOnly, carried);
|
|
108
|
+
expect(reqfields.current(withSpan)['encore-test']).toBe(
|
|
109
|
+
wire('another-test'),
|
|
110
|
+
);
|
|
111
|
+
expect(trace.getSpanContext(withSpan)!.spanId).toBe(TEST_SPAN);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('adds no span where the process records none', () => {
|
|
115
|
+
const ctx = withCarried(ROOT_CONTEXT, {
|
|
116
|
+
fieldName: 'encore-test',
|
|
117
|
+
field: carried.field,
|
|
118
|
+
traceparent: null,
|
|
119
|
+
tracestate: null,
|
|
120
|
+
});
|
|
121
|
+
expect(reqfields.current(ctx)).toEqual({'encore-test': carried.field});
|
|
122
|
+
expect(trace.getSpanContext(ctx)).toBeUndefined();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe('outgoingContext', () => {
|
|
127
|
+
test('is the context itself outside a system test', () => {
|
|
128
|
+
expect(outgoingContext(ROOT_CONTEXT)).toBe(ROOT_CONTEXT);
|
|
129
|
+
expect(outgoingContext(requestContext)).toBe(requestContext);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('is the context itself where there is no current test', () => {
|
|
133
|
+
setCarriedLookup(() => null);
|
|
134
|
+
expect(outgoingContext(ROOT_CONTEXT)).toBe(ROOT_CONTEXT);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('adds the current test', () => {
|
|
138
|
+
setCarriedLookup(() => carried);
|
|
139
|
+
const ctx = outgoingContext(ROOT_CONTEXT);
|
|
140
|
+
expect(reqfields.current(ctx)).toEqual({'encore-test': carried.field});
|
|
141
|
+
expect(
|
|
142
|
+
reqfields.messageAttributes({source: 'web'}, outgoingContext()),
|
|
143
|
+
).toEqual({source: 'web', 'encore-ctx-encore-test': carried.field});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("makes what the test's own code does the test's", () => {
|
|
147
|
+
// A query, an object read: the runtime's spans of it are under the
|
|
148
|
+
// test's span, and carry its id.
|
|
149
|
+
setCarriedLookup(() => carried);
|
|
150
|
+
const bytes = contextCtxBytes(ROOT_CONTEXT)!;
|
|
151
|
+
expect(bytes.subarray(0, 25).toString('hex')).toBe(
|
|
152
|
+
`${TEST_TRACE}${TEST_SPAN}01`,
|
|
153
|
+
);
|
|
154
|
+
expect(bytes.subarray(25).toString('latin1')).toBe(carried.field);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('a call made in a system test', () => {
|
|
159
|
+
/** A `fetch` that records the headers of what it is asked to send. */
|
|
160
|
+
function recording() {
|
|
161
|
+
const sent: {url: string; headers: Record<string, string>}[] = [];
|
|
162
|
+
const base = (async (input: string | URL | Request, init?: RequestInit) => {
|
|
163
|
+
sent.push({
|
|
164
|
+
url: String(input instanceof Request ? input.url : input),
|
|
165
|
+
headers: Object.fromEntries(new Headers(init?.headers)),
|
|
166
|
+
});
|
|
167
|
+
return new Response('ok');
|
|
168
|
+
}) as typeof globalThis.fetch;
|
|
169
|
+
return {sent, fetch: wrapFetch(base)};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const SERVICE = 'http://127.0.0.1:4000/orders';
|
|
173
|
+
recordOrigin(SERVICE);
|
|
174
|
+
|
|
175
|
+
test('carries the test, and continues its span', async () => {
|
|
176
|
+
setCarriedLookup(() => carried);
|
|
177
|
+
const {sent, fetch} = recording();
|
|
178
|
+
await fetch(`${SERVICE}/1`, {headers: {accept: 'application/json'}});
|
|
179
|
+
expect(sent[0]!.headers).toEqual({
|
|
180
|
+
accept: 'application/json',
|
|
181
|
+
'x-encore-meta-ctx-encore-test': carried.field,
|
|
182
|
+
traceparent: `00-${TEST_TRACE}-${TEST_SPAN}-01`,
|
|
183
|
+
tracestate: 'encore=rc',
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test('keeps the test and the span of the context it is made in', async () => {
|
|
188
|
+
setCarriedLookup(() => carried);
|
|
189
|
+
const {sent, fetch} = recording();
|
|
190
|
+
await withContext(requestContext, () => fetch(`${SERVICE}/1`));
|
|
191
|
+
expect(sent[0]!.headers).toEqual({
|
|
192
|
+
'x-encore-meta-ctx-encore-test': wire('another-test'),
|
|
193
|
+
'x-encore-meta-ctx-plan': wire('pro'),
|
|
194
|
+
traceparent: '00-1bf7651916cd43dd8448eb211c80319d-c8ad6b7169203332-01',
|
|
195
|
+
tracestate: 'vendor=1',
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('keeps a traceparent the request carries', async () => {
|
|
200
|
+
setCarriedLookup(() => carried);
|
|
201
|
+
const {sent, fetch} = recording();
|
|
202
|
+
const traceparent =
|
|
203
|
+
'00-2cf7651916cd43dd8448eb211c80319e-d9ad6b7169203333-01';
|
|
204
|
+
await fetch(`${SERVICE}/1`, {headers: {traceparent}});
|
|
205
|
+
expect(sent[0]!.headers).toEqual({
|
|
206
|
+
'x-encore-meta-ctx-encore-test': carried.field,
|
|
207
|
+
traceparent,
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test('carries nothing to anything but the app’s services', async () => {
|
|
212
|
+
setCarriedLookup(() => carried);
|
|
213
|
+
const {sent, fetch} = recording();
|
|
214
|
+
await fetch('http://127.0.0.1:9/hook', {headers: {accept: 'text/plain'}});
|
|
215
|
+
expect(sent[0]!.headers).toEqual({accept: 'text/plain'});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('refuses a call to a service where no test can be told from another', async () => {
|
|
219
|
+
// As `./scope` answers when several tests run at once and none is
|
|
220
|
+
// current: a call sent then would carry no test.
|
|
221
|
+
setCarriedLookup(() => {
|
|
222
|
+
throw new Error('several tests are running at once');
|
|
223
|
+
});
|
|
224
|
+
const {sent, fetch} = recording();
|
|
225
|
+
await expect(fetch(`${SERVICE}/1`)).rejects.toThrow(
|
|
226
|
+
'several tests are running at once',
|
|
227
|
+
);
|
|
228
|
+
expect(sent).toEqual([]);
|
|
229
|
+
expect(() => outgoingContext(ROOT_CONTEXT)).toThrow();
|
|
230
|
+
// A call to anything else carries no test anyway, and what is only
|
|
231
|
+
// recorded is recorded as no test's.
|
|
232
|
+
await fetch('http://127.0.0.1:9/hook');
|
|
233
|
+
expect(sent[0]!.headers).toEqual({});
|
|
234
|
+
expect(contextCtxBytes(ROOT_CONTEXT)).toBeUndefined();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('carries nothing outside a system test', async () => {
|
|
238
|
+
const {sent, fetch} = recording();
|
|
239
|
+
await fetch(`${SERVICE}/1`);
|
|
240
|
+
expect(sent[0]!.headers).toEqual({});
|
|
241
|
+
});
|
|
242
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a system test's calls and messages carry of the test: the Request
|
|
3
|
+
* Context field that holds its id, and its span for them to continue, so
|
|
4
|
+
* that what the app does for the test can be told from the rest.
|
|
5
|
+
*
|
|
6
|
+
* Apart from the module that knows the tests (`./scope`), which needs the
|
|
7
|
+
* runtime: what sends calls loads without it, and asks here. Outside a
|
|
8
|
+
* system test nothing is registered, and a context is sent as it is.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
import {
|
|
13
|
+
createTraceState,
|
|
14
|
+
isSpanContextValid,
|
|
15
|
+
trace,
|
|
16
|
+
type Context,
|
|
17
|
+
} from '@opentelemetry/api';
|
|
18
|
+
import {activeContext} from '../otel/context';
|
|
19
|
+
import {parseTraceparent} from '../otel/propagator';
|
|
20
|
+
import * as reqfields from '../reqfields';
|
|
21
|
+
|
|
22
|
+
/** What one system test's calls and messages carry. */
|
|
23
|
+
export interface Carried {
|
|
24
|
+
/** The name of the Request Context field that holds the test's id. */
|
|
25
|
+
readonly fieldName: string;
|
|
26
|
+
/** The field's value, in a field's wire form. */
|
|
27
|
+
readonly field: string;
|
|
28
|
+
/** The test's span as a `traceparent`, where the process records spans. */
|
|
29
|
+
readonly traceparent?: string | null;
|
|
30
|
+
/** The `tracestate` that goes with it. */
|
|
31
|
+
readonly tracestate?: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Finds what the current test carries: `null` when there is none. It
|
|
35
|
+
* throws where several tests run at once and none is current, as it cannot
|
|
36
|
+
* tell which one a call belongs to. */
|
|
37
|
+
let lookup: (() => Carried | null) | undefined;
|
|
38
|
+
|
|
39
|
+
/** Registers how the current system test is found: by `./scope`, in a
|
|
40
|
+
* system test's process only. */
|
|
41
|
+
export function setCarriedLookup(fn: (() => Carried | null) | undefined): void {
|
|
42
|
+
lookup = fn;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The context a call or a publish made in `ctx` is made in: in a system
|
|
47
|
+
* test, `ctx` with what the current test carries added where `ctx` has none
|
|
48
|
+
* (see {@link withCarried}). Anywhere else, `ctx` itself.
|
|
49
|
+
*
|
|
50
|
+
* It throws where several tests run at once and none is current: a call
|
|
51
|
+
* sent then would carry no test, and what the app did for it would be done
|
|
52
|
+
* on data no test sees and waited for by none.
|
|
53
|
+
*/
|
|
54
|
+
export function outgoingContext(ctx: Context = activeContext()): Context {
|
|
55
|
+
if (lookup === undefined) return ctx;
|
|
56
|
+
const carried = lookup();
|
|
57
|
+
return carried === null ? ctx : withCarried(ctx, carried);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* {@link outgoingContext}, for what is recorded or sent elsewhere than to
|
|
62
|
+
* the app: where no test can be told from another, `ctx` itself.
|
|
63
|
+
*/
|
|
64
|
+
export function outgoingContextIfKnown(ctx: Context): Context {
|
|
65
|
+
try {
|
|
66
|
+
return outgoingContext(ctx);
|
|
67
|
+
} catch {
|
|
68
|
+
return ctx;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* `ctx` with what a test carries added where `ctx` has none: the field that
|
|
74
|
+
* holds the test's id, unless `ctx` holds one, as the context of a request
|
|
75
|
+
* made for another test does, and the test's span, unless `ctx` has a span
|
|
76
|
+
* of its own.
|
|
77
|
+
*/
|
|
78
|
+
export function withCarried(ctx: Context, carried: Carried): Context {
|
|
79
|
+
let out = ctx;
|
|
80
|
+
const fields = reqfields.current(ctx);
|
|
81
|
+
if (!Object.hasOwn(fields, carried.fieldName)) {
|
|
82
|
+
out = reqfields.withFields(
|
|
83
|
+
{...fields, [carried.fieldName]: carried.field},
|
|
84
|
+
out,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const span = trace.getSpanContext(ctx);
|
|
88
|
+
if (
|
|
89
|
+
(span === undefined || !isSpanContextValid(span)) &&
|
|
90
|
+
carried.traceparent
|
|
91
|
+
) {
|
|
92
|
+
const parent = parseTraceparent(carried.traceparent);
|
|
93
|
+
if (parent !== undefined) {
|
|
94
|
+
if (carried.tracestate) {
|
|
95
|
+
parent.traceState = createTraceState(carried.tracestate);
|
|
96
|
+
}
|
|
97
|
+
out = trace.setSpanContext(out, parent);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
@@ -9,10 +9,16 @@
|
|
|
9
9
|
* hooks began ({@link setupHooks}). Outside test mode there are no scopes,
|
|
10
10
|
* and every lookup is `null` at once.
|
|
11
11
|
*
|
|
12
|
+
* In a system test's process the tests run against the app's test session
|
|
13
|
+
* ({@link systemTest}): each scope is then a test the session knows, with an
|
|
14
|
+
* id its calls and messages carry ({@link Scope.carried}).
|
|
15
|
+
*
|
|
12
16
|
* @internal
|
|
13
17
|
*/
|
|
14
18
|
import {AsyncLocalStorage} from 'node:async_hooks';
|
|
15
19
|
import * as runtime from '../runtime/mod';
|
|
20
|
+
import {setCarriedLookup} from './carry';
|
|
21
|
+
import {causedBy, Telemetry} from './telemetry';
|
|
16
22
|
|
|
17
23
|
/** The core's scopes: `null` outside test mode. */
|
|
18
24
|
const scopes: runtime.TestScopes | null =
|
|
@@ -21,6 +27,16 @@ const scopes: runtime.TestScopes | null =
|
|
|
21
27
|
/** Whether the process runs as a test, under `encore test` or a test runner. */
|
|
22
28
|
export const testMode = scopes !== null;
|
|
23
29
|
|
|
30
|
+
/**
|
|
31
|
+
* The test session the process's tests run against, when they are system
|
|
32
|
+
* tests, those of a suite: `null` for a service's tests, and outside test
|
|
33
|
+
* mode.
|
|
34
|
+
*/
|
|
35
|
+
export const systemTest: runtime.SystemTest | null = scopes?.system() ?? null;
|
|
36
|
+
|
|
37
|
+
/** How a test ended, as its runner says. */
|
|
38
|
+
export type Outcome = 'passed' | 'failed' | 'skipped';
|
|
39
|
+
|
|
24
40
|
/** One test: the core's scope, and the handles made of it. */
|
|
25
41
|
export class Scope {
|
|
26
42
|
readonly native: runtime.TestScope;
|
|
@@ -34,12 +50,46 @@ export class Scope {
|
|
|
34
50
|
readonly configs = new Map<string, unknown>();
|
|
35
51
|
/** Done with, but left to the hooks that run after the test's own. */
|
|
36
52
|
finished = false;
|
|
53
|
+
/** How the test ended, once something has said. */
|
|
54
|
+
private outcome?: Outcome;
|
|
55
|
+
private carry?: runtime.TestCarried | null;
|
|
37
56
|
|
|
38
57
|
constructor(native: runtime.TestScope, name?: string) {
|
|
39
58
|
this.native = native;
|
|
40
59
|
this.name = name;
|
|
41
60
|
}
|
|
42
61
|
|
|
62
|
+
/** What the test's calls and messages carry of it, when it is a system
|
|
63
|
+
* test: `null` for a service's test. */
|
|
64
|
+
get carried(): runtime.TestCarried | null {
|
|
65
|
+
if (this.carry === undefined) this.carry = this.native.carried();
|
|
66
|
+
return this.carry;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Says how a system test ended, for its session to record, and prints
|
|
71
|
+
* what a failed one caused in the app to standard error, under the
|
|
72
|
+
* failure its runner prints. Once per test: the first to say so is the
|
|
73
|
+
* one that saw it end. Nothing for a service's test.
|
|
74
|
+
*/
|
|
75
|
+
async ended(outcome: Outcome): Promise<void> {
|
|
76
|
+
if (systemTest === null || this.outcome !== undefined) return;
|
|
77
|
+
this.outcome = outcome;
|
|
78
|
+
this.native.setOutcome(outcome);
|
|
79
|
+
if (outcome !== 'failed') return;
|
|
80
|
+
let caused: string;
|
|
81
|
+
try {
|
|
82
|
+
// As it is now: a failed test may have left the app at work.
|
|
83
|
+
caused = causedBy(
|
|
84
|
+
Telemetry.parse(await this.native.telemetry(false, null)),
|
|
85
|
+
);
|
|
86
|
+
} catch {
|
|
87
|
+
// The test's own failure is what there is to say.
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (caused !== '') console.error(caused);
|
|
91
|
+
}
|
|
92
|
+
|
|
43
93
|
/** The test's instance of the database `name`, for `access`. */
|
|
44
94
|
sqlDatabase(name: string, access?: runtime.SQLAccess): runtime.SQLDatabase {
|
|
45
95
|
let db = this.databases.get(name);
|
|
@@ -187,6 +237,9 @@ type HookArgs = unknown[];
|
|
|
187
237
|
/** The scope the hooks began for the test of a runner's test context. */
|
|
188
238
|
const byContext = new WeakMap<object, Scope>();
|
|
189
239
|
|
|
240
|
+
/** The scopes that end with their runner's `onTestFinished`. */
|
|
241
|
+
const endsItself = new WeakSet<Scope>();
|
|
242
|
+
|
|
190
243
|
/** Whether a hook's argument is its runner's test context: an object, or
|
|
191
244
|
* vitest's, a function with properties. */
|
|
192
245
|
export function isContext(ctx: unknown): ctx is object {
|
|
@@ -216,11 +269,41 @@ export function testName(ctx: unknown): string | undefined {
|
|
|
216
269
|
: undefined;
|
|
217
270
|
}
|
|
218
271
|
|
|
272
|
+
if (systemTest !== null) {
|
|
273
|
+
// A call or a publish carries the test it is made for, found as a
|
|
274
|
+
// resource finds its test. Where several tests run at once and none is
|
|
275
|
+
// current, it cannot be told which, and the lookup throws as a resource's
|
|
276
|
+
// does: a call made without the test would be done on data no test sees.
|
|
277
|
+
setCarriedLookup(() => lookupScope()?.carried ?? null);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* How the test of a runner's test context ended, where the context says:
|
|
282
|
+
* vitest's has the test's result once it is done, and node:test's whether it
|
|
283
|
+
* passed. `undefined` where it does not, as in jest and bun, whose hooks are
|
|
284
|
+
* not told.
|
|
285
|
+
*/
|
|
286
|
+
export function outcomeOf(ctx: unknown): Outcome | undefined {
|
|
287
|
+
if (!isContext(ctx)) return undefined;
|
|
288
|
+
const c = ctx as {task?: {result?: {state?: unknown}}; passed?: unknown};
|
|
289
|
+
switch (c.task?.result?.state) {
|
|
290
|
+
case 'pass':
|
|
291
|
+
return 'passed';
|
|
292
|
+
case 'fail':
|
|
293
|
+
return 'failed';
|
|
294
|
+
case 'skip':
|
|
295
|
+
case 'todo':
|
|
296
|
+
return 'skipped';
|
|
297
|
+
}
|
|
298
|
+
if (typeof c.passed === 'boolean') return c.passed ? 'passed' : 'failed';
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
|
|
219
302
|
/** A runner's hooks, as `setup` takes them. */
|
|
220
303
|
export interface Hooks {
|
|
221
|
-
beforeEach(fn: (...args:
|
|
222
|
-
afterEach(fn: (...args:
|
|
223
|
-
afterAll?(fn: (...args:
|
|
304
|
+
beforeEach(fn: (...args: unknown[]) => unknown): void;
|
|
305
|
+
afterEach(fn: (...args: unknown[]) => unknown): void;
|
|
306
|
+
afterAll?(fn: (...args: unknown[]) => unknown): void;
|
|
224
307
|
}
|
|
225
308
|
|
|
226
309
|
/**
|
|
@@ -233,6 +316,9 @@ export interface Hooks {
|
|
|
233
316
|
* registers after these, so the scope is only marked done after the test,
|
|
234
317
|
* for the hooks after it to use, and ends when the next test begins or the
|
|
235
318
|
* file's tests are done.
|
|
319
|
+
*
|
|
320
|
+
* A system test is also told how it ended, where the runner's context says
|
|
321
|
+
* ({@link outcomeOf}), and a failed one prints what it caused in the app.
|
|
236
322
|
*/
|
|
237
323
|
export function setupHooks(
|
|
238
324
|
hooks: Hooks,
|
|
@@ -251,9 +337,15 @@ export function setupHooks(
|
|
|
251
337
|
byContext.set(ctx, scope);
|
|
252
338
|
const onFinished = (ctx as {onTestFinished?: unknown}).onTestFinished;
|
|
253
339
|
if (typeof onFinished === 'function') {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
340
|
+
endsItself.add(scope);
|
|
341
|
+
onFinished.call(ctx, async () => {
|
|
342
|
+
try {
|
|
343
|
+
const outcome = outcomeOf(ctx);
|
|
344
|
+
if (outcome !== undefined) await scope.ended(outcome);
|
|
345
|
+
} finally {
|
|
346
|
+
hooked.delete(scope);
|
|
347
|
+
scope.end();
|
|
348
|
+
}
|
|
257
349
|
});
|
|
258
350
|
}
|
|
259
351
|
}
|
|
@@ -267,7 +359,14 @@ export function setupHooks(
|
|
|
267
359
|
const scope = isContext(ctx)
|
|
268
360
|
? byContext.get(ctx)
|
|
269
361
|
: [...hooked].find(scope => !scope.finished);
|
|
270
|
-
if (scope
|
|
362
|
+
if (scope === undefined || !hooked.has(scope)) return undefined;
|
|
363
|
+
scope.finished = true;
|
|
364
|
+
// A scope that ends itself says how its test ended then, once the
|
|
365
|
+
// hooks after this one have run. Any other says so now, while the
|
|
366
|
+
// failure is what the runner prints next.
|
|
367
|
+
if (endsItself.has(scope)) return undefined;
|
|
368
|
+
const outcome = outcomeOf(ctx);
|
|
369
|
+
return outcome === undefined ? undefined : scope.ended(outcome);
|
|
271
370
|
});
|
|
272
371
|
hooks.afterAll?.(() => endFinished());
|
|
273
372
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {describe, expect, test} from 'bun:test';
|
|
2
|
+
import {caller, Steps, unwrap, wrap} from './steps';
|
|
3
|
+
|
|
4
|
+
/** What was sent, in order, with the calls a fake tool made between. */
|
|
5
|
+
function recorder(): {said: string[]; steps: Steps} {
|
|
6
|
+
const said: string[] = [];
|
|
7
|
+
let next = 0;
|
|
8
|
+
const steps = new Steps(async (method, params) => {
|
|
9
|
+
const what = method.replace('Encore.', '');
|
|
10
|
+
if (what === 'stepBegin') {
|
|
11
|
+
const id = String(++next);
|
|
12
|
+
said.push(`begin ${id} ${String(params.name)}`);
|
|
13
|
+
return {id};
|
|
14
|
+
}
|
|
15
|
+
const how =
|
|
16
|
+
params.discard === true
|
|
17
|
+
? ' discarded'
|
|
18
|
+
: params.error
|
|
19
|
+
? ` error: ${String(params.error)}`
|
|
20
|
+
: '';
|
|
21
|
+
said.push(`end ${String(params.id)}${how}`);
|
|
22
|
+
return {};
|
|
23
|
+
});
|
|
24
|
+
return {said, steps};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class Locator {
|
|
28
|
+
constructor(
|
|
29
|
+
private readonly log: string[],
|
|
30
|
+
readonly selector: string,
|
|
31
|
+
) {}
|
|
32
|
+
async click(): Promise<void> {
|
|
33
|
+
this.log.push(`tool: click ${this.selector}`);
|
|
34
|
+
}
|
|
35
|
+
first(): Locator {
|
|
36
|
+
return new Locator(this.log, `${this.selector} >> first`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class Keyboard {
|
|
41
|
+
constructor(private readonly log: string[]) {}
|
|
42
|
+
async press(key: string): Promise<void> {
|
|
43
|
+
this.log.push(`tool: press ${key}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class Page {
|
|
48
|
+
readonly #secret = 'kept';
|
|
49
|
+
readonly keyboard: Keyboard;
|
|
50
|
+
constructor(private readonly log: string[]) {
|
|
51
|
+
this.keyboard = new Keyboard(log);
|
|
52
|
+
}
|
|
53
|
+
async goto(url: string): Promise<string> {
|
|
54
|
+
this.log.push(`tool: goto ${url}`);
|
|
55
|
+
return this.#secret;
|
|
56
|
+
}
|
|
57
|
+
locator(selector: string): Locator {
|
|
58
|
+
return new Locator(this.log, selector);
|
|
59
|
+
}
|
|
60
|
+
async fail(): Promise<void> {
|
|
61
|
+
throw new Error('no such element');
|
|
62
|
+
}
|
|
63
|
+
async takes(locator: Locator): Promise<boolean> {
|
|
64
|
+
return locator instanceof Locator && unwrap(locator) === locator;
|
|
65
|
+
}
|
|
66
|
+
on(): this {
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe("a tool's objects, wrapped", () => {
|
|
72
|
+
test('a call is a step, begun before the tool hears of it', async () => {
|
|
73
|
+
const {said, steps} = recorder();
|
|
74
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
75
|
+
// A method that reads a private field is called on the page itself.
|
|
76
|
+
expect(await page.goto('http://localhost:5173/')).toBe('kept');
|
|
77
|
+
expect(said).toEqual([
|
|
78
|
+
'begin 1 page.goto("http://localhost:5173/")',
|
|
79
|
+
'tool: goto http://localhost:5173/',
|
|
80
|
+
'end 1',
|
|
81
|
+
]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('what only names what to act on is no step, and names the next one', async () => {
|
|
85
|
+
const {said, steps} = recorder();
|
|
86
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
87
|
+
await page.locator('#pay').first().click();
|
|
88
|
+
await Promise.resolve();
|
|
89
|
+
expect(said.filter(line => !line.includes('discarded'))).toEqual([
|
|
90
|
+
'begin 1 page.locator("#pay")',
|
|
91
|
+
'begin 2 page.locator("#pay").first()',
|
|
92
|
+
'begin 3 page.locator("#pay").first().click()',
|
|
93
|
+
'tool: click #pay >> first',
|
|
94
|
+
'end 3',
|
|
95
|
+
]);
|
|
96
|
+
expect(said.filter(line => line.includes('discarded')).sort()).toEqual([
|
|
97
|
+
'end 1 discarded',
|
|
98
|
+
'end 2 discarded',
|
|
99
|
+
]);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('what hangs off a page is wrapped too', async () => {
|
|
103
|
+
const {said, steps} = recorder();
|
|
104
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
105
|
+
await page.keyboard.press('Enter');
|
|
106
|
+
expect(said[0]).toBe('begin 1 page.keyboard.press("Enter")');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('a call that fails is a step that failed', async () => {
|
|
110
|
+
const {said, steps} = recorder();
|
|
111
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
112
|
+
await expect(page.fail()).rejects.toThrow('no such element');
|
|
113
|
+
expect(said).toEqual([
|
|
114
|
+
'begin 1 page.fail()',
|
|
115
|
+
'end 1 error: no such element',
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("a wrapper handed back to the tool is the tool's own object", async () => {
|
|
120
|
+
const {said, steps} = recorder();
|
|
121
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
122
|
+
const locator = page.locator('#pay');
|
|
123
|
+
expect(locator).toBeInstanceOf(Locator);
|
|
124
|
+
expect(await page.takes(locator)).toBe(true);
|
|
125
|
+
// One object has one wrapper.
|
|
126
|
+
expect(wrap(unwrap(page), 'page', steps)).toBe(page);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('listening is no step', () => {
|
|
130
|
+
const {said, steps} = recorder();
|
|
131
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
132
|
+
page.on();
|
|
133
|
+
expect(said).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('a connection that takes no steps is left alone', async () => {
|
|
137
|
+
const said: string[] = [];
|
|
138
|
+
const steps = new Steps(async () => {
|
|
139
|
+
throw new Error("not Encore's browser");
|
|
140
|
+
});
|
|
141
|
+
const page = wrap(new Page(said), 'page', steps);
|
|
142
|
+
await page.goto('a');
|
|
143
|
+
await page.goto('b');
|
|
144
|
+
expect(said).toEqual(['tool: goto a', 'tool: goto b']);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("the caller is the test's own frame", () => {
|
|
149
|
+
const at = caller();
|
|
150
|
+
expect(at?.file.endsWith('steps.test.ts')).toBe(true);
|
|
151
|
+
expect(at?.line).toBeGreaterThan(0);
|
|
152
|
+
});
|