mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-30 19:32:27 +00:00
chore(test): harden channel plugin registry against malformed runtime state
This commit is contained in:
24
src/channels/plugins/registry.test.ts
Normal file
24
src/channels/plugins/registry.test.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { createEmptyPluginRegistry } from "../../plugins/registry-empty.js";
|
||||
import type { PluginRegistry } from "../../plugins/registry.js";
|
||||
import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
import { listChannelPlugins } from "./registry.js";
|
||||
|
||||
function withMalformedChannels(registry: PluginRegistry): PluginRegistry {
|
||||
const malformed = { ...registry } as PluginRegistry;
|
||||
(malformed as { channels?: unknown }).channels = undefined;
|
||||
return malformed;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
resetPluginRuntimeStateForTest();
|
||||
});
|
||||
|
||||
describe("listChannelPlugins", () => {
|
||||
it("returns an empty list when runtime registry has no channels field", () => {
|
||||
const malformedRegistry = withMalformedChannels(createEmptyPluginRegistry());
|
||||
setActivePluginRegistry(malformedRegistry);
|
||||
|
||||
expect(listChannelPlugins()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -41,7 +41,16 @@ function resolveCachedChannelPlugins(): CachedChannelPlugins {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const sorted = dedupeChannels(registry.channels.map((entry) => entry.plugin)).toSorted((a, b) => {
|
||||
const channelPlugins: ChannelPlugin[] = [];
|
||||
if (Array.isArray(registry.channels)) {
|
||||
for (const entry of registry.channels) {
|
||||
if (entry?.plugin) {
|
||||
channelPlugins.push(entry.plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const sorted = dedupeChannels(channelPlugins).toSorted((a, b) => {
|
||||
const indexA = CHAT_CHANNEL_ORDER.indexOf(a.id as ChatChannelId);
|
||||
const indexB = CHAT_CHANNEL_ORDER.indexOf(b.id as ChatChannelId);
|
||||
const orderA = a.meta.order ?? (indexA === -1 ? 999 : indexA);
|
||||
|
||||
Reference in New Issue
Block a user