settingsFormActions.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. function readSource(path: string): string {
  4. return readFileSync(new URL(`../${path}`, import.meta.url), "utf8");
  5. }
  6. describe("settings form actions", () => {
  7. // 各资源页的头部 New 按钮已统一由 SettingsResourceHeaderActions 渲染,尺寸约束随之
  8. // 移到该共享组件;这里改为在真实位置断言尺寸,并确认子智能体页确实走同一组件。
  9. it("列表头部 New 按钮统一由共享组件渲染", () => {
  10. const actions = readSource("src/settings/SettingsResourceHeaderActions.tsx");
  11. expect(actions).toMatch(
  12. /size="default"\s+className="rounded-lg"\s+data-settings-create-action=\{newActionId\}\s+aria-label=\{newLabel\}/,
  13. );
  14. expect(readSource("src/settings/SubagentsSection.tsx")).toContain(
  15. "<SettingsResourceHeaderActions",
  16. );
  17. });
  18. it.each([
  19. "McpServerForm.tsx",
  20. "CommandForm.tsx",
  21. "HookForm.tsx",
  22. "SubagentsSection.tsx",
  23. ])("统一 %s 的删除、保存、取消布局", (file) => {
  24. const source = readSource(`src/settings/${file}`);
  25. expect(source).toContain("<SettingsFormActions");
  26. expect(source.lastIndexOf('id: "common.cancel"')).toBeGreaterThan(
  27. source.lastIndexOf('id: "common.save"'),
  28. );
  29. });
  30. // Bugfix 回归:CommandForm 曾因迁移 SettingsFormActions 后又插入一个按钮而渲染两个
  31. // 完全相同的保存按钮;只断言顺序无法发现重复,必须断言数量。
  32. it.each([
  33. "McpServerForm.tsx",
  34. "CommandForm.tsx",
  35. "HookForm.tsx",
  36. "SubagentsSection.tsx",
  37. ])("%s 只渲染一个保存按钮", (file) => {
  38. const source = readSource(`src/settings/${file}`);
  39. expect(source.split('id: "common.save"').length - 1).toBe(1);
  40. });
  41. it("新建和编辑 MCP 都使用保存文案", () => {
  42. const source = readSource("src/settings/McpServerForm.tsx");
  43. expect(source).toContain('id: "common.save"');
  44. expect(source).not.toContain('id: "settings.mcp.form.add"');
  45. });
  46. it("操作栏始终把取消与保存放到右侧", () => {
  47. const source = readSource("src/settings/SettingsFormActions.tsx");
  48. expect(source).toContain("sm:ml-auto");
  49. expect(source).toContain("justify-end");
  50. });
  51. it.each([
  52. "McpServerForm.tsx",
  53. "CommandForm.tsx",
  54. "HookForm.tsx",
  55. "SubagentsSection.tsx",
  56. ])("统一 %s 的保存可用状态", (file) => {
  57. const source = readSource(`src/settings/${file}`);
  58. expect(source).toContain("const canSave =");
  59. expect(source).toMatch(/disabled=\{[^}]*!canSave/);
  60. });
  61. it("子智能体的保存判定属于编辑表单", () => {
  62. const source = readSource("src/settings/SubagentsSection.tsx");
  63. const formSource = source.slice(source.indexOf("function SubagentForm("));
  64. expect(formSource).toContain("const canSave =");
  65. expect(formSource).toContain("disabled={!canSave || saving}");
  66. });
  67. });