settingsCommandWorkspaceScope.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. getCommandWorkspaceTabs,
  5. resolveCommandScopeRecovery,
  6. resolveCommandStorageTarget,
  7. shouldRefreshCurrentCommandList,
  8. } from "../src/settings/commandWorkspaceScope.js";
  9. import type { WorkspaceTabState } from "../src/store/tabStore.js";
  10. function readSource(path: string): string {
  11. return readFileSync(new URL(`../${path}`, import.meta.url), "utf8");
  12. }
  13. describe("Command settings concrete Workspace scope", () => {
  14. const localTab = {
  15. id: "local",
  16. kind: "workspace",
  17. label: "repo",
  18. workspacePath: "/repo",
  19. } as WorkspaceTabState;
  20. const remoteTabA = {
  21. id: "remote-a",
  22. kind: "workspace",
  23. label: "default",
  24. workspaceIdentity: "ssh://host-a/root",
  25. workspacePath: "/root",
  26. remoteSessionId: "session-a",
  27. } as WorkspaceTabState;
  28. const remoteTabB = {
  29. id: "remote-b",
  30. kind: "workspace",
  31. label: "default",
  32. workspaceIdentity: "ssh://host-b/root",
  33. workspacePath: "/root",
  34. remoteSessionId: "session-b",
  35. } as WorkspaceTabState;
  36. it("keeps same-path remote Workspaces isolated by identity", () => {
  37. const tabs = getCommandWorkspaceTabs([
  38. localTab,
  39. { ...localTab, id: "local-duplicate" },
  40. remoteTabA,
  41. remoteTabB,
  42. ]);
  43. expect(tabs).toEqual([localTab, remoteTabA, remoteTabB]);
  44. expect(resolveCommandStorageTarget("ssh://host-b/root", tabs)).toEqual({
  45. storageLevel: "project",
  46. workspace: remoteTabB,
  47. });
  48. });
  49. it("falls back only for create and closes an edit whose owner disappeared", () => {
  50. expect(
  51. resolveCommandScopeRecovery({
  52. editing: false,
  53. scopeKey: "ssh://closed/root",
  54. workspaceTabs: [remoteTabA],
  55. }),
  56. ).toBe("fallback-user");
  57. expect(
  58. resolveCommandScopeRecovery({
  59. editing: true,
  60. scopeKey: "ssh://closed/root",
  61. workspaceTabs: [remoteTabA],
  62. }),
  63. ).toBe("close-editor");
  64. });
  65. it("refreshes the parent projection only for User or the current Workspace", () => {
  66. expect(shouldRefreshCurrentCommandList("user", "workspace-a")).toBe(true);
  67. expect(shouldRefreshCurrentCommandList("workspace-a", "workspace-a")).toBe(true);
  68. expect(shouldRefreshCurrentCommandList("workspace-b", "workspace-a")).toBe(false);
  69. });
  70. it("reuses the concrete User and Workspace scope menu", () => {
  71. const formSource = readSource("src/settings/CommandForm.tsx");
  72. expect(formSource).toContain("PluginScopeMenu");
  73. expect(formSource).toContain("selectedScopeKey");
  74. expect(formSource).toContain("workspaceTabs");
  75. expect(formSource).toContain("onScopeKeyChange");
  76. expect(formSource).toMatch(/<PluginScopeMenu\s+align="end"/);
  77. expect(formSource).not.toContain("function CommandScopeSelect");
  78. expect(formSource).not.toContain("<SelectItem");
  79. });
  80. it("resolves the selected Workspace Host instead of reusing the current service", () => {
  81. const sectionSource = readSource("src/settings/CommandsSection.tsx");
  82. expect(sectionSource).toContain("useWorkspaceServicesResolution");
  83. expect(sectionSource).toContain("getPluginWorkspaceKey");
  84. expect(sectionSource).toContain("formScopeKey");
  85. expect(sectionSource).toContain("formTargetWorkspace");
  86. expect(sectionSource).toContain("formServices.commandsService");
  87. expect(sectionSource).toContain("currentWorkspaceKey");
  88. expect(sectionSource).not.toContain(
  89. 'storageLevel === "project" ? (workspacePath ?? undefined) : undefined',
  90. );
  91. });
  92. it("keeps the current list projection isolated from another Workspace save", () => {
  93. const sectionSource = readSource("src/settings/CommandsSection.tsx");
  94. expect(sectionSource).toContain("shouldRefreshCurrentCommandList");
  95. expect(sectionSource).toContain("await refresh()");
  96. expect(sectionSource).toContain("writeCommandFile");
  97. expect(sectionSource).not.toContain("await createCommand(");
  98. });
  99. it("locks edit ownership and recovers when a selected Workspace closes", () => {
  100. const sectionSource = readSource("src/settings/CommandsSection.tsx");
  101. const formSource = readSource("src/settings/CommandForm.tsx");
  102. expect(sectionSource).toContain("setFormScopeKey");
  103. expect(sectionSource).toContain('setFormScopeKey("user")');
  104. expect(sectionSource).toContain("resolveCommandScopeRecovery");
  105. expect(formSource).toContain("disabled={Boolean(initial)}");
  106. });
  107. });