Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

test: 隔离桌面测试的 Vite 缓存

Zhehui Bib35aec490452
diff --git a/scripts/testing/one-workspace-fixture.ts b/scripts/testing/one-workspace-fixture.ts
index 2698768cab..2c6c7526ff 100644
--- a/scripts/testing/one-workspace-fixture.ts
+++ b/scripts/testing/one-workspace-fixture.ts
@@ -294,9 +294,9 @@ export async function welcomed(fx: CloudSendFixture): Promise<void> {
await fx.onCloud("PATCH", "/api/config", { onboarding: { ...onboarding, hintsSeen: [...onboarding.hintsSeen, "cloud-welcome-done", "cloud-setup-hidden"] }, language: "en" });
}
-async function vite(port: number, host: string, plugin: (server: ViteDevServer) => void, proxy: Record<string, unknown>): Promise<ViteDevServer> {
+async function vite(cacheDir: string, port: number, host: string, plugin: (server: ViteDevServer) => void, proxy: Record<string, unknown>): Promise<ViteDevServer> {
const server = await createViteServer({
- root: REPO_ROOT, logLevel: "warn", clearScreen: false,
+ root: REPO_ROOT, cacheDir, logLevel: "warn", clearScreen: false,
server: { host, port, strictPort: true, proxy: proxy as never, hmr: { port: port + 1 } },
plugins: [{ name: "one-workspace-edge", configureServer: plugin }],
});
@@ -337,7 +337,7 @@ export async function launchHomeWorld(fx: CloudSendFixture, { chrome, versionEve
const size = { width: 1280, height: 800 };
// My Cloud's edge: the renderer from source, and its API on the real Cloud-mode server with the edge's own headers.
- const cloudEdge = await vite(cloudPort, "localhost", (server) => {
+ const cloudEdge = await vite(join(fx.root, "vite-cloud"), cloudPort, "localhost", (server) => {
server.middlewares.use((req, res, next) => {
if (network.down || network.nameMissing) { req.socket.destroy(); return; }
if ((req.url ?? "").startsWith("/api/") || (req.url ?? "").startsWith("/.well-known/")) cloudRequests.push({ path: (req.url ?? "").split("?")[0]!, at: Date.now() });
@@ -354,7 +354,7 @@ export async function launchHomeWorld(fx: CloudSendFixture, { chrome, versionEve
}])));
cleanup.push(() => cloudEdge.close());
// The team's Cloud: the same server, its own origin (so its own cookie, its own page).
- const teamEdge = await vite(teamPort, "::1", (server) => {
+ const teamEdge = await vite(join(fx.root, "vite-team"), teamPort, "::1", (server) => {
server.middlewares.use((req, _res, next) => { if (network.teamDown) { req.socket.destroy(); return; } next(); });
}, Object.fromEntries(PROXIED.map((path) => [path, {
target: fx.cloudBase, ws: true, headers: fx.edge,
@@ -368,7 +368,7 @@ export async function launchHomeWorld(fx: CloudSendFixture, { chrome, versionEve
cleanup.push(() => teamEdge.close());
// This computer's page: the same renderer, and its real server, as the desktop's owner, decides which page it serves.
- const localEdge = await vite(localPort, "127.0.0.1", (server) => {
+ const localEdge = await vite(join(fx.root, "vite-local"), localPort, "127.0.0.1", (server) => {
server.middlewares.use(async (req, res, next) => {
const path = (req.url ?? "/").split("?")[0]!;
const page = req.method === "GET" && !path.startsWith("/api/") && !path.startsWith("/@") && !path.startsWith("/src/") && !path.startsWith("/node_modules/")
diff --git a/scripts/testing/preview-fixture.ts b/scripts/testing/preview-fixture.ts
index e4319d6e6d..1f60b6bd2f 100644
--- a/scripts/testing/preview-fixture.ts
+++ b/scripts/testing/preview-fixture.ts
@@ -4,10 +4,13 @@
// so the page renders exactly what the app renders; only the host, the port
// and the /api target are pinned here.
import { realpathSync } from "node:fs";
+import { mkdtemp } from "node:fs/promises";
import type { IncomingMessage, ServerResponse } from "node:http";
+import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { createServer } from "vite";
+import { removeTempDir } from "../../server/testing/cleanup.ts";
export const REPO_ROOT = fileURLToPath(new URL("../..", import.meta.url));
@@ -58,8 +61,9 @@ export async function mountPreview(
): Promise<MountedPreview> {
const { entry, route, title, extraRoutes = [], viewport = "width=device-width, initial-scale=1", logLevel, proxyHeaders } = options;
const page = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="${escapeHtml(viewport)}"><title>${escapeHtml(title)}</title></head><body><div id="root"></div><script type="module" src="${escapeHtml(entry)}"></script></body></html>`;
+ const cacheDir = await mkdtemp(join(tmpdir(), "omb-preview-vite-"));
const ui = await createServer({
- root: REPO_ROOT,
+ root: REPO_ROOT, cacheDir,
...(logLevel ? { logLevel } : {}),
// A worktree's node_modules may be a link to another checkout's: its files (the fonts) are served from there.
server: { host: "127.0.0.1", port: 0, fs: { allow: [REPO_ROOT, realpathSync(join(REPO_ROOT, "node_modules"))] }, proxy: {
@@ -87,17 +91,20 @@ export async function mountPreview(
});
},
}],
- });
- await ui.listen();
- const base = ui.resolvedUrls?.local[0];
- if (!base) {
- await ui.close();
- throw new Error("the preview server did not report a local URL");
- }
- return {
- previewUrl: new URL(route, base).href,
- close: () => ui.close(),
+ }).catch(async (error: unknown) => { await removeTempDir(cacheDir); throw error; });
+ const close = async () => {
+ try { await ui.close(); }
+ finally { await removeTempDir(cacheDir); }
};
+ try {
+ await ui.listen();
+ const base = ui.resolvedUrls?.local[0];
+ if (!base) throw new Error("the preview server did not report a local URL");
+ return { previewUrl: new URL(route, base).href, close };
+ } catch (error) {
+ await close();
+ throw error;
+ }
}
/** Resolve on the first SIGINT or SIGTERM: the "print the URL and wait" ending. */