Skip to content

Commit 236a6b8

Browse files
authored
refactor(diffs): trim internal export surface (#107400)
1 parent 610c258 commit 236a6b8

12 files changed

Lines changed: 158 additions & 118 deletions

‎extensions/diffs/src/config.test.ts‎

Lines changed: 8 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
// Diffs tests cover config plugin behavior.
22
import fs from "node:fs";
3-
import os from "node:os";
4-
import { join } from "node:path";
5-
import { fileURLToPath, pathToFileURL } from "node:url";
63
import {
74
validateJsonSchemaValue,
85
type JsonSchemaObject,
96
} from "openclaw/plugin-sdk/json-schema-runtime";
10-
import { beforeAll, describe, expect, it, vi } from "vitest";
7+
import { beforeAll, describe, expect, it } from "vitest";
118
import {
12-
DEFAULT_DIFFS_PLUGIN_SECURITY,
13-
DEFAULT_DIFFS_TOOL_DEFAULTS,
149
diffsPluginConfigSchema,
1510
resolveDiffImageRenderOptions,
1611
resolveDiffsPluginDefaults,
1712
resolveDiffsPluginSecurity,
1813
resolveDiffsPluginViewerBaseUrl,
1914
} from "./config.js";
20-
import { resolveDiffsLanguagePackAvailability } from "./plugin.js";
2115
import { ensureCuratedViewerRuntimeForTests } from "./test-helpers.js";
2216
import { buildViewerUrl, normalizeViewerBaseUrl } from "./url.js";
2317
import {
2418
getServedLanguagePackViewerAsset,
2519
getServedViewerAsset,
26-
resolveViewerRuntimeFileUrl,
27-
LANGUAGE_PACK_VIEWER_LOADER_PATH,
28-
VIEWER_LOADER_PATH,
20+
LANGUAGE_PACK_VIEWER_ASSET_PREFIX,
21+
VIEWER_ASSET_PREFIX,
2922
VIEWER_RUNTIME_PATH,
3023
} from "./viewer-assets.js";
3124
import { parseViewerPayloadJson } from "./viewer-payload.js";
3225

26+
const DEFAULT_DIFFS_TOOL_DEFAULTS = resolveDiffsPluginDefaults(undefined);
27+
const DEFAULT_DIFFS_PLUGIN_SECURITY = resolveDiffsPluginSecurity(undefined);
28+
const VIEWER_LOADER_PATH = `${VIEWER_ASSET_PREFIX}viewer.js`;
29+
const LANGUAGE_PACK_VIEWER_LOADER_PATH = `${LANGUAGE_PACK_VIEWER_ASSET_PREFIX}viewer.js`;
30+
3331
const FULL_DEFAULTS = {
3432
fontFamily: "JetBrains Mono",
3533
fontSize: 17,
@@ -477,62 +475,6 @@ describe("diffs viewer URL helpers", () => {
477475
});
478476

479477
describe("viewer assets", () => {
480-
it("prefers the built plugin asset layout when present", async () => {
481-
const repoRoot = join(process.cwd(), "tmp", "diffs-viewer-assets-test-repo");
482-
const builtRuntimePath = join(
483-
repoRoot,
484-
"dist",
485-
"extensions",
486-
"diffs",
487-
"assets",
488-
"viewer-runtime.js",
489-
);
490-
const stat = vi.fn(async (path: string) => {
491-
if (path === builtRuntimePath) {
492-
return { mtimeMs: 1 };
493-
}
494-
const error = Object.assign(new Error(`missing: ${path}`), { code: "ENOENT" });
495-
throw error;
496-
});
497-
498-
const runtimeUrl = await resolveViewerRuntimeFileUrl({
499-
baseUrl: pathToFileURL(join(repoRoot, "dist", "extensions", "diffs", "index.js")),
500-
stat,
501-
});
502-
503-
expect(fileURLToPath(runtimeUrl)).toBe(builtRuntimePath);
504-
expect(stat).toHaveBeenCalledTimes(1);
505-
});
506-
507-
it("falls back to the source asset layout when the built artifact is absent", async () => {
508-
const repoRoot = join(process.cwd(), "tmp", "diffs-viewer-assets-test-repo");
509-
const sourceCandidatePath = join(
510-
repoRoot,
511-
"extensions",
512-
"diffs",
513-
"src",
514-
"assets",
515-
"viewer-runtime.js",
516-
);
517-
const sourceRuntimePath = join(repoRoot, "extensions", "diffs", "assets", "viewer-runtime.js");
518-
const stat = vi.fn(async (path: string) => {
519-
if (path === sourceRuntimePath) {
520-
return { mtimeMs: 1 };
521-
}
522-
const error = Object.assign(new Error(`missing: ${path}`), { code: "ENOENT" });
523-
throw error;
524-
});
525-
526-
const runtimeUrl = await resolveViewerRuntimeFileUrl({
527-
baseUrl: pathToFileURL(join(repoRoot, "extensions", "diffs", "src", "viewer-assets.js")),
528-
stat,
529-
});
530-
531-
expect(fileURLToPath(runtimeUrl)).toBe(sourceRuntimePath);
532-
expect(stat).toHaveBeenNthCalledWith(1, sourceCandidatePath);
533-
expect(stat).toHaveBeenNthCalledWith(2, sourceRuntimePath);
534-
});
535-
536478
it("serves a stable loader that points at the current runtime bundle", async () => {
537479
const loader = await getServedViewerAsset(VIEWER_LOADER_PATH);
538480

@@ -565,39 +507,6 @@ describe("viewer assets", () => {
565507
});
566508
});
567509

568-
describe("resolveDiffsLanguagePackAvailability", () => {
569-
it.each(["assets", "dist/assets"])(
570-
"requires both the sibling language-pack manifest and generated runtime asset in %s",
571-
(assetDir) => {
572-
const root = fs.mkdtempSync(join(os.tmpdir(), "openclaw-diffs-language-pack-"));
573-
try {
574-
const diffsRoot = join(root, "diffs");
575-
const languagePackRoot = join(root, "diffs-language-pack");
576-
fs.mkdirSync(diffsRoot, { recursive: true });
577-
fs.mkdirSync(languagePackRoot, { recursive: true });
578-
fs.writeFileSync(
579-
join(languagePackRoot, "openclaw.plugin.json"),
580-
'{"id":"diffs-language-pack"}\n',
581-
);
582-
const api = {
583-
rootDir: diffsRoot,
584-
config: { plugins: {} },
585-
runtime: { config: { current: () => ({ plugins: {} }) } },
586-
} as Parameters<typeof resolveDiffsLanguagePackAvailability>[0];
587-
588-
expect(resolveDiffsLanguagePackAvailability(api)).toBe(false);
589-
590-
fs.mkdirSync(join(languagePackRoot, assetDir), { recursive: true });
591-
fs.writeFileSync(join(languagePackRoot, assetDir, "viewer-runtime.js"), "export {};\n");
592-
593-
expect(resolveDiffsLanguagePackAvailability(api)).toBe(true);
594-
} finally {
595-
fs.rmSync(root, { force: true, recursive: true });
596-
}
597-
},
598-
);
599-
});
600-
601510
describe("parseViewerPayloadJson", () => {
602511
function buildValidPayload(): Record<string, unknown> {
603512
return {

‎extensions/diffs/src/config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const DEFAULT_IMAGE_QUALITY_PROFILES = {
7676
{ scale: number; maxWidth: number; maxPixels: number }
7777
>;
7878

79-
export const DEFAULT_DIFFS_TOOL_DEFAULTS: DiffToolDefaults = {
79+
const DEFAULT_DIFFS_TOOL_DEFAULTS: DiffToolDefaults = {
8080
fontFamily: "Fira Code",
8181
fontSize: 15,
8282
lineSpacing: 1.6,
@@ -98,7 +98,7 @@ type DiffsPluginSecurityConfig = {
9898
allowRemoteViewer: boolean;
9999
};
100100

101-
export const DEFAULT_DIFFS_PLUGIN_SECURITY: DiffsPluginSecurityConfig = {
101+
const DEFAULT_DIFFS_PLUGIN_SECURITY: DiffsPluginSecurityConfig = {
102102
allowRemoteViewer: false,
103103
};
104104

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import { join } from "node:path";
4+
import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
5+
import { afterAll, describe, expect, it, vi } from "vitest";
6+
import type { OpenClawConfig, OpenClawPluginApi, OpenClawPluginToolContext } from "../api.js";
7+
import { registerDiffsPlugin } from "./plugin.js";
8+
9+
const { createDiffsToolMock } = vi.hoisted(() => ({
10+
createDiffsToolMock: vi.fn(() => ({ name: "diffs" })),
11+
}));
12+
13+
vi.mock("./tool.js", () => ({
14+
createDiffsTool: createDiffsToolMock,
15+
}));
16+
17+
afterAll(() => {
18+
vi.doUnmock("./tool.js");
19+
vi.resetModules();
20+
});
21+
22+
describe("diffs plugin language-pack discovery", () => {
23+
it.each(["assets", "dist/assets"])(
24+
"requires both the sibling manifest and generated runtime asset in %s",
25+
(assetDir) => {
26+
type RegisteredTool = { name?: string };
27+
const root = fs.mkdtempSync(join(os.tmpdir(), "openclaw-diffs-language-pack-"));
28+
try {
29+
const diffsRoot = join(root, "diffs");
30+
const languagePackRoot = join(root, "diffs-language-pack");
31+
fs.mkdirSync(diffsRoot, { recursive: true });
32+
fs.mkdirSync(languagePackRoot, { recursive: true });
33+
fs.writeFileSync(
34+
join(languagePackRoot, "openclaw.plugin.json"),
35+
'{"id":"diffs-language-pack"}\n',
36+
);
37+
const config = { plugins: {} } as OpenClawConfig;
38+
let registeredToolFactory:
39+
| ((
40+
ctx: OpenClawPluginToolContext,
41+
) => RegisteredTool | RegisteredTool[] | null | undefined)
42+
| undefined;
43+
const api = createTestPluginApi({
44+
rootDir: diffsRoot,
45+
config,
46+
runtime: { config: { current: () => config } } as never,
47+
registerTool(tool: Parameters<OpenClawPluginApi["registerTool"]>[0]) {
48+
registeredToolFactory = typeof tool === "function" ? tool : () => tool;
49+
},
50+
});
51+
52+
registerDiffsPlugin(api);
53+
const context = {
54+
agentId: "main",
55+
sessionId: "session-1",
56+
messageChannel: "test",
57+
agentAccountId: "default",
58+
} satisfies OpenClawPluginToolContext;
59+
60+
registeredToolFactory?.(context);
61+
expect(createDiffsToolMock).toHaveBeenLastCalledWith(
62+
expect.objectContaining({ languagePackAvailable: false }),
63+
);
64+
65+
fs.mkdirSync(join(languagePackRoot, assetDir), { recursive: true });
66+
fs.writeFileSync(join(languagePackRoot, assetDir, "viewer-runtime.js"), "export {};\n");
67+
68+
registeredToolFactory?.(context);
69+
expect(createDiffsToolMock).toHaveBeenLastCalledWith(
70+
expect.objectContaining({ languagePackAvailable: true }),
71+
);
72+
} finally {
73+
fs.rmSync(root, { force: true, recursive: true });
74+
createDiffsToolMock.mockClear();
75+
}
76+
},
77+
);
78+
});

‎extensions/diffs/src/plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function registerDiffsPlugin(api: OpenClawPluginApi): void {
7777
}));
7878
}
7979

80-
export function resolveDiffsLanguagePackAvailability(api: OpenClawPluginApi): boolean {
80+
function resolveDiffsLanguagePackAvailability(api: OpenClawPluginApi): boolean {
8181
const currentConfig = (api.runtime.config?.current?.() ?? api.config) as OpenClawConfig;
8282
const plugins = currentConfig.plugins;
8383
if (plugins?.enabled === false) {

‎extensions/diffs/src/render-target.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ afterAll(() => {
2525
vi.resetModules();
2626
});
2727

28-
import { DEFAULT_DIFFS_TOOL_DEFAULTS, resolveDiffImageRenderOptions } from "./config.js";
28+
import { resolveDiffImageRenderOptions, resolveDiffsPluginDefaults } from "./config.js";
2929
import { renderDiffDocument } from "./render.js";
3030
import { parseViewerPayloadJson } from "./viewer-payload.js";
3131

32+
const DEFAULT_DIFFS_TOOL_DEFAULTS = resolveDiffsPluginDefaults(undefined);
33+
3234
function createRenderOptions() {
3335
return {
3436
presentation: DEFAULT_DIFFS_TOOL_DEFAULTS,

‎extensions/diffs/src/render.preload.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import { disposeHighlighter } from "@pierre/diffs";
33
import * as diffsSsr from "@pierre/diffs/ssr";
44
import { afterEach, describe, expect, it, vi } from "vitest";
5-
import { DEFAULT_DIFFS_TOOL_DEFAULTS, resolveDiffImageRenderOptions } from "./config.js";
5+
import { resolveDiffImageRenderOptions, resolveDiffsPluginDefaults } from "./config.js";
66
import { renderDiffDocument } from "./render.js";
77

8+
const DEFAULT_DIFFS_TOOL_DEFAULTS = resolveDiffsPluginDefaults(undefined);
9+
810
vi.mock("@pierre/diffs/ssr", async (importOriginal) => {
911
const actual = await importOriginal<typeof import("@pierre/diffs/ssr")>();
1012
return {

‎extensions/diffs/src/render.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Diffs tests cover render plugin behavior.
22
import { disposeHighlighter } from "@pierre/diffs";
33
import { afterEach, describe, expect, it } from "vitest";
4-
import { DEFAULT_DIFFS_TOOL_DEFAULTS, resolveDiffImageRenderOptions } from "./config.js";
4+
import { resolveDiffImageRenderOptions, resolveDiffsPluginDefaults } from "./config.js";
55
import { renderDiffDocument } from "./render.js";
66
import { parseViewerPayloadJson } from "./viewer-payload.js";
77

8+
const DEFAULT_DIFFS_TOOL_DEFAULTS = resolveDiffsPluginDefaults(undefined);
9+
810
describe("renderDiffDocument", () => {
911
afterEach(async () => {
1012
await disposeHighlighter();

‎extensions/diffs/src/tool-render-output.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
55
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
66
import type { OpenClawPluginApi } from "../api.js";
77
import type { DiffScreenshotter } from "./browser.js";
8-
import { DEFAULT_DIFFS_TOOL_DEFAULTS } from "./config.js";
8+
import { resolveDiffsPluginDefaults } from "./config.js";
99
import { createDiffStoreHarness } from "./test-helpers.js";
1010

11+
const DEFAULT_DIFFS_TOOL_DEFAULTS = resolveDiffsPluginDefaults(undefined);
12+
1113
const { renderDiffDocumentMock } = vi.hoisted(() => ({
1214
renderDiffDocumentMock: vi.fn(),
1315
}));

‎extensions/diffs/src/tool.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
55
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import type { OpenClawPluginApi, OpenClawPluginToolContext } from "../api.js";
77
import type { DiffScreenshotter } from "./browser.js";
8-
import { DEFAULT_DIFFS_TOOL_DEFAULTS } from "./config.js";
8+
import { resolveDiffsPluginDefaults } from "./config.js";
99
import { DiffArtifactStore } from "./store.js";
1010
import { createDiffStoreHarness } from "./test-helpers.js";
1111
import { createDiffsTool } from "./tool.js";
1212
import type { DiffRenderOptions } from "./types.js";
1313

14+
const DEFAULT_DIFFS_TOOL_DEFAULTS = resolveDiffsPluginDefaults(undefined);
15+
1416
describe("diffs tool", () => {
1517
let rootDir: string;
1618
let store: DiffArtifactStore;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fs from "node:fs/promises";
2+
import { fileURLToPath } from "node:url";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
5+
const VIEWER_LOADER_PATH = "/plugins/diffs/assets/viewer.js";
6+
const BUILT_RUNTIME_PATH = fileURLToPath(new URL("./assets/viewer-runtime.js", import.meta.url));
7+
const SOURCE_RUNTIME_PATH = fileURLToPath(new URL("../assets/viewer-runtime.js", import.meta.url));
8+
9+
function missingFile(path: string): NodeJS.ErrnoException {
10+
return Object.assign(new Error(`missing: ${path}`), { code: "ENOENT" });
11+
}
12+
13+
async function loadViewerWithRuntimeAt(runtimePath: string) {
14+
const stat = vi.spyOn(fs, "stat").mockImplementation(async (path) => {
15+
const candidate = String(path);
16+
if (candidate === runtimePath) {
17+
return { mtimeMs: 1 } as never;
18+
}
19+
throw missingFile(candidate);
20+
});
21+
vi.spyOn(fs, "readFile").mockResolvedValue(
22+
Buffer.from("window.openclawDiffsReady = true;\n") as never,
23+
);
24+
const { getServedViewerAsset } = await import("./viewer-assets.js");
25+
const loader = await getServedViewerAsset(VIEWER_LOADER_PATH);
26+
expect(loader?.contentType).toBe("text/javascript; charset=utf-8");
27+
expect(String(loader?.body)).toContain("./viewer-runtime.js?v=");
28+
return stat;
29+
}
30+
31+
afterEach(() => {
32+
vi.restoreAllMocks();
33+
vi.resetModules();
34+
});
35+
36+
describe("viewer runtime resolution", () => {
37+
it("prefers the built plugin asset layout when present", async () => {
38+
const stat = await loadViewerWithRuntimeAt(BUILT_RUNTIME_PATH);
39+
40+
expect(stat).toHaveBeenNthCalledWith(1, BUILT_RUNTIME_PATH);
41+
expect(stat).toHaveBeenNthCalledWith(2, BUILT_RUNTIME_PATH);
42+
});
43+
44+
it("falls back to the source asset layout when the built artifact is absent", async () => {
45+
const stat = await loadViewerWithRuntimeAt(SOURCE_RUNTIME_PATH);
46+
47+
expect(stat).toHaveBeenNthCalledWith(1, BUILT_RUNTIME_PATH);
48+
expect(stat).toHaveBeenNthCalledWith(2, SOURCE_RUNTIME_PATH);
49+
expect(stat).toHaveBeenNthCalledWith(3, SOURCE_RUNTIME_PATH);
50+
});
51+
});

0 commit comments

Comments
 (0)