@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,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A system test's browser, driven with Playwright.
|
|
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.
|
|
11
|
+
*
|
|
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.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // With any test runner:
|
|
19
|
+
* import {chromium} from "playwright-core";
|
|
20
|
+
* import {connect, url} from "@encore.dev/sdk/testing/playwright";
|
|
21
|
+
*
|
|
22
|
+
* test("pays from the browser", async () => {
|
|
23
|
+
* const browser = await connect(chromium);
|
|
24
|
+
* const page = await browser.newPage();
|
|
25
|
+
* await page.goto(await url("web"));
|
|
26
|
+
* await page.getByRole("button", {name: "Pay"}).click();
|
|
27
|
+
* await page.getByText("Thanks!").waitFor();
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // With Playwright Test, which is its own runner:
|
|
32
|
+
* import {test as base, expect} from "@playwright/test";
|
|
33
|
+
* import {extend} from "@encore.dev/sdk/testing/playwright";
|
|
34
|
+
*
|
|
35
|
+
* const test = extend(base);
|
|
36
|
+
* test.use({frontend: "web"}); // page.goto("/") opens it
|
|
37
|
+
*
|
|
38
|
+
* test("pays from the browser", async ({page}) => {
|
|
39
|
+
* await page.goto("/");
|
|
40
|
+
* await page.getByRole("button", {name: "Pay"}).click();
|
|
41
|
+
* await expect(page.getByText("Thanks!")).toBeVisible();
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* @module
|
|
45
|
+
*/
|
|
46
|
+
import '../internal/testing/runner';
|
|
47
|
+
import {Steps, wrap} from '../internal/testing/steps';
|
|
48
|
+
import {setup} from './mod';
|
|
49
|
+
import {browser as testBrowser, type BrowserOptions} from './system';
|
|
50
|
+
|
|
51
|
+
/** Playwright's `chromium`, as much of it as {@link connect} uses. */
|
|
52
|
+
export interface Chromium<B> {
|
|
53
|
+
connectOverCDP(
|
|
54
|
+
endpointURL: string,
|
|
55
|
+
options?: Record<string, unknown>,
|
|
56
|
+
): Promise<B>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The options of {@link connect}. */
|
|
60
|
+
export interface ConnectOptions extends BrowserOptions {
|
|
61
|
+
/** More of Playwright's own options for `connectOverCDP`, such as `slowMo`. */
|
|
62
|
+
connect?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** What {@link connect} uses of a Playwright browser. */
|
|
66
|
+
interface Connected {
|
|
67
|
+
newBrowserCDPSession(): Promise<{
|
|
68
|
+
send(method: string, params?: Record<string, unknown>): Promise<unknown>;
|
|
69
|
+
}>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
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.
|
|
76
|
+
*/
|
|
77
|
+
export async function connect<B>(
|
|
78
|
+
chromium: Chromium<B>,
|
|
79
|
+
options?: ConnectOptions,
|
|
80
|
+
): Promise<B> {
|
|
81
|
+
const info = await testBrowser(options);
|
|
82
|
+
const browser = await chromium.connectOverCDP(
|
|
83
|
+
info.wsEndpoint,
|
|
84
|
+
options?.connect,
|
|
85
|
+
);
|
|
86
|
+
const session = await (
|
|
87
|
+
browser as unknown as Connected
|
|
88
|
+
).newBrowserCDPSession();
|
|
89
|
+
const steps = new Steps((method, params) => session.send(method, params));
|
|
90
|
+
return wrap(browser as object, 'browser', steps) as B;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
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.
|
|
97
|
+
*/
|
|
98
|
+
export async function url(name: string): Promise<string> {
|
|
99
|
+
return (await testBrowser()).url(name);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What {@link extend} adds to Playwright Test's options. */
|
|
103
|
+
export interface Options {
|
|
104
|
+
/**
|
|
105
|
+
* The frontend whose address is the tests' `baseURL`, by name: `goto("/")`
|
|
106
|
+
* then opens it. Without it the `baseURL` is the config's.
|
|
107
|
+
*/
|
|
108
|
+
frontend: string | undefined;
|
|
109
|
+
/** How each test wants its browser: see `system.browser`. */
|
|
110
|
+
encoreBrowser: BrowserOptions;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Playwright Test's `test`, as much of it as {@link extend} uses. */
|
|
114
|
+
interface Extendable {
|
|
115
|
+
extend(fixtures: Record<string, unknown>): unknown;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** A test, as Playwright Test describes it to a fixture. */
|
|
119
|
+
interface TestInfo {
|
|
120
|
+
titlePath: string[];
|
|
121
|
+
status?: string;
|
|
122
|
+
expectedStatus?: string;
|
|
123
|
+
attach(
|
|
124
|
+
name: string,
|
|
125
|
+
options: {path?: string; contentType?: string},
|
|
126
|
+
): Promise<void>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
type Hook = (ctx: {name: string; passed?: boolean}) => unknown;
|
|
130
|
+
|
|
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. */
|
|
133
|
+
let hooks: {before: Hook; after: Hook} | undefined;
|
|
134
|
+
|
|
135
|
+
function scopeHooks(): {before: Hook; after: Hook} | undefined {
|
|
136
|
+
if (hooks !== undefined) return hooks;
|
|
137
|
+
let before: Hook | undefined;
|
|
138
|
+
let after: Hook | undefined;
|
|
139
|
+
setup({
|
|
140
|
+
beforeEach: fn => {
|
|
141
|
+
before = fn as Hook;
|
|
142
|
+
},
|
|
143
|
+
afterEach: fn => {
|
|
144
|
+
after = fn as Hook;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
// Outside test mode nothing is registered, and the tests run as they are.
|
|
148
|
+
if (before !== undefined && after !== undefined) hooks = {before, after};
|
|
149
|
+
return hooks;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
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.
|
|
156
|
+
*
|
|
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.
|
|
160
|
+
*
|
|
161
|
+
* ```ts
|
|
162
|
+
* const test = extend(base);
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export function extend<T>(base: T): T {
|
|
166
|
+
const fixtures: Record<string, unknown> = {
|
|
167
|
+
frontend: [undefined, {option: true}],
|
|
168
|
+
encoreBrowser: [{}, {option: true}],
|
|
169
|
+
|
|
170
|
+
// The test's scope: begun before anything of the test's, ended after.
|
|
171
|
+
_encoreTest: [
|
|
172
|
+
async (
|
|
173
|
+
// Playwright Test reads a fixture's needs off this pattern, and
|
|
174
|
+
// takes nothing else for it: this one needs none.
|
|
175
|
+
// eslint-disable-next-line no-empty-pattern
|
|
176
|
+
{}: object,
|
|
177
|
+
use: () => Promise<void>,
|
|
178
|
+
testInfo: TestInfo,
|
|
179
|
+
) => {
|
|
180
|
+
const scope = scopeHooks();
|
|
181
|
+
const ctx: {name: string; passed?: boolean} = {
|
|
182
|
+
name: testInfo.titlePath.slice(1).join(' › '),
|
|
183
|
+
};
|
|
184
|
+
await scope?.before(ctx);
|
|
185
|
+
try {
|
|
186
|
+
await use();
|
|
187
|
+
} finally {
|
|
188
|
+
if (testInfo.status !== 'skipped') {
|
|
189
|
+
ctx.passed = testInfo.status === testInfo.expectedStatus;
|
|
190
|
+
}
|
|
191
|
+
await scope?.after(ctx);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
{auto: true},
|
|
195
|
+
],
|
|
196
|
+
|
|
197
|
+
context: async (
|
|
198
|
+
{
|
|
199
|
+
playwright,
|
|
200
|
+
contextOptions,
|
|
201
|
+
frontend,
|
|
202
|
+
encoreBrowser,
|
|
203
|
+
// Named so that the test's scope is begun first.
|
|
204
|
+
_encoreTest: _scope,
|
|
205
|
+
}: {
|
|
206
|
+
playwright: {chromium: Chromium<unknown>};
|
|
207
|
+
contextOptions: Record<string, unknown>;
|
|
208
|
+
frontend: string | undefined;
|
|
209
|
+
encoreBrowser: BrowserOptions;
|
|
210
|
+
_encoreTest: void;
|
|
211
|
+
},
|
|
212
|
+
use: (context: unknown) => Promise<void>,
|
|
213
|
+
testInfo: TestInfo,
|
|
214
|
+
) => {
|
|
215
|
+
const info = await testBrowser(encoreBrowser);
|
|
216
|
+
const browser = (await connect(playwright.chromium, encoreBrowser)) as {
|
|
217
|
+
newContext(options: Record<string, unknown>): Promise<{
|
|
218
|
+
close(): Promise<void>;
|
|
219
|
+
}>;
|
|
220
|
+
close(): Promise<void>;
|
|
221
|
+
};
|
|
222
|
+
const context = await browser.newContext({
|
|
223
|
+
...contextOptions,
|
|
224
|
+
...(frontend === undefined ? {} : {baseURL: info.url(frontend)}),
|
|
225
|
+
});
|
|
226
|
+
try {
|
|
227
|
+
await use(context);
|
|
228
|
+
} finally {
|
|
229
|
+
const failed = testInfo.status !== testInfo.expectedStatus;
|
|
230
|
+
if (failed && info.recording !== null) {
|
|
231
|
+
// Written when the test ends, a moment from now: named, so that
|
|
232
|
+
// the report says where to find it.
|
|
233
|
+
await testInfo
|
|
234
|
+
.attach('encore-recording', {
|
|
235
|
+
path: info.recording,
|
|
236
|
+
contentType: 'text/html',
|
|
237
|
+
})
|
|
238
|
+
.catch(() => undefined);
|
|
239
|
+
}
|
|
240
|
+
await context.close().catch(() => undefined);
|
|
241
|
+
await browser.close().catch(() => undefined);
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
// `context` waits for the test's scope by naming it among its fixtures.
|
|
246
|
+
return (base as unknown as Extendable).extend(fixtures) as T;
|
|
247
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A system test's browser, driven with Puppeteer.
|
|
3
|
+
*
|
|
4
|
+
* {@link connect} connects Puppeteer 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.
|
|
8
|
+
*
|
|
9
|
+
* Each call the test makes on a page is reported as it is written, with
|
|
10
|
+
* where it is, so that the recording and the test's telemetry say which
|
|
11
|
+
* statement had the browser do what, and which requests came of it:
|
|
12
|
+
*
|
|
13
|
+
* page.locator("#pay").click() checkout.test.ts:14
|
|
14
|
+
* click button "Pay"
|
|
15
|
+
* POST http://localhost:4000/orders 500
|
|
16
|
+
* POST orders /orders 500
|
|
17
|
+
*
|
|
18
|
+
* Puppeteer is the test's own, passed in: `puppeteer` or `puppeteer-core`.
|
|
19
|
+
* Neither downloads a browser for this, as Encore runs its own.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* import puppeteer from "puppeteer-core";
|
|
23
|
+
* import {system} from "@encore.dev/sdk/testing";
|
|
24
|
+
* import {connect, url} from "@encore.dev/sdk/testing/puppeteer";
|
|
25
|
+
*
|
|
26
|
+
* test("pays from the browser", async () => {
|
|
27
|
+
* const browser = await connect(puppeteer);
|
|
28
|
+
* const page = await browser.newPage();
|
|
29
|
+
* await page.goto(await url("web"));
|
|
30
|
+
* await page.locator("button#pay").click();
|
|
31
|
+
* await page.waitForSelector("#thanks");
|
|
32
|
+
* await system.settled();
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* @module
|
|
36
|
+
*/
|
|
37
|
+
import {Steps, wrap} from '../internal/testing/steps';
|
|
38
|
+
import {browser as testBrowser, type BrowserOptions} from './system';
|
|
39
|
+
|
|
40
|
+
/** Puppeteer, as much of it as {@link connect} uses. */
|
|
41
|
+
export interface Puppeteer<B> {
|
|
42
|
+
connect(options: {
|
|
43
|
+
browserWSEndpoint: string;
|
|
44
|
+
defaultViewport?: {width: number; height: number} | null;
|
|
45
|
+
[option: string]: unknown;
|
|
46
|
+
}): Promise<B>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The options of {@link connect}. */
|
|
50
|
+
export interface ConnectOptions extends BrowserOptions {
|
|
51
|
+
/**
|
|
52
|
+
* More of Puppeteer's own options for `connect`, such as `slowMo` and
|
|
53
|
+
* `protocolTimeout`.
|
|
54
|
+
*/
|
|
55
|
+
connect?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What {@link connect} uses of a Puppeteer browser. */
|
|
59
|
+
interface Connected {
|
|
60
|
+
target(): {
|
|
61
|
+
createCDPSession(): Promise<{
|
|
62
|
+
send(method: string, params?: Record<string, unknown>): Promise<unknown>;
|
|
63
|
+
}>;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Connects `puppeteer` to the current test's browser, starting it if the
|
|
69
|
+
* test has none yet, and returns Puppeteer's `Browser`. The browser ends
|
|
70
|
+
* with the test: there is nothing to close.
|
|
71
|
+
*
|
|
72
|
+
* Its pages' viewport is the browser's window (`options.window`), unless
|
|
73
|
+
* `options.connect.defaultViewport` says otherwise.
|
|
74
|
+
*/
|
|
75
|
+
export async function connect<B>(
|
|
76
|
+
puppeteer: Puppeteer<B>,
|
|
77
|
+
options?: ConnectOptions,
|
|
78
|
+
): Promise<B> {
|
|
79
|
+
const info = await testBrowser(options);
|
|
80
|
+
const browser = await puppeteer.connect({
|
|
81
|
+
defaultViewport: options?.window ?? {width: 1280, height: 800},
|
|
82
|
+
...options?.connect,
|
|
83
|
+
browserWSEndpoint: info.wsEndpoint,
|
|
84
|
+
});
|
|
85
|
+
// The commands that say which statement is running go over the
|
|
86
|
+
// connection Puppeteer's own do, so they arrive in order with them.
|
|
87
|
+
const session = await (browser as unknown as Connected)
|
|
88
|
+
.target()
|
|
89
|
+
.createCDPSession();
|
|
90
|
+
const steps = new Steps((method, params) => session.send(method, params));
|
|
91
|
+
return wrap(browser as object, 'browser', steps) as B;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The URL a page of the current test's browser reaches the gateway or
|
|
96
|
+
* frontend `name` at: the one `encore run` has it at, which the frontend is
|
|
97
|
+
* written against.
|
|
98
|
+
*/
|
|
99
|
+
export async function url(name: string): Promise<string> {
|
|
100
|
+
return (await testBrowser()).url(name);
|
|
101
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {describe, expect, test} from 'bun:test';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the `system` helpers say in a process that is no system test's. The
|
|
5
|
+
* native runtime reads its config from the environment when it is first
|
|
6
|
+
* imported, so this runs in a child process, as `app_meta.test.ts` does: a
|
|
7
|
+
* service's, which runs no tests at all.
|
|
8
|
+
*/
|
|
9
|
+
const outcomes = (() => {
|
|
10
|
+
const probe = `
|
|
11
|
+
import { deliver, published, system } from ${JSON.stringify(`${import.meta.dir}/mod`)};
|
|
12
|
+
const said = (fn) => {
|
|
13
|
+
try {
|
|
14
|
+
return Promise.resolve(fn()).then(() => "returned", (e) => String(e.message ?? e));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return String(e.message ?? e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
console.log(JSON.stringify({
|
|
20
|
+
settled: await said(() => system.settled()),
|
|
21
|
+
url: await said(() => system.url("api")),
|
|
22
|
+
headers: await said(() => system.headers()),
|
|
23
|
+
telemetry: await said(() => system.telemetry({ settle: false })),
|
|
24
|
+
// Its other helpers are a service test's, as before.
|
|
25
|
+
published: await said(() => published({ name: "events" })),
|
|
26
|
+
deliver: await said(() => deliver({})),
|
|
27
|
+
}));
|
|
28
|
+
`;
|
|
29
|
+
const proc = Bun.spawnSync({
|
|
30
|
+
cmd: [process.execPath, '-e', probe],
|
|
31
|
+
cwd: import.meta.dir,
|
|
32
|
+
env: {...process.env, ENCORE_RUNTIME_CONFIG: runtimeConfig()},
|
|
33
|
+
});
|
|
34
|
+
const stdout = proc.stdout.toString();
|
|
35
|
+
if (proc.exitCode !== 0) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`system probe failed (exit ${proc.exitCode}):\n${stdout}\n${proc.stderr.toString()}`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
// The core logs to stdout as it initializes; the probe's JSON is the last line.
|
|
41
|
+
const lines = stdout.trim().split('\n');
|
|
42
|
+
return JSON.parse(lines[lines.length - 1]!);
|
|
43
|
+
})();
|
|
44
|
+
|
|
45
|
+
describe('outside a system test', () => {
|
|
46
|
+
const NOT_A_SYSTEM_TEST =
|
|
47
|
+
'this is not a system test: only the tests of a system test suite, a directory ' +
|
|
48
|
+
'with an `encore.systest.json`, run against the app';
|
|
49
|
+
|
|
50
|
+
test('each helper says it is for a system test', () => {
|
|
51
|
+
expect(outcomes.settled).toBe(NOT_A_SYSTEM_TEST);
|
|
52
|
+
expect(outcomes.url).toBe(NOT_A_SYSTEM_TEST);
|
|
53
|
+
expect(outcomes.headers).toBe(NOT_A_SYSTEM_TEST);
|
|
54
|
+
expect(outcomes.telemetry).toBe(NOT_A_SYSTEM_TEST);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('published and deliver are a service test’s', () => {
|
|
58
|
+
expect(outcomes.published).toContain('not running as a test');
|
|
59
|
+
expect(outcomes.deliver).toContain('not running as a test');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A serialized `encore.runtime.v1.RuntimeConfig` of a process that hosts a
|
|
65
|
+
* service, base64-encoded as the runtime expects it. Hand-encoded, as in
|
|
66
|
+
* `app_meta.test.ts`.
|
|
67
|
+
*/
|
|
68
|
+
function runtimeConfig(): string {
|
|
69
|
+
const varint = (n: number): number[] => {
|
|
70
|
+
const out: number[] = [];
|
|
71
|
+
for (;;) {
|
|
72
|
+
const b = n & 0x7f;
|
|
73
|
+
n >>>= 7;
|
|
74
|
+
out.push(n ? b | 0x80 : b);
|
|
75
|
+
if (!n) return out;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const tag = (field: number, wire: number) => varint((field << 3) | wire);
|
|
79
|
+
const str = (field: number, value: string): number[] => {
|
|
80
|
+
const raw = [...new TextEncoder().encode(value)];
|
|
81
|
+
return [...tag(field, 2), ...varint(raw.length), ...raw];
|
|
82
|
+
};
|
|
83
|
+
const msg = (field: number, body: number[]): number[] => [
|
|
84
|
+
...tag(field, 2),
|
|
85
|
+
...varint(body.length),
|
|
86
|
+
...body,
|
|
87
|
+
];
|
|
88
|
+
const varintField = (field: number, value: number): number[] => [
|
|
89
|
+
...tag(field, 0),
|
|
90
|
+
...varint(value),
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
// Environment{app_id=1, app_slug=2, env_id=3, env_name=4, env_type=5, cloud=6}
|
|
94
|
+
const environment = [
|
|
95
|
+
...str(1, 'app-id'),
|
|
96
|
+
...str(2, 'my-app'),
|
|
97
|
+
...str(3, 'env-1'),
|
|
98
|
+
...str(4, 'local'),
|
|
99
|
+
...varintField(5, 1), // TYPE_DEVELOPMENT
|
|
100
|
+
...varintField(6, 1), // CLOUD_LOCAL
|
|
101
|
+
];
|
|
102
|
+
// Deployment{deploy_id=1, hosted_services=5}
|
|
103
|
+
const deployment = [...str(1, 'deploy-1'), ...msg(5, str(1, 'svc'))];
|
|
104
|
+
|
|
105
|
+
// RuntimeConfig{environment=1, deployment=3}
|
|
106
|
+
const cfg = [...msg(1, environment), ...msg(3, deployment)];
|
|
107
|
+
return Buffer.from(Uint8Array.from(cfg)).toString('base64');
|
|
108
|
+
}
|