惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
博客园 - 司徒正美

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(gateway): refresh model catalog off request path · op...
steipete · 2026-04-30 · via Recent Commits to openclaw:main

@@ -0,0 +1,107 @@

1+

import { beforeEach, describe, expect, it, vi } from "vitest";

2+

import type { OpenClawConfig } from "../config/types.openclaw.js";

3+

import type { GatewayModelChoice } from "./server-model-catalog.js";

4+

import {

5+

__resetModelCatalogCacheForTest,

6+

loadGatewayModelCatalog,

7+

markGatewayModelCatalogStaleForReload,

8+

} from "./server-model-catalog.js";

9+10+

type Deferred<T> = {

11+

promise: Promise<T>;

12+

resolve: (value: T) => void;

13+

reject: (error: unknown) => void;

14+

};

15+

type LoadModelCatalogForTest = NonNullable<

16+

NonNullable<Parameters<typeof loadGatewayModelCatalog>[0]>["loadModelCatalog"]

17+

>;

18+19+

function createDeferred<T>(): Deferred<T> {

20+

let resolve!: (value: T) => void;

21+

let reject!: (error: unknown) => void;

22+

const promise = new Promise<T>((resolvePromise, rejectPromise) => {

23+

resolve = resolvePromise;

24+

reject = rejectPromise;

25+

});

26+

return { promise, resolve, reject };

27+

}

28+29+

function model(id: string): GatewayModelChoice {

30+

return { id, name: id, provider: "openai" } as GatewayModelChoice;

31+

}

32+33+

const getConfig = () => ({}) as OpenClawConfig;

34+35+

describe("loadGatewayModelCatalog", () => {

36+

beforeEach(async () => {

37+

await __resetModelCatalogCacheForTest();

38+

});

39+40+

it("caches the first successful catalog until reload marks it stale", async () => {

41+

const catalog = [model("gpt-5.4")];

42+

const loadModelCatalog = vi.fn(async () => catalog);

43+44+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(catalog);

45+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(catalog);

46+47+

expect(loadModelCatalog).toHaveBeenCalledTimes(1);

48+

});

49+50+

it("returns the last catalog while a stale reload refresh is still pending", async () => {

51+

const staleCatalog = [model("gpt-5.4")];

52+

const freshCatalog = [model("gpt-5.5")];

53+

const refresh = createDeferred<GatewayModelChoice[]>();

54+

const loadModelCatalog = vi

55+

.fn<LoadModelCatalogForTest>()

56+

.mockResolvedValueOnce(staleCatalog)

57+

.mockReturnValueOnce(refresh.promise);

58+59+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

60+

staleCatalog,

61+

);

62+63+

markGatewayModelCatalogStaleForReload();

64+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

65+

staleCatalog,

66+

);

67+

await vi.waitFor(() => expect(loadModelCatalog).toHaveBeenCalledTimes(2));

68+69+

refresh.resolve(freshCatalog);

70+

await vi.waitFor(async () => {

71+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

72+

freshCatalog,

73+

);

74+

});

75+

});

76+77+

it("keeps serving the last catalog when a stale background refresh fails", async () => {

78+

const staleCatalog = [model("gpt-5.4")];

79+

const freshCatalog = [model("gpt-5.5")];

80+

const loadModelCatalog = vi

81+

.fn<LoadModelCatalogForTest>()

82+

.mockResolvedValueOnce(staleCatalog)

83+

.mockRejectedValueOnce(new Error("provider offline"))

84+

.mockResolvedValueOnce(freshCatalog);

85+86+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

87+

staleCatalog,

88+

);

89+90+

markGatewayModelCatalogStaleForReload();

91+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

92+

staleCatalog,

93+

);

94+

await vi.waitFor(() => expect(loadModelCatalog).toHaveBeenCalledTimes(2));

95+96+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

97+

staleCatalog,

98+

);

99+

await vi.waitFor(() => expect(loadModelCatalog).toHaveBeenCalledTimes(3));

100+101+

await vi.waitFor(async () => {

102+

await expect(loadGatewayModelCatalog({ getConfig, loadModelCatalog })).resolves.toBe(

103+

freshCatalog,

104+

);

105+

});

106+

});

107+

});