Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
115 lines (106 loc) · 3.57 KB
/
Copy pathtest.js
File metadata and controls
115 lines (106 loc) · 3.57 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
// Verifies that isInLambdaRuntime() correctly detects whether the SDK
// is running from inside the Lambda runtime directory, in BOTH CJS
// and ESM module contexts. The function is invoked at module-load
// time and the result is baked into the SDK_VERSION the package
// exports — so we spawn a child Node process for each scenario and
// read SDK_VERSION, checking it for the `-bundled` suffix.
//
// We can't unit-test this in Jest: the real version module reads
// `import.meta.url` at top level, which ts-jest cannot compile
// (Jest forces module: commonjs). The Jest config substitutes a
// hardcoded mock for `version.ts`, so the real detection logic
// never runs under Jest at all.
import { spawnSync } from "node:child_process";
import { createRequire } from "node:module";
import { dirname } from "node:path";
const sdkRequire = createRequire(import.meta.url);
const sdkRoot = dirname(
sdkRequire.resolve("@aws/durable-execution-sdk-js"),
).replace(/\/dist-cjs$/, "");
// Probe script: prints the SDK_VERSION the package resolved at module
// load, which encodes the result of the runtime-directory detection.
const cjsProbe = `
const sdk = require("@aws/durable-execution-sdk-js");
process.stdout.write(JSON.stringify(sdk.SDK_VERSION));
`;
const esmProbe = `
import * as sdk from "@aws/durable-execution-sdk-js";
process.stdout.write(JSON.stringify(sdk.SDK_VERSION));
`;
function probe({ moduleSystem, env }) {
const inputType = moduleSystem === "esm" ? "module" : "commonjs";
const script = moduleSystem === "esm" ? esmProbe : cjsProbe;
const result = spawnSync(
process.execPath,
[`--input-type=${inputType}`, "-e", script],
{
encoding: "utf8",
env: {
// Start from a clean slate so the parent's NODE_ENV (set by
// some CI configurations) doesn't leak into the child.
PATH: process.env.PATH,
...env,
},
},
);
if (result.status !== 0) {
throw new Error(`child process exited ${result.status}: ${result.stderr}`);
}
return JSON.parse(result.stdout);
}
function getSdkVersion(probeOutput) {
if (typeof probeOutput !== "string" || probeOutput.length === 0) {
throw new Error(
`probe did not return an SDK_VERSION: ${JSON.stringify(probeOutput)}`,
);
}
return probeOutput;
}
const scenarios = [
{
label: "CJS, runtime dir matches SDK install",
moduleSystem: "cjs",
env: { LAMBDA_RUNTIME_DIR: sdkRoot },
expectBundled: true,
},
{
label: "CJS, runtime dir does not match",
moduleSystem: "cjs",
env: { LAMBDA_RUNTIME_DIR: "/var/runtime" },
expectBundled: false,
},
{
label: "ESM, runtime dir matches SDK install",
moduleSystem: "esm",
env: { LAMBDA_RUNTIME_DIR: sdkRoot },
expectBundled: true,
},
{
label: "ESM, runtime dir does not match",
moduleSystem: "esm",
env: { LAMBDA_RUNTIME_DIR: "/var/runtime" },
expectBundled: false,
},
];
let failed = 0;
for (const scenario of scenarios) {
try {
const version = getSdkVersion(probe(scenario));
const isBundled = version.endsWith("-bundled");
if (isBundled !== scenario.expectBundled) {
throw new Error(
`expected SDK_VERSION to ${scenario.expectBundled ? "" : "NOT "}` +
`end with "-bundled", got "${version}"`,
);
}
console.log(`✓ [${scenario.label}] SDK_VERSION = "${version}"`);
} catch (error) {
console.error(`✗ [${scenario.label}] ${error.message}`);
failed += 1;
}
}
if (failed > 0) {
console.error(`✗ ${failed} scenario(s) failed`);
process.exit(1);
}
console.log("✓ Lambda runtime detection integration test passed");