fix(providers): preserve custom endpoints in meta during add/edit operations

Fixed two critical data loss bugs where user-added custom endpoints were discarded:

1. **AddProviderDialog**: Form submission ignored values.meta from ProviderForm and
   re-inferred URLs only from presets/config, causing loss of endpoints added via
   speed test modal. Now prioritizes form-collected meta and uses fallback inference
   only when custom_endpoints is missing.

2. **ProviderForm**: Edit mode always returned initialData.meta, discarding any
   changes made in the speed test modal. Now uses mergeProviderMeta to properly
   merge customEndpointsMap with existing meta fields.

Changes:
- Extract mergeProviderMeta utility to handle meta field merging logic
- Preserve other meta fields (e.g., usage_script) during endpoint updates
- Unify new/edit code paths to use consistent meta handling
- Add comprehensive unit tests for meta merging scenarios
- Add integration tests for AddProviderDialog submission flow

Impact:
- Third-party and custom providers can now reliably manage multiple endpoints
- Edit operations correctly reflect user modifications
- No data loss for existing meta fields like usage_script
This commit is contained in:
Jason
2025-10-28 20:28:11 +08:00
parent 1841f8b462
commit 7d56aed543
5 changed files with 318 additions and 75 deletions

View File

@@ -0,0 +1,36 @@
import type { CustomEndpoint, ProviderMeta } from "@/types";
/**
* 合并供应商元数据中的自定义端点。
* - 当 customEndpoints 为空对象或 null 时,移除自定义端点但保留其它元数据。
* - 当 customEndpoints 存在时,覆盖原有自定义端点。
* - 若结果为空对象则返回 undefined避免写入空 meta。
*/
export function mergeProviderMeta(
initialMeta: ProviderMeta | undefined,
customEndpoints:
| Record<string, CustomEndpoint>
| null
| undefined,
): ProviderMeta | undefined {
const hasCustomEndpoints =
!!customEndpoints && Object.keys(customEndpoints).length > 0;
if (hasCustomEndpoints) {
return {
...(initialMeta ? { ...initialMeta } : {}),
custom_endpoints: customEndpoints!,
};
}
if (!initialMeta) {
return undefined;
}
if ("custom_endpoints" in initialMeta) {
const { custom_endpoints, ...rest } = initialMeta;
return Object.keys(rest).length > 0 ? rest : undefined;
}
return { ...initialMeta };
}