chore(test): harden channel plugin registry against malformed runtime state

This commit is contained in:
Vignesh Natarajan 2026-03-29 00:47:39 -07:00
parent 8bdb518bde
commit 08b5206b19
No known key found for this signature in database
GPG Key ID: C5E014CC92E2A144
2 changed files with 34 additions and 1 deletions

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

View File

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