@terreno/test 57.19.2 → 57.21.0
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/CHANGELOG.md +443 -0
- package/dist/expo/registerExpoTestEnvironment.js +2 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +13 -1
- package/dist/smoke/childProcesses.d.ts +32 -0
- package/dist/smoke/childProcesses.js +85 -0
- package/dist/smoke/freePort.d.ts +5 -0
- package/dist/smoke/freePort.js +22 -0
- package/dist/smoke/runBin.d.ts +21 -0
- package/dist/smoke/runBin.js +33 -0
- package/dist/smoke/smokeMongoUri.d.ts +11 -0
- package/dist/smoke/smokeMongoUri.js +44 -0
- package/dist/smoke/spawnExampleBackend.d.ts +23 -0
- package/dist/smoke/spawnExampleBackend.js +44 -0
- package/dist/smoke/spawnUntilReady.d.ts +29 -0
- package/dist/smoke/spawnUntilReady.js +64 -0
- package/package.json +8 -6
- package/src/expo/registerExpoTestEnvironment.ts +3 -1
- package/src/index.ts +13 -0
- package/src/smoke/childProcesses.ts +103 -0
- package/src/smoke/freePort.ts +19 -0
- package/src/smoke/runBin.ts +57 -0
- package/src/smoke/smokeMongoUri.ts +55 -0
- package/src/smoke/spawnExampleBackend.ts +68 -0
- package/src/smoke/spawnUntilReady.ts +103 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {collectOutput, killGroup, spawnTracked} from "./childProcesses";
|
|
2
|
+
|
|
3
|
+
export interface RunBinOptions {
|
|
4
|
+
/** A command on PATH, or a path to a script. `.js`, `.mjs`, `.cjs`, and `.ts` files run with Bun. */
|
|
5
|
+
bin: string;
|
|
6
|
+
args?: string[];
|
|
7
|
+
cwd?: string;
|
|
8
|
+
/** Merged over the parent environment. */
|
|
9
|
+
env?: Record<string, string | undefined>;
|
|
10
|
+
/** The process and everything it started are killed, and the call rejects, after this long. Default 30 000 ms. */
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface RunBinResult {
|
|
15
|
+
exitCode: number;
|
|
16
|
+
stdout: string;
|
|
17
|
+
stderr: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const DEFAULT_TIMEOUT_MS = 30000;
|
|
21
|
+
const SCRIPT_EXTENSION = /\.(c|m)?(j|t)s$/;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Runs a binary to completion and returns its exit code and output. A non-zero exit resolves;
|
|
25
|
+
* assert on `exitCode`. Running longer than `timeoutMs` rejects. Background processes the
|
|
26
|
+
* binary leaves behind are killed when it exits.
|
|
27
|
+
*/
|
|
28
|
+
export const runBin = async ({
|
|
29
|
+
bin,
|
|
30
|
+
args = [],
|
|
31
|
+
cwd,
|
|
32
|
+
env,
|
|
33
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
34
|
+
}: RunBinOptions): Promise<RunBinResult> => {
|
|
35
|
+
const cmd = SCRIPT_EXTENSION.test(bin) ? [process.execPath, bin, ...args] : [bin, ...args];
|
|
36
|
+
const child = spawnTracked({cmd, cwd, env});
|
|
37
|
+
const output = collectOutput(child);
|
|
38
|
+
|
|
39
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
40
|
+
const isTimedOut = await Promise.race([
|
|
41
|
+
child.exited.then(() => false),
|
|
42
|
+
new Promise<boolean>((resolve) => {
|
|
43
|
+
timer = setTimeout(() => resolve(true), timeoutMs);
|
|
44
|
+
}),
|
|
45
|
+
]);
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
|
|
48
|
+
// Grandchildren keep the pipes open, so the whole group goes before reading the rest.
|
|
49
|
+
killGroup(child, "SIGKILL");
|
|
50
|
+
const exitCode = await child.exited;
|
|
51
|
+
await output.drained;
|
|
52
|
+
|
|
53
|
+
if (isTimedOut) {
|
|
54
|
+
throw new Error(`${cmd.join(" ")} timed out after ${timeoutMs} ms.\n${output.combined()}`);
|
|
55
|
+
}
|
|
56
|
+
return {exitCode, stderr: output.stderr(), stdout: output.stdout()};
|
|
57
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {buildDatabaseUri} from "../mongo/connection";
|
|
2
|
+
import {resolveMongoLaunchTimeoutMs} from "../mongo/mongoServer";
|
|
3
|
+
|
|
4
|
+
export interface SmokeMongoUriOptions {
|
|
5
|
+
/** Database name written into the URI. Default `terrenoSmoke`. */
|
|
6
|
+
databaseName?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface ReplSetHandle {
|
|
10
|
+
getUri: () => string;
|
|
11
|
+
stop: (options: {doCleanup: boolean; force: boolean}) => Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let replSet: Promise<ReplSetHandle> | undefined;
|
|
15
|
+
|
|
16
|
+
const startReplSet = async (): Promise<ReplSetHandle> => {
|
|
17
|
+
const {MongoMemoryReplSet} = await import("mongodb-memory-server-global");
|
|
18
|
+
const started = new MongoMemoryReplSet({
|
|
19
|
+
instanceOpts: [{launchTimeout: resolveMongoLaunchTimeoutMs()}],
|
|
20
|
+
replSet: {args: ["--wiredTigerCacheSizeGB", "0.25"], count: 1, storageEngine: "wiredTiger"},
|
|
21
|
+
});
|
|
22
|
+
await started.start();
|
|
23
|
+
await started.waitUntilRunning();
|
|
24
|
+
return started;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* MongoDB URI for a smoke's server. Uses `TERRENO_TEST_MONGODB_URI` when set (the CI sidecar);
|
|
29
|
+
* otherwise starts one single-node in-memory replica set per process, so change streams work.
|
|
30
|
+
*/
|
|
31
|
+
export const smokeMongoUri = async ({
|
|
32
|
+
databaseName = "terrenoSmoke",
|
|
33
|
+
}: SmokeMongoUriOptions = {}): Promise<string> => {
|
|
34
|
+
const externalUri = process.env.TERRENO_TEST_MONGODB_URI?.trim();
|
|
35
|
+
if (externalUri) {
|
|
36
|
+
return buildDatabaseUri({databaseName, uri: externalUri});
|
|
37
|
+
}
|
|
38
|
+
replSet ??= startReplSet().catch((error: unknown) => {
|
|
39
|
+
replSet = undefined;
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
const handle = await replSet;
|
|
43
|
+
return buildDatabaseUri({databaseName, uri: handle.getUri()});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Stops the in-memory replica set that `smokeMongoUri` started, if any. */
|
|
47
|
+
export const stopSmokeMongo = async (): Promise<void> => {
|
|
48
|
+
const pending = replSet;
|
|
49
|
+
replSet = undefined;
|
|
50
|
+
if (!pending) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const handle = await pending;
|
|
54
|
+
await handle.stop({doCleanup: true, force: true});
|
|
55
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {resolve} from "node:path";
|
|
2
|
+
|
|
3
|
+
import {freePort} from "./freePort";
|
|
4
|
+
import {type SpawnedProcess, spawnUntilReady} from "./spawnUntilReady";
|
|
5
|
+
|
|
6
|
+
export interface SpawnExampleBackendOptions {
|
|
7
|
+
/** From `smokeMongoUri()`. */
|
|
8
|
+
mongoUri: string;
|
|
9
|
+
/** Default: a free port. */
|
|
10
|
+
port?: number;
|
|
11
|
+
/** Merged over the defaults, e.g. `{AUTH_PROVIDER: "better-auth"}`. */
|
|
12
|
+
env?: Record<string, string | undefined>;
|
|
13
|
+
/** How long the backend may take to answer `/health`. Default 60 000 ms. */
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
/** Directory holding `src/index.ts`. Default: the monorepo's example-backend. */
|
|
16
|
+
backendDir?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ExampleBackend extends SpawnedProcess {
|
|
20
|
+
baseUrl: string;
|
|
21
|
+
port: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* example-backend's SIGTERM hook stops the jobs worker and leaves the HTTP server running, so the
|
|
26
|
+
* process never exits on SIGTERM alone. Its smoke database is disposable, so `stop()` waits only
|
|
27
|
+
* briefly before SIGKILL instead of the default 5 s.
|
|
28
|
+
*/
|
|
29
|
+
const STOP_GRACE_MS = 500;
|
|
30
|
+
|
|
31
|
+
/** Same depth from `src/smoke` and `dist/smoke`, so source and compiled helpers agree. */
|
|
32
|
+
const EXAMPLE_BACKEND_DIR = resolve(__dirname, "../../../example-backend");
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Starts the monorepo's example-backend by path and resolves once `/health` answers 2xx. Client
|
|
36
|
+
* smokes (rtk, syncdb, admin-frontend) use it so they get a real server without importing
|
|
37
|
+
* `@terreno/api`. Auth secrets are fixed dev values; the clinic demo model avoids AI providers.
|
|
38
|
+
*/
|
|
39
|
+
export const spawnExampleBackend = async ({
|
|
40
|
+
mongoUri,
|
|
41
|
+
port,
|
|
42
|
+
env,
|
|
43
|
+
timeoutMs = 60000,
|
|
44
|
+
backendDir = EXAMPLE_BACKEND_DIR,
|
|
45
|
+
}: SpawnExampleBackendOptions): Promise<ExampleBackend> => {
|
|
46
|
+
const resolvedPort = port ?? (await freePort());
|
|
47
|
+
const baseUrl = `http://127.0.0.1:${resolvedPort}`;
|
|
48
|
+
const backend = await spawnUntilReady({
|
|
49
|
+
cmd: [process.execPath, "src/index.ts"],
|
|
50
|
+
cwd: backendDir,
|
|
51
|
+
env: {
|
|
52
|
+
BETTER_AUTH_SECRET: "terreno-smoke-better-auth-secret-32",
|
|
53
|
+
BETTER_AUTH_URL: baseUrl,
|
|
54
|
+
CLINIC_DEMO_MODEL: "true",
|
|
55
|
+
MONGO_URI: mongoUri,
|
|
56
|
+
PORT: String(resolvedPort),
|
|
57
|
+
REFRESH_TOKEN_SECRET: "terreno-smoke-refresh-secret",
|
|
58
|
+
SESSION_SECRET: "terreno-smoke-session-secret",
|
|
59
|
+
TOKEN_ISSUER: "terreno-smoke",
|
|
60
|
+
TOKEN_SECRET: "terreno-smoke-token-secret",
|
|
61
|
+
...env,
|
|
62
|
+
},
|
|
63
|
+
stopGraceMs: STOP_GRACE_MS,
|
|
64
|
+
timeoutMs,
|
|
65
|
+
url: `${baseUrl}/health`,
|
|
66
|
+
});
|
|
67
|
+
return {...backend, baseUrl, port: resolvedPort};
|
|
68
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {DateTime} from "luxon";
|
|
2
|
+
|
|
3
|
+
import {timeout} from "../utils/timeout";
|
|
4
|
+
import {collectOutput, killGroup, spawnTracked} from "./childProcesses";
|
|
5
|
+
|
|
6
|
+
export interface SpawnUntilReadyOptions {
|
|
7
|
+
/** Command and arguments, e.g. `[process.execPath, "e2e/smoke.ts", "serve"]`. */
|
|
8
|
+
cmd: string[];
|
|
9
|
+
/** URL polled until it answers 2xx. */
|
|
10
|
+
url: string;
|
|
11
|
+
/** How long the child may take to answer 2xx. Default 20 000 ms. */
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
cwd?: string;
|
|
14
|
+
/** Merged over the parent environment. */
|
|
15
|
+
env?: Record<string, string | undefined>;
|
|
16
|
+
/** How long `stop()` waits after SIGTERM before SIGKILL. Default 5 000 ms. */
|
|
17
|
+
stopGraceMs?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SpawnedProcess {
|
|
21
|
+
pid: number;
|
|
22
|
+
/** Everything the child wrote to stdout and stderr so far. */
|
|
23
|
+
output: () => string;
|
|
24
|
+
/**
|
|
25
|
+
* Sends SIGTERM to the child's process group, then SIGKILL after `stopGraceMs`. Safe to call
|
|
26
|
+
* more than once.
|
|
27
|
+
*/
|
|
28
|
+
stop: () => Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_TIMEOUT_MS = 20000;
|
|
32
|
+
const POLL_INTERVAL_MS = 100;
|
|
33
|
+
const DEFAULT_STOP_GRACE_MS = 5000;
|
|
34
|
+
const OUTPUT_TAIL_CHARS = 4000;
|
|
35
|
+
|
|
36
|
+
const tail = (text: string): string =>
|
|
37
|
+
text.length > OUTPUT_TAIL_CHARS ? `…${text.slice(-OUTPUT_TAIL_CHARS)}` : text;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Starts a long-running child (a server) and resolves once `url` answers 2xx. Rejects with the
|
|
41
|
+
* child's output when it exits first or is not ready within `timeoutMs`, and kills it. The
|
|
42
|
+
* child's process group is also killed when the smoke process exits.
|
|
43
|
+
*/
|
|
44
|
+
export const spawnUntilReady = async ({
|
|
45
|
+
cmd,
|
|
46
|
+
url,
|
|
47
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
48
|
+
cwd,
|
|
49
|
+
env,
|
|
50
|
+
stopGraceMs = DEFAULT_STOP_GRACE_MS,
|
|
51
|
+
}: SpawnUntilReadyOptions): Promise<SpawnedProcess> => {
|
|
52
|
+
const child = spawnTracked({cmd, cwd, env});
|
|
53
|
+
const output = collectOutput(child);
|
|
54
|
+
let isStopped = false;
|
|
55
|
+
|
|
56
|
+
const stop = async (): Promise<void> => {
|
|
57
|
+
if (isStopped) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
isStopped = true;
|
|
61
|
+
killGroup(child, "SIGTERM");
|
|
62
|
+
const exited = await Promise.race([
|
|
63
|
+
child.exited.then(() => true),
|
|
64
|
+
timeout(stopGraceMs).then(() => false),
|
|
65
|
+
]);
|
|
66
|
+
// Anything the child started and left behind goes too.
|
|
67
|
+
killGroup(child, "SIGKILL");
|
|
68
|
+
if (!exited) {
|
|
69
|
+
await child.exited;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const command = cmd.join(" ");
|
|
74
|
+
const deadline = DateTime.now().plus({milliseconds: timeoutMs});
|
|
75
|
+
let lastResult = "no response";
|
|
76
|
+
while (DateTime.now() < deadline) {
|
|
77
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
78
|
+
await stop();
|
|
79
|
+
await output.drained;
|
|
80
|
+
throw new Error(
|
|
81
|
+
`${command} exited with code ${child.exitCode ?? child.signalCode} before ${url} was ready.\n${tail(output.combined())}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
// The bun and node type packages each declare AbortSignal; fetch expects the global one.
|
|
86
|
+
const signal = AbortSignal.timeout(1000) as unknown as RequestInit["signal"];
|
|
87
|
+
const res = await fetch(url, {signal});
|
|
88
|
+
await res.body?.cancel();
|
|
89
|
+
if (res.ok) {
|
|
90
|
+
return {output: output.combined, pid: child.pid, stop};
|
|
91
|
+
}
|
|
92
|
+
lastResult = `status ${res.status}`;
|
|
93
|
+
} catch (error: unknown) {
|
|
94
|
+
lastResult = error instanceof Error ? error.message : String(error);
|
|
95
|
+
}
|
|
96
|
+
await timeout(POLL_INTERVAL_MS);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
await stop();
|
|
100
|
+
throw new Error(
|
|
101
|
+
`${command} (pid ${child.pid}) was not ready at ${url} within ${timeoutMs} ms (last: ${lastResult}).\n${tail(output.combined())}`
|
|
102
|
+
);
|
|
103
|
+
};
|