|
9 | 9 | bundledPluginRoot, |
10 | 10 | } from "openclaw/plugin-sdk/test-fixtures"; |
11 | 11 | import { afterAll, describe, expect, it, vi } from "vitest"; |
12 | | -import { withEnv } from "../test-utils/env.js"; |
| 12 | +import { withEnv, withEnvAsync } from "../test-utils/env.js"; |
13 | 13 | import { |
14 | 14 | buildPluginLoaderAliasMap, |
15 | 15 | createPluginLoaderModuleCacheKey, |
@@ -2352,18 +2352,121 @@ describe("buildPluginLoaderAliasMap memoization", () => { |
2352 | 2352 | }); |
2353 | 2353 |
|
2354 | 2354 | describe("buildPluginLoaderJitiOptions", () => { |
2355 | | - it("adds the versioned fs cache directory to plugin loader jiti options", () => { |
| 2355 | + it("scopes the jiti cache to the durable user cache and OpenClaw install", () => { |
2356 | 2356 | const root = createTrustedOpenClawPackageFixture("2.0.0"); |
2357 | 2357 | const tmpDir = path.join(root, "tmp"); |
| 2358 | + const cacheRoot = path.join(root, "cache"); |
2358 | 2359 |
|
2359 | | - const options = withEnv({ TMPDIR: tmpDir }, () => |
| 2360 | + const options = withEnv({ TMPDIR: tmpDir, XDG_CACHE_HOME: ` ${cacheRoot} ` }, () => |
2360 | 2361 | buildPluginLoaderJitiOptions( |
2361 | 2362 | { "openclaw/plugin-sdk/core": path.join(root, "dist", "plugin-sdk", "core.js") }, |
2362 | 2363 | { modulePath: path.join(root, "dist", "plugins", "loader.js") }, |
2363 | 2364 | ), |
2364 | 2365 | ); |
2365 | 2366 |
|
2366 | | - expect(options.fsCache).toContain(path.join(tmpDir, "jiti", "openclaw", "2.0.0")); |
| 2367 | + expect(options.fsCache).toContain(path.join(cacheRoot, "openclaw", "jiti", "2.0.0")); |
| 2368 | + expect(options.fsCache).not.toContain(tmpDir); |
| 2369 | + }); |
| 2370 | + |
| 2371 | + it.each(["", " ", "relative/cache"])( |
| 2372 | + "ignores non-absolute XDG cache roots (%j)", |
| 2373 | + (xdgCacheHome) => { |
| 2374 | + const root = createTrustedOpenClawPackageFixture("2.0.0"); |
| 2375 | + const homeDir = path.join(root, "home"); |
| 2376 | + const options = withEnv( |
| 2377 | + { |
| 2378 | + XDG_CACHE_HOME: xdgCacheHome, |
| 2379 | + LOCALAPPDATA: undefined, |
| 2380 | + OPENCLAW_HOME: homeDir, |
| 2381 | + }, |
| 2382 | + () => buildPluginLoaderJitiOptions({}, { modulePath: path.join(root, "dist", "plugins") }), |
| 2383 | + ); |
| 2384 | + const platformCacheRoot = |
| 2385 | + process.platform === "win32" |
| 2386 | + ? path.join(homeDir, "AppData", "Local") |
| 2387 | + : process.platform === "darwin" |
| 2388 | + ? path.join(homeDir, "Library", "Caches") |
| 2389 | + : path.join(homeDir, ".cache"); |
| 2390 | + |
| 2391 | + expect(options.fsCache).toContain(path.join(platformCacheRoot, "openclaw", "jiti", "2.0.0")); |
| 2392 | + }, |
| 2393 | + ); |
| 2394 | + |
| 2395 | + it.skipIf(process.platform !== "win32")( |
| 2396 | + "uses an absolute Windows local application-data cache root", |
| 2397 | + () => { |
| 2398 | + const root = createTrustedOpenClawPackageFixture("2.0.0"); |
| 2399 | + const localAppData = path.join(root, "local-app-data"); |
| 2400 | + const options = withEnv( |
| 2401 | + { |
| 2402 | + XDG_CACHE_HOME: undefined, |
| 2403 | + LOCALAPPDATA: ` ${localAppData} `, |
| 2404 | + OPENCLAW_HOME: path.join(root, "home"), |
| 2405 | + }, |
| 2406 | + () => buildPluginLoaderJitiOptions({}, { modulePath: path.join(root, "dist", "plugins") }), |
| 2407 | + ); |
| 2408 | + |
| 2409 | + expect(options.fsCache).toContain(path.join(localAppData, "openclaw", "jiti", "2.0.0")); |
| 2410 | + }, |
| 2411 | + ); |
| 2412 | + |
| 2413 | + it.each(["JITI_FS_CACHE", "JITI_CACHE"])("honors the %s filesystem-cache opt-out", (envKey) => { |
| 2414 | + const root = createTrustedOpenClawPackageFixture("2.0.0"); |
| 2415 | + const options = withEnv( |
| 2416 | + { |
| 2417 | + JITI_FS_CACHE: envKey === "JITI_FS_CACHE" ? "false" : undefined, |
| 2418 | + JITI_CACHE: envKey === "JITI_CACHE" ? "false" : undefined, |
| 2419 | + XDG_CACHE_HOME: path.join(root, "cache"), |
| 2420 | + }, |
| 2421 | + () => buildPluginLoaderJitiOptions({}, { modulePath: path.join(root, "dist", "plugins") }), |
| 2422 | + ); |
| 2423 | + |
| 2424 | + expect(options.fsCache).toBe(false); |
| 2425 | + }); |
| 2426 | + |
| 2427 | + it("keeps deferred jiti imports working after the temporary directory is deleted", async () => { |
| 2428 | + const root = createTrustedOpenClawPackageFixture("2.0.0"); |
| 2429 | + const transientTmpRoot = path.join(root, "tmp"); |
| 2430 | + const durableCacheRoot = path.join(root, "cache"); |
| 2431 | + const sourceRoot = path.join(root, "source"); |
| 2432 | + const parentModulePath = path.join(sourceRoot, "parent.ts"); |
| 2433 | + const childModulePath = path.join(sourceRoot, "child.ts"); |
| 2434 | + mkdirSafeDir(sourceRoot); |
| 2435 | + mkdirSafeDir(transientTmpRoot); |
| 2436 | + fs.writeFileSync( |
| 2437 | + parentModulePath, |
| 2438 | + 'export const loadChild = () => import("./child.ts");\n', |
| 2439 | + "utf-8", |
| 2440 | + ); |
| 2441 | + fs.writeFileSync(childModulePath, 'export const marker = "still-delivered";\n', "utf-8"); |
| 2442 | + const createJiti = await getCreateJiti(); |
| 2443 | + |
| 2444 | + await withEnvAsync( |
| 2445 | + { |
| 2446 | + TMPDIR: transientTmpRoot, |
| 2447 | + XDG_CACHE_HOME: durableCacheRoot, |
| 2448 | + JITI_FS_CACHE: undefined, |
| 2449 | + JITI_CACHE: undefined, |
| 2450 | + }, |
| 2451 | + async () => { |
| 2452 | + const options = buildPluginLoaderJitiOptions({}, { modulePath: parentModulePath }); |
| 2453 | + const loadSourceModule = createJiti(parentModulePath, { |
| 2454 | + ...options, |
| 2455 | + tryNative: false, |
| 2456 | + }); |
| 2457 | + const parent = loadSourceModule(parentModulePath) as { |
| 2458 | + loadChild: () => Promise<{ marker: string }>; |
| 2459 | + }; |
| 2460 | + |
| 2461 | + fs.rmSync(transientTmpRoot, { recursive: true, force: true }); |
| 2462 | + |
| 2463 | + await expect(parent.loadChild()).resolves.toMatchObject({ marker: "still-delivered" }); |
| 2464 | + expect(String(options.fsCache)).toContain(path.join(durableCacheRoot, "openclaw", "jiti")); |
| 2465 | + expect(fs.readdirSync(String(options.fsCache)).some((file) => file.includes("child"))).toBe( |
| 2466 | + true, |
| 2467 | + ); |
| 2468 | + }, |
| 2469 | + ); |
2367 | 2470 | }); |
2368 | 2471 |
|
2369 | 2472 | it("pre-normalizes and marks alias maps for source transforms", () => { |
|
0 commit comments