|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +import { defined } from "../../../testkit/defined.js"; |
| 7 | +import { withMockedModuleDuring } from "../../../testkit/mock-module.js"; |
| 8 | +import { |
| 9 | + contextWindowFor, |
| 10 | + setModelContextWindows, |
| 11 | + setProviderContextWindowOverrides, |
| 12 | +} from "../../provider/context-window.js"; |
| 13 | +import { refreshCatalogAfterPrefetch } from "./catalog-refresh.js"; |
| 14 | +import type { RunnerServices, RunnerState } from "./state.js"; |
| 15 | + |
| 16 | +const dirs: string[] = []; |
| 17 | +afterEach(() => { |
| 18 | + setModelContextWindows(undefined); |
| 19 | + setProviderContextWindowOverrides(undefined); |
| 20 | + for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true }); |
| 21 | +}); |
| 22 | + |
| 23 | +function settingsFile(): string { |
| 24 | + const dir = mkdtempSync(join(tmpdir(), "catalog-refresh-")); |
| 25 | + dirs.push(dir); |
| 26 | + const path = join(dir, "settings.json"); |
| 27 | + writeFileSync( |
| 28 | + path, |
| 29 | + JSON.stringify({ |
| 30 | + providers: { |
| 31 | + first: { |
| 32 | + baseURL: "https://first.example/v1", |
| 33 | + keyless: true, |
| 34 | + models: ["shared-model"], |
| 35 | + contextWindow: 32_000, |
| 36 | + }, |
| 37 | + second: { |
| 38 | + baseURL: "https://second.example/v1", |
| 39 | + keyless: true, |
| 40 | + models: ["shared-model"], |
| 41 | + contextWindow: 64_000, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }), |
| 45 | + ); |
| 46 | + return path; |
| 47 | +} |
| 48 | + |
| 49 | +function fixture(withHost: boolean) { |
| 50 | + const calls: unknown[][] = []; |
| 51 | + const host = { |
| 52 | + refreshModels: (...args: unknown[]) => { |
| 53 | + calls.push(args); |
| 54 | + }, |
| 55 | + }; |
| 56 | + const state = { |
| 57 | + trueGlobalSettingsPath: settingsFile(), |
| 58 | + config: { |
| 59 | + apiKey: "", |
| 60 | + baseURL: "https://first.example/v1", |
| 61 | + model: "shared-model", |
| 62 | + providerName: "first", |
| 63 | + keyless: true, |
| 64 | + providers: [], |
| 65 | + }, |
| 66 | + } as unknown as RunnerState; |
| 67 | + const services = { |
| 68 | + hostHolder: withHost ? { instance: host } : {}, |
| 69 | + } as unknown as RunnerServices; |
| 70 | + return { state, services, calls }; |
| 71 | +} |
| 72 | + |
| 73 | +function withNoOAuthProfiles<T>(run: () => Promise<T>): Promise<T> { |
| 74 | + return withMockedModuleDuring( |
| 75 | + import.meta.resolve("../../config/oauth-stores.js"), |
| 76 | + (real: typeof import("../../config/oauth-stores.js")) => ({ |
| 77 | + ...real, |
| 78 | + listCodexProfiles: async () => [], |
| 79 | + listXaiProfiles: async () => [], |
| 80 | + listMetaProfiles: async () => [], |
| 81 | + }), |
| 82 | + run, |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +const flush = () => new Promise((r) => setTimeout(r, 50)); |
| 87 | + |
| 88 | +describe("refreshCatalogAfterPrefetch", () => { |
| 89 | + test("rebuilds the catalog from disk and hands it to the host after the prefetch settles", async () => { |
| 90 | + const { state, services, calls } = fixture(true); |
| 91 | + const prefetch = Promise.withResolvers<undefined>(); |
| 92 | + await withNoOAuthProfiles(async () => { |
| 93 | + refreshCatalogAfterPrefetch( |
| 94 | + state, |
| 95 | + services, |
| 96 | + "go", |
| 97 | + () => prefetch.promise, |
| 98 | + ); |
| 99 | + await flush(); |
| 100 | + expect(calls).toHaveLength(0); |
| 101 | + prefetch.resolve(undefined); |
| 102 | + await flush(); |
| 103 | + }); |
| 104 | + expect(calls).toHaveLength(1); |
| 105 | + expect(defined(calls[0])[2]).toBe(state.config.providers); |
| 106 | + expect(state.config.providers.map((p) => p.name).sort()).toEqual([ |
| 107 | + "first", |
| 108 | + "second", |
| 109 | + ]); |
| 110 | + expect(state.config.settings).toBeDefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + test("a model switch while the prefetch is in flight is the selection the refresh keeps", async () => { |
| 114 | + const { state, services } = fixture(true); |
| 115 | + const prefetch = Promise.withResolvers<undefined>(); |
| 116 | + await withNoOAuthProfiles(async () => { |
| 117 | + refreshCatalogAfterPrefetch( |
| 118 | + state, |
| 119 | + services, |
| 120 | + "zen", |
| 121 | + () => prefetch.promise, |
| 122 | + ); |
| 123 | + state.config = { ...state.config, providerName: "second" }; |
| 124 | + prefetch.resolve(undefined); |
| 125 | + await flush(); |
| 126 | + }); |
| 127 | + expect(contextWindowFor("shared-model")).toBe(64_000); |
| 128 | + }); |
| 129 | + |
| 130 | + test("does nothing when no host is mounted", async () => { |
| 131 | + const { state, services } = fixture(false); |
| 132 | + const before = state.config; |
| 133 | + await withNoOAuthProfiles(async () => { |
| 134 | + refreshCatalogAfterPrefetch(state, services, "go", async () => undefined); |
| 135 | + await flush(); |
| 136 | + }); |
| 137 | + expect(state.config).toBe(before); |
| 138 | + }); |
| 139 | + |
| 140 | + test("a failed prefetch leaves state and the host untouched", async () => { |
| 141 | + const { state, services, calls } = fixture(true); |
| 142 | + const before = state.config; |
| 143 | + await withNoOAuthProfiles(async () => { |
| 144 | + refreshCatalogAfterPrefetch(state, services, "go", () => |
| 145 | + Promise.reject(new Error("offline")), |
| 146 | + ); |
| 147 | + await flush(); |
| 148 | + }); |
| 149 | + expect(calls).toHaveLength(0); |
| 150 | + expect(state.config).toBe(before); |
| 151 | + }); |
| 152 | + |
| 153 | + test("a model switch during the post-prefetch credential discovery is the selection the refresh keeps", async () => { |
| 154 | + const { state, services } = fixture(true); |
| 155 | + const discovery = Promise.withResolvers<[]>(); |
| 156 | + const discoveryStarted = Promise.withResolvers<undefined>(); |
| 157 | + await withMockedModuleDuring( |
| 158 | + import.meta.resolve("../../config/oauth-stores.js"), |
| 159 | + (real: typeof import("../../config/oauth-stores.js")) => ({ |
| 160 | + ...real, |
| 161 | + listCodexProfiles: () => { |
| 162 | + discoveryStarted.resolve(undefined); |
| 163 | + return discovery.promise; |
| 164 | + }, |
| 165 | + listXaiProfiles: async () => [], |
| 166 | + listMetaProfiles: async () => [], |
| 167 | + }), |
| 168 | + async () => { |
| 169 | + refreshCatalogAfterPrefetch( |
| 170 | + state, |
| 171 | + services, |
| 172 | + "go", |
| 173 | + async () => undefined, |
| 174 | + ); |
| 175 | + await discoveryStarted.promise; |
| 176 | + state.config = { ...state.config, providerName: "second" }; |
| 177 | + discovery.resolve([]); |
| 178 | + await flush(); |
| 179 | + }, |
| 180 | + ); |
| 181 | + expect(contextWindowFor("shared-model")).toBe(64_000); |
| 182 | + }); |
| 183 | +}); |
0 commit comments