settingsListCreateAction.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it } from "vitest";
  3. // 各资源页头部动作已统一由 SettingsResourceHeaderActions 渲染:
  4. // markup 断言落在共享组件上,分区文件只断言委托与传入的 action id。
  5. const delegatingSections = [
  6. "CommandsSection.tsx",
  7. "SubagentsSection.tsx",
  8. "HooksSection.tsx",
  9. "McpSettingsSection.tsx",
  10. "SkillsSection.tsx",
  11. ] as const;
  12. describe("settings list create actions", () => {
  13. it("renders the shared create action as a default icon-and-label button", async () => {
  14. const source = await readFile(
  15. new URL("../src/settings/SettingsResourceHeaderActions.tsx", import.meta.url),
  16. "utf8",
  17. );
  18. const markerStart = source.indexOf("data-settings-create-action={newActionId}");
  19. expect(markerStart).toBeGreaterThan(-1);
  20. const actionStart = source.lastIndexOf("<Button", markerStart);
  21. const actionEnd = source.indexOf("</Button>", actionStart) + "</Button>".length;
  22. const action = source.slice(actionStart, actionEnd);
  23. expect(action).toContain('variant="default"');
  24. expect(action).toContain('size="default"');
  25. expect(action).toContain('className="rounded-lg"');
  26. expect(action).toContain('<Plus data-icon="inline-start"');
  27. expect(action).toContain("aria-label={newLabel}");
  28. expect(source).toContain('id: "settings.create.action"');
  29. });
  30. it.each(delegatingSections)(
  31. "%s routes create and import through the shared header actions",
  32. async (fileName) => {
  33. const source = await readFile(
  34. new URL(`../src/settings/${fileName}`, import.meta.url),
  35. "utf8",
  36. );
  37. expect(source).toContain("<SettingsResourceHeaderActions");
  38. expect(source).toMatch(/onNew=\{/);
  39. },
  40. );
  41. it("labels the shared overflow import action with a visible menu item", async () => {
  42. const source = await readFile(
  43. new URL("../src/settings/SettingsResourceHeaderActions.tsx", import.meta.url),
  44. "utf8",
  45. );
  46. const markerStart = source.indexOf("data-settings-import-action={importActionId}");
  47. expect(markerStart).toBeGreaterThan(-1);
  48. const itemStart = source.lastIndexOf("<DropdownMenuItem", markerStart);
  49. const itemEnd = source.indexOf("</DropdownMenuItem>", itemStart) + "</DropdownMenuItem>".length;
  50. const item = source.slice(itemStart, itemEnd);
  51. expect(item).toContain("onSelect={onImport}");
  52. expect(item).toContain("{importLabel}");
  53. expect(source).toContain('id: "settings.resourceActions.import"');
  54. });
  55. it.each([
  56. ["McpSettingsSection.tsx", "settings.mcp.import.open", "settings.mcp.create.open"],
  57. ["SkillsSection.tsx", "settings.skills.import.open", "settings.skills.create.open"],
  58. ["HooksSection.tsx", undefined, "settings.hooks.add"],
  59. ] as const)(
  60. "%s passes its action ids to the shared header actions",
  61. async (fileName, importActionId, newActionId) => {
  62. const source = await readFile(
  63. new URL(`../src/settings/${fileName}`, import.meta.url),
  64. "utf8",
  65. );
  66. if (importActionId) {
  67. expect(source).toContain(`importActionId="${importActionId}"`);
  68. }
  69. expect(source).toContain(`newActionId="${newActionId}"`);
  70. },
  71. );
  72. it.each([
  73. ["McpSettingsSection.tsx", "settings.mcp.import.action"],
  74. ["SkillsSection.tsx", "settings.skills.import.action"],
  75. ] as const)(
  76. "%s keeps a visible label on the inline import dialog button",
  77. async (fileName, messageId) => {
  78. const source = await readFile(
  79. new URL(`../src/settings/${fileName}`, import.meta.url),
  80. "utf8",
  81. );
  82. expect(source).toMatch(
  83. /variant="outline"\s+size="lg"[^]*?<Import data-icon="inline-start"[^]*?id: "[\w.]*import\.action"/,
  84. );
  85. expect(source).toContain(`id: "${messageId}"`);
  86. },
  87. );
  88. it("keeps Automations on its dedicated create controls", async () => {
  89. const source = await readFile(
  90. new URL("../src/settings/AutomationsSection.tsx", import.meta.url),
  91. "utf8",
  92. );
  93. expect(source).not.toContain("SettingsResourceHeaderActions");
  94. });
  95. it("shares the two-item Add menu across plugin pages", async () => {
  96. const menu = await readFile(
  97. new URL("../src/settings/PluginAddMenu.tsx", import.meta.url),
  98. "utf8",
  99. );
  100. expect(menu.match(/<DropdownMenuItem[\s>]/g)).toHaveLength(2);
  101. expect(menu).toContain('id: "pluginCreator.create"');
  102. expect(menu).toContain('id: "pluginCreator.addMarketplace"');
  103. for (const file of ["PluginStorePage.tsx", "PluginsSection.tsx"]) {
  104. const source = await readFile(new URL(`../src/settings/${file}`, import.meta.url), "utf8");
  105. expect(source).toContain("<PluginAddMenu");
  106. }
  107. });
  108. it("keeps source addition at the end of the marketplace header through the Add menu", async () => {
  109. const source = await readFile(
  110. new URL("../src/settings/PluginStorePage.tsx", import.meta.url),
  111. "utf8",
  112. );
  113. const actionStart = source.indexOf("<PluginAddMenu");
  114. expect(actionStart).toBeGreaterThan(source.indexOf('data-testid="plugin-store-sources-open"'));
  115. const action = source.slice(actionStart, source.indexOf("/>", actionStart));
  116. expect(action).toContain("onAddMarketplace={() => {");
  117. expect(action).toContain("setAddMarketplaceError(null)");
  118. expect(action).toContain("setAddSourceOpen(true)");
  119. });
  120. });