openclaw/src/plugins/stage-bundled-plugin-runtim...

110 lines
3.1 KiB
TypeScript

import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
type StageBundledPluginRuntimeDeps = (params?: { cwd?: string; repoRoot?: string }) => void;
async function loadStageBundledPluginRuntimeDeps(): Promise<StageBundledPluginRuntimeDeps> {
const moduleUrl = new URL("../../scripts/stage-bundled-plugin-runtime-deps.mjs", import.meta.url);
const loaded = (await import(moduleUrl.href)) as {
stageBundledPluginRuntimeDeps: StageBundledPluginRuntimeDeps;
};
return loaded.stageBundledPluginRuntimeDeps;
}
const tempDirs: string[] = [];
function makeRepoRoot(prefix: string): string {
const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
tempDirs.push(repoRoot);
return repoRoot;
}
function writeRepoFile(repoRoot: string, relativePath: string, value: string) {
const fullPath = path.join(repoRoot, relativePath);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, value, "utf8");
}
afterEach(() => {
for (const dir of tempDirs.splice(0, tempDirs.length)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
describe("stageBundledPluginRuntimeDeps", () => {
it("drops Lark SDK type cargo while keeping runtime entrypoints", () => {
const repoRoot = makeRepoRoot("openclaw-stage-bundled-runtime-deps-");
writeRepoFile(
repoRoot,
"dist/extensions/feishu/package.json",
JSON.stringify(
{
name: "@openclaw/feishu",
version: "2026.4.5",
dependencies: {
"@larksuiteoapi/node-sdk": "^1.60.0",
},
openclaw: {
bundle: {
stageRuntimeDependencies: true,
},
},
},
null,
2,
),
);
writeRepoFile(
repoRoot,
"node_modules/@larksuiteoapi/node-sdk/package.json",
JSON.stringify(
{
name: "@larksuiteoapi/node-sdk",
version: "1.60.0",
main: "./lib/index.js",
module: "./es/index.js",
types: "./types",
},
null,
2,
),
);
writeRepoFile(
repoRoot,
"node_modules/@larksuiteoapi/node-sdk/lib/index.js",
"export const runtime = true;\n",
);
writeRepoFile(
repoRoot,
"node_modules/@larksuiteoapi/node-sdk/es/index.js",
"export const moduleRuntime = true;\n",
);
writeRepoFile(
repoRoot,
"node_modules/@larksuiteoapi/node-sdk/types/index.d.ts",
"export interface HugeTypeSurface {}\n",
);
return loadStageBundledPluginRuntimeDeps().then((stageBundledPluginRuntimeDeps) => {
stageBundledPluginRuntimeDeps({ repoRoot });
const stagedRoot = path.join(
repoRoot,
"dist",
"extensions",
"feishu",
"node_modules",
"@larksuiteoapi",
"node-sdk",
);
expect(fs.existsSync(path.join(stagedRoot, "lib", "index.js"))).toBe(true);
expect(fs.existsSync(path.join(stagedRoot, "es", "index.js"))).toBe(true);
expect(fs.existsSync(path.join(stagedRoot, "types"))).toBe(false);
});
});
});