providerSettingsFormBoundary.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. it.each(["ProviderConnectionModeSection", "useActiveModelProviderWorkspace"])(
  65. "Provider 设置不再保留无调用方的迁移 helper:%s",
  66. async (retiredHelper) => {
  67. const settingsRoot = join(import.meta.dirname, "../src/settings/model-provider-section");
  68. const sourceFiles = await collectSourceFiles(settingsRoot);
  69. const offenders: string[] = [];
  70. for (const filePath of sourceFiles) {
  71. if (new RegExp(`\\b${retiredHelper}\\b`, "u").test(await readFile(filePath, "utf8"))) {
  72. offenders.push(filePath.slice(settingsRoot.length + 1));
  73. }
  74. }
  75. expect(offenders).toEqual([]);
  76. },
  77. );
  78. it("模型交互不再受 Provider 级 modelsReadOnly 与表单 API Key 门禁", async () => {
  79. const settingsRoot = join(import.meta.dirname, "../src/settings/model-provider-section");
  80. const sourceFiles = await collectSourceFiles(settingsRoot);
  81. const source = (
  82. await Promise.all(sourceFiles.map((filePath) => readFile(filePath, "utf8")))
  83. ).join("\n");
  84. expect(source).not.toContain("modelsReadOnly");
  85. expect(source).not.toMatch(/model\.executable\s*&&\s*apiKeyValue\.trim/u);
  86. });
  87. });
  88. async function collectSourceFiles(directory: string): Promise<string[]> {
  89. const entries = await readdir(directory, { withFileTypes: true });
  90. const files: string[] = [];
  91. for (const entry of entries) {
  92. const filePath = join(directory, entry.name);
  93. if (entry.isDirectory()) {
  94. files.push(...(await collectSourceFiles(filePath)));
  95. } else if (/\.(?:ts|tsx)$/u.test(entry.name)) {
  96. files.push(filePath);
  97. }
  98. }
  99. return files;
  100. }