-
Notifications
You must be signed in to change notification settings - Fork 1
fix: model catalog follows the session's backend (web) #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // @vitest-environment jsdom | ||
| import { describe, it, expect, afterEach, beforeEach, vi } from "vitest"; | ||
|
|
||
| const requestMock = vi.hoisted(() => | ||
| vi.fn<(msg: unknown, opts?: unknown) => Promise<unknown>>(), | ||
| ); | ||
| vi.mock("./connection", () => ({ | ||
| getClient: () => ({ request: requestMock }), | ||
| newRequestId: () => `r-${Math.random()}`, | ||
| })); | ||
|
|
||
| import { | ||
| fetchModels, | ||
| modelCatalog, | ||
| modelsLive, | ||
| _resetModelsForTest, | ||
| } from "./models"; | ||
| import type { ModelInfo } from "../protocol/types"; | ||
|
|
||
| function result(provider: string, models: ModelInfo[], live = true) { | ||
| return { | ||
| type: "models.list.result" as const, | ||
| requestId: "x", | ||
| provider, | ||
| models, | ||
| live, | ||
| }; | ||
| } | ||
|
|
||
| const CLAUDE: ModelInfo[] = [ | ||
| { value: "opus", displayName: "Opus" }, | ||
| { value: "sonnet", displayName: "Sonnet" }, | ||
| ]; | ||
| const CODEX: ModelInfo[] = [{ value: "gpt-5-codex", displayName: "GPT-5 Codex" }]; | ||
|
|
||
| beforeEach(() => _resetModelsForTest()); | ||
| afterEach(() => { | ||
| requestMock.mockReset(); | ||
| _resetModelsForTest(); | ||
| }); | ||
|
|
||
| describe("fetchModels — per-backend catalog", () => { | ||
| it("sends the requested provider on the wire", async () => { | ||
| requestMock.mockResolvedValueOnce(result("codex", CODEX)); | ||
| await fetchModels("codex"); | ||
| expect(requestMock).toHaveBeenCalledTimes(1); | ||
| expect(requestMock.mock.calls[0]![0]).toMatchObject({ | ||
| type: "models.list", | ||
| provider: "codex", | ||
| }); | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["gpt-5-codex"]); | ||
| expect(modelsLive()).toBe(true); | ||
| }); | ||
|
|
||
| it("omits provider when none is given (daemon default)", async () => { | ||
| requestMock.mockResolvedValueOnce(result("claude", CLAUDE)); | ||
| await fetchModels(); | ||
| expect(requestMock.mock.calls[0]![0]).not.toHaveProperty("provider"); | ||
| }); | ||
|
|
||
| it("switching backends swaps the catalog — never shows the old backend's models", async () => { | ||
| requestMock.mockResolvedValueOnce(result("claude", CLAUDE)); | ||
| await fetchModels("claude"); | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["opus", "sonnet"]); | ||
|
|
||
| // Switch to codex: the claude list must not linger. | ||
| requestMock.mockResolvedValueOnce(result("codex", CODEX)); | ||
| await fetchModels("codex", true); | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["gpt-5-codex"]); | ||
| expect(requestMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("serves a cached live list without refetching (unless forced)", async () => { | ||
| requestMock.mockResolvedValueOnce(result("claude", CLAUDE)); | ||
| await fetchModels("claude"); | ||
| await fetchModels("claude"); // cached + live → no second request | ||
| expect(requestMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| await fetchModels("claude", true); // force → refetch | ||
| // (mockResolvedValueOnce is exhausted; force still issues the request) | ||
| expect(requestMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("a not-yet-live backend is refetched (models unknown until first use)", async () => { | ||
| requestMock.mockResolvedValueOnce(result("codex", [], false)); // empty + not live | ||
| await fetchModels("codex"); | ||
| expect(modelCatalog()).toEqual([]); | ||
| expect(modelsLive()).toBe(false); | ||
|
|
||
| requestMock.mockResolvedValueOnce(result("codex", CODEX, true)); // now populated | ||
| await fetchModels("codex"); | ||
| expect(requestMock).toHaveBeenCalledTimes(2); | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["gpt-5-codex"]); | ||
| }); | ||
|
|
||
| it("clears a stale catalog immediately when switching to an unfetched backend", async () => { | ||
| requestMock.mockResolvedValueOnce(result("claude", CLAUDE)); | ||
| await fetchModels("claude"); | ||
|
|
||
| // Switch to codex; the response is slow — the claude list must clear NOW, | ||
| // not after the await resolves. | ||
| let resolveCodex!: (v: unknown) => void; | ||
| requestMock.mockImplementationOnce(() => new Promise((r) => (resolveCodex = r))); | ||
| const pending = fetchModels("codex", true); | ||
| expect(modelCatalog()).toEqual([]); // synchronously cleared | ||
| resolveCodex(result("codex", CODEX)); | ||
| await pending; | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["gpt-5-codex"]); | ||
| }); | ||
|
|
||
| it("a slow response for a backend the user already switched away from is dropped", async () => { | ||
| // codex fetch is slow… | ||
| let resolveCodex!: (v: unknown) => void; | ||
| requestMock.mockImplementationOnce(() => new Promise((r) => (resolveCodex = r))); | ||
| const codexFetch = fetchModels("codex", true); | ||
|
|
||
| // …user switches to claude, which resolves first. | ||
| requestMock.mockResolvedValueOnce(result("claude", CLAUDE)); | ||
| await fetchModels("claude", true); | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["opus", "sonnet"]); | ||
|
|
||
| // Now the stale codex response lands — it must NOT clobber the claude view. | ||
| resolveCodex(result("codex", CODEX)); | ||
| await codexFetch; | ||
| expect(modelCatalog().map((m) => m.value)).toEqual(["opus", "sonnet"]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.