Files
Netcatty/components/ai/ComposerModelPicker.test.tsx
T
GOUKI9999 02f8c798f7 feat(ai): hydrate model pickers from last-known-good and fetch only on demand
The list-models pipeline already served LKG on failure; this wires the
rest of the LKG-first, manual-refresh policy end to end.

Main: the list-models IPC accepts a force flag — manual refresh always
skips the in-memory cache and the persisted entry and goes live, and a
forced success overwrites the persisted catalog.

Renderer: the panel passes force only from the retry banner and the
picker's refresh button; panel-visibility and send-preflight loads stay
non-force and replay the renderer cache, so with a persisted catalog
main answers them without spending the live-fetch budget.

Picker: a footer refresh icon-button next to the status chip, visible
whenever any fetched catalog (live or last-known-good) or the degraded
warning is present; the spinner row remains the in-flight feedback.
Tooltip localized as ai.chat.refreshModelList in en/es/ru/zh-CN/zh-TW.

Fresh installs are unchanged (no persisted entry → live fetch as
before); existing users stop seeing jitter warnings on panel open and
pick up new CLI models via the refresh button.
2026-10-23 17:22:25 +08:00

192 lines
6.8 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { ComposerModelPicker } from './ComposerModelPicker';
import { I18nProvider } from '../../application/i18n/I18nProvider';
for (const [locale, cachedLabel] of [
['en', 'Cached'],
['zh-CN', '缓存'],
['zh-TW', '快取'],
['es', 'En caché'],
['ru', 'Кэш'],
]) {
test(`cached catalog without a usable timestamp has a clean label in ${locale}`, () => {
for (const fetchedAt of [undefined, 'invalid-date']) {
const html = renderToStaticMarkup(
<I18nProvider locale={locale}>
<ComposerModelPicker
modelPresets={[{ id: 'saved-model', name: 'Saved model' }]}
selectedModelId="saved-model"
catalogSource="last-known-good"
catalogFetchedAt={fetchedAt}
prefs={{ recent: [], pinned: [] }}
onSelectModel={() => {}}
onTogglePinned={() => {}}
/>
</I18nProvider>,
);
assert.ok(html.includes(`title="${cachedLabel}"`));
assert.doesNotMatch(html, /\{time\}|ai\.chat\.modelCatalog/);
}
});
}
test('Catty picker keeps a single model list and hides other providers until the submenu opens', () => {
const html = renderToStaticMarkup(
<ComposerModelPicker
providers={[
{
id: 'p1',
providerId: 'deepseek',
name: 'DeepSeek',
defaultModel: 'deepseek-v4-pro',
enabled: true,
},
{
id: 'p2',
providerId: 'openai',
name: 'OpenAI',
defaultModel: 'gpt-5.5',
enabled: true,
},
]}
selectedProviderId="p1"
selectedModelId="deepseek-v4-pro"
prefs={{ recent: [], pinned: [] }}
onSelectProviderModel={() => {}}
onTogglePinned={() => {}}
/>,
);
assert.match(html, /DeepSeek/);
assert.match(html, /placeholder="ai\.chat\.searchModels"/);
assert.match(html, /deepseek-v4-pro/);
assert.match(html, /aria-label="ai\.chat\.selectProvider"/);
assert.doesNotMatch(html, /OpenAI/);
assert.doesNotMatch(html, /w-\[128px\]/);
});
test('external agent picker lists presets without a provider column', () => {
const html = renderToStaticMarkup(
<ComposerModelPicker
modelPresets={[
{ id: 'gpt-5.5', name: 'GPT-5.5' },
{ id: 'gpt-5.4', name: 'GPT-5.4' },
]}
selectedModelId="gpt-5.5"
prefs={{ recent: [{ modelId: 'gpt-5.5' }], pinned: [] }}
onSelectModel={() => {}}
onTogglePinned={() => {}}
/>,
);
assert.match(html, /GPT-5\.5/);
assert.match(html, /GPT-5\.4/);
assert.match(html, /ai\.chat\.recent/);
assert.doesNotMatch(html, /ai\.chat\.providers/);
});
test('custom model action is offered in provider-switcher and preset modes', () => {
const source = readFileSync(new URL('./ComposerModelPicker.tsx', import.meta.url), 'utf8');
// Custom ids are accepted regardless of mode (provider catalog or CLI
// presets) — #3534 wants manual model entry for external agents too.
assert.match(source, /const showCustom = Boolean\(\s*allowCustomEntry\s*&&\s*trimmedQuery/s);
assert.match(source, /resolveComposerEnterModelId/);
});
test('custom model action can be suppressed when the host locks the model', () => {
const source = readFileSync(new URL('./ComposerModelPicker.tsx', import.meta.url), 'utf8');
// Codex config-locked models override every selection on send; the host
// turns the action off so it never becomes a silent no-op.
assert.match(source, /allowCustomEntry = true/);
const html = renderToStaticMarkup(
<ComposerModelPicker
modelPresets={[{ id: 'gpt-5.5', name: 'GPT-5.5' }]}
selectedModelId="gpt-5.5"
prefs={{ recent: [], pinned: [] }}
allowCustomEntry={false}
onSelectModel={() => {}}
onTogglePinned={() => {}}
/>,
);
assert.doesNotMatch(html, /ai\.chat\.useCustomModel/);
});
test('reselecting a saved custom model preserves its custom provenance', () => {
const source = readFileSync(new URL('./ComposerModelPicker.tsx', import.meta.url), 'utf8');
// A saved custom id is appended to the preset list, so picking it from
// Recent has showCustom === false. The callback must keep the custom flag
// anyway, or the host overwrites the pref entry without it and
// resolveComposerCustomModelIds drops the model from the presets.
assert.match(source, /customPrefIds\.has\(modelId\.toLowerCase\(\)\)/);
});
test('external agent picker keeps preset rows truncation-friendly with a full-name tooltip', () => {
const html = renderToStaticMarkup(
<ComposerModelPicker
modelPresets={[{ id: 'gpt-5.6-sol-very-long-preview-id', name: 'GPT-5.6 Sol (preview)' }]}
selectedModelId="gpt-5.6-sol-very-long-preview-id"
prefs={{ recent: [], pinned: [] }}
onSelectModel={() => {}}
onTogglePinned={() => {}}
/>,
);
assert.match(html, /title="GPT-5\.6 Sol \(preview\)"/);
});
test('external agent picker shows the loading row while the live catalog is pending', () => {
const html = renderToStaticMarkup(
<ComposerModelPicker
modelPresets={[]}
loading
selectedModelId={undefined}
prefs={{ recent: [], pinned: [] }}
onSelectModel={() => {}}
onTogglePinned={() => {}}
/>,
);
assert.match(html, /ai\.chat\.loadingModels/);
// With no presets the picker renders only the loading row; the host now
// keeps built-in presets visible during loading and passes them through.
assert.doesNotMatch(html, /gpt-5\.1/);
});
test('manual refresh button renders for any fetched catalog or warning and stays localized', () => {
const base = {
modelPresets: [{ id: 'm-1', name: 'M1' }],
selectedModelId: 'm-1',
prefs: { recent: [], pinned: [] },
onSelectModel: () => {},
onTogglePinned: () => {},
};
for (const [locale, refreshLabel] of [
['en', 'Refresh model list'],
['zh-CN', '刷新模型列表'],
['zh-TW', '重新整理模型清單'],
['es', 'Actualizar la lista de modelos'],
['ru', 'Обновить список моделей'],
]) {
const html = renderToStaticMarkup(
<I18nProvider locale={locale}>
<ComposerModelPicker {...base} catalogSource="last-known-good" onRefreshCatalog={() => {}} />
</I18nProvider>,
);
assert.ok(html.includes(`aria-label="${refreshLabel}"`));
}
// A plain preset list without a host refresh action stays buttonless.
assert.doesNotMatch(renderToStaticMarkup(<ComposerModelPicker {...base} />), /refreshModelList/);
// A degraded preset serve (warning, no source) still offers the refresh.
const warned = renderToStaticMarkup(
<ComposerModelPicker {...base} catalogWarning="live fetch failed" onRefreshCatalog={() => {}} />,
);
assert.match(warned, /aria-label="ai\.chat\.refreshModelList"/);
});