chore(tests): properly check logging in tests

This commit is contained in:
Gustavo Madeira Santana
2026-02-21 17:21:48 -05:00
parent dea154ccae
commit 738e2c21dd
2 changed files with 65 additions and 47 deletions

View File

@@ -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,7 +12,9 @@ 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(() => {});
try {
const getCallCount = mockCatalogImportFailThenRecover(); const getCallCount = mockCatalogImportFailThenRecover();
const cfg = {} as OpenClawConfig; const cfg = {} as OpenClawConfig;
@@ -22,11 +25,16 @@ describe("loadModelCatalog", () => {
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 () =>
({ ({
@@ -51,6 +59,10 @@ describe("loadModelCatalog", () => {
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 () => {

View File

@@ -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,7 +147,9 @@ 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(() => {});
try {
const cfg: Partial<OpenClawConfig> = { const cfg: Partial<OpenClawConfig> = {
agents: { agents: {
defaults: { defaults: {
@@ -165,7 +168,10 @@ describe("model-selection", () => {
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", () => {