mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-27 00:52:05 +00:00
fix(config): lazy bootstrap markdown table defaults
This commit is contained in:
39
src/config/markdown-tables.import.test.ts
Normal file
39
src/config/markdown-tables.import.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const listBootstrapChannelPlugins = vi.hoisted(() =>
|
||||
vi.fn(() => [
|
||||
{
|
||||
id: "signal",
|
||||
messaging: {
|
||||
defaultMarkdownTableMode: "bullets",
|
||||
},
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
vi.mock("../channels/plugins/bootstrap-registry.js", () => ({
|
||||
listBootstrapChannelPlugins,
|
||||
}));
|
||||
|
||||
describe("markdown table defaults import behavior", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
listBootstrapChannelPlugins.mockClear();
|
||||
});
|
||||
|
||||
it("does not bootstrap channel plugins during module import", async () => {
|
||||
const module = await import("./markdown-tables.js");
|
||||
|
||||
expect(module.DEFAULT_TABLE_MODES).toBeDefined();
|
||||
expect(listBootstrapChannelPlugins).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("loads bootstrap defaults lazily on first access and memoizes them", async () => {
|
||||
const { DEFAULT_TABLE_MODES, resolveMarkdownTableMode } = await import("./markdown-tables.js");
|
||||
|
||||
expect(DEFAULT_TABLE_MODES.get("signal")).toBe("bullets");
|
||||
expect(listBootstrapChannelPlugins).toHaveBeenCalledTimes(1);
|
||||
expect(resolveMarkdownTableMode({ channel: "signal" })).toBe("bullets");
|
||||
expect(listBootstrapChannelPlugins).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -33,7 +33,23 @@ function getDefaultTableModes(): Map<string, MarkdownTableMode> {
|
||||
return cachedDefaultTableModes;
|
||||
}
|
||||
|
||||
export const DEFAULT_TABLE_MODES = getDefaultTableModes();
|
||||
const EMPTY_DEFAULT_TABLE_MODES = new Map<string, MarkdownTableMode>();
|
||||
|
||||
function bindDefaultTableModesMethod<TValue>(value: TValue): TValue {
|
||||
if (typeof value !== "function") {
|
||||
return value;
|
||||
}
|
||||
return value.bind(getDefaultTableModes()) as TValue;
|
||||
}
|
||||
|
||||
export const DEFAULT_TABLE_MODES: ReadonlyMap<string, MarkdownTableMode> = new Proxy(
|
||||
EMPTY_DEFAULT_TABLE_MODES,
|
||||
{
|
||||
get(_target, prop, _receiver) {
|
||||
return bindDefaultTableModesMethod(Reflect.get(getDefaultTableModes(), prop));
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const isMarkdownTableMode = (value: unknown): value is MarkdownTableMode =>
|
||||
value === "off" || value === "bullets" || value === "code" || value === "block";
|
||||
@@ -64,7 +80,7 @@ export function resolveMarkdownTableMode(params: {
|
||||
accountId?: string | null;
|
||||
}): MarkdownTableMode {
|
||||
const channel = normalizeChannelId(params.channel);
|
||||
const defaultMode = channel ? (DEFAULT_TABLE_MODES.get(channel) ?? "code") : "code";
|
||||
const defaultMode = channel ? (getDefaultTableModes().get(channel) ?? "code") : "code";
|
||||
if (!channel || !params.cfg) {
|
||||
return defaultMode;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user