Skip to content

Commit 4fdc9bd

Browse files
committed
fix(config): fix false-positive oversized catalog test
Replace the broken spy-on-object-literal mock with a real sparse file that genuinely triggers readRegularFileSync rejection via stat.size. The previous vi.spyOn({ readRegularFileSync }, ...) intercepted a throwaway object, never the actual module import, so the test passed regardless of whether the fix was applied or not. Also fix the no-unused-expressions lint error on the env-primary assertion that used a discarded ternary (? undefined : undefined) instead of a proper expect assertion.
1 parent 31a9f08 commit 4fdc9bd

1 file changed

Lines changed: 48 additions & 46 deletions

File tree

‎src/config/plugin-auto-enable.channels.test.ts‎

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import fs from "node:fs";
33
import path from "node:path";
44
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+
612
import {
713
applyPluginAutoEnable,
814
materializePluginAutoEnableCandidates,
@@ -34,6 +40,7 @@ beforeEach(() => {
3440

3541
afterEach(() => {
3642
resetPluginAutoEnableTestState();
43+
logWarnSpy.mockClear();
3744
});
3845

3946
describe("applyPluginAutoEnable channels", () => {
@@ -239,55 +246,50 @@ describe("applyPluginAutoEnable channels", () => {
239246
const stateDir = makeTempDir();
240247
const catalogPath = path.join(stateDir, "plugins", "catalog.json");
241248
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");
255253
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" },
262265
},
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",
279272
},
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+
});
282286

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+
);
291293
});
292294

293295
describe("third-party channel plugins", () => {

0 commit comments

Comments
 (0)