Skip to content

Commit b0d230f

Browse files
committed
fix(doctor): read packaged channel metadata
Punchcard-Session: amber-workshop-river-yr
1 parent 3f72ccf commit b0d230f

6 files changed

Lines changed: 148 additions & 21 deletions

‎src/channels/bundled-channel-catalog-read.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ vi.mock("../infra/openclaw-root.js", () => ({
3939

4040
import { resolveBundledPluginsDir } from "../plugins/bundled-dir.js";
4141
import { clearPluginMetadataLifecycleCaches } from "../plugins/plugin-metadata-lifecycle.js";
42-
import { listBundledChannelCatalogEntries } from "./bundled-channel-catalog-read.js";
42+
import {
43+
findBundledChannelCatalogMetadata,
44+
listBundledChannelCatalogEntries,
45+
} from "./bundled-channel-catalog-read.js";
4346
import { listBundledChannelIds } from "./plugins/bundled-ids.js";
4447

4548
const tempDirs: string[] = [];
@@ -126,6 +129,12 @@ function seedGeneratedChannelCatalog(
126129
label: string;
127130
docsPath: string;
128131
blurb: string;
132+
doctorCapabilities?: {
133+
dmAllowFromMode?: "topOnly" | "nestedOnly";
134+
groupModel?: "sender" | "route" | "hybrid";
135+
groupAllowFromFallbackToAllowFrom?: boolean;
136+
warnOnEmptyGroupSenderAllowlist?: boolean;
137+
};
129138
},
130139
): void {
131140
const { packageName, ...channel } = params;
@@ -222,6 +231,31 @@ describe("listBundledChannelCatalogEntries", () => {
222231
expect(ids.has("telegram")).toBe(true);
223232
});
224233

234+
it("finds doctor capabilities from the generated catalog when the package is excluded", () => {
235+
const root = seedRoot("bcr-generated-doctor-");
236+
useBundledPluginsDir(undefined);
237+
seedGeneratedChannelCatalog(root, {
238+
packageName: "@openclaw/discord",
239+
id: "discord",
240+
label: "Discord",
241+
docsPath: "/channels/discord",
242+
blurb: "downloadable channel",
243+
doctorCapabilities: {
244+
dmAllowFromMode: "topOnly",
245+
groupModel: "route",
246+
groupAllowFromFallbackToAllowFrom: false,
247+
warnOnEmptyGroupSenderAllowlist: false,
248+
},
249+
});
250+
251+
expect(findBundledChannelCatalogMetadata("Discord")?.doctorCapabilities).toEqual({
252+
dmAllowFromMode: "topOnly",
253+
groupModel: "route",
254+
groupAllowFromFallbackToAllowFrom: false,
255+
warnOnEmptyGroupSenderAllowlist: false,
256+
});
257+
});
258+
225259
it("keeps bundled package metadata when generated catalog entries are stale", () => {
226260
const root = seedRoot("bcr-package-wins-");
227261
const extensionsRoot = path.join(root, "dist", "extensions");

‎src/channels/bundled-channel-catalog-read.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,16 @@ export function listBundledChannelCatalogEntries(): BundledChannelCatalogEntry[]
157157
(left, right) => left.order - right.order || left.id.localeCompare(right.id),
158158
);
159159
}
160+
161+
/** Finds bundled or generated channel metadata by id or alias. */
162+
export function findBundledChannelCatalogMetadata(
163+
channelId: string,
164+
): PluginPackageChannel | undefined {
165+
const normalized = normalizeOptionalLowercaseString(channelId);
166+
if (!normalized) {
167+
return undefined;
168+
}
169+
return listBundledChannelCatalogEntries().find(
170+
(entry) => entry.id === normalized || entry.aliases.includes(normalized),
171+
)?.channel;
172+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Packaged doctor capability tests cover generated catalog lookup without plugin runtime loading.
2+
import path from "node:path";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import {
5+
cleanupTempDirs,
6+
makeTempRepoRoot,
7+
writeJsonFile,
8+
} from "../../../test/helpers/temp-repo.js";
9+
10+
const packageRootMock = vi.hoisted(() => ({ value: "" }));
11+
const channelPluginMocks = vi.hoisted(() => ({
12+
getBundledChannelPlugin: vi.fn(() => undefined),
13+
getChannelPlugin: vi.fn(() => undefined),
14+
}));
15+
16+
vi.mock("../../channels/plugins/bundled.js", () => ({
17+
getBundledChannelPlugin: channelPluginMocks.getBundledChannelPlugin,
18+
}));
19+
20+
vi.mock("../../channels/plugins/index.js", () => ({
21+
getChannelPlugin: channelPluginMocks.getChannelPlugin,
22+
}));
23+
24+
vi.mock("../../plugins/bundled-dir.js", () => ({
25+
resolveBundledPluginsDir: () => undefined,
26+
resolveSourceCheckoutDependencyDiagnostic: () => null,
27+
}));
28+
29+
vi.mock("../../infra/openclaw-root.js", () => ({
30+
resolveOpenClawPackageRootSync: () => packageRootMock.value,
31+
resolveOpenClawPackageRoot: async () => packageRootMock.value,
32+
}));
33+
34+
import { clearPluginMetadataLifecycleCaches } from "../../plugins/plugin-metadata-lifecycle.js";
35+
import { getDoctorChannelCapabilities } from "./channel-capabilities.js";
36+
37+
const tempDirs: string[] = [];
38+
39+
beforeEach(() => {
40+
const root = makeTempRepoRoot(tempDirs, "doctor-channel-packaged-");
41+
packageRootMock.value = root;
42+
writeJsonFile(path.join(root, "package.json"), { name: "openclaw" });
43+
writeJsonFile(path.join(root, "dist", "channel-catalog.json"), {
44+
entries: [
45+
{
46+
name: "@openclaw/discord",
47+
openclaw: {
48+
channel: {
49+
id: "discord",
50+
label: "Discord",
51+
doctorCapabilities: {
52+
dmAllowFromMode: "topOnly",
53+
groupModel: "route",
54+
groupAllowFromFallbackToAllowFrom: false,
55+
warnOnEmptyGroupSenderAllowlist: false,
56+
},
57+
},
58+
},
59+
},
60+
],
61+
});
62+
clearPluginMetadataLifecycleCaches();
63+
channelPluginMocks.getBundledChannelPlugin.mockReset().mockReturnValue(undefined);
64+
channelPluginMocks.getChannelPlugin.mockReset().mockReturnValue(undefined);
65+
});
66+
67+
afterEach(() => {
68+
packageRootMock.value = "";
69+
clearPluginMetadataLifecycleCaches();
70+
cleanupTempDirs(tempDirs);
71+
vi.restoreAllMocks();
72+
});
73+
74+
describe("doctor channel capabilities in a packaged install", () => {
75+
it("reads Discord semantics from the generated catalog without loading a plugin", () => {
76+
expect(getDoctorChannelCapabilities("discord")).toEqual({
77+
dmAllowFromMode: "topOnly",
78+
groupModel: "route",
79+
groupAllowFromFallbackToAllowFrom: false,
80+
warnOnEmptyGroupSenderAllowlist: false,
81+
});
82+
expect(channelPluginMocks.getChannelPlugin).not.toHaveBeenCalled();
83+
expect(channelPluginMocks.getBundledChannelPlugin).not.toHaveBeenCalled();
84+
});
85+
});

‎src/commands/doctor/channel-capabilities.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { findBundledChannelCatalogMetadata } from "../../channels/bundled-channel-catalog-read.js";
12
// Doctor capability lookup for channel-specific policy and migration behavior.
23
import { getBundledChannelPlugin } from "../../channels/plugins/bundled.js";
34
import type { ChannelDmAllowFromMode } from "../../channels/plugins/dm-access.js";
45
import { getChannelPlugin } from "../../channels/plugins/index.js";
56
import { normalizeAnyChannelId } from "../../channels/registry.js";
67
import type { OpenClawConfig } from "../../config/types.openclaw.js";
7-
import { findBundledPackageChannelMetadata } from "../../plugins/bundled-package-channel-metadata.js";
88
import type { PluginPackageChannelDoctorCapabilities } from "../../plugins/manifest.js";
99

1010
type DoctorGroupModel = "sender" | "route" | "hybrid";
@@ -39,10 +39,10 @@ function mergeDoctorChannelCapabilities(
3939
};
4040
}
4141

42-
function getManifestDoctorCapabilities(
42+
function getCatalogDoctorCapabilities(
4343
channelId: string,
4444
): PluginPackageChannelDoctorCapabilities | undefined {
45-
return findBundledPackageChannelMetadata(channelId)?.doctorCapabilities;
45+
return findBundledChannelCatalogMetadata(channelId)?.doctorCapabilities;
4646
}
4747

4848
/** Resolve doctor behavior capabilities from channel metadata, plugin runtime, or defaults. */
@@ -51,9 +51,9 @@ export function getDoctorChannelCapabilities(channelName?: string): DoctorChanne
5151
return DEFAULT_DOCTOR_CHANNEL_CAPABILITIES;
5252
}
5353

54-
const manifestCapabilities = getManifestDoctorCapabilities(channelName);
55-
if (manifestCapabilities) {
56-
return mergeDoctorChannelCapabilities(manifestCapabilities);
54+
const catalogCapabilities = getCatalogDoctorCapabilities(channelName);
55+
if (catalogCapabilities) {
56+
return mergeDoctorChannelCapabilities(catalogCapabilities);
5757
}
5858

5959
const channelId = normalizeAnyChannelId(channelName);
@@ -65,7 +65,7 @@ export function getDoctorChannelCapabilities(channelName?: string): DoctorChanne
6565
if (pluginDoctor) {
6666
return mergeDoctorChannelCapabilities(pluginDoctor);
6767
}
68-
return mergeDoctorChannelCapabilities(getManifestDoctorCapabilities(channelId));
68+
return mergeDoctorChannelCapabilities(getCatalogDoctorCapabilities(channelId));
6969
}
7070

7171
type DoctorChannelAccountIds = {

‎src/plugins/bundled-package-channel-metadata.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ vi.mock("./bundled-dir.js", () => ({
1010
}));
1111

1212
import { resolveBundledPluginsDir } from "./bundled-dir.js";
13-
import { findBundledPackageChannelMetadata } from "./bundled-package-channel-metadata.js";
13+
import { listBundledPackageChannelMetadata } from "./bundled-package-channel-metadata.js";
1414
import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js";
1515

1616
const tempDirs: string[] = [];
@@ -72,7 +72,7 @@ describe("bundled package channel metadata", () => {
7272
);
7373
useBundledPluginsDir(extensionsRoot);
7474

75-
const matrix = findBundledPackageChannelMetadata("matrix");
75+
const matrix = listBundledPackageChannelMetadata().find((channel) => channel.id === "matrix");
7676

7777
expect(matrix?.doctorCapabilities).toEqual({
7878
dmAllowFromMode: "nestedOnly",
@@ -107,7 +107,9 @@ describe("bundled package channel metadata", () => {
107107
"export default {};\n",
108108
"utf8",
109109
);
110-
expect(findBundledPackageChannelMetadata("matrix")?.label).toBe("Before");
110+
expect(
111+
listBundledPackageChannelMetadata().find((channel) => channel.id === "matrix")?.label,
112+
).toBe("Before");
111113

112114
writeJsonFile(packagePath, {
113115
name: "@openclaw/matrix",
@@ -120,6 +122,8 @@ describe("bundled package channel metadata", () => {
120122
});
121123

122124
clearPluginMetadataLifecycleCaches();
123-
expect(findBundledPackageChannelMetadata("matrix")?.label).toBe("After");
125+
expect(
126+
listBundledPackageChannelMetadata().find((channel) => channel.id === "matrix")?.label,
127+
).toBe("After");
124128
});
125129
});

‎src/plugins/bundled-package-channel-metadata.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,3 @@ import type { PluginPackageChannel } from "./manifest.js";
66
export function listBundledPackageChannelMetadata(): readonly PluginPackageChannel[] {
77
return listChannelCatalogEntries({ origin: "bundled" }).map((entry) => entry.channel);
88
}
9-
10-
/** Finds bundled package channel metadata by id or alias. */
11-
export function findBundledPackageChannelMetadata(
12-
channelId: string,
13-
): PluginPackageChannel | undefined {
14-
return listBundledPackageChannelMetadata().find(
15-
(channel) => channel.id === channelId || channel.aliases?.includes(channelId),
16-
);
17-
}

0 commit comments

Comments
 (0)