|
2 | 2 | import fs from "node:fs"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
5 | | -import { readRegularFileSync } from "../infra/regular-file.js"; |
| 5 | + |
| 6 | +const logWarnSpy = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("../logging/subsystem.js", () => ({ |
| 9 | + createSubsystemLogger: () => ({ warn: logWarnSpy }), |
| 10 | +})); |
| 11 | + |
6 | 12 | import { |
7 | 13 | applyPluginAutoEnable, |
8 | 14 | materializePluginAutoEnableCandidates, |
@@ -34,6 +40,7 @@ beforeEach(() => { |
34 | 40 |
|
35 | 41 | afterEach(() => { |
36 | 42 | resetPluginAutoEnableTestState(); |
| 43 | + logWarnSpy.mockClear(); |
37 | 44 | }); |
38 | 45 |
|
39 | 46 | describe("applyPluginAutoEnable channels", () => { |
@@ -239,55 +246,50 @@ describe("applyPluginAutoEnable channels", () => { |
239 | 246 | const stateDir = makeTempDir(); |
240 | 247 | const catalogPath = path.join(stateDir, "plugins", "catalog.json"); |
241 | 248 | fs.mkdirSync(path.dirname(catalogPath), { recursive: true }); |
242 | | - // Write a valid-but-tiny catalog so the stat check passes. The test |
243 | | - // intercepts the read itself to simulate an oversized rejection. |
244 | | - fs.writeFileSync(catalogPath, "{}", "utf-8"); |
245 | | - |
246 | | - // Intercept readRegularFileSync to throw as if the file exceeded the cap. |
247 | | - const readStub = vi |
248 | | - .spyOn({ readRegularFileSync }, "readRegularFileSync" as never) |
249 | | - .mockImplementationOnce(() => { |
250 | | - throw Object.assign(new Error("File exceeds 16777216 bytes: /fake/catalog.json"), { |
251 | | - code: "ERR_FS_FILE_TOO_LARGE", |
252 | | - }); |
253 | | - }); |
254 | | - |
| 249 | + // Create a sparse file whose stat.size exceeds the 16 MiB cap without |
| 250 | + // allocating actual disk blocks — the bounded read rejects it by size |
| 251 | + // before loading content into memory. |
| 252 | + const fd = fs.openSync(catalogPath, "w"); |
255 | 253 | try { |
256 | | - const result = materializePluginAutoEnableCandidates({ |
257 | | - config: { |
258 | | - channels: { |
259 | | - "env-primary": { token: "primary" }, |
260 | | - "env-secondary": { token: "secondary" }, |
261 | | - }, |
| 254 | + fs.writeSync(fd, "{}\n"); |
| 255 | + fs.ftruncateSync(fd, 17 * 1024 * 1024); |
| 256 | + } finally { |
| 257 | + fs.closeSync(fd); |
| 258 | + } |
| 259 | + |
| 260 | + const result = materializePluginAutoEnableCandidates({ |
| 261 | + config: { |
| 262 | + channels: { |
| 263 | + "env-primary": { token: "primary" }, |
| 264 | + "env-secondary": { token: "secondary" }, |
262 | 265 | }, |
263 | | - candidates: [ |
264 | | - { |
265 | | - pluginId: "env-primary", |
266 | | - kind: "channel-configured" as const, |
267 | | - channelId: "env-primary", |
268 | | - }, |
269 | | - { |
270 | | - pluginId: "env-secondary", |
271 | | - kind: "channel-configured" as const, |
272 | | - channelId: "env-secondary", |
273 | | - }, |
274 | | - ], |
275 | | - env: { |
276 | | - ...makeIsolatedEnv(), |
277 | | - OPENCLAW_STATE_DIR: stateDir, |
278 | | - OPENCLAW_BUNDLED_PLUGINS_DIR: "/nonexistent/bundled/plugins", |
| 266 | + }, |
| 267 | + candidates: [ |
| 268 | + { |
| 269 | + pluginId: "env-primary", |
| 270 | + kind: "channel-configured" as const, |
| 271 | + channelId: "env-primary", |
279 | 272 | }, |
280 | | - manifestRegistry: makeRegistry([]), |
281 | | - }); |
| 273 | + { |
| 274 | + pluginId: "env-secondary", |
| 275 | + kind: "channel-configured" as const, |
| 276 | + channelId: "env-secondary", |
| 277 | + }, |
| 278 | + ], |
| 279 | + env: { |
| 280 | + ...makeIsolatedEnv(), |
| 281 | + OPENCLAW_STATE_DIR: stateDir, |
| 282 | + OPENCLAW_BUNDLED_PLUGINS_DIR: "/nonexistent/bundled/plugins", |
| 283 | + }, |
| 284 | + manifestRegistry: makeRegistry([]), |
| 285 | + }); |
282 | 286 |
|
283 | | - // The oversized catalog is skipped; without its preferOver data, |
284 | | - // env-secondary does NOT claim precedence over env-primary, so |
285 | | - // env-primary should still be present (not overridden). |
286 | | - expect(result.config.plugins?.entries?.["env-secondary"]?.enabled).toBe(true); |
287 | | - expect(result.config.plugins?.entries?.["env-primary"]).toBeUndefined ? undefined : undefined; |
288 | | - } finally { |
289 | | - readStub.mockRestore(); |
290 | | - } |
| 287 | + // Selection continues: env-secondary is still auto-enabled. |
| 288 | + expect(result.config.plugins?.entries?.["env-secondary"]?.enabled).toBe(true); |
| 289 | + // Warning was logged for the oversized catalog. |
| 290 | + expect(logWarnSpy).toHaveBeenCalledWith( |
| 291 | + expect.stringContaining("skipping oversized external catalog file"), |
| 292 | + ); |
291 | 293 | }); |
292 | 294 |
|
293 | 295 | describe("third-party channel plugins", () => { |
|
0 commit comments