providerSettingsFormBoundary.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { readdir, readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. describe("Provider Settings 表单边界", () => {
  5. it("生产 UI 不再把旧 ModelProviderConfig 当作当前设置类型", async () => {
  6. const sourceRoot = join(import.meta.dirname, "../src");
  7. const sourceFiles = await collectSourceFiles(sourceRoot);
  8. const offenders: string[] = [];
  9. for (const filePath of sourceFiles) {
  10. const source = await readFile(filePath, "utf8");
  11. if (/\bModelProvider(?:Model)?Config\b/u.test(source)) {
  12. offenders.push(filePath.slice(sourceRoot.length + 1));
  13. }
  14. }
  15. expect(offenders).toEqual([]);
  16. });
  17. it("生产 UI 不再重新解释旧 Provider 启动可用性", async () => {
  18. const sourceRoot = join(import.meta.dirname, "../src");
  19. const sourceFiles = await collectSourceFiles(sourceRoot);
  20. const offenders: string[] = [];
  21. for (const filePath of sourceFiles) {
  22. const source = await readFile(filePath, "utf8");
  23. if (
  24. /\b(?:isModelProviderUsableForAgentStartup|hasUsableModelProviderForAgentStartup)\b/u.test(
  25. source,
  26. )
  27. ) {
  28. offenders.push(filePath.slice(sourceRoot.length + 1));
  29. }
  30. }
  31. expect(offenders).toEqual([]);
  32. });
  33. it("Renderer 编辑态直接复用正式 Config 字段,不再复刻旧 Store 结构", async () => {
  34. const formTypesPath = join(import.meta.dirname, "../src/lib/providerSettingsFormTypes.ts");
  35. const source = await readFile(formTypesPath, "utf8");
  36. expect(source).toContain("ProviderConfigObject");
  37. expect(source).toContain("ModelConfigObject");
  38. expect(source).not.toMatch(
  39. /\b(?:endpoints|defaultKind|kinds|modalities|ProviderOptionsPatch|createdAt|updatedAt)\b/u,
  40. );
  41. expect(source).not.toContain('from "@zcode/shared"');
  42. });
  43. it("API 格式展示不再保留模型级 kind 与 endpoint 编辑旁路", async () => {
  44. const apiFormatDisplayPath = join(
  45. import.meta.dirname,
  46. "../src/settings/model-provider-section/ProviderApiFormatSelect.tsx",
  47. );
  48. const source = await readFile(apiFormatDisplayPath, "utf8");
  49. expect(source).not.toMatch(
  50. /\b(?:ModelProviderKind|getDefaultModelProviderEndpointPathForKind|mapModelProviderApiFormatToKind|ProviderModelApiFormatMultiSelect|endpointPaths)\b/u,
  51. );
  52. });
  53. it("Provider 设置使用正式 ProviderApiType,不再引用旧 Store API Format 类型", async () => {
  54. const settingsRoot = join(import.meta.dirname, "../src/settings/model-provider-section");
  55. const sourceFiles = await collectSourceFiles(settingsRoot);
  56. const offenders: string[] = [];
  57. for (const filePath of sourceFiles) {
  58. if (/\bModelProviderApiFormat\b/u.test(await readFile(filePath, "utf8"))) {
  59. offenders.push(filePath.slice(settingsRoot.length + 1));
  60. }
  61. }
  62. expect(offenders).toEqual([]);
  63. });
  64. });
  65. async function collectSourceFiles(directory: string): Promise<string[]> {
  66. const entries = await readdir(directory, { withFileTypes: true });
  67. const files: string[] = [];
  68. for (const entry of entries) {
  69. const filePath = join(directory, entry.name);
  70. if (entry.isDirectory()) {
  71. files.push(...(await collectSourceFiles(filePath)));
  72. } else if (/\.(?:ts|tsx)$/u.test(entry.name)) {
  73. files.push(filePath);
  74. }
  75. }
  76. return files;
  77. }