Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
280 lines
6.6 KiB
TypeScript
280 lines
6.6 KiB
TypeScript
import type { AppId } from "@/lib/api/types";
|
|
import type { McpServer, Provider, Settings } from "@/types";
|
|
|
|
type ProvidersByApp = Record<AppId, Record<string, Provider>>;
|
|
type CurrentProviderState = Record<AppId, string>;
|
|
type McpConfigState = Record<AppId, Record<string, McpServer>>;
|
|
|
|
const createDefaultProviders = (): ProvidersByApp => ({
|
|
claude: {
|
|
"claude-1": {
|
|
id: "claude-1",
|
|
name: "Claude Default",
|
|
settingsConfig: {},
|
|
category: "official",
|
|
sortIndex: 0,
|
|
createdAt: Date.now(),
|
|
},
|
|
"claude-2": {
|
|
id: "claude-2",
|
|
name: "Claude Custom",
|
|
settingsConfig: {},
|
|
category: "custom",
|
|
sortIndex: 1,
|
|
createdAt: Date.now() + 1,
|
|
},
|
|
},
|
|
codex: {
|
|
"codex-1": {
|
|
id: "codex-1",
|
|
name: "Codex Default",
|
|
settingsConfig: {},
|
|
category: "official",
|
|
sortIndex: 0,
|
|
createdAt: Date.now(),
|
|
},
|
|
"codex-2": {
|
|
id: "codex-2",
|
|
name: "Codex Secondary",
|
|
settingsConfig: {},
|
|
category: "custom",
|
|
sortIndex: 1,
|
|
createdAt: Date.now() + 1,
|
|
},
|
|
},
|
|
gemini: {
|
|
"gemini-1": {
|
|
id: "gemini-1",
|
|
name: "Gemini Default",
|
|
settingsConfig: {
|
|
env: {
|
|
GEMINI_API_KEY: "test-key",
|
|
GOOGLE_GEMINI_BASE_URL: "https://generativelanguage.googleapis.com",
|
|
},
|
|
},
|
|
category: "official",
|
|
sortIndex: 0,
|
|
createdAt: Date.now(),
|
|
},
|
|
},
|
|
});
|
|
|
|
const createDefaultCurrent = (): CurrentProviderState => ({
|
|
claude: "claude-1",
|
|
codex: "codex-1",
|
|
gemini: "gemini-1",
|
|
});
|
|
|
|
let providers = createDefaultProviders();
|
|
let current = createDefaultCurrent();
|
|
let settingsState: Settings = {
|
|
showInTray: true,
|
|
minimizeToTrayOnClose: true,
|
|
enableClaudePluginIntegration: false,
|
|
claudeConfigDir: "/default/claude",
|
|
codexConfigDir: "/default/codex",
|
|
language: "zh",
|
|
};
|
|
let appConfigDirOverride: string | null = null;
|
|
let mcpConfigs: McpConfigState = {
|
|
claude: {
|
|
sample: {
|
|
id: "sample",
|
|
name: "Sample Claude Server",
|
|
enabled: true,
|
|
apps: { claude: true, codex: false, gemini: false },
|
|
server: {
|
|
type: "stdio",
|
|
command: "claude-server",
|
|
},
|
|
},
|
|
},
|
|
codex: {
|
|
httpServer: {
|
|
id: "httpServer",
|
|
name: "HTTP Codex Server",
|
|
enabled: false,
|
|
apps: { claude: false, codex: true, gemini: false },
|
|
server: {
|
|
type: "http",
|
|
url: "http://localhost:3000",
|
|
},
|
|
},
|
|
},
|
|
gemini: {},
|
|
};
|
|
|
|
const cloneProviders = (value: ProvidersByApp) =>
|
|
JSON.parse(JSON.stringify(value)) as ProvidersByApp;
|
|
|
|
export const resetProviderState = () => {
|
|
providers = createDefaultProviders();
|
|
current = createDefaultCurrent();
|
|
settingsState = {
|
|
showInTray: true,
|
|
minimizeToTrayOnClose: true,
|
|
enableClaudePluginIntegration: false,
|
|
claudeConfigDir: "/default/claude",
|
|
codexConfigDir: "/default/codex",
|
|
language: "zh",
|
|
};
|
|
appConfigDirOverride = null;
|
|
mcpConfigs = {
|
|
claude: {
|
|
sample: {
|
|
id: "sample",
|
|
name: "Sample Claude Server",
|
|
enabled: true,
|
|
apps: { claude: true, codex: false, gemini: false },
|
|
server: {
|
|
type: "stdio",
|
|
command: "claude-server",
|
|
},
|
|
},
|
|
},
|
|
codex: {
|
|
httpServer: {
|
|
id: "httpServer",
|
|
name: "HTTP Codex Server",
|
|
enabled: false,
|
|
apps: { claude: false, codex: true, gemini: false },
|
|
server: {
|
|
type: "http",
|
|
url: "http://localhost:3000",
|
|
},
|
|
},
|
|
},
|
|
gemini: {},
|
|
};
|
|
};
|
|
|
|
export const getProviders = (appType: AppId) =>
|
|
cloneProviders(providers)[appType] ?? {};
|
|
|
|
export const getCurrentProviderId = (appType: AppId) => current[appType] ?? "";
|
|
|
|
export const setCurrentProviderId = (appType: AppId, providerId: string) => {
|
|
current[appType] = providerId;
|
|
};
|
|
|
|
export const updateProviders = (
|
|
appType: AppId,
|
|
data: Record<string, Provider>,
|
|
) => {
|
|
providers[appType] = cloneProviders({ [appType]: data } as ProvidersByApp)[
|
|
appType
|
|
];
|
|
};
|
|
|
|
export const setProviders = (
|
|
appType: AppId,
|
|
data: Record<string, Provider>,
|
|
) => {
|
|
providers[appType] = JSON.parse(JSON.stringify(data)) as Record<
|
|
string,
|
|
Provider
|
|
>;
|
|
};
|
|
|
|
export const addProvider = (appType: AppId, provider: Provider) => {
|
|
providers[appType] = providers[appType] ?? {};
|
|
providers[appType][provider.id] = provider;
|
|
};
|
|
|
|
export const updateProvider = (appType: AppId, provider: Provider) => {
|
|
if (!providers[appType]) return;
|
|
providers[appType][provider.id] = {
|
|
...providers[appType][provider.id],
|
|
...provider,
|
|
};
|
|
};
|
|
|
|
export const deleteProvider = (appType: AppId, providerId: string) => {
|
|
if (!providers[appType]) return;
|
|
delete providers[appType][providerId];
|
|
if (current[appType] === providerId) {
|
|
const fallback = Object.keys(providers[appType])[0] ?? "";
|
|
current[appType] = fallback;
|
|
}
|
|
};
|
|
|
|
export const updateSortOrder = (
|
|
appType: AppId,
|
|
updates: { id: string; sortIndex: number }[],
|
|
) => {
|
|
if (!providers[appType]) return;
|
|
updates.forEach(({ id, sortIndex }) => {
|
|
const provider = providers[appType][id];
|
|
if (provider) {
|
|
providers[appType][id] = { ...provider, sortIndex };
|
|
}
|
|
});
|
|
};
|
|
|
|
export const listProviders = (appType: AppId) =>
|
|
JSON.parse(JSON.stringify(providers[appType] ?? {})) as Record<
|
|
string,
|
|
Provider
|
|
>;
|
|
|
|
export const getSettings = () =>
|
|
JSON.parse(JSON.stringify(settingsState)) as Settings;
|
|
|
|
export const setSettings = (data: Partial<Settings>) => {
|
|
settingsState = { ...settingsState, ...data };
|
|
};
|
|
|
|
export const getAppConfigDirOverride = () => appConfigDirOverride;
|
|
|
|
export const setAppConfigDirOverrideState = (value: string | null) => {
|
|
appConfigDirOverride = value;
|
|
};
|
|
|
|
export const getMcpConfig = (appType: AppId) => {
|
|
const servers = JSON.parse(
|
|
JSON.stringify(mcpConfigs[appType] ?? {}),
|
|
) as Record<string, McpServer>;
|
|
return {
|
|
configPath: `/mock/${appType}.mcp.json`,
|
|
servers,
|
|
};
|
|
};
|
|
|
|
export const setMcpConfig = (
|
|
appType: AppId,
|
|
value: Record<string, McpServer>,
|
|
) => {
|
|
mcpConfigs[appType] = JSON.parse(JSON.stringify(value)) as Record<
|
|
string,
|
|
McpServer
|
|
>;
|
|
};
|
|
|
|
export const setMcpServerEnabled = (
|
|
appType: AppId,
|
|
id: string,
|
|
enabled: boolean,
|
|
) => {
|
|
if (!mcpConfigs[appType]?.[id]) return;
|
|
mcpConfigs[appType][id] = {
|
|
...mcpConfigs[appType][id],
|
|
enabled,
|
|
};
|
|
};
|
|
|
|
export const upsertMcpServer = (
|
|
appType: AppId,
|
|
id: string,
|
|
server: McpServer,
|
|
) => {
|
|
if (!mcpConfigs[appType]) {
|
|
mcpConfigs[appType] = {};
|
|
}
|
|
mcpConfigs[appType][id] = JSON.parse(JSON.stringify(server)) as McpServer;
|
|
};
|
|
|
|
export const deleteMcpServer = (appType: AppId, id: string) => {
|
|
if (!mcpConfigs[appType]) return;
|
|
delete mcpConfigs[appType][id];
|
|
};
|