|
1 | 1 | // Tsdown config tests protect package artifact build contracts. |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { createHash } from "node:crypto"; |
2 | 4 | import fs from "node:fs"; |
| 5 | +import { createRequire } from "node:module"; |
3 | 6 | import path from "node:path"; |
4 | 7 | import { build } from "tsdown"; |
5 | 8 | import { afterEach, describe, expect, it, vi } from "vitest"; |
@@ -39,7 +42,162 @@ const isWorkerRsyncReceiverConfig = (config: TsdownConfig) => |
39 | 42 | const isWorkerBuildConfig = (config: TsdownConfig) => |
40 | 43 | isWorkerDeployConfig(config) || isWorkerRsyncReceiverConfig(config); |
41 | 44 |
|
| 45 | +function nativeAssetInventory(directory: string) { |
| 46 | + return fs |
| 47 | + .readdirSync(directory, { recursive: true, encoding: "utf8" }) |
| 48 | + .filter((file) => fs.statSync(path.join(directory, file)).isFile()) |
| 49 | + .toSorted() |
| 50 | + .map((file) => ({ |
| 51 | + file, |
| 52 | + sha256: createHash("sha256") |
| 53 | + .update(fs.readFileSync(path.join(directory, file))) |
| 54 | + .digest("hex"), |
| 55 | + })); |
| 56 | +} |
| 57 | + |
| 58 | +const FS_SAFE_CALLER_PROBE = ` |
| 59 | +import assert from "node:assert/strict"; |
| 60 | +import fs from "node:fs"; |
| 61 | +import { createRequire } from "node:module"; |
| 62 | +import path from "node:path"; |
| 63 | +import { pathToFileURL } from "node:url"; |
| 64 | +const [entry, observer, rootDir, mode, outcome] = process.argv.slice(1); |
| 65 | +const { root } = await import(pathToFileURL(entry).href); |
| 66 | +const { configureFsSafeNative, getFsSafeNativeConfig, FsSafeError } = await import(pathToFileURL(observer).href); |
| 67 | +assert.equal(getFsSafeNativeConfig().mode, mode === "configured" ? "off" : mode); |
| 68 | +if (mode === "configured") configureFsSafeNative({ mode: "require" }); |
| 69 | +const scoped = await root(rootDir); |
| 70 | +if (outcome === "missing") { |
| 71 | + await assert.rejects(scoped.write("proof.txt", "native proof"), (error) => { |
| 72 | + assert(error instanceof FsSafeError); |
| 73 | + assert.equal(error.code, "helper-unavailable"); |
| 74 | + assert.equal(error.cause?.code, "MODULE_NOT_FOUND"); |
| 75 | + return true; |
| 76 | + }); |
| 77 | + assert.deepEqual(fs.readdirSync(rootDir), []); |
| 78 | +} else { |
| 79 | + await scoped.write("proof.txt", "native proof"); |
| 80 | + await scoped.create("created.txt", "create proof"); |
| 81 | + assert.equal(fs.readFileSync(path.join(rootDir, "proof.txt"), "utf8"), "native proof"); |
| 82 | + assert.equal(fs.readFileSync(path.join(rootDir, "created.txt"), "utf8"), "create proof"); |
| 83 | +} |
| 84 | +const loaded = Object.keys(createRequire(import.meta.url).cache).filter((file) => file.endsWith("fs-safe-native.node")); |
| 85 | +assert.equal(loaded.length, outcome === "native" ? 1 : 0); |
| 86 | +if (loaded.length) assert(loaded[0].startsWith(path.dirname(rootDir) + path.sep)); |
| 87 | +`; |
| 88 | + |
42 | 89 | describe("tsdown config", () => { |
| 90 | + it.each(["runtime", "worker"])( |
| 91 | + "preserves native fs-safe assets and policy in relocated %s output", |
| 92 | + async (target) => { |
| 93 | + const temporaryRoot = fs.realpathSync(createTempDir("openclaw-tsdown-fs-safe-")); |
| 94 | + const sourceRoot = path.join(temporaryRoot, "build"); |
| 95 | + const relocatedRoot = path.join(temporaryRoot, "relocated"); |
| 96 | + const require = createRequire(import.meta.url); |
| 97 | + const nativeSource = path.join( |
| 98 | + path.dirname(require.resolve("@openclaw/fs-safe/package.json")), |
| 99 | + "dist/native", |
| 100 | + ); |
| 101 | + const sdkSource = path.resolve("src/plugin-sdk/memory-core-host-engine-fs.ts"); |
| 102 | + const observerSource = path.join(temporaryRoot, "observer.ts"); |
| 103 | + fs.writeFileSync( |
| 104 | + observerSource, |
| 105 | + [ |
| 106 | + `export { root } from ${JSON.stringify(sdkSource)};`, |
| 107 | + `export { configureFsSafeNative, getFsSafeNativeConfig } from ${JSON.stringify(require.resolve("@openclaw/fs-safe/config"))};`, |
| 108 | + `export { FsSafeError } from ${JSON.stringify(require.resolve("@openclaw/fs-safe/errors"))};`, |
| 109 | + ].join("\n"), |
| 110 | + ); |
| 111 | + const worker = target === "worker"; |
| 112 | + const selected = configs.find( |
| 113 | + worker ? isWorkerDeployConfig : (config) => config.name === TSDOWN_UNIFIED_CONFIG_GROUP, |
| 114 | + ); |
| 115 | + expect(selected).toBeDefined(); |
| 116 | + // Deliberately not named dist: the dependency's URL is relative to the |
| 117 | + // emitted loader, including the worker's extra directory component. |
| 118 | + const bundles = await build({ |
| 119 | + ...selected, |
| 120 | + config: false, |
| 121 | + entry: worker |
| 122 | + ? { "worker/worker": observerSource } |
| 123 | + : { "plugin-sdk/memory-core-host-engine-fs": sdkSource, observer: observerSource }, |
| 124 | + outDir: path.join(sourceRoot, "output"), |
| 125 | + dts: false, |
| 126 | + logLevel: "silent", |
| 127 | + }); |
| 128 | + try { |
| 129 | + fs.writeFileSync(path.join(sourceRoot, "package.json"), '{"type":"module"}'); |
| 130 | + fs.renameSync(sourceRoot, relocatedRoot); |
| 131 | + const entry = path.join( |
| 132 | + relocatedRoot, |
| 133 | + worker ? "output/worker/worker.mjs" : "output/plugin-sdk/memory-core-host-engine-fs.js", |
| 134 | + ); |
| 135 | + const observer = worker ? entry : path.join(relocatedRoot, "output/observer.js"); |
| 136 | + const nativeOutput = path.join( |
| 137 | + relocatedRoot, |
| 138 | + worker ? "output/dist/native" : "dist/native", |
| 139 | + ); |
| 140 | + const probe = ( |
| 141 | + name: string, |
| 142 | + mode: string, |
| 143 | + outcome: string, |
| 144 | + override: NodeJS.ProcessEnv = {}, |
| 145 | + ) => { |
| 146 | + const rootDir = path.join(relocatedRoot, name); |
| 147 | + fs.mkdirSync(rootDir); |
| 148 | + const result = spawnSync( |
| 149 | + process.execPath, |
| 150 | + [ |
| 151 | + "--input-type=module", |
| 152 | + "--eval", |
| 153 | + FS_SAFE_CALLER_PROBE, |
| 154 | + entry, |
| 155 | + observer, |
| 156 | + rootDir, |
| 157 | + mode, |
| 158 | + outcome, |
| 159 | + ], |
| 160 | + { |
| 161 | + cwd: relocatedRoot, |
| 162 | + encoding: "utf8", |
| 163 | + timeout: 30_000, |
| 164 | + env: { |
| 165 | + PATH: process.env.PATH, |
| 166 | + SystemRoot: process.env.SystemRoot, |
| 167 | + WINDIR: process.env.WINDIR, |
| 168 | + HOME: temporaryRoot, |
| 169 | + USERPROFILE: temporaryRoot, |
| 170 | + TMPDIR: temporaryRoot, |
| 171 | + TMP: temporaryRoot, |
| 172 | + TEMP: temporaryRoot, |
| 173 | + ...override, |
| 174 | + }, |
| 175 | + }, |
| 176 | + ); |
| 177 | + expect(result.error, name).toBeUndefined(); |
| 178 | + expect(result.status, `${name}\n${result.stdout}\n${result.stderr}`).toBe(0); |
| 179 | + }; |
| 180 | + for (const key of ["FS_SAFE_NATIVE_MODE", "OPENCLAW_FS_SAFE_NATIVE_MODE"]) { |
| 181 | + probe(key, "require", "native", { [key]: "require" }); |
| 182 | + } |
| 183 | + probe("shared-config", "configured", "native"); |
| 184 | + probe("default", "off", "fallback"); |
| 185 | + const assets = nativeAssetInventory(nativeSource); |
| 186 | + expect(assets).toHaveLength(7); |
| 187 | + expect(nativeAssetInventory(nativeOutput)).toEqual(assets); |
| 188 | + fs.rmSync(nativeOutput, { recursive: true }); |
| 189 | + probe("missing", "require", "missing", { FS_SAFE_NATIVE_MODE: "require" }); |
| 190 | + for (const mode of ["off", "auto"]) { |
| 191 | + probe(mode, mode, "fallback", { FS_SAFE_NATIVE_MODE: mode }); |
| 192 | + } |
| 193 | + } finally { |
| 194 | + for (const bundle of bundles) { |
| 195 | + await bundle[Symbol.asyncDispose](); |
| 196 | + } |
| 197 | + } |
| 198 | + }, |
| 199 | + ); |
| 200 | + |
43 | 201 | it.each( |
44 | 202 | ["runtime", "declarations", "worker", "receiver"].flatMap((target) => |
45 | 203 | [false, true].map((verbose) => ({ target, verbose })), |
|
0 commit comments