rwsdk 1.0.0-alpha.6 → 1.0.0-alpha.7
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/dist/lib/e2e/browser.d.mts +10 -0
- package/dist/lib/e2e/browser.mjs +107 -0
- package/dist/lib/e2e/dev.d.mts +8 -0
- package/dist/lib/e2e/dev.mjs +232 -0
- package/dist/lib/e2e/environment.d.mts +14 -0
- package/dist/lib/e2e/environment.mjs +201 -0
- package/dist/lib/e2e/index.d.mts +7 -0
- package/dist/lib/e2e/index.mjs +7 -0
- package/dist/lib/e2e/release.d.mts +56 -0
- package/dist/lib/e2e/release.mjs +537 -0
- package/dist/lib/e2e/tarball.d.mts +14 -0
- package/dist/lib/e2e/tarball.mjs +189 -0
- package/dist/lib/e2e/testHarness.d.mts +98 -0
- package/dist/lib/e2e/testHarness.mjs +393 -0
- package/dist/lib/e2e/types.d.mts +31 -0
- package/dist/lib/e2e/types.mjs +1 -0
- package/dist/lib/smokeTests/browser.mjs +3 -94
- package/dist/lib/smokeTests/development.mjs +2 -223
- package/dist/lib/smokeTests/environment.d.mts +4 -11
- package/dist/lib/smokeTests/environment.mjs +10 -158
- package/dist/lib/smokeTests/release.d.mts +2 -49
- package/dist/lib/smokeTests/release.mjs +3 -503
- package/dist/runtime/lib/injectHtmlAtMarker.d.ts +11 -0
- package/dist/runtime/lib/injectHtmlAtMarker.js +90 -0
- package/dist/runtime/lib/realtime/worker.d.ts +1 -1
- package/dist/runtime/lib/rwContext.d.ts +22 -0
- package/dist/runtime/lib/rwContext.js +1 -0
- package/dist/runtime/render/assembleDocument.d.ts +6 -0
- package/dist/runtime/render/assembleDocument.js +22 -0
- package/dist/runtime/render/createThenableFromReadableStream.d.ts +1 -0
- package/dist/runtime/render/createThenableFromReadableStream.js +9 -0
- package/dist/runtime/render/normalizeActionResult.d.ts +1 -0
- package/dist/runtime/render/normalizeActionResult.js +43 -0
- package/dist/runtime/render/preloads.d.ts +2 -2
- package/dist/runtime/render/preloads.js +2 -3
- package/dist/runtime/render/{renderRscThenableToHtmlStream.d.ts → renderDocumentHtmlStream.d.ts} +3 -3
- package/dist/runtime/render/renderDocumentHtmlStream.js +39 -0
- package/dist/runtime/render/renderHtmlStream.d.ts +7 -0
- package/dist/runtime/render/renderHtmlStream.js +31 -0
- package/dist/runtime/render/renderToRscStream.d.ts +2 -3
- package/dist/runtime/render/renderToRscStream.js +2 -41
- package/dist/runtime/render/renderToStream.d.ts +2 -1
- package/dist/runtime/render/renderToStream.js +15 -8
- package/dist/runtime/render/stylesheets.d.ts +2 -2
- package/dist/runtime/render/stylesheets.js +2 -3
- package/dist/runtime/ssrBridge.d.ts +2 -1
- package/dist/runtime/ssrBridge.js +2 -1
- package/dist/runtime/worker.d.ts +1 -0
- package/dist/runtime/worker.js +11 -6
- package/dist/vite/configPlugin.mjs +2 -2
- package/package.json +8 -4
- package/dist/runtime/render/renderRscThenableToHtmlStream.js +0 -54
- package/dist/runtime/render/transformRscToHtmlStream.d.ts +0 -8
- package/dist/runtime/render/transformRscToHtmlStream.js +0 -19
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SmokeTestOptions } from "./types.mjs";
|
|
2
|
+
import type { Browser } from "puppeteer-core";
|
|
3
|
+
/**
|
|
4
|
+
* Launch a browser instance
|
|
5
|
+
*/
|
|
6
|
+
export declare function launchBrowser(browserPath?: string, headless?: boolean): Promise<Browser>;
|
|
7
|
+
/**
|
|
8
|
+
* Get the browser executable path
|
|
9
|
+
*/
|
|
10
|
+
export declare function getBrowserPath(testOptions?: SmokeTestOptions): Promise<string>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import * as os from "os";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { pathExists } from "fs-extra";
|
|
4
|
+
import debug from "debug";
|
|
5
|
+
import { mkdirp } from "fs-extra";
|
|
6
|
+
import { install, resolveBuildId, computeExecutablePath, detectBrowserPlatform, Browser as PuppeteerBrowser, } from "@puppeteer/browsers";
|
|
7
|
+
import puppeteer from "puppeteer-core";
|
|
8
|
+
const log = debug("rwsdk:e2e:browser");
|
|
9
|
+
/**
|
|
10
|
+
* Launch a browser instance
|
|
11
|
+
*/
|
|
12
|
+
export async function launchBrowser(browserPath, headless = true) {
|
|
13
|
+
// Get browser path if not provided
|
|
14
|
+
if (!browserPath) {
|
|
15
|
+
log("Getting browser executable path");
|
|
16
|
+
browserPath = await getBrowserPath({ headless });
|
|
17
|
+
}
|
|
18
|
+
console.log(`🚀 Launching browser from ${browserPath} (headless: ${headless})`);
|
|
19
|
+
log("Starting browser with puppeteer");
|
|
20
|
+
return await puppeteer.launch({
|
|
21
|
+
executablePath: browserPath,
|
|
22
|
+
headless,
|
|
23
|
+
args: ["--no-sandbox", "--disable-setuid-sandbox"],
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get the browser executable path
|
|
28
|
+
*/
|
|
29
|
+
export async function getBrowserPath(testOptions) {
|
|
30
|
+
console.log("Finding Chrome executable...");
|
|
31
|
+
// First try using environment variable if set
|
|
32
|
+
if (process.env.CHROME_PATH) {
|
|
33
|
+
console.log(`Using Chrome from environment variable: ${process.env.CHROME_PATH}`);
|
|
34
|
+
return process.env.CHROME_PATH;
|
|
35
|
+
}
|
|
36
|
+
// Detect platform
|
|
37
|
+
log("Detecting platform");
|
|
38
|
+
const platform = detectBrowserPlatform();
|
|
39
|
+
if (!platform) {
|
|
40
|
+
log("ERROR: Failed to detect browser platform");
|
|
41
|
+
throw new Error("Failed to detect browser platform");
|
|
42
|
+
}
|
|
43
|
+
log("Detected platform: %s", platform);
|
|
44
|
+
// Define a consistent cache directory path in system temp folder
|
|
45
|
+
const rwCacheDir = join(os.tmpdir(), "redwoodjs-smoke-test-cache");
|
|
46
|
+
await mkdirp(rwCacheDir);
|
|
47
|
+
log("Using cache directory: %s", rwCacheDir);
|
|
48
|
+
// Determine browser type based on headless option
|
|
49
|
+
const browser = testOptions?.headless === false
|
|
50
|
+
? PuppeteerBrowser.CHROME
|
|
51
|
+
: PuppeteerBrowser.CHROMEHEADLESSSHELL;
|
|
52
|
+
log(`Using browser type: ${browser}`);
|
|
53
|
+
// Resolve the buildId for the stable Chrome version - do this once
|
|
54
|
+
log("Resolving Chrome buildId for stable channel");
|
|
55
|
+
const buildId = await resolveBuildId(browser, platform, "stable");
|
|
56
|
+
log("Resolved buildId: %s", buildId);
|
|
57
|
+
// Create installation options - use them consistently
|
|
58
|
+
const installOptions = {
|
|
59
|
+
browser,
|
|
60
|
+
platform,
|
|
61
|
+
cacheDir: rwCacheDir,
|
|
62
|
+
buildId,
|
|
63
|
+
unpack: true,
|
|
64
|
+
};
|
|
65
|
+
try {
|
|
66
|
+
// Try to compute the path first (this will check if it's installed)
|
|
67
|
+
log("Attempting to find existing Chrome installation");
|
|
68
|
+
const path = computeExecutablePath(installOptions);
|
|
69
|
+
if (await pathExists(path)) {
|
|
70
|
+
console.log(`Found existing Chrome at: ${path}`);
|
|
71
|
+
return path;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new Error("Chrome not found at: " + path);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
// If path computation fails, install Chrome
|
|
79
|
+
console.log("No Chrome installation found. Installing Chrome...");
|
|
80
|
+
// Add better error handling for the install step
|
|
81
|
+
try {
|
|
82
|
+
console.log("Downloading Chrome (this may take a few minutes)...");
|
|
83
|
+
await install(installOptions);
|
|
84
|
+
console.log("✅ Chrome installation completed successfully");
|
|
85
|
+
// Now compute the path for the installed browser
|
|
86
|
+
const path = computeExecutablePath(installOptions);
|
|
87
|
+
console.log(`Installed and using Chrome at: ${path}`);
|
|
88
|
+
return path;
|
|
89
|
+
}
|
|
90
|
+
catch (installError) {
|
|
91
|
+
// Provide more detailed error about the browser download failure
|
|
92
|
+
log("ERROR: Failed to download/install Chrome: %O", installError);
|
|
93
|
+
console.error(`❌ Failed to download/install Chrome browser.`);
|
|
94
|
+
console.error(`This is likely a network issue or the browser download URL is unavailable.`);
|
|
95
|
+
console.error(`Error details: ${installError instanceof Error ? installError.message : String(installError)}`);
|
|
96
|
+
// For debug builds, show the full error stack if available
|
|
97
|
+
if (installError instanceof Error && installError.stack) {
|
|
98
|
+
log("Error stack: %s", installError.stack);
|
|
99
|
+
}
|
|
100
|
+
console.log("\nPossible solutions:");
|
|
101
|
+
console.log("1. Check your internet connection");
|
|
102
|
+
console.log("2. Set CHROME_PATH environment variable to an existing Chrome installation");
|
|
103
|
+
console.log("3. Install Chrome manually and run the tests again");
|
|
104
|
+
throw new Error(`Failed to install Chrome browser: ${installError instanceof Error ? installError.message : String(installError)}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
2
|
+
import debug from "debug";
|
|
3
|
+
import { $ } from "../../lib/$.mjs";
|
|
4
|
+
const log = debug("rwsdk:e2e:dev");
|
|
5
|
+
/**
|
|
6
|
+
* Run the local development server and return the URL
|
|
7
|
+
*/
|
|
8
|
+
export async function runDevServer(packageManager = "pnpm", cwd) {
|
|
9
|
+
console.log("🚀 Starting development server...");
|
|
10
|
+
// Function to stop the dev server - defined early so we can use it in error handling
|
|
11
|
+
let devProcess = null;
|
|
12
|
+
let isErrorExpected = false;
|
|
13
|
+
const stopDev = async () => {
|
|
14
|
+
isErrorExpected = true;
|
|
15
|
+
if (!devProcess) {
|
|
16
|
+
log("No dev process to stop");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
console.log("Stopping development server...");
|
|
20
|
+
try {
|
|
21
|
+
// Send a regular termination signal first
|
|
22
|
+
devProcess.kill();
|
|
23
|
+
// Wait for the process to terminate with a timeout
|
|
24
|
+
const terminationTimeout = 5000; // 5 seconds timeout
|
|
25
|
+
const terminationPromise = Promise.race([
|
|
26
|
+
// Wait for natural process termination
|
|
27
|
+
(async () => {
|
|
28
|
+
try {
|
|
29
|
+
await devProcess;
|
|
30
|
+
log("Dev server process was terminated normally");
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
// Expected error when the process is killed
|
|
35
|
+
log("Dev server process was terminated");
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
})(),
|
|
39
|
+
// Or timeout
|
|
40
|
+
(async () => {
|
|
41
|
+
await setTimeout(terminationTimeout);
|
|
42
|
+
return false;
|
|
43
|
+
})(),
|
|
44
|
+
]);
|
|
45
|
+
// Check if process terminated within timeout
|
|
46
|
+
const terminated = await terminationPromise;
|
|
47
|
+
// If not terminated within timeout, force kill
|
|
48
|
+
if (!terminated) {
|
|
49
|
+
log("Dev server process did not terminate within timeout, force killing with SIGKILL");
|
|
50
|
+
console.log("⚠️ Development server not responding after 5 seconds timeout, force killing...");
|
|
51
|
+
// Try to kill with SIGKILL if the process still has a pid
|
|
52
|
+
if (devProcess.pid) {
|
|
53
|
+
try {
|
|
54
|
+
// Use process.kill with SIGKILL for a stronger termination
|
|
55
|
+
process.kill(devProcess.pid, "SIGKILL");
|
|
56
|
+
log("Sent SIGKILL to process %d", devProcess.pid);
|
|
57
|
+
}
|
|
58
|
+
catch (killError) {
|
|
59
|
+
log("Error sending SIGKILL to process: %O", killError);
|
|
60
|
+
// Non-fatal, as the process might already be gone
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
// Process might already have exited
|
|
67
|
+
log("Could not kill dev server process: %O", e);
|
|
68
|
+
}
|
|
69
|
+
console.log("Development server stopped");
|
|
70
|
+
};
|
|
71
|
+
try {
|
|
72
|
+
// Check if we're in CI mode
|
|
73
|
+
const inCIMode = process.env.CI === "true" || process.env.CI === "1";
|
|
74
|
+
// Start dev server with stdout pipe to capture URL
|
|
75
|
+
// Create environment variables object
|
|
76
|
+
const env = {
|
|
77
|
+
...process.env,
|
|
78
|
+
NODE_ENV: "development",
|
|
79
|
+
};
|
|
80
|
+
// Disable colors when running in CI mode to make URL parsing more reliable
|
|
81
|
+
if (inCIMode) {
|
|
82
|
+
log("Running in CI mode, disabling colors for dev server output");
|
|
83
|
+
env.NO_COLOR = "1";
|
|
84
|
+
env.FORCE_COLOR = "0";
|
|
85
|
+
}
|
|
86
|
+
// Map package manager names to actual commands
|
|
87
|
+
const getPackageManagerCommand = (pm) => {
|
|
88
|
+
switch (pm) {
|
|
89
|
+
case "yarn-classic":
|
|
90
|
+
return "yarn";
|
|
91
|
+
default:
|
|
92
|
+
return pm;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const pm = getPackageManagerCommand(packageManager);
|
|
96
|
+
// Use the provided cwd if available
|
|
97
|
+
devProcess = $({
|
|
98
|
+
all: true,
|
|
99
|
+
detached: false, // Keep attached so we can access streams
|
|
100
|
+
cleanup: false, // Don't auto-kill on exit
|
|
101
|
+
cwd: cwd || process.cwd(), // Use provided directory or current directory
|
|
102
|
+
env, // Pass the updated environment variables
|
|
103
|
+
stdio: "pipe", // Ensure streams are piped
|
|
104
|
+
}) `${pm} run dev`;
|
|
105
|
+
devProcess.catch((error) => {
|
|
106
|
+
if (!isErrorExpected) {
|
|
107
|
+
// Just throw the error, let the caller handle it.
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
log("Development server process spawned in directory: %s", cwd || process.cwd());
|
|
112
|
+
// Store chunks to parse the URL
|
|
113
|
+
let url = "";
|
|
114
|
+
let allOutput = "";
|
|
115
|
+
// Listen for all output to get the URL
|
|
116
|
+
const handleOutput = (data, source) => {
|
|
117
|
+
const output = data.toString();
|
|
118
|
+
console.log(output);
|
|
119
|
+
allOutput += output; // Accumulate all output
|
|
120
|
+
log("Received output from %s: %s", source, output.replace(/\n/g, "\\n"));
|
|
121
|
+
if (!url) {
|
|
122
|
+
// Multiple patterns to catch different package manager outputs
|
|
123
|
+
const patterns = [
|
|
124
|
+
// Standard Vite output: "Local: http://localhost:5173/"
|
|
125
|
+
/Local:\s*(?:\u001b\[\d+m)?(https?:\/\/localhost:\d+)/i,
|
|
126
|
+
// Alternative Vite output: "➜ Local: http://localhost:5173/"
|
|
127
|
+
/[➜→]\s*Local:\s*(?:\u001b\[\d+m)?(https?:\/\/localhost:\d+)/i,
|
|
128
|
+
// Unicode-safe arrow pattern
|
|
129
|
+
/[\u27A1\u2192\u279C]\s*Local:\s*(?:\u001b\[\d+m)?(https?:\/\/localhost:\d+)/i,
|
|
130
|
+
// Direct URL pattern: "http://localhost:5173"
|
|
131
|
+
/(https?:\/\/localhost:\d+)/i,
|
|
132
|
+
// Port-only pattern: "localhost:5173"
|
|
133
|
+
/localhost:(\d+)/i,
|
|
134
|
+
// Server ready messages
|
|
135
|
+
/server.*ready.*localhost:(\d+)/i,
|
|
136
|
+
/dev server.*localhost:(\d+)/i,
|
|
137
|
+
];
|
|
138
|
+
for (const pattern of patterns) {
|
|
139
|
+
const match = output.match(pattern);
|
|
140
|
+
log("Testing pattern %s against output: %s", pattern.source, output.replace(/\n/g, "\\n"));
|
|
141
|
+
if (match) {
|
|
142
|
+
log("Pattern matched: %s, groups: %o", pattern.source, match);
|
|
143
|
+
if (match[1] && match[1].startsWith("http")) {
|
|
144
|
+
url = match[1];
|
|
145
|
+
log("Found development server URL with pattern %s: %s", pattern.source, url);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
else if (match[1] && /^\d+$/.test(match[1])) {
|
|
149
|
+
url = `http://localhost:${match[1]}`;
|
|
150
|
+
log("Found development server URL with port pattern %s: %s", pattern.source, url);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Log potential matches for debugging
|
|
156
|
+
if (!url &&
|
|
157
|
+
(output.includes("localhost") ||
|
|
158
|
+
output.includes("Local") ||
|
|
159
|
+
output.includes("server"))) {
|
|
160
|
+
log("Potential URL pattern found but not matched: %s", output.trim());
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
// Listen to all possible output streams
|
|
165
|
+
log("Setting up stream listeners. Available streams: all=%s, stdout=%s, stderr=%s", !!devProcess.all, !!devProcess.stdout, !!devProcess.stderr);
|
|
166
|
+
devProcess.all?.on("data", (data) => handleOutput(data, "all"));
|
|
167
|
+
devProcess.stdout?.on("data", (data) => handleOutput(data, "stdout"));
|
|
168
|
+
devProcess.stderr?.on("data", (data) => handleOutput(data, "stderr"));
|
|
169
|
+
// Also try listening to the raw process output
|
|
170
|
+
if (devProcess.child) {
|
|
171
|
+
log("Setting up child process stream listeners");
|
|
172
|
+
devProcess.child.stdout?.on("data", (data) => handleOutput(data, "child.stdout"));
|
|
173
|
+
devProcess.child.stderr?.on("data", (data) => handleOutput(data, "child.stderr"));
|
|
174
|
+
}
|
|
175
|
+
// Wait for URL with timeout
|
|
176
|
+
const waitForUrl = async () => {
|
|
177
|
+
const start = Date.now();
|
|
178
|
+
const timeout = 60000; // 60 seconds
|
|
179
|
+
while (Date.now() - start < timeout) {
|
|
180
|
+
if (url) {
|
|
181
|
+
return url;
|
|
182
|
+
}
|
|
183
|
+
// Fallback: check accumulated output if stream listeners aren't working
|
|
184
|
+
if (!url && allOutput) {
|
|
185
|
+
log("Checking accumulated output for URL patterns: %s", allOutput.replace(/\n/g, "\\n"));
|
|
186
|
+
const patterns = [
|
|
187
|
+
/Local:\s*(?:\u001b\[\d+m)?(https?:\/\/localhost:\d+)/i,
|
|
188
|
+
/[➜→]\s*Local:\s*(?:\u001b\[\d+m)?(https?:\/\/localhost:\d+)/i,
|
|
189
|
+
/[\u27A1\u2192\u279C]\s*Local:\s*(?:\u001b\[\d+m)?(https?:\/\/localhost:\d+)/i,
|
|
190
|
+
/(https?:\/\/localhost:\d+)/i,
|
|
191
|
+
/localhost:(\d+)/i,
|
|
192
|
+
];
|
|
193
|
+
for (const pattern of patterns) {
|
|
194
|
+
const match = allOutput.match(pattern);
|
|
195
|
+
if (match) {
|
|
196
|
+
if (match[1] && match[1].startsWith("http")) {
|
|
197
|
+
url = match[1];
|
|
198
|
+
log("Found URL in accumulated output with pattern %s: %s", pattern.source, url);
|
|
199
|
+
return url;
|
|
200
|
+
}
|
|
201
|
+
else if (match[1] && /^\d+$/.test(match[1])) {
|
|
202
|
+
url = `http://localhost:${match[1]}`;
|
|
203
|
+
log("Found URL in accumulated output with port pattern %s: %s", pattern.source, url);
|
|
204
|
+
return url;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Check if the process is still running
|
|
210
|
+
if (devProcess.exitCode !== null) {
|
|
211
|
+
log("ERROR: Development server process exited with code %d. Final output: %s", devProcess.exitCode, allOutput);
|
|
212
|
+
throw new Error(`Development server process exited with code ${devProcess.exitCode}`);
|
|
213
|
+
}
|
|
214
|
+
await setTimeout(500); // Check every 500ms
|
|
215
|
+
}
|
|
216
|
+
log("ERROR: Timed out waiting for dev server URL. Final accumulated output: %s", allOutput);
|
|
217
|
+
throw new Error("Timed out waiting for dev server URL");
|
|
218
|
+
};
|
|
219
|
+
// Wait for the URL
|
|
220
|
+
const serverUrl = await waitForUrl();
|
|
221
|
+
console.log(`✅ Development server started at ${serverUrl}`);
|
|
222
|
+
return { url: serverUrl, stopDev };
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
// Make sure to try to stop the server on error
|
|
226
|
+
log("Error during dev server startup: %O", error);
|
|
227
|
+
await stopDev().catch((e) => {
|
|
228
|
+
log("Failed to stop dev server during error handling: %O", e);
|
|
229
|
+
});
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import tmp from "tmp-promise";
|
|
2
|
+
import { SmokeTestOptions, TestResources, PackageManager } from "./types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Sets up the test environment, preparing any resources needed for testing
|
|
5
|
+
*/
|
|
6
|
+
export declare function setupTestEnvironment(options?: SmokeTestOptions): Promise<TestResources>;
|
|
7
|
+
/**
|
|
8
|
+
* Copy project to a temporary directory with a unique name
|
|
9
|
+
*/
|
|
10
|
+
export declare function copyProjectToTempDir(projectDir: string, sync: boolean | undefined, resourceUniqueKey: string, packageManager?: PackageManager): Promise<{
|
|
11
|
+
tempDir: tmp.DirectoryResult;
|
|
12
|
+
targetDir: string;
|
|
13
|
+
workerName: string;
|
|
14
|
+
}>;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { join } from "path";
|
|
2
|
+
import debug from "debug";
|
|
3
|
+
import { pathExists, copy } from "fs-extra";
|
|
4
|
+
import * as fs from "fs/promises";
|
|
5
|
+
import tmp from "tmp-promise";
|
|
6
|
+
import ignore from "ignore";
|
|
7
|
+
import { relative, basename, resolve } from "path";
|
|
8
|
+
import { uniqueNamesGenerator, adjectives, animals, } from "unique-names-generator";
|
|
9
|
+
import { $ } from "../../lib/$.mjs";
|
|
10
|
+
import { debugSync } from "../../scripts/debug-sync.mjs";
|
|
11
|
+
import { createHash } from "crypto";
|
|
12
|
+
const log = debug("rwsdk:e2e:environment");
|
|
13
|
+
/**
|
|
14
|
+
* Sets up the test environment, preparing any resources needed for testing
|
|
15
|
+
*/
|
|
16
|
+
export async function setupTestEnvironment(options = {}) {
|
|
17
|
+
log("Setting up test environment with options: %O", options);
|
|
18
|
+
// Generate a resource unique key for this test run right at the start
|
|
19
|
+
const uniqueNameSuffix = uniqueNamesGenerator({
|
|
20
|
+
dictionaries: [adjectives, animals],
|
|
21
|
+
separator: "-",
|
|
22
|
+
length: 2,
|
|
23
|
+
style: "lowerCase",
|
|
24
|
+
});
|
|
25
|
+
// Create a short unique hash based on the timestamp
|
|
26
|
+
const hash = createHash("md5")
|
|
27
|
+
.update(Date.now().toString())
|
|
28
|
+
.digest("hex")
|
|
29
|
+
.substring(0, 8);
|
|
30
|
+
// Create a resource unique key even if we're not copying a project
|
|
31
|
+
const resourceUniqueKey = `${uniqueNameSuffix}-${hash}`;
|
|
32
|
+
const resources = {
|
|
33
|
+
tempDirCleanup: undefined,
|
|
34
|
+
workerName: undefined,
|
|
35
|
+
originalCwd: process.cwd(),
|
|
36
|
+
targetDir: undefined,
|
|
37
|
+
workerCreatedDuringTest: false,
|
|
38
|
+
stopDev: undefined,
|
|
39
|
+
resourceUniqueKey, // Set at initialization
|
|
40
|
+
};
|
|
41
|
+
log("Current working directory: %s", resources.originalCwd);
|
|
42
|
+
try {
|
|
43
|
+
// If a project dir is specified, copy it to a temp dir with a unique name
|
|
44
|
+
if (options.projectDir) {
|
|
45
|
+
log("Project directory specified: %s", options.projectDir);
|
|
46
|
+
const { tempDir, targetDir, workerName } = await copyProjectToTempDir(options.projectDir, options.sync !== false, // default to true if undefined
|
|
47
|
+
resourceUniqueKey, // Pass in the existing resourceUniqueKey
|
|
48
|
+
options.packageManager);
|
|
49
|
+
// Store cleanup function
|
|
50
|
+
resources.tempDirCleanup = tempDir.cleanup;
|
|
51
|
+
resources.workerName = workerName;
|
|
52
|
+
resources.targetDir = targetDir;
|
|
53
|
+
log("Target directory: %s", targetDir);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
log("No project directory specified, using current directory");
|
|
57
|
+
// When no project dir is specified, we'll use the current directory
|
|
58
|
+
resources.targetDir = resources.originalCwd;
|
|
59
|
+
}
|
|
60
|
+
return resources;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
log("Error during test environment setup: %O", error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Copy project to a temporary directory with a unique name
|
|
69
|
+
*/
|
|
70
|
+
export async function copyProjectToTempDir(projectDir, sync = true, resourceUniqueKey, packageManager) {
|
|
71
|
+
log("Creating temporary directory for project");
|
|
72
|
+
// Create a temporary directory
|
|
73
|
+
const tempDir = await tmp.dir({ unsafeCleanup: true });
|
|
74
|
+
// Create unique project directory name
|
|
75
|
+
const originalDirName = basename(projectDir);
|
|
76
|
+
const workerName = `${originalDirName}-smoke-test-${resourceUniqueKey}`;
|
|
77
|
+
const targetDir = resolve(tempDir.path, workerName);
|
|
78
|
+
console.log(`Copying project from ${projectDir} to ${targetDir}`);
|
|
79
|
+
// Read project's .gitignore if it exists
|
|
80
|
+
let ig = ignore();
|
|
81
|
+
const gitignorePath = join(projectDir, ".gitignore");
|
|
82
|
+
if (await pathExists(gitignorePath)) {
|
|
83
|
+
log("Found .gitignore file at %s", gitignorePath);
|
|
84
|
+
const gitignoreContent = await fs.readFile(gitignorePath, "utf-8");
|
|
85
|
+
ig = ig.add(gitignoreContent);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
log("No .gitignore found, using default ignore patterns");
|
|
89
|
+
// Add default ignores if no .gitignore exists
|
|
90
|
+
ig = ig.add([
|
|
91
|
+
"node_modules",
|
|
92
|
+
".git",
|
|
93
|
+
"dist",
|
|
94
|
+
"build",
|
|
95
|
+
".DS_Store",
|
|
96
|
+
"coverage",
|
|
97
|
+
".cache",
|
|
98
|
+
".wrangler",
|
|
99
|
+
".env",
|
|
100
|
+
].join("\n"));
|
|
101
|
+
}
|
|
102
|
+
// Copy the project directory, respecting .gitignore
|
|
103
|
+
log("Starting copy process with ignored patterns");
|
|
104
|
+
await copy(projectDir, targetDir, {
|
|
105
|
+
filter: (src) => {
|
|
106
|
+
// Get path relative to project directory
|
|
107
|
+
const relativePath = relative(projectDir, src);
|
|
108
|
+
if (!relativePath)
|
|
109
|
+
return true; // Include the root directory
|
|
110
|
+
// Check against ignore patterns
|
|
111
|
+
const result = !ig.ignores(relativePath);
|
|
112
|
+
return result;
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
log("Project copy completed successfully");
|
|
116
|
+
// For yarn, create .yarnrc.yml to disable PnP and use node_modules
|
|
117
|
+
if (packageManager === "yarn" || packageManager === "yarn-classic") {
|
|
118
|
+
const yarnrcPath = join(targetDir, ".yarnrc.yml");
|
|
119
|
+
await fs.writeFile(yarnrcPath, "nodeLinker: node-modules\n");
|
|
120
|
+
log("Created .yarnrc.yml to disable PnP for yarn");
|
|
121
|
+
}
|
|
122
|
+
// Replace workspace:* dependencies with a placeholder before installing
|
|
123
|
+
await replaceWorkspaceDependencies(targetDir);
|
|
124
|
+
// Install dependencies in the target directory
|
|
125
|
+
await installDependencies(targetDir, packageManager);
|
|
126
|
+
// Sync SDK to the temp dir if requested
|
|
127
|
+
if (sync) {
|
|
128
|
+
console.log(`🔄 Syncing SDK to ${targetDir} after installing dependencies...`);
|
|
129
|
+
await debugSync({ targetDir });
|
|
130
|
+
}
|
|
131
|
+
return { tempDir, targetDir, workerName };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Replace workspace:* dependencies with a placeholder version to allow installation
|
|
135
|
+
*/
|
|
136
|
+
async function replaceWorkspaceDependencies(targetDir) {
|
|
137
|
+
const packageJsonPath = join(targetDir, "package.json");
|
|
138
|
+
try {
|
|
139
|
+
const packageJsonContent = await fs.readFile(packageJsonPath, "utf-8");
|
|
140
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
141
|
+
let modified = false;
|
|
142
|
+
// Replace workspace:* dependencies with a placeholder version
|
|
143
|
+
if (packageJson.dependencies) {
|
|
144
|
+
for (const [name, version] of Object.entries(packageJson.dependencies)) {
|
|
145
|
+
if (version === "workspace:*") {
|
|
146
|
+
packageJson.dependencies[name] = "0.0.80"; // Use latest published version as placeholder
|
|
147
|
+
modified = true;
|
|
148
|
+
log(`Replaced workspace dependency ${name} with placeholder version`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (packageJson.devDependencies) {
|
|
153
|
+
for (const [name, version] of Object.entries(packageJson.devDependencies)) {
|
|
154
|
+
if (version === "workspace:*") {
|
|
155
|
+
packageJson.devDependencies[name] = "0.0.80"; // Use latest published version as placeholder
|
|
156
|
+
modified = true;
|
|
157
|
+
log(`Replaced workspace devDependency ${name} with placeholder version`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (modified) {
|
|
162
|
+
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
163
|
+
log("Updated package.json with placeholder versions for workspace dependencies");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
log("Error replacing workspace dependencies: %O", error);
|
|
168
|
+
throw new Error(`Failed to replace workspace dependencies: ${error}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Install project dependencies using pnpm
|
|
173
|
+
*/
|
|
174
|
+
async function installDependencies(targetDir, packageManager = "pnpm") {
|
|
175
|
+
console.log(`📦 Installing project dependencies in ${targetDir} using ${packageManager}...`);
|
|
176
|
+
try {
|
|
177
|
+
const installCommand = {
|
|
178
|
+
pnpm: ["pnpm", "install"],
|
|
179
|
+
npm: ["npm", "install"],
|
|
180
|
+
yarn: ["yarn", "install", "--immutable"],
|
|
181
|
+
"yarn-classic": ["yarn", "install", "--immutable"],
|
|
182
|
+
}[packageManager];
|
|
183
|
+
// Run install command in the target directory
|
|
184
|
+
log(`Running ${installCommand.join(" ")}`);
|
|
185
|
+
const [command, ...args] = installCommand;
|
|
186
|
+
const result = await $(command, args, {
|
|
187
|
+
cwd: targetDir,
|
|
188
|
+
stdio: "pipe", // Capture output
|
|
189
|
+
});
|
|
190
|
+
console.log("✅ Dependencies installed successfully");
|
|
191
|
+
// Log installation details at debug level
|
|
192
|
+
if (result.stdout) {
|
|
193
|
+
log(`${packageManager} install output: %s`, result.stdout);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
log("ERROR: Failed to install dependencies: %O", error);
|
|
198
|
+
console.error(`❌ Failed to install dependencies: ${error instanceof Error ? error.message : String(error)}`);
|
|
199
|
+
throw new Error(`Failed to install project dependencies. Please ensure the project can be installed with ${packageManager}.`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
interface ExpectOptions {
|
|
2
|
+
expect: string | RegExp;
|
|
3
|
+
send?: string;
|
|
4
|
+
}
|
|
5
|
+
interface ExpectResult {
|
|
6
|
+
stdout: string;
|
|
7
|
+
stderr: string;
|
|
8
|
+
code: number | null;
|
|
9
|
+
}
|
|
10
|
+
interface D1Database {
|
|
11
|
+
name: string;
|
|
12
|
+
uuid: string;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A mini expect-like utility for handling interactive CLI prompts and verifying output
|
|
17
|
+
* @param command The command to execute
|
|
18
|
+
* @param expectations Array of {expect, send} objects for interactive responses and verification
|
|
19
|
+
* @param options Additional options for command execution including working directory and environment
|
|
20
|
+
* @returns Promise that resolves when the command completes
|
|
21
|
+
*/
|
|
22
|
+
export declare function $expect(command: string, expectations: Array<ExpectOptions>, options?: {
|
|
23
|
+
reject?: boolean;
|
|
24
|
+
env?: NodeJS.ProcessEnv;
|
|
25
|
+
cwd?: string;
|
|
26
|
+
}): Promise<ExpectResult>;
|
|
27
|
+
/**
|
|
28
|
+
* Ensures Cloudflare account ID is set in environment
|
|
29
|
+
* First checks wrangler cache, then environment variables, and finally guides the user
|
|
30
|
+
*/
|
|
31
|
+
export declare function ensureCloudflareAccountId(cwd?: string, projectDir?: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Run the release command to deploy to Cloudflare
|
|
34
|
+
*/
|
|
35
|
+
export declare function runRelease(cwd: string, projectDir: string, resourceUniqueKey: string): Promise<{
|
|
36
|
+
url: string;
|
|
37
|
+
workerName: string;
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a resource name includes a specific resource unique key
|
|
41
|
+
* This is used to identify resources created during our tests
|
|
42
|
+
*/
|
|
43
|
+
export declare function isRelatedToTest(resourceName: string, resourceUniqueKey: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Delete the worker using wrangler
|
|
46
|
+
*/
|
|
47
|
+
export declare function deleteWorker(name: string, cwd: string, resourceUniqueKey: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* List D1 databases using wrangler
|
|
50
|
+
*/
|
|
51
|
+
export declare function listD1Databases(cwd?: string): Promise<Array<D1Database>>;
|
|
52
|
+
/**
|
|
53
|
+
* Delete a D1 database using wrangler
|
|
54
|
+
*/
|
|
55
|
+
export declare function deleteD1Database(name: string, cwd: string, resourceUniqueKey: string): Promise<void>;
|
|
56
|
+
export {};
|