gateway: reuse account resolution for health monitor overrides

This commit is contained in:
Tak Hoffman 2026-03-14 20:17:45 -05:00
parent 84292d92bf
commit 4399782bea
2 changed files with 68 additions and 20 deletions

View File

@ -44,12 +44,13 @@ function createTestPlugin(params?: {
account?: TestAccount;
startAccount?: NonNullable<ChannelPlugin<TestAccount>["gateway"]>["startAccount"];
includeDescribeAccount?: boolean;
resolveAccount?: ChannelPlugin<TestAccount>["config"]["resolveAccount"];
}): ChannelPlugin<TestAccount> {
const account = params?.account ?? { enabled: true, configured: true };
const includeDescribeAccount = params?.includeDescribeAccount !== false;
const config: ChannelPlugin<TestAccount>["config"] = {
listAccountIds: () => [DEFAULT_ACCOUNT_ID],
resolveAccount: () => account,
resolveAccount: params?.resolveAccount ?? (() => account),
isEnabled: (resolved) => resolved.enabled !== false,
};
if (includeDescribeAccount) {
@ -88,13 +89,16 @@ function installTestRegistry(plugin: ChannelPlugin<TestAccount>) {
setActivePluginRegistry(registry);
}
function createManager(options?: { channelRuntime?: PluginRuntime["channel"] }) {
function createManager(options?: {
channelRuntime?: PluginRuntime["channel"];
loadConfig?: () => Record<string, unknown>;
}) {
const log = createSubsystemLogger("gateway/server-channels-test");
const channelLogs = { discord: log } as Record<ChannelId, SubsystemLogger>;
const runtime = runtimeForLogger(log);
const channelRuntimeEnvs = { discord: runtime } as Record<ChannelId, RuntimeEnv>;
return createChannelManager({
loadConfig: () => ({}),
loadConfig: () => options?.loadConfig?.() ?? {},
channelLogs,
channelRuntimeEnvs,
...(options?.channelRuntime ? { channelRuntime: options.channelRuntime } : {}),
@ -180,4 +184,55 @@ describe("server-channels auto restart", () => {
await manager.startChannels();
expect(startAccount).toHaveBeenCalledTimes(1);
});
it("reuses plugin account resolution for health monitor overrides", () => {
installTestRegistry(
createTestPlugin({
resolveAccount: (cfg, accountId) => {
const accounts = (
cfg as {
channels?: {
discord?: {
accounts?: Record<
string,
TestAccount & { healthMonitor?: { enabled?: boolean } }
>;
};
};
}
).channels?.discord?.accounts;
if (!accounts) {
return { enabled: true, configured: true };
}
const direct = accounts[accountId ?? DEFAULT_ACCOUNT_ID];
if (direct) {
return direct;
}
const normalized = (accountId ?? DEFAULT_ACCOUNT_ID).toLowerCase().replaceAll(" ", "-");
const matchKey = Object.keys(accounts).find(
(key) => key.toLowerCase().replaceAll(" ", "-") === normalized,
);
return matchKey ? (accounts[matchKey] ?? { enabled: true, configured: true }) : {};
},
}),
);
const manager = createManager({
loadConfig: () => ({
channels: {
discord: {
accounts: {
"Router D": {
enabled: true,
configured: true,
healthMonitor: { enabled: false },
},
},
},
},
}),
});
expect(manager.isHealthMonitorEnabled("discord", "router-d")).toBe(false);
});
});

View File

@ -31,16 +31,6 @@ type ChannelRuntimeStore = {
runtimes: Map<string, ChannelAccountSnapshot>;
};
type RawHealthMonitorEntry = {
healthMonitor?: {
enabled?: boolean;
};
};
type RawChannelConfig = RawHealthMonitorEntry & {
accounts?: Record<string, RawHealthMonitorEntry | undefined>;
};
function createRuntimeStore(): ChannelRuntimeStore {
return {
aborts: new Map(),
@ -132,17 +122,20 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
const isHealthMonitorEnabled = (channelId: ChannelId, accountId: string): boolean => {
const cfg = loadConfig();
const channelConfig = cfg.channels?.[channelId] as RawChannelConfig | undefined;
const accountOverride = channelConfig?.accounts?.[accountId]?.healthMonitor?.enabled;
const plugin = getChannelPlugin(channelId);
const resolvedAccount = plugin?.config.resolveAccount(cfg, accountId) as
| {
healthMonitor?: {
enabled?: boolean;
};
}
| undefined;
const accountOverride = resolvedAccount?.healthMonitor?.enabled;
if (typeof accountOverride === "boolean") {
return accountOverride;
}
const channelOverride = channelConfig?.healthMonitor?.enabled;
if (typeof channelOverride === "boolean") {
return channelOverride;
}
return true;
};