File size: 10,477 Bytes
4c76b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Native JavaScript extension loader tests cover when compiled JS can bypass
// jiti and when alias-sensitive graphs must keep jiti resolution.
import { mkdtemp, rm, stat, utimes, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";

const jitiCalls = vi.hoisted(() => ({
  imports: [] as string[],
  options: [] as Array<Record<string, unknown>>,
}));

vi.mock("jiti/static", () => ({
  createJiti: vi.fn((_url: string, options: Record<string, unknown>) => {
    jitiCalls.options.push(options);
    return {
      import: vi.fn(async (target: string) => {
        jitiCalls.imports.push(target);
        return async (api: { registerCommand: (id: string, command: unknown) => void }) => {
          api.registerCommand(`jiti-${jitiCalls.imports.length}`, {
            description: "probe",
            handler() {},
          });
        };
      }),
    };
  }),
}));

const tempDirs: string[] = [];
let clearExtensionCache: typeof import("./loader.js").clearExtensionCache;
let loadExtensionsCached: typeof import("./loader.js").loadExtensionsCached;

beforeAll(async () => {
  vi.resetModules();
  ({ clearExtensionCache, loadExtensionsCached } = await import("./loader.js"));
});

beforeEach(() => {
  jitiCalls.imports.length = 0;
  jitiCalls.options.length = 0;
});

afterEach(async () => {
  clearExtensionCache();
  await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
});

describe("loadExtensionsCached native JavaScript path", () => {
  it("loads compiled JavaScript extensions without creating a jiti loader", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.mjs");
    await writeFile(
      extensionPath,
      `
export default async function extension(api) {
  api.registerCommand("native-js-probe", {
    description: "probe",
    handler() {},
  });
}
`,
    );

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(result.extensions[0]?.commands.has("native-js-probe")).toBe(true);
    expect(jitiCalls.options).toEqual([]);
    expect(jitiCalls.imports).toEqual([]);
  });

  it("reloads native JavaScript extensions when the file changes without stat-key drift", async () => {
    // Explicit reload clears the factory cache. Native imports still need a
    // fresh URL so same-size, same-mtime edits are observed.
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.cjs");
    const beforeSource = `
module.exports = async function(api) {
  api.registerCommand("native-reload-one", {
    description: "probe",
    handler() {},
  });
};
`;
    const afterSource = `
module.exports = async function(api) {
  api.registerCommand("native-reload-two", {
    description: "probe",
    handler() {},
  });
};
`;
    expect(afterSource).toHaveLength(beforeSource.length);
    await writeFile(extensionPath, beforeSource);

    const before = await loadExtensionsCached([extensionPath], dir);
    const beforeStat = await stat(extensionPath);

    await writeFile(extensionPath, afterSource);
    await utimes(extensionPath, beforeStat.atime, beforeStat.mtime);
    clearExtensionCache();
    const after = await loadExtensionsCached([extensionPath], dir);

    expect(before.errors).toEqual([]);
    expect(before.extensions[0]?.commands.has("native-reload-one")).toBe(true);
    expect(after.errors).toEqual([]);
    expect(after.extensions[0]?.commands.has("native-reload-two")).toBe(true);
    expect(jitiCalls.options).toEqual([]);
  });

  it("loads transpiled CommonJS default exports through the native path", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.cjs");
    await writeFile(
      extensionPath,
      `
exports.default = async function(api) {
  api.registerCommand("native-cjs-default-probe", {
    description: "probe",
    handler() {},
  });
};
`,
    );

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(result.extensions[0]?.commands.has("native-cjs-default-probe")).toBe(true);
    expect(jitiCalls.options).toEqual([]);
  });

  it("keeps CommonJS-shaped .js extensions on jiti", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    await writeFile(join(dir, "package.json"), '{"type":"module"}\n');
    const extensionPath = join(dir, "extension.js");
    await writeFile(
      extensionPath,
      `
module.exports = async function(api) {
  api.registerCommand("should-not-native-load", {
    description: "probe",
    handler() {},
  });
};
`,
    );

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([extensionPath]);
  });

  it("keeps plain ESM .js extensions on jiti", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.js");
    await writeFile(
      extensionPath,
      `
export default async function extension(api) {
  api.registerCommand("should-not-native-load", {
    description: "probe",
    handler() {},
  });
}
`,
    );

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([extensionPath]);
  });

  it("keeps SDK-alias JavaScript extensions on one shared jiti loader", async () => {
    // SDK aliases need jiti's virtual resolution, but one shared loader keeps
    // multi-extension imports consistent and cheap.
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const firstPath = join(dir, "first.js");
    const secondPath = join(dir, "second.js");
    const source = `
require("@openclaw/plugin-sdk/agent-sessions");
module.exports = async function(api) {
  api.registerCommand("should-not-native-load", {
    description: "probe",
    handler() {},
  });
};
`;
    await writeFile(firstPath, source);
    await writeFile(secondPath, source);

    const result = await loadExtensionsCached([firstPath, secondPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(2);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([firstPath, secondPath]);
  });

  it("keeps TypeBox-alias JavaScript extensions on jiti", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.js");
    await writeFile(
      extensionPath,
      `
require("typebox");
module.exports = async function(api) {
  api.registerCommand("should-not-native-load", {
    description: "probe",
    handler() {},
  });
};
`,
    );

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([extensionPath]);
  });

  it("keeps multi-file JavaScript extensions on jiti for graph-wide aliases", async () => {
    // Alias detection walks relative helper files; a clean entrypoint can still
    // need jiti when its dependency graph imports SDK/TypeBox aliases.
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.js");
    await writeFile(
      extensionPath,
      `
require("./helper.js");
module.exports = async function(api) {
  api.registerCommand("should-not-native-load", {
    description: "probe",
    handler() {},
  });
};
`,
    );
    await writeFile(join(dir, "helper.js"), 'require("typebox");\n');

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([extensionPath]);
  });

  it("keeps ESM re-export JavaScript extensions on jiti for graph-wide aliases", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    await writeFile(join(dir, "package.json"), '{"type":"module"}\n');
    const extensionPath = join(dir, "extension.js");
    await writeFile(
      extensionPath,
      `
export { helper } from "./helper.js";
export default async function extension(api) {
  api.registerCommand("should-not-native-load", {
    description: "probe",
    handler() {},
  });
}
`,
    );
    await writeFile(join(dir, "helper.js"), 'import "typebox"; export const helper = true;\n');

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([extensionPath]);
  });

  it("keeps minified ESM relative imports on jiti for graph-wide aliases", async () => {
    const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-js-"));
    tempDirs.push(dir);
    const extensionPath = join(dir, "extension.mjs");
    await writeFile(
      extensionPath,
      `import{helper}from"./helper.mjs";export{helper}from"./helper.mjs";export default async function extension(api){api.registerCommand("should-not-native-load",{description:"probe",handler(){}});}`,
    );
    await writeFile(join(dir, "helper.mjs"), 'import "typebox"; export const helper = true;\n');

    const result = await loadExtensionsCached([extensionPath], dir);

    expect(result.errors).toEqual([]);
    expect(result.extensions).toHaveLength(1);
    expect(jitiCalls.options).toHaveLength(1);
    expect(jitiCalls.imports).toEqual([extensionPath]);
  });
});