fix(config): lazy bootstrap markdown table defaults

This commit is contained in:
Vincent Koc
2026-04-05 13:04:12 +01:00
parent 227a13bd55
commit 35b132c7eb
2 changed files with 57 additions and 2 deletions

View 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);
});
});

View File

@@ -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;
}