test(perf): standardize loader fixtures to cjs

This commit is contained in:
Peter Steinberger
2026-03-02 13:43:48 +00:00
parent 097ad88f9d
commit 60b8d645de

View File

@@ -51,7 +51,7 @@ function writePlugin(params: {
filename?: string; filename?: string;
}): TempPlugin { }): TempPlugin {
const dir = params.dir ?? makeTempDir(); const dir = params.dir ?? makeTempDir();
const filename = params.filename ?? `${params.id}.js`; const filename = params.filename ?? `${params.id}.cjs`;
const file = path.join(dir, filename); const file = path.join(dir, filename);
fs.writeFileSync(file, params.body, "utf-8"); fs.writeFileSync(file, params.body, "utf-8");
fs.writeFileSync( fs.writeFileSync(
@@ -193,8 +193,8 @@ function createWarningLogger(warnings: string[]) {
function createEscapingEntryFixture(params: { id: string; sourceBody: string }) { function createEscapingEntryFixture(params: { id: string; sourceBody: string }) {
const pluginDir = makeTempDir(); const pluginDir = makeTempDir();
const outsideDir = makeTempDir(); const outsideDir = makeTempDir();
const outsideEntry = path.join(outsideDir, "outside.js"); const outsideEntry = path.join(outsideDir, "outside.cjs");
const linkedEntry = path.join(pluginDir, "entry.js"); const linkedEntry = path.join(pluginDir, "entry.cjs");
fs.writeFileSync(outsideEntry, params.sourceBody, "utf-8"); fs.writeFileSync(outsideEntry, params.sourceBody, "utf-8");
fs.writeFileSync( fs.writeFileSync(
path.join(pluginDir, "openclaw.plugin.json"), path.join(pluginDir, "openclaw.plugin.json"),
@@ -246,9 +246,9 @@ describe("loadOpenClawPlugins", () => {
const bundledDir = makeTempDir(); const bundledDir = makeTempDir();
writePlugin({ writePlugin({
id: "bundled", id: "bundled",
body: `export default { id: "bundled", register() {} };`, body: `module.exports = { id: "bundled", register() {} };`,
dir: bundledDir, dir: bundledDir,
filename: "bundled.js", filename: "bundled.cjs",
}); });
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir;
@@ -387,7 +387,8 @@ describe("loadOpenClawPlugins", () => {
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
const plugin = writePlugin({ const plugin = writePlugin({
id: "cache-hook-runner", id: "cache-hook-runner",
body: `export default { id: "cache-hook-runner", register() {} };`, filename: "cache-hook-runner.cjs",
body: `module.exports = { id: "cache-hook-runner", register() {} };`,
}); });
const options = { const options = {
@@ -417,7 +418,8 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "alias-safe", id: "alias-safe",
body: `export default { id: "alias-safe", register() {} };`, filename: "alias-safe.cjs",
body: `module.exports = { id: "alias-safe", register() {} };`,
}); });
const realRoot = fs.realpathSync(plugin.dir); const realRoot = fs.realpathSync(plugin.dir);
if (realRoot === plugin.dir) { if (realRoot === plugin.dir) {
@@ -439,7 +441,7 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "blocked", id: "blocked",
body: `export default { id: "blocked", register() {} };`, body: `module.exports = { id: "blocked", register() {} };`,
}); });
const registry = loadRegistryFromSinglePlugin({ const registry = loadRegistryFromSinglePlugin({
@@ -458,7 +460,8 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "configurable", id: "configurable",
body: `export default { id: "configurable", register() {} };`, filename: "configurable.cjs",
body: `module.exports = { id: "configurable", register() {} };`,
}); });
const registry = loadRegistryFromSinglePlugin({ const registry = loadRegistryFromSinglePlugin({
@@ -481,7 +484,8 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "channel-demo", id: "channel-demo",
body: `export default { id: "channel-demo", register(api) { filename: "channel-demo.cjs",
body: `module.exports = { id: "channel-demo", register(api) {
api.registerChannel({ api.registerChannel({
plugin: { plugin: {
id: "demo", id: "demo",
@@ -518,7 +522,8 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "http-demo", id: "http-demo",
body: `export default { id: "http-demo", register(api) { filename: "http-demo.cjs",
body: `module.exports = { id: "http-demo", register(api) {
api.registerHttpHandler(async () => false); api.registerHttpHandler(async () => false);
} };`, } };`,
}); });
@@ -540,7 +545,8 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "http-route-demo", id: "http-route-demo",
body: `export default { id: "http-route-demo", register(api) { filename: "http-route-demo.cjs",
body: `module.exports = { id: "http-route-demo", register(api) {
api.registerHttpRoute({ path: "/demo", handler: async (_req, res) => { res.statusCode = 200; res.end("ok"); } }); api.registerHttpRoute({ path: "/demo", handler: async (_req, res) => { res.statusCode = 200; res.end("ok"); } });
} };`, } };`,
}); });
@@ -563,7 +569,7 @@ describe("loadOpenClawPlugins", () => {
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
const plugin = writePlugin({ const plugin = writePlugin({
id: "config-disable", id: "config-disable",
body: `export default { id: "config-disable", register() {} };`, body: `module.exports = { id: "config-disable", register() {} };`,
}); });
const registry = loadOpenClawPlugins({ const registry = loadOpenClawPlugins({
@@ -586,11 +592,11 @@ describe("loadOpenClawPlugins", () => {
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
const memoryA = writePlugin({ const memoryA = writePlugin({
id: "memory-a", id: "memory-a",
body: `export default { id: "memory-a", kind: "memory", register() {} };`, body: `module.exports = { id: "memory-a", kind: "memory", register() {} };`,
}); });
const memoryB = writePlugin({ const memoryB = writePlugin({
id: "memory-b", id: "memory-b",
body: `export default { id: "memory-b", kind: "memory", register() {} };`, body: `module.exports = { id: "memory-b", kind: "memory", register() {} };`,
}); });
const registry = loadOpenClawPlugins({ const registry = loadOpenClawPlugins({
@@ -613,7 +619,7 @@ describe("loadOpenClawPlugins", () => {
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
const memory = writePlugin({ const memory = writePlugin({
id: "memory-off", id: "memory-off",
body: `export default { id: "memory-off", kind: "memory", register() {} };`, body: `module.exports = { id: "memory-off", kind: "memory", register() {} };`,
}); });
const registry = loadOpenClawPlugins({ const registry = loadOpenClawPlugins({
@@ -634,15 +640,15 @@ describe("loadOpenClawPlugins", () => {
const bundledDir = makeTempDir(); const bundledDir = makeTempDir();
writePlugin({ writePlugin({
id: "shadow", id: "shadow",
body: `export default { id: "shadow", register() {} };`, body: `module.exports = { id: "shadow", register() {} };`,
dir: bundledDir, dir: bundledDir,
filename: "shadow.js", filename: "shadow.cjs",
}); });
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir;
const override = writePlugin({ const override = writePlugin({
id: "shadow", id: "shadow",
body: `export default { id: "shadow", register() {} };`, body: `module.exports = { id: "shadow", register() {} };`,
}); });
const registry = loadOpenClawPlugins({ const registry = loadOpenClawPlugins({
@@ -668,9 +674,9 @@ describe("loadOpenClawPlugins", () => {
const bundledDir = makeTempDir(); const bundledDir = makeTempDir();
writePlugin({ writePlugin({
id: "feishu", id: "feishu",
body: `export default { id: "feishu", register() {} };`, body: `module.exports = { id: "feishu", register() {} };`,
dir: bundledDir, dir: bundledDir,
filename: "index.js", filename: "index.cjs",
}); });
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir; process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir;
@@ -680,9 +686,9 @@ describe("loadOpenClawPlugins", () => {
fs.mkdirSync(globalDir, { recursive: true }); fs.mkdirSync(globalDir, { recursive: true });
writePlugin({ writePlugin({
id: "feishu", id: "feishu",
body: `export default { id: "feishu", register() {} };`, body: `module.exports = { id: "feishu", register() {} };`,
dir: globalDir, dir: globalDir,
filename: "index.js", filename: "index.cjs",
}); });
const registry = loadOpenClawPlugins({ const registry = loadOpenClawPlugins({
@@ -710,7 +716,7 @@ describe("loadOpenClawPlugins", () => {
useNoBundledPlugins(); useNoBundledPlugins();
const plugin = writePlugin({ const plugin = writePlugin({
id: "warn-open-allow", id: "warn-open-allow",
body: `export default { id: "warn-open-allow", register() {} };`, body: `module.exports = { id: "warn-open-allow", register() {} };`,
}); });
const warnings: string[] = []; const warnings: string[] = [];
loadOpenClawPlugins({ loadOpenClawPlugins({
@@ -735,9 +741,9 @@ describe("loadOpenClawPlugins", () => {
fs.mkdirSync(globalDir, { recursive: true }); fs.mkdirSync(globalDir, { recursive: true });
writePlugin({ writePlugin({
id: "rogue", id: "rogue",
body: `export default { id: "rogue", register() {} };`, body: `module.exports = { id: "rogue", register() {} };`,
dir: globalDir, dir: globalDir,
filename: "index.js", filename: "index.cjs",
}); });
const warnings: string[] = []; const warnings: string[] = [];
@@ -767,7 +773,7 @@ describe("loadOpenClawPlugins", () => {
const { outsideEntry, linkedEntry } = createEscapingEntryFixture({ const { outsideEntry, linkedEntry } = createEscapingEntryFixture({
id: "symlinked", id: "symlinked",
sourceBody: sourceBody:
'export default { id: "symlinked", register() { throw new Error("should not run"); } };', 'module.exports = { id: "symlinked", register() { throw new Error("should not run"); } };',
}); });
try { try {
fs.symlinkSync(outsideEntry, linkedEntry); fs.symlinkSync(outsideEntry, linkedEntry);
@@ -798,7 +804,7 @@ describe("loadOpenClawPlugins", () => {
const { outsideEntry, linkedEntry } = createEscapingEntryFixture({ const { outsideEntry, linkedEntry } = createEscapingEntryFixture({
id: "hardlinked", id: "hardlinked",
sourceBody: sourceBody:
'export default { id: "hardlinked", register() { throw new Error("should not run"); } };', 'module.exports = { id: "hardlinked", register() { throw new Error("should not run"); } };',
}); });
try { try {
fs.linkSync(outsideEntry, linkedEntry); fs.linkSync(outsideEntry, linkedEntry);