import test, { before } 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';
import { loadLocaleMessages } from '../../application/i18n/messages';
// Non-English dictionaries load on demand; static rendering runs no effects.
before(async () => {
for (const locale of ['zh-CN', 'zh-TW', 'es', 'ru']) await loadLocaleMessages(locale);
});
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(
{}}
onTogglePinned={() => {}}
/>
,
);
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(
{}}
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(
{}}
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(
{}}
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(
{}}
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(
{}}
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(
{}} />
,
);
assert.ok(html.includes(`aria-label="${refreshLabel}"`));
}
// A plain preset list without a host refresh action stays buttonless.
assert.doesNotMatch(renderToStaticMarkup(), /refreshModelList/);
// A degraded preset serve (warning, no source) still offers the refresh.
const warned = renderToStaticMarkup(
{}} />,
);
assert.match(warned, /aria-label="ai\.chat\.refreshModelList"/);
});