mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 21:44:32 +00:00
chore(tests): properly check logging in tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import { resetLogger, setLoggerOverride } from "../logging/logger.js";
|
||||||
import { __setModelCatalogImportForTest, loadModelCatalog } from "./model-catalog.js";
|
import { __setModelCatalogImportForTest, loadModelCatalog } from "./model-catalog.js";
|
||||||
import {
|
import {
|
||||||
installModelCatalogTestHooks,
|
installModelCatalogTestHooks,
|
||||||
@@ -11,46 +12,57 @@ describe("loadModelCatalog", () => {
|
|||||||
installModelCatalogTestHooks();
|
installModelCatalogTestHooks();
|
||||||
|
|
||||||
it("retries after import failure without poisoning the cache", async () => {
|
it("retries after import failure without poisoning the cache", async () => {
|
||||||
|
setLoggerOverride({ level: "silent", consoleLevel: "warn" });
|
||||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||||
const getCallCount = mockCatalogImportFailThenRecover();
|
try {
|
||||||
|
const getCallCount = mockCatalogImportFailThenRecover();
|
||||||
|
|
||||||
const cfg = {} as OpenClawConfig;
|
const cfg = {} as OpenClawConfig;
|
||||||
const first = await loadModelCatalog({ config: cfg });
|
const first = await loadModelCatalog({ config: cfg });
|
||||||
expect(first).toEqual([]);
|
expect(first).toEqual([]);
|
||||||
|
|
||||||
const second = await loadModelCatalog({ config: cfg });
|
const second = await loadModelCatalog({ config: cfg });
|
||||||
expect(second).toEqual([{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }]);
|
expect(second).toEqual([{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }]);
|
||||||
expect(getCallCount()).toBe(2);
|
expect(getCallCount()).toBe(2);
|
||||||
expect(warnSpy).toHaveBeenCalledTimes(1);
|
expect(warnSpy).toHaveBeenCalledTimes(1);
|
||||||
|
} finally {
|
||||||
|
setLoggerOverride(null);
|
||||||
|
resetLogger();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns partial results on discovery errors", async () => {
|
it("returns partial results on discovery errors", async () => {
|
||||||
|
setLoggerOverride({ level: "silent", consoleLevel: "warn" });
|
||||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||||
|
try {
|
||||||
__setModelCatalogImportForTest(
|
__setModelCatalogImportForTest(
|
||||||
async () =>
|
async () =>
|
||||||
({
|
({
|
||||||
AuthStorage: class {},
|
AuthStorage: class {},
|
||||||
ModelRegistry: class {
|
ModelRegistry: class {
|
||||||
getAll() {
|
getAll() {
|
||||||
return [
|
return [
|
||||||
{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" },
|
{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" },
|
||||||
{
|
{
|
||||||
get id() {
|
get id() {
|
||||||
throw new Error("boom");
|
throw new Error("boom");
|
||||||
|
},
|
||||||
|
provider: "openai",
|
||||||
|
name: "bad",
|
||||||
},
|
},
|
||||||
provider: "openai",
|
];
|
||||||
name: "bad",
|
}
|
||||||
},
|
},
|
||||||
];
|
}) as unknown as PiSdkModule,
|
||||||
}
|
);
|
||||||
},
|
|
||||||
}) as unknown as PiSdkModule,
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = await loadModelCatalog({ config: {} as OpenClawConfig });
|
const result = await loadModelCatalog({ config: {} as OpenClawConfig });
|
||||||
expect(result).toEqual([{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }]);
|
expect(result).toEqual([{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }]);
|
||||||
expect(warnSpy).toHaveBeenCalledTimes(1);
|
expect(warnSpy).toHaveBeenCalledTimes(1);
|
||||||
|
} finally {
|
||||||
|
setLoggerOverride(null);
|
||||||
|
resetLogger();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("adds openai-codex/gpt-5.3-codex-spark when base gpt-5.3-codex exists", async () => {
|
it("adds openai-codex/gpt-5.3-codex-spark when base gpt-5.3-codex exists", async () => {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { describe, it, expect, vi } from "vitest";
|
import { describe, it, expect, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import { resetLogger, setLoggerOverride } from "../logging/logger.js";
|
||||||
import {
|
import {
|
||||||
parseModelRef,
|
parseModelRef,
|
||||||
resolveModelRefFromString,
|
resolveModelRefFromString,
|
||||||
@@ -146,26 +147,31 @@ describe("model-selection", () => {
|
|||||||
|
|
||||||
describe("resolveConfiguredModelRef", () => {
|
describe("resolveConfiguredModelRef", () => {
|
||||||
it("should fall back to anthropic and warn if provider is missing for non-alias", () => {
|
it("should fall back to anthropic and warn if provider is missing for non-alias", () => {
|
||||||
|
setLoggerOverride({ level: "silent", consoleLevel: "warn" });
|
||||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||||
const cfg: Partial<OpenClawConfig> = {
|
try {
|
||||||
agents: {
|
const cfg: Partial<OpenClawConfig> = {
|
||||||
defaults: {
|
agents: {
|
||||||
model: { primary: "claude-3-5-sonnet" },
|
defaults: {
|
||||||
|
model: { primary: "claude-3-5-sonnet" },
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
};
|
|
||||||
|
|
||||||
const result = resolveConfiguredModelRef({
|
const result = resolveConfiguredModelRef({
|
||||||
cfg: cfg as OpenClawConfig,
|
cfg: cfg as OpenClawConfig,
|
||||||
defaultProvider: "google",
|
defaultProvider: "google",
|
||||||
defaultModel: "gemini-pro",
|
defaultModel: "gemini-pro",
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toEqual({ provider: "anthropic", model: "claude-3-5-sonnet" });
|
expect(result).toEqual({ provider: "anthropic", model: "claude-3-5-sonnet" });
|
||||||
expect(warnSpy).toHaveBeenCalledWith(
|
expect(warnSpy).toHaveBeenCalledWith(
|
||||||
expect.stringContaining('Falling back to "anthropic/claude-3-5-sonnet"'),
|
expect.stringContaining('Falling back to "anthropic/claude-3-5-sonnet"'),
|
||||||
);
|
);
|
||||||
warnSpy.mockRestore();
|
} finally {
|
||||||
|
setLoggerOverride(null);
|
||||||
|
resetLogger();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should use default provider/model if config is empty", () => {
|
it("should use default provider/model if config is empty", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user