[AI-assisted] test: fix typing and test fixture issues (#31444)

* test: fix typing and test fixture issues

* Fix type-test harness issues from session routing and mock typing

* Add routing regression test for session.mainKey precedence
This commit is contained in:
Vincent Koc
2026-03-02 00:41:21 -08:00
committed by GitHub
parent 1443bb9a84
commit 29c3ce9454
13 changed files with 195 additions and 104 deletions

View File

@@ -10,6 +10,20 @@ import type { ChannelChoice } from "./onboard-types.js";
import { getChannelOnboardingAdapter } from "./onboarding/registry.js";
import type { ChannelOnboardingAdapter } from "./onboarding/types.js";
type ChannelOnboardingAdapterPatch = Partial<
Pick<
ChannelOnboardingAdapter,
"configure" | "configureInteractive" | "configureWhenConfigured" | "getStatus"
>
>;
type PatchedOnboardingAdapterFields = {
configure?: ChannelOnboardingAdapter["configure"];
configureInteractive?: ChannelOnboardingAdapter["configureInteractive"];
configureWhenConfigured?: ChannelOnboardingAdapter["configureWhenConfigured"];
getStatus?: ChannelOnboardingAdapter["getStatus"];
};
export function setDefaultChannelPluginRegistryForTests(): void {
const channels = [
{ pluginId: "discord", plugin: discordPlugin, source: "test" },
@@ -24,23 +38,44 @@ export function setDefaultChannelPluginRegistryForTests(): void {
export function patchChannelOnboardingAdapter(
channel: ChannelChoice,
patch: Partial<ChannelOnboardingAdapter>,
patch: ChannelOnboardingAdapterPatch,
): () => void {
const adapter = getChannelOnboardingAdapter(channel);
if (!adapter) {
throw new Error(`missing onboarding adapter for ${channel}`);
}
const keys = Object.keys(patch);
const adapterRecord = adapter as unknown as Record<string, unknown>;
const patchRecord = patch as Record<string, unknown>;
const previous = new Map<string, unknown>();
for (const key of keys) {
previous.set(key, adapterRecord[key]);
adapterRecord[key] = patchRecord[key];
const previous: PatchedOnboardingAdapterFields = {};
if (Object.prototype.hasOwnProperty.call(patch, "getStatus")) {
previous.getStatus = adapter.getStatus;
adapter.getStatus = patch.getStatus ?? adapter.getStatus;
}
if (Object.prototype.hasOwnProperty.call(patch, "configure")) {
previous.configure = adapter.configure;
adapter.configure = patch.configure ?? adapter.configure;
}
if (Object.prototype.hasOwnProperty.call(patch, "configureInteractive")) {
previous.configureInteractive = adapter.configureInteractive;
adapter.configureInteractive = patch.configureInteractive;
}
if (Object.prototype.hasOwnProperty.call(patch, "configureWhenConfigured")) {
previous.configureWhenConfigured = adapter.configureWhenConfigured;
adapter.configureWhenConfigured = patch.configureWhenConfigured;
}
return () => {
for (const key of keys) {
adapterRecord[key] = previous.get(key);
if (Object.prototype.hasOwnProperty.call(patch, "getStatus")) {
adapter.getStatus = previous.getStatus!;
}
if (Object.prototype.hasOwnProperty.call(patch, "configure")) {
adapter.configure = previous.configure!;
}
if (Object.prototype.hasOwnProperty.call(patch, "configureInteractive")) {
adapter.configureInteractive = previous.configureInteractive;
}
if (Object.prototype.hasOwnProperty.call(patch, "configureWhenConfigured")) {
adapter.configureWhenConfigured = previous.configureWhenConfigured;
}
};
}

View File

@@ -1,5 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelOnboardingAdapter } from "../channels/plugins/onboarding-types.js";
import type { OpenClawConfig } from "../config/config.js";
import { createEmptyPluginRegistry } from "../plugins/registry.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
@@ -83,17 +82,16 @@ function createTelegramCfg(botToken: string, enabled?: boolean): OpenClawConfig
} as OpenClawConfig;
}
function patchTelegramAdapter(overrides: Partial<ChannelOnboardingAdapter>) {
const getStatus =
overrides.getStatus ??
vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({
channel: "telegram",
configured: Boolean(cfg.channels?.telegram?.botToken),
statusLines: [],
}));
function patchTelegramAdapter(overrides: Parameters<typeof patchChannelOnboardingAdapter>[1]) {
return patchChannelOnboardingAdapter("telegram", {
...overrides,
getStatus,
getStatus:
overrides.getStatus ??
vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({
channel: "telegram",
configured: Boolean(cfg.channels?.telegram?.botToken),
statusLines: [],
})),
});
}

View File

@@ -1,7 +1,6 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { CONTEXT_WINDOW_HARD_MIN_TOKENS } from "../agents/context-window-guard.js";
import type { OpenClawConfig } from "../config/config.js";
import type { ModelProviderConfig } from "../config/types.models.js";
import { defaultRuntime } from "../runtime.js";
import {
applyCustomApiConfig,
@@ -78,32 +77,31 @@ function expectOpenAiCompatResult(params: {
expect(params.result.config.models?.providers?.custom?.api).toBe("openai-completions");
}
function buildCustomProviderConfig(contextWindow?: number): OpenClawConfig {
function buildCustomProviderConfig(contextWindow?: number) {
if (contextWindow === undefined) {
return {};
return {} as OpenClawConfig;
}
const customProvider = {
api: "openai-completions",
baseUrl: "https://llm.example.com/v1",
models: [
{
id: "foo-large",
name: "foo-large",
contextWindow,
maxTokens: contextWindow > CONTEXT_WINDOW_HARD_MIN_TOKENS ? 4096 : 1024,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
reasoning: false,
},
],
} satisfies ModelProviderConfig;
return {
models: {
providers: {
custom: customProvider,
custom: {
api: "openai-completions" as const,
baseUrl: "https://llm.example.com/v1",
models: [
{
id: "foo-large",
name: "foo-large",
contextWindow,
maxTokens: contextWindow > CONTEXT_WINDOW_HARD_MIN_TOKENS ? 4096 : 1024,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
reasoning: false,
},
],
},
},
},
};
} as OpenClawConfig;
}
function applyCustomModelConfigWithContextWindow(contextWindow?: number) {