| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379 |
- import { act, createElement, type ComponentProps } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { renderToStaticMarkup } from "react-dom/server";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
- import type { ProviderSettingsView } from "@zcode/services";
- import { TooltipProvider } from "@/components/ui/tooltip.js";
- import { V4ComposerModelControls } from "@/v4/composer/V4ComposerToolbar.js";
- import type { ModelConfigSelect } from "@/ModelConfigSelect.js";
- globalThis.IS_REACT_ACT_ENVIRONMENT = true;
- type ModelConfigSelectProps = ComponentProps<typeof ModelConfigSelect>;
- type V4ComposerModelControlsProps = ComponentProps<typeof V4ComposerModelControls>;
- const customValue = "custom:custom-openai:gpt-4o";
- const captured = vi.hoisted(() => ({
- modelSelectProps: null as ModelConfigSelectProps | null,
- thoughtControlProps: null as {
- indicatorClassName?: string;
- triggerClassName?: string;
- composerCollapsePriority?: number;
- onValueChange: (value: string) => void;
- option: { currentValue?: unknown; options?: Array<{ value: string }> };
- } | null,
- usageProps: [] as Array<Record<string, unknown>>,
- }));
- const mocks = vi.hoisted(() => ({
- codingPlanEntitlements: {} as Record<string, unknown>,
- codingPlanEntitlementsRefresh: vi.fn(async () => undefined),
- // 修复原因:context hover 的 start plan onAccess 按 enabledStartPlanProviderIds 挂载;
- // 默认空数组模拟 Start Plan 尚无独立 Account Access,需要入口的用例显式提供 provider id。
- codingPlanEntitlementsEnabledStartPlanIds: [] as string[],
- configOptions: [] as Array<Record<string, unknown>>,
- configOptionsError: true,
- enterpriseProducts: { snapshot: { productList: [] }, loading: false, error: null } as Record<
- string,
- unknown
- >,
- modelSelectionView: {
- revision: 1,
- providers: [
- {
- providerId: "custom-openai",
- providerName: "Custom OpenAI",
- config: {
- access: { type: "api-key", apiKey: "test-key" },
- api: {
- type: "openai-chat-completions",
- baseUrl: "https://api.example.com/v1",
- },
- models: ["gpt-4o"],
- },
- models: [
- {
- modelId: "gpt-4o",
- config: {
- optionSpecs: {
- reasoningLevel: { values: ["disabled"], map: "{}" },
- maxOutputTokens: { max: 32_000, map: "{}" },
- },
- },
- },
- ],
- },
- ],
- } as null | {
- revision: number;
- providers: Array<{
- providerId: string;
- providerName?: string;
- config: Record<string, unknown>;
- models: Array<{ modelId: string; config: Record<string, unknown> }>;
- }>;
- },
- providerSettingsView: {
- revision: 1,
- addableProviders: [],
- providerOrder: [],
- providers: [],
- } as ProviderSettingsView,
- recoverCustomModelSelection: vi.fn(async () => {}),
- selectModel: vi.fn(),
- settings: {
- providerFamilyConnectionSelections: {},
- providerFamilyDomain: {},
- } as Record<string, unknown>,
- usageEntitlement: { snapshot: null, loading: false, error: null } as Record<string, unknown>,
- usageEntitlementRefresh: vi.fn(async () => undefined),
- }));
- function completeDisabledModelConfig() {
- return {
- optionSpecs: {
- reasoningLevel: { values: ["disabled"], map: "{}" },
- maxOutputTokens: { max: 32_000, map: "{}" },
- },
- };
- }
- vi.mock("@/ModelConfigSelect.js", async () => {
- const React = await import("react");
- return {
- ModelConfigSelect: (props: ModelConfigSelectProps) => {
- captured.modelSelectProps = props;
- if (props.triggerRef && "current" in props.triggerRef) {
- (props.triggerRef as { current: HTMLSpanElement | null }).current = {
- contains: () => false,
- getClientRects: () => [{ width: 1 }] as unknown as DOMRectList,
- } as HTMLSpanElement;
- }
- return React.createElement("button", {
- "data-testid": "mock-model-select",
- disabled: props.disabled,
- });
- },
- };
- });
- vi.mock("@/chat-input-toolbar/display.js", async () => {
- const React = await import("react");
- return {
- ChatContextUsage: (props: Record<string, unknown>) => {
- captured.usageProps.push(props);
- return React.createElement("span", { "data-testid": "mock-usage" });
- },
- };
- });
- vi.mock("@/chat-input-toolbar/ThoughtLevelCycleControl.js", async () => {
- const React = await import("react");
- return {
- ThoughtLevelCycleControl: (props: {
- onValueChange: (value: string) => void;
- option: { currentValue?: unknown; options?: Array<{ value: string }> };
- }) => {
- captured.thoughtControlProps = props;
- return React.createElement("span", { "data-testid": "mock-thought" });
- },
- };
- });
- vi.mock("@/v4/composer/toolbarShortcuts.js", () => ({
- useToolbarShortcutBindings: vi.fn(),
- }));
- vi.mock("@/chat-input-toolbar/modelSelection.js", () => ({
- resolveModelSelectTriggerDisplay: (value: string) => ({ value }),
- shouldShowManageModelsAction: () => false,
- }));
- vi.mock("@/store/TabStoreProvider.js", () => ({
- useTabStore: () => vi.fn(),
- }));
- vi.mock("@/hooks/useProviderSettingsView.js", () => ({
- useProviderSettingsView: () => ({
- state: mocks.providerSettingsView
- ? { status: "ready", view: mocks.providerSettingsView }
- : { status: "loading" },
- reload: vi.fn(),
- }),
- }));
- vi.mock("@/hooks/useSettingService.js", () => ({
- useSettings: () => ({
- settings: mocks.settings,
- }),
- }));
- vi.mock("@/hooks/useUsageEntitlement.js", () => ({
- useUsageEntitlement: () => ({
- ...mocks.usageEntitlement,
- refresh: mocks.usageEntitlementRefresh,
- }),
- }));
- vi.mock("@/hooks/useZCodeConfig.js", () => ({
- useToolbarConfigOptions: () => ({
- configOptions: mocks.configOptions,
- loading: false,
- error: mocks.configOptionsError,
- }),
- }));
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({
- intl: {
- formatMessage: ({ id }: { id: string }) => id,
- },
- locale: "zh-CN",
- }),
- }));
- vi.mock("@/settings/CodingPlanUpgradeDialogProvider.js", () => ({
- useCodingPlanUpgradeDialog: () => ({ openCodingPlanUpgrade: vi.fn() }),
- }));
- vi.mock("@/settings/model-provider-section/useCodingPlanEntitlements.js", () => ({
- useCodingPlanEntitlements: () => ({
- entitlements: mocks.codingPlanEntitlements,
- enabledStartPlanProviderIds: mocks.codingPlanEntitlementsEnabledStartPlanIds,
- refresh: mocks.codingPlanEntitlementsRefresh,
- }),
- }));
- vi.mock("@/settings/model-provider-section/useEnterpriseCodingPlanProducts.js", () => ({
- useEnterpriseCodingPlanProducts: () => mocks.enterpriseProducts,
- }));
- vi.mock("@/lib/sidebarUsageCodingPlanProviderPreference.js", async () => {
- const actual = await vi.importActual("@/lib/sidebarUsageCodingPlanProviderPreference.js");
- return {
- ...(actual as Record<string, unknown>),
- writeSidebarUsageCodingPlanProviderPreference: vi.fn(),
- };
- });
- vi.mock("@/lib/codingPlanUsageSources.js", async () => {
- const actual = await vi.importActual("@/lib/codingPlanUsageSources.js");
- return actual;
- });
- vi.mock("@/lib/settingsNavigation.js", () => ({
- setPendingSettingsSectionIntent: vi.fn(),
- setPendingSettingsUsageIntent: vi.fn(),
- }));
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- addEventListener: () => {},
- appendChild: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- childNodes: [] as unknown[],
- getAttribute: () => null,
- insertBefore: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as unknown,
- removeAttribute: () => {},
- removeChild: (child: { parentNode?: unknown }) => {
- child.parentNode = null;
- return child;
- },
- removeEventListener: () => {},
- setAttribute: () => {},
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as Element;
- }
- function installMinimalDom() {
- const documentMock = {
- addEventListener: () => {},
- createElement: (tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createElementNS: (_namespace: string, tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createTextNode: (nodeValue: string) => ({
- nodeType: 3,
- nodeValue,
- ownerDocument: documentMock,
- parentNode: null,
- }),
- nodeType: 9,
- removeEventListener: () => {},
- } as unknown as Document;
- const windowMock = {
- addEventListener: () => {},
- clearTimeout,
- document: documentMock,
- HTMLElement: function HTMLElement() {},
- HTMLIFrameElement: function HTMLIFrameElement() {},
- Node: function Node() {},
- removeEventListener: () => {},
- setTimeout,
- } as unknown as Window & typeof globalThis;
- Object.assign(globalThis, {
- document: documentMock,
- window: windowMock,
- HTMLElement: windowMock.HTMLElement,
- HTMLIFrameElement: windowMock.HTMLIFrameElement,
- Node: windowMock.Node,
- });
- return createMinimalElement(documentMock, "div");
- }
- async function flushMicrotasks(): Promise<void> {
- await Promise.resolve();
- await Promise.resolve();
- }
- function addAccountProviderAccess(
- providerId: string,
- access:
- | {
- accountType: "zai" | "bigmodel";
- mode: "individual-coding-plan";
- }
- | {
- accountType: "zai" | "bigmodel";
- mode: "team-coding-plan";
- },
- label = providerId,
- ) {
- mocks.providerSettingsView = {
- revision: mocks.providerSettingsView.revision + 1,
- addableProviders: mocks.providerSettingsView.addableProviders,
- providerOrder: mocks.providerSettingsView.providerOrder,
- providers: [
- ...mocks.providerSettingsView.providers.filter(
- (provider) => provider.providerId !== providerId,
- ),
- {
- providerId,
- enabled: true,
- executable: true,
- effectiveConfig: {
- access: {
- type: "zhipu-account",
- entitled: true,
- ...access,
- },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://example.com",
- },
- label,
- builtinModelIds: [],
- },
- issues: [],
- models: [],
- },
- ],
- };
- }
- function createActiveEntitlement(providerId: string, extra: Record<string, unknown> = {}) {
- return {
- generatedAt: 1,
- authenticated: true,
- provider: {
- id: providerId,
- name: providerId,
- },
- remaining: null,
- subscription: null,
- quota: {
- level: "pro",
- limits: [
- {
- type: "TOKENS_LIMIT",
- unit: 3,
- number: 5,
- percentage: 35,
- usageDetails: [],
- },
- ],
- },
- ...extra,
- };
- }
- function renderToolbarMarkup(overrides: Partial<V4ComposerModelControlsProps> = {}): string {
- // 纯工具条测试显式提供已初始化的 Composer;生产初始化由 scope owner 负责。
- const seed = overrides.config ?? {
- provider: "custom-openai",
- model: "gpt-4o",
- thought: "disabled",
- mode: "build",
- };
- return renderToStaticMarkup(
- // HighspeedModelIndicator 走 ControlHintTooltip,缺少 TooltipProvider 会直接抛错,
- // 所以工具条快照统一包一层 Provider。
- createElement(
- TooltipProvider,
- null,
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: null,
- phase: null,
- config: {
- provider: "custom-openai",
- model: "gpt-4o",
- thought: "medium",
- thoughtLevels: ["medium", "high"],
- followupMode: "queue",
- mode: "build",
- },
- usage: {
- contextWindow: {
- usedTokens: 1024,
- maxTokens: 2048,
- autoCompactThresholdTokens: null,
- },
- cumulative: {
- inputTokens: 1024,
- outputTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- },
- },
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- draftConfig: {
- mode: seed.mode,
- modelSelection: {
- providerId: seed.provider,
- modelId: seed.model,
- ...(seed.thought ? { options: { reasoningLevel: seed.thought } } : {}),
- },
- },
- ...overrides,
- }),
- ),
- );
- }
- function latestUsageProps(): Record<string, unknown> {
- return captured.usageProps[captured.usageProps.length - 1] ?? {};
- }
- afterEach(() => {
- captured.modelSelectProps = null;
- captured.thoughtControlProps = null;
- captured.usageProps = [];
- mocks.codingPlanEntitlementsRefresh.mockClear();
- mocks.usageEntitlementRefresh.mockClear();
- mocks.codingPlanEntitlements = {};
- mocks.codingPlanEntitlementsEnabledStartPlanIds = [];
- mocks.configOptions = [];
- mocks.configOptionsError = true;
- mocks.enterpriseProducts = { snapshot: { productList: [] }, loading: false, error: null };
- mocks.modelSelectionView = {
- revision: 1,
- providers: [
- {
- providerId: "custom-openai",
- providerName: "Custom OpenAI",
- config: {
- access: { type: "api-key", apiKey: "test-key" },
- api: {
- type: "openai-chat-completions",
- baseUrl: "https://api.example.com/v1",
- },
- models: ["gpt-4o"],
- },
- models: [{ modelId: "gpt-4o", config: completeDisabledModelConfig() }],
- },
- ],
- };
- mocks.providerSettingsView = {
- revision: 1,
- addableProviders: [],
- providerOrder: [],
- providers: [],
- };
- mocks.settings = {
- providerFamilyConnectionSelections: {},
- providerFamilyDomain: {},
- };
- mocks.usageEntitlement = { snapshot: null, loading: false, error: null };
- vi.clearAllMocks();
- });
- describe("V4ComposerModelControls recovery", () => {
- it("Highspeed 卡生效期间隐藏 context token usage 圈圈", () => {
- const normalHtml = renderToolbarMarkup();
- const highspeedHtml = renderToolbarMarkup({ modelLocked: true });
- expect(normalHtml).toContain('data-testid="mock-usage"');
- expect(highspeedHtml).not.toContain('data-testid="mock-usage"');
- });
- it("把无晃动的固定宽度倒计时放在模型和 Think 之间", () => {
- vi.spyOn(Date, "now").mockReturnValue(1_000);
- const html = renderToolbarMarkup({
- highspeedExpiresAt: 60_000,
- highspeedModel: "GLM-5.3",
- highspeedPresented: true,
- });
- const modelIndex = html.indexOf('data-highspeed-model-indicator="true"');
- const countdownIndex = html.indexOf('data-highspeed-countdown="true"');
- const thoughtIndex = html.indexOf('data-testid="mock-thought"');
- expect(modelIndex).toBeGreaterThanOrEqual(0);
- expect(countdownIndex).toBeGreaterThan(modelIndex);
- expect(thoughtIndex).toBeGreaterThan(countdownIndex);
- expect(html).toContain("00:00:59");
- expect(html).toContain("h-7");
- expect(html).toContain("w-[82px]");
- expect(html).toContain("w-[54px]");
- expect(html).toContain("tabular-nums");
- expect(html).toMatch(/data-highspeed-countdown="true"[^>]*@max-xl\/composer:w-7/);
- expect(html).toMatch(
- /title="chat.highspeed.remainingTime"[^>]*data-highspeed-countdown="true"/,
- );
- expect(html).toMatch(/data-highspeed-countdown-value="true"[^>]*@max-xl\/composer:hidden/);
- expect(html).not.toContain("animate-zcode-alarm-ring");
- expect(html).not.toMatch(/data-highspeed-countdown="true"[^>]*text-icon-purple/);
- });
- it("已有会话的 Composer 模型留空时仍能打开模型菜单重选", () => {
- renderToolbarMarkup({
- sessionId: "existing-session",
- draftMode: false,
- draftConfig: { mode: "build" },
- });
- expect(captured.modelSelectProps).not.toBeNull();
- expect(captured.modelSelectProps?.normalizedValue).toBe("");
- expect(captured.thoughtControlProps).toBeNull();
- });
- it("renders an explicit retry action when the target Host model view fails", () => {
- const html = renderToolbarMarkup({
- modelSelectionView: null,
- modelSelectionState: { status: "error", error: new Error("read failed") },
- modelSelectionReload: vi.fn(),
- });
- expect(html).toContain("chat.toolbar.model.loadFailedRetry");
- expect(html).not.toContain('data-testid="mock-model-select"');
- });
- it("Start 权益目录更新不自动改输入框模型或显示旧领取引导", async () => {
- const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan;
- mocks.settings = {
- providerFamilyConnectionSelections: { bigmodel: { kind: "start-plan" } },
- providerFamilyDomain: {},
- };
- mocks.modelSelectionView = {
- revision: 2,
- providers: [
- {
- providerId,
- providerName: "BigModel Start Plan",
- config: {
- access: { type: "zhipu-account", accountType: "bigmodel", mode: "start-plan" },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://open.bigmodel.cn/api/anthropic",
- },
- models: ["GLM-5.3-Flash"],
- },
- models: [{ modelId: "GLM-5.3-Flash", config: completeDisabledModelConfig() }],
- },
- ],
- };
- const root: Root = createRoot(installMinimalDom());
- const props = {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: null,
- phase: null,
- draftConfig: {
- mode: "build",
- modelSelection: {
- providerId: "custom-openai",
- modelId: "gpt-4o",
- options: { reasoningLevel: "medium" },
- },
- },
- config: {
- provider: "custom-openai",
- model: "gpt-4o",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue" as const,
- mode: "build" as const,
- },
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- };
- await act(async () => {
- root.render(createElement(V4ComposerModelControls, props));
- await flushMicrotasks();
- });
- expect(mocks.selectModel).not.toHaveBeenCalled();
- expect(captured.modelSelectProps?.guideTooltipOpen).toBeUndefined();
- await act(async () => {
- root.render(createElement(V4ComposerModelControls, props));
- await flushMicrotasks();
- });
- expect(mocks.selectModel).not.toHaveBeenCalled();
- await act(async () => root.unmount());
- });
- it("将供应商配置名称传给模型触发器展示", () => {
- renderToolbarMarkup();
- expect(captured.modelSelectProps?.triggerLabel).toBe("Custom OpenAI/gpt-4o");
- expect(captured.modelSelectProps?.indicatorClassName).not.toContain("@sm/composer");
- expect(captured.modelSelectProps?.indicatorClassName).toContain(
- "group-data-[composer-model-icon=true]/toolbar:hidden",
- );
- expect(captured.modelSelectProps?.triggerClassName).toContain(
- "group-data-[composer-model-icon=true]/toolbar:size-7",
- );
- expect(captured.modelSelectProps?.triggerLabelPrefixClassName).not.toContain("@2xl/composer");
- expect(captured.modelSelectProps?.triggerLabelPrefixClassName).toContain(
- "group-data-[composer-provider-compact=true]/toolbar:hidden",
- );
- });
- it("Registry 已发布时只使用 Registry providerName", () => {
- mocks.modelSelectionView = {
- revision: 1,
- providers: [
- {
- providerId: "custom-openai",
- providerName: "Registry OpenAI",
- config: {
- access: { type: "api-key", apiKey: "test-key" },
- api: {
- type: "openai-chat-completions",
- baseUrl: "https://registry.example.com/v1",
- },
- models: ["gpt-4o"],
- },
- models: [{ modelId: "gpt-4o", config: completeDisabledModelConfig() }],
- },
- ],
- };
- renderToolbarMarkup();
- expect(captured.modelSelectProps?.triggerLabel).toBe("Registry OpenAI/gpt-4o");
- });
- it("prewarm 未就绪时不把旧 configOptions 模型当作 Selection 来源", async () => {
- mocks.configOptions = [
- {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue: customValue,
- options: [{ value: customValue, name: "GPT-4o" }],
- },
- ];
- mocks.modelSelectionView = {
- revision: 2,
- providers: [
- ...mocks.modelSelectionView!.providers,
- {
- providerId: "alternate-provider",
- providerName: "Alternate API",
- config: {
- access: { type: "api-key", apiKey: "alternate-key" },
- api: {
- type: "openai-chat-completions",
- baseUrl: "https://alt.example.com/v1",
- },
- models: ["deepseek-v4-pro"],
- },
- models: [{ modelId: "deepseek-v4-pro", config: completeDisabledModelConfig() }],
- },
- ],
- };
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: null,
- phase: null,
- config: null,
- draftMode: true,
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- }),
- );
- await flushMicrotasks();
- });
- await act(async () => {
- captured.modelSelectProps?.onValueChange("custom:alternate-provider:deepseek-v4-pro");
- await flushMicrotasks();
- });
- expect(mocks.selectModel).toHaveBeenCalledWith("alternate-provider", "deepseek-v4-pro", null);
- await act(async () => {
- root.unmount();
- });
- });
- it("V4 任务边界不再刷新 entitlement", async () => {
- const root: Root = createRoot(installMinimalDom());
- const renderPhase = (phase: V4ComposerModelControlsProps["phase"]) =>
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: "session-usage-refresh",
- phase,
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- });
- await act(async () => {
- root.render(renderPhase("running"));
- await flushMicrotasks();
- });
- expect(mocks.codingPlanEntitlementsRefresh).not.toHaveBeenCalled();
- await act(async () => {
- root.render(renderPhase("completedInterrupted"));
- await flushMicrotasks();
- });
- expect(mocks.codingPlanEntitlementsRefresh).not.toHaveBeenCalled();
- await act(async () => {
- root.render(renderPhase("running"));
- await flushMicrotasks();
- });
- expect(mocks.codingPlanEntitlementsRefresh).not.toHaveBeenCalled();
- await act(async () => {
- root.render(renderPhase("completedSuccess"));
- await flushMicrotasks();
- });
- expect(mocks.codingPlanEntitlementsRefresh).not.toHaveBeenCalled();
- await act(async () => {
- root.unmount();
- });
- });
- it("忽略模型切换期间受控 Select 产生的空 thought 事件", () => {
- mocks.configOptions = [
- {
- id: "thought_level",
- name: "Thought Level",
- category: "thought_level",
- type: "select",
- currentValue: "medium",
- options: [
- { label: "Medium", value: "medium" },
- { label: "High", value: "high" },
- ],
- },
- ];
- mocks.configOptionsError = false;
- const onSelectThought = vi.fn();
- renderToolbarMarkup({ onSelectThought });
- expect(captured.thoughtControlProps?.indicatorClassName).toBe("block");
- expect(captured.thoughtControlProps?.composerCollapsePriority).toBe(3);
- captured.thoughtControlProps?.onValueChange("");
- expect(onSelectThought).not.toHaveBeenCalled();
- captured.thoughtControlProps?.onValueChange("high");
- expect(onSelectThought).toHaveBeenCalledWith("high", {
- provider: "custom-openai",
- model: "gpt-4o",
- });
- });
- it("会话态候选档位只取目标 Host View,不取旧 Session 或 workspace 目录", () => {
- mocks.configOptions = [
- {
- id: "thought_level",
- name: "Thought Level",
- category: "thought_level",
- type: "select",
- currentValue: "off",
- options: ["off", "low", "medium", "high", "max"].map((value) => ({ value })),
- },
- ];
- mocks.modelSelectionView!.providers[0]!.models.push({
- modelId: "GLM-5.2",
- config: {
- optionSpecs: {
- reasoningLevel: { values: ["low", "high", "max"], map: "{}" },
- },
- },
- });
- renderToolbarMarkup({
- config: {
- provider: "custom-openai",
- model: "GLM-5.2",
- thought: "high",
- thoughtLevels: ["max", "high", "nothink"],
- followupMode: "queue",
- mode: "build",
- },
- });
- expect(captured.thoughtControlProps?.option.options?.map(({ value }) => value)).toEqual([
- "low",
- "high",
- "max",
- ]);
- });
- it.each([{ thoughtLevels: [] }, { thoughtLevels: ["disabled"] }])(
- "恢复的空档位不被目录补齐:$thoughtLevels",
- ({ thoughtLevels }) => {
- renderToolbarMarkup({
- config: {
- provider: "custom-openai",
- model: "gpt-4o",
- thought: "",
- thoughtLevels,
- followupMode: "queue",
- mode: "build",
- },
- });
- expect(captured.thoughtControlProps?.option.currentValue).toBe("");
- },
- );
- it("草稿预热完成后仍展示 Composer 选择与 Host 候选,不消费旧投影档位", () => {
- renderToolbarMarkup({
- draftMode: true,
- draftConfig: {
- mode: "build",
- modelSelection: {
- providerId: "custom-openai",
- modelId: "gpt-4o",
- options: { reasoningLevel: "disabled" },
- },
- },
- config: {
- provider: "custom-openai",
- model: "gpt-4o",
- thought: "high",
- thoughtLevels: ["max", "high", "nothink"],
- followupMode: "queue",
- mode: "build",
- },
- });
- expect(captured.thoughtControlProps?.option).toMatchObject({
- currentValue: "disabled",
- options: [{ value: "disabled" }],
- });
- });
- it("草稿切换目标模型时不复用旧预热投影的 thought 档位", () => {
- renderToolbarMarkup({
- draftMode: true,
- config: {
- provider: "custom-openai",
- model: "gpt-4o",
- thought: "high",
- thoughtLevels: ["medium", "high"],
- followupMode: "queue",
- mode: "build",
- },
- draftConfig: {
- modelSelection: { providerId: "custom-openai", modelId: "gpt-4.1" },
- },
- });
- expect(captured.thoughtControlProps).toBeNull();
- });
- it("输入框工具条不再渲染 provider retry 状态", () => {
- const html = renderToolbarMarkup();
- expect(html).not.toContain("重新连接中...");
- expect(html).toContain('data-testid="mock-usage"');
- });
- it("does not pass derived capacity or plan props for a non-plan model with no usage", () => {
- renderToolbarMarkup({
- usage: null,
- });
- const props = latestUsageProps();
- expect(props).not.toHaveProperty("contextCapacityTokens");
- expect(props.codingPlanUsageRemaining).toBeUndefined();
- expect(props.startPlanBalance).toBeUndefined();
- });
- it("Registry 尚未发布时不从旧 Provider Snapshot 构造模型菜单", () => {
- mocks.modelSelectionView = null;
- renderToolbarMarkup();
- expect(captured.modelSelectProps).toBeNull();
- });
- it("passes Coding Plan usage only when the current connection is the selected OAuth Coding Plan", () => {
- addAccountProviderAccess(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- { accountType: "zai", mode: "individual-coding-plan" },
- "Z.ai - Coding Plan",
- );
- mocks.codingPlanEntitlements = {
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
- snapshot: createActiveEntitlement(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan),
- loading: false,
- error: null,
- },
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- expect(latestUsageProps().codingPlanUsageRemaining).toBeUndefined();
- captured.usageProps = [];
- mocks.settings = {
- providerFamilyConnectionSelections: {
- zai: { kind: "individual-coding-plan" },
- },
- providerFamilyDomain: {},
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- const props = latestUsageProps();
- expect(props.codingPlanUsageRemaining).toMatchObject({
- selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- });
- expect(props.startPlanBalance).toBeUndefined();
- });
- it("does not derive Coding Plan usage from a legacy provider without Account Access", () => {
- mocks.codingPlanEntitlements = {
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
- snapshot: createActiveEntitlement(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan),
- loading: false,
- error: null,
- },
- };
- mocks.settings = {
- providerFamilyConnectionSelections: {
- zai: { kind: "individual-coding-plan" },
- },
- providerFamilyDomain: {},
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- expect(latestUsageProps().codingPlanUsageRemaining).toBeUndefined();
- });
- it.each([
- undefined,
- { kind: "start-plan" },
- { kind: "individual-coding-plan" },
- { kind: "team-coding-plan", organizationId: "org", projectId: "project", productId: "plan" },
- ] as const)(
- "Start balance follows composer selection independent of saved connection %j",
- (selection) => {
- mocks.codingPlanEntitlements = {
- [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: {
- snapshot: createActiveEntitlement(BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan),
- loading: false,
- error: null,
- },
- };
- mocks.settings = {
- providerFamilyConnectionSelections: {
- zai: selection,
- },
- providerFamilyDomain: {},
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- const props = latestUsageProps();
- expect(props.codingPlanUsageRemaining).toBeUndefined();
- expect(props.startPlanBalance).toMatchObject({
- loading: false,
- snapshot: {
- provider: {
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- },
- },
- });
- },
- );
- it("does not pass Start Plan balance when the entitlement hook hides fallback balance", () => {
- mocks.codingPlanEntitlements = {
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
- snapshot: createActiveEntitlement(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan),
- loading: false,
- error: null,
- },
- [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: {
- snapshot: null,
- loading: false,
- error: null,
- },
- };
- mocks.settings = {
- providerFamilyConnectionSelections: {
- zai: { kind: "start-plan" },
- },
- providerFamilyDomain: {},
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- expect(latestUsageProps().startPlanBalance).toBeUndefined();
- });
- it("attaches the hover access refresh entry when the Start Plan entitlement is enabled", () => {
- mocks.codingPlanEntitlements = {
- [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: {
- snapshot: null,
- loading: false,
- error: null,
- },
- };
- mocks.codingPlanEntitlementsEnabledStartPlanIds = [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan];
- mocks.settings = {
- providerFamilyConnectionSelections: {
- zai: { kind: "start-plan" },
- },
- providerFamilyDomain: {},
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- const startPlanBalance = latestUsageProps().startPlanBalance as {
- onAccess?: () => unknown;
- };
- // 修复原因:start plan 用户原先没有 hover onAccess 入口,context 面板从不主动刷新今日余额。
- expect(typeof startPlanBalance?.onAccess).toBe("function");
- expect(startPlanBalance.onAccess).not.toBeNull();
- });
- it("recovers a Team Plan usage source from entitlement snapshot when products are not hydrated", () => {
- const teamSourceId = "team:bigmodel:team-pro:org-1:project-1";
- addAccountProviderAccess(BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan, {
- accountType: "bigmodel",
- mode: "team-coding-plan",
- });
- mocks.settings = {
- providerFamilyConnectionSelections: {
- bigmodel: {
- kind: "team-coding-plan",
- productId: "team-pro",
- organizationId: "org-1",
- projectId: "project-1",
- },
- },
- providerFamilyDomain: {},
- };
- mocks.codingPlanEntitlements = {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
- snapshot: createActiveEntitlement(BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan, {
- context: {
- scope: "team",
- organizationId: "org-1",
- projectId: "project-1",
- productId: "team-pro",
- displayName: "Platform",
- },
- subscription: {
- details: [
- {
- beginTime: "2026-01-01T00:00:00Z",
- expireTime: "2026-12-31T00:00:00Z",
- productId: "team-pro",
- productName: "Platform",
- purchaseTime: "2026-01-01T00:00:00Z",
- },
- ],
- },
- }),
- loading: false,
- error: null,
- },
- };
- mocks.usageEntitlement = {
- snapshot: createActiveEntitlement(BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan, {
- context: {
- scope: "team",
- organizationId: "org-1",
- projectId: "project-1",
- productId: "team-pro",
- displayName: "Platform",
- },
- }),
- loading: false,
- error: null,
- };
- renderToolbarMarkup({
- config: {
- provider: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- model: "glm-5",
- thought: "medium",
- thoughtLevels: ["medium"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- });
- expect(latestUsageProps().codingPlanUsageRemaining).toMatchObject({
- selectedProviderId: teamSourceId,
- });
- });
- it("routes custom model selection to the recovery callback when configOptions failed", async () => {
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: null,
- phase: null,
- draftConfig: {
- mode: "build",
- modelSelection: {
- providerId: "primary-provider",
- modelId: "source-model",
- options: { reasoningLevel: "high" },
- },
- },
- config: {
- provider: "primary-provider",
- model: "source-model",
- thought: "high",
- thoughtLevels: ["high"],
- followupMode: "queue",
- mode: "build",
- },
- draftMode: true,
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- onRecoverCustomModelSelection: mocks.recoverCustomModelSelection,
- }),
- );
- await flushMicrotasks();
- });
- await act(async () => {
- captured.modelSelectProps?.onValueChange(customValue);
- await flushMicrotasks();
- });
- expect(mocks.recoverCustomModelSelection).toHaveBeenCalledWith(customValue, {
- provider: "primary-provider",
- model: "source-model",
- });
- expect(mocks.selectModel).not.toHaveBeenCalled();
- await act(async () => {
- root.unmount();
- });
- });
- it("does not expose a legacy-only provider after an empty Registry view is published", async () => {
- mocks.modelSelectionView = { revision: 1, providers: [] };
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: null,
- phase: null,
- config: {
- provider: "primary-provider",
- model: "source-model",
- thought: "high",
- thoughtLevels: ["high"],
- followupMode: "queue",
- mode: "build",
- },
- draftMode: true,
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- onRecoverCustomModelSelection: mocks.recoverCustomModelSelection,
- }),
- );
- await flushMicrotasks();
- });
- expect(captured.modelSelectProps).toBeNull();
- expect(mocks.recoverCustomModelSelection).not.toHaveBeenCalled();
- expect(mocks.selectModel).not.toHaveBeenCalled();
- await act(async () => {
- root.unmount();
- });
- });
- it("does not route builtin model selection through custom recovery when configOptions failed", async () => {
- const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
- mocks.modelSelectionView = {
- revision: 2,
- providers: [
- {
- providerId,
- providerName: "BigModel Coding",
- config: {
- access: {
- type: "zhipu-account",
- },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://open.bigmodel.cn/api/anthropic",
- },
- models: ["glm-5"],
- },
- models: [{ modelId: "glm-5", config: completeDisabledModelConfig() }],
- },
- ],
- };
- const value = `custom:${encodeURIComponent(providerId)}:glm-5`;
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: null,
- phase: null,
- config: null,
- draftMode: true,
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- onRecoverCustomModelSelection: mocks.recoverCustomModelSelection,
- }),
- );
- await flushMicrotasks();
- });
- await act(async () => {
- captured.modelSelectProps?.onValueChange(value);
- await flushMicrotasks();
- });
- expect(mocks.recoverCustomModelSelection).not.toHaveBeenCalled();
- expect(mocks.selectModel).toHaveBeenCalledWith(providerId, "glm-5", null);
- await act(async () => {
- root.unmount();
- });
- });
- it("does not write a menu-first fallback when a session model is absent from the visible catalog", async () => {
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(V4ComposerModelControls, {
- workspacePath: "/workspace",
- modelSelectionView: mocks.modelSelectionView,
- sessionId: "session-removed-model",
- phase: "completedSuccess",
- config: {
- provider: "removed-provider",
- model: "removed-model",
- thought: "high",
- thoughtLevels: ["high"],
- followupMode: "queue",
- mode: "build",
- },
- usage: null,
- disabled: false,
- onSelectModel: mocks.selectModel,
- onSelectThought: vi.fn(),
- onSwitchMode: vi.fn(),
- }),
- );
- await flushMicrotasks();
- });
- expect(mocks.selectModel).not.toHaveBeenCalled();
- await act(async () => {
- root.unmount();
- });
- });
- });
|