Repository navigation
Expand file tree
/
Copy pathplaywright.analytics-local.config.ts
More file actions
164 lines (152 loc) · 5.62 KB
/
Copy pathplaywright.analytics-local.config.ts
File metadata and controls
164 lines (152 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { randomBytes, randomInt } from "node:crypto";
import path from "node:path";
import { defineConfig, devices } from "@playwright/test";
import { resolvePlaywrightBackingState } from "./tests/playwright-backing-state.mjs";
function resolveRunId(): string {
const runId = process.env.PLAYWRIGHT_RUN_ID ?? `${process.pid}_${randomBytes(4).toString("hex")}`;
if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(runId)) {
throw new Error(
`PLAYWRIGHT_RUN_ID must be 1-64 letters, digits, underscores, or hyphens and start with a letter or digit, received ${JSON.stringify(runId)}`,
);
}
process.env.PLAYWRIGHT_RUN_ID = runId;
return runId;
}
type E2eEndpoint = {
port: number;
url: string;
};
function parsePort(value: string, envName: string): number {
if (!/^\d+$/.test(value)) {
throw new Error(`${envName} must be an integer port, received "${value}"`);
}
const port = Number(value);
if (!Number.isSafeInteger(port) || port < 1024 || port > 65_535) {
throw new Error(`${envName} must be between 1024 and 65535, received "${value}"`);
}
return port;
}
function randomPort(min: number): number {
return randomInt(min, min + 10_000);
}
function resolveEndpoint(kind: "API" | "WEB", defaultPortFloor: number): E2eEndpoint {
const portEnvName = `PLAYWRIGHT_${kind}_PORT`;
const urlEnvName = `PLAYWRIGHT_${kind}_URL`;
const urlOverride = process.env[urlEnvName] ?? (kind === "API" ? process.env.API_URL : undefined);
const parsedOverride = urlOverride ? new URL(urlOverride) : undefined;
const port = process.env[portEnvName]
? parsePort(process.env[portEnvName], portEnvName)
: parsedOverride?.port
? parsePort(parsedOverride.port, urlEnvName)
: randomPort(defaultPortFloor);
const parsedUrl = parsedOverride ?? new URL(`http://127.0.0.1:${port}`);
if (parsedUrl.protocol !== "http:") {
throw new Error(`${urlEnvName} must use http, received "${parsedUrl.protocol}"`);
}
if (!["127.0.0.1", "localhost"].includes(parsedUrl.hostname)) {
throw new Error(`${urlEnvName} must use a loopback host, received "${parsedUrl.hostname}"`);
}
if (
parsedUrl.username ||
parsedUrl.password ||
parsedUrl.pathname !== "/" ||
parsedUrl.search ||
parsedUrl.hash
) {
throw new Error(`${urlEnvName} must be an origin without credentials, path, query, or hash`);
}
if (parsePort(parsedUrl.port || "80", urlEnvName) !== port) {
throw new Error(`${portEnvName} must match the port in ${urlEnvName}`);
}
const url = parsedUrl.origin;
process.env[portEnvName] = String(port);
process.env[urlEnvName] = url;
return { port, url };
}
const runId = resolveRunId();
const runRoot = path.join(__dirname, "test-results", "e2e-analytics-runs", runId);
const authFile = path.join(runRoot, "auth", "analytics-local-user.json");
process.env.PLAYWRIGHT_RUN_ROOT = runRoot;
process.env.PLAYWRIGHT_AUTH_FILE = authFile;
const apiEndpoint = resolveEndpoint("API", 20_000);
const webEndpoint = resolveEndpoint("WEB", 30_000);
process.env.API_URL = apiEndpoint.url;
const E2E_PG_BASE_URL =
process.env.E2E_PG_BASE_URL || "postgres://snapotter:snapotter@localhost:5432/snapotter";
const backingState = resolvePlaywrightBackingState({
postgresBaseUrl: E2E_PG_BASE_URL,
redisUrl: process.env.REDIS_URL ?? "redis://localhost:6379",
runId,
scope: "analytics-local",
});
export default defineConfig({
testDir: "./tests/e2e-analytics",
timeout: 30_000,
expect: { timeout: 10_000 },
fullyParallel: false,
retries: 0,
workers: 1,
outputDir: path.join(runRoot, "playwright-output"),
reporter: [["html", { open: "never", outputFolder: path.join(runRoot, "playwright-report") }]],
use: {
baseURL: webEndpoint.url,
screenshot: "only-on-failure",
trace: "retain-on-failure",
},
projects: [
{
name: "setup",
testMatch: /(?:^|[/\\])auth\.setup\.ts$/,
},
{
name: "chromium",
use: {
...devices["Desktop Chrome"],
storageState: authFile,
},
dependencies: ["setup"],
},
],
webServer: [
{
// NOTE: use the `start` script, not `dev`. The `dev` script hard-codes
// `PORT=13490`, which would override the isolated API endpoint below and
// collide with a developer's running dev server. `start` reads PORT from
// the env, so the PORT set in this webServer.env block is honored.
command: `node tests/playwright-api-lifecycle.mjs ${runId} analytics-local -- pnpm --filter @snapotter/api start`,
url: `${apiEndpoint.url}/api/v1/health`,
reuseExistingServer: false,
gracefulShutdown: { signal: "SIGTERM", timeout: 30_000 },
env: {
AUTH_ENABLED: "true",
DEFAULT_USERNAME: "admin",
DEFAULT_PASSWORD: "admin",
RATE_LIMIT_PER_MIN: "50000",
SKIP_MUST_CHANGE_PASSWORD: "true",
ANALYTICS_ENABLED: "true",
// Force the compile-time bake ON (dev/test only) so the effective
// analytics state is enabled by default and the opt-out toggle can be
// exercised end to end. bakedEnabled() honors this when
// NODE_ENV !== "production".
ANALYTICS_BAKED_OVERRIDE: "on",
DATABASE_URL: backingState.databaseUrl,
E2E_PG_BASE_URL: backingState.postgresBaseUrl,
REDIS_URL: backingState.redisUrl,
BULLMQ_PREFIX: backingState.bullmqPrefix,
PORT: String(apiEndpoint.port),
},
timeout: 30_000,
},
{
command: "pnpm --filter @snapotter/web dev",
url: webEndpoint.url,
reuseExistingServer: false,
env: {
PORT: String(webEndpoint.port),
VITE_API_URL: apiEndpoint.url,
},
timeout: 30_000,
},
],
});
export { authFile };