remotePluginSyncDialog.test.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. PluginSyncCandidate,
  4. PluginSyncRemoteStatus,
  5. ZCodePluginInfo,
  6. ZCodePluginsOverviewResult,
  7. } from "@zcode/shared";
  8. import {
  9. buildMarketplaceRemotePluginStatuses,
  10. buildMarketplaceRemotePluginSyncCandidates,
  11. buildRemotePluginSyncRows,
  12. buildRemotePluginSyncRowsForCandidates,
  13. filterRemotePluginSyncRows,
  14. resolveDefaultRemotePluginSyncSelection,
  15. shouldAllowRemotePluginSyncDialogOpenChange,
  16. shouldFinishRemotePluginSyncRun,
  17. syncSelectedRemotePlugins,
  18. } from "../src/settings/RemotePluginSyncDialog.js";
  19. type RemotePluginSyncParams = Parameters<typeof syncSelectedRemotePlugins>[0];
  20. type RemotePluginSyncRow = RemotePluginSyncParams["rows"][number];
  21. type RemoteInstallResult = Awaited<
  22. ReturnType<NonNullable<RemotePluginSyncParams["remoteZCodeAgentService"]>["installPlugin"]>
  23. >;
  24. function makeCandidate(
  25. overrides: Partial<PluginSyncCandidate>,
  26. ): PluginSyncCandidate {
  27. return {
  28. id: "remote-tools-id",
  29. name: "remote-tools",
  30. pluginId: "remote-tools@inline",
  31. directoryName: "remote-tools",
  32. path: "/Users/me/.zcode/plugins/remote-tools",
  33. sizeBytes: 128,
  34. enabled: true,
  35. componentTypes: ["skills"],
  36. ...overrides,
  37. };
  38. }
  39. function makeOverview(
  40. installed: ZCodePluginsOverviewResult["installedPlugins"],
  41. source: ZCodePluginsOverviewResult["marketplaces"][number]["source"] = {
  42. source: "github",
  43. repo: "example/team-plugins",
  44. ref: "main",
  45. },
  46. ): ZCodePluginsOverviewResult {
  47. return {
  48. marketplaces: [
  49. {
  50. id: "team-market",
  51. name: "team-market",
  52. source,
  53. pluginCount: 1,
  54. },
  55. ],
  56. availablePlugins: [],
  57. installedPlugins: installed,
  58. restorableBuiltins: [],
  59. diagnostics: [],
  60. capability: { supported: true },
  61. };
  62. }
  63. function deferred<T>() {
  64. let resolve!: (value: T) => void;
  65. let reject!: (reason?: unknown) => void;
  66. const promise = new Promise<T>((promiseResolve, promiseReject) => {
  67. resolve = promiseResolve;
  68. reject = promiseReject;
  69. });
  70. return { promise, reject, resolve };
  71. }
  72. function makeMarketplaceRow(name: string): RemotePluginSyncRow {
  73. return {
  74. candidate: {
  75. id: `marketplace:${name}@team-market`,
  76. kind: "marketplace",
  77. name,
  78. pluginId: `${name}@team-market`,
  79. marketplace: "team-market",
  80. enabled: true,
  81. componentTypes: ["command"],
  82. marketplacePlugin: {
  83. marketplaceSourceInput: "example/team-plugins",
  84. pluginName: name,
  85. },
  86. },
  87. exists: false,
  88. remoteMarketplaceExists: true,
  89. };
  90. }
  91. function makePluginInfo(
  92. overrides: Partial<ZCodePluginInfo>,
  93. ): ZCodePluginInfo {
  94. return {
  95. id: "formatter@team-market",
  96. name: "formatter",
  97. enabled: true,
  98. source: "cache",
  99. marketplace: "team-market",
  100. skillRootCount: 0,
  101. commandRootCount: 0,
  102. mcpServerNames: [],
  103. rootPath: "/cache/formatter",
  104. ...overrides,
  105. };
  106. }
  107. function delay(ms: number): Promise<"timeout"> {
  108. return new Promise((resolve) => {
  109. setTimeout(() => resolve("timeout"), ms);
  110. });
  111. }
  112. describe("RemotePluginSyncDialog helpers", () => {
  113. it("only finishes the currently active remote plugin sync run", () => {
  114. const currentRun = new AbortController();
  115. const staleRun = new AbortController();
  116. expect(shouldFinishRemotePluginSyncRun(currentRun, currentRun)).toBe(true);
  117. expect(shouldFinishRemotePluginSyncRun(currentRun, staleRun)).toBe(false);
  118. });
  119. it("joins local plugin candidates with remote status by plugin id and directory", () => {
  120. const rows = buildRemotePluginSyncRows(
  121. [
  122. makeCandidate({ id: "missing-id", pluginId: "missing@inline", directoryName: "missing" }),
  123. makeCandidate({ id: "existing-id", pluginId: "existing@inline", directoryName: "existing" }),
  124. ],
  125. [
  126. {
  127. pluginId: "existing@inline",
  128. directoryName: "existing",
  129. exists: true,
  130. reason: "samePluginId",
  131. },
  132. ] satisfies PluginSyncRemoteStatus[],
  133. );
  134. expect(rows).toMatchObject([
  135. { candidate: { id: "inline:missing-id" }, exists: false },
  136. { candidate: { id: "inline:existing-id" }, exists: true },
  137. ]);
  138. });
  139. it("selects only missing remote plugins by default", () => {
  140. const rows = buildRemotePluginSyncRows(
  141. [
  142. makeCandidate({ id: "missing-id", pluginId: "missing@inline", directoryName: "missing" }),
  143. makeCandidate({ id: "existing-id", pluginId: "existing@inline", directoryName: "existing" }),
  144. ],
  145. [
  146. {
  147. pluginId: "existing@inline",
  148. directoryName: "existing",
  149. exists: true,
  150. },
  151. ],
  152. );
  153. expect(Array.from(resolveDefaultRemotePluginSyncSelection(rows))).toEqual([
  154. "inline:missing-id",
  155. ]);
  156. });
  157. it("filters existing remote plugins unless requested", () => {
  158. const rows = buildRemotePluginSyncRows(
  159. [
  160. makeCandidate({ id: "missing-id", pluginId: "missing@inline", directoryName: "missing" }),
  161. makeCandidate({ id: "existing-id", pluginId: "existing@inline", directoryName: "existing" }),
  162. ],
  163. [
  164. {
  165. pluginId: "existing@inline",
  166. directoryName: "existing",
  167. exists: true,
  168. },
  169. ],
  170. );
  171. expect(filterRemotePluginSyncRows(rows, false).map((row) => row.candidate.id)).toEqual([
  172. "inline:missing-id",
  173. ]);
  174. expect(filterRemotePluginSyncRows(rows, true).map((row) => row.candidate.id)).toEqual([
  175. "inline:missing-id",
  176. "inline:existing-id",
  177. ]);
  178. });
  179. it("blocks dialog close requests while plugin sync is running", () => {
  180. expect(shouldAllowRemotePluginSyncDialogOpenChange("preflighting", false)).toBe(false);
  181. expect(shouldAllowRemotePluginSyncDialogOpenChange("preflighting", true)).toBe(true);
  182. expect(shouldAllowRemotePluginSyncDialogOpenChange("syncing", false)).toBe(false);
  183. expect(shouldAllowRemotePluginSyncDialogOpenChange("syncing", true)).toBe(true);
  184. expect(shouldAllowRemotePluginSyncDialogOpenChange("selection", false)).toBe(true);
  185. expect(shouldAllowRemotePluginSyncDialogOpenChange("complete", false)).toBe(true);
  186. });
  187. it("does not carry legacy install scope into marketplace candidates", () => {
  188. const candidates = buildMarketplaceRemotePluginSyncCandidates(
  189. makeOverview([
  190. {
  191. id: "formatter@team-market",
  192. name: "formatter",
  193. marketplace: "team-market",
  194. enabled: false,
  195. scope: "workspace",
  196. version: "2.0.0",
  197. installPath: "/Users/me/.zcode/cli/plugins/cache/team-market/formatter/2.0.0",
  198. componentTypes: ["command", "skill"],
  199. },
  200. ]),
  201. [],
  202. );
  203. expect(candidates).toMatchObject([
  204. {
  205. id: "marketplace:formatter@team-market",
  206. kind: "marketplace",
  207. name: "formatter",
  208. pluginId: "formatter@team-market",
  209. marketplace: "team-market",
  210. enabled: false,
  211. componentTypes: ["command", "skill"],
  212. marketplacePlugin: {
  213. marketplaceSourceInput: "example/team-plugins#main",
  214. pluginName: "formatter",
  215. },
  216. },
  217. ]);
  218. });
  219. it("keeps local configured plugin options on marketplace sync candidates", () => {
  220. const candidates = buildMarketplaceRemotePluginSyncCandidates(
  221. makeOverview([
  222. {
  223. id: "formatter@team-market",
  224. name: "formatter",
  225. marketplace: "team-market",
  226. enabled: true,
  227. scope: "user",
  228. },
  229. ]),
  230. [
  231. makePluginInfo({
  232. userConfig: {
  233. region: { type: "string" },
  234. token: { type: "string", sensitive: true },
  235. },
  236. configuredOptions: {
  237. region: "cn",
  238. token: "local-secret",
  239. },
  240. }),
  241. ],
  242. );
  243. expect(candidates[0]?.pluginOptions).toEqual({
  244. configuredOptions: {
  245. region: "cn",
  246. token: "local-secret",
  247. },
  248. userConfig: {
  249. region: { type: "string" },
  250. token: { type: "string", sensitive: true },
  251. },
  252. });
  253. });
  254. it("builds marketplace candidates with source archive metadata for local directory sources", () => {
  255. const candidates = buildMarketplaceRemotePluginSyncCandidates(
  256. makeOverview(
  257. [
  258. {
  259. id: "formatter@team-market",
  260. name: "formatter",
  261. marketplace: "team-market",
  262. enabled: true,
  263. scope: "user",
  264. },
  265. ],
  266. { source: "directory", path: "/Users/me/plugins/team-market" },
  267. ),
  268. [],
  269. );
  270. expect(candidates).toMatchObject([
  271. {
  272. id: "marketplace:formatter@team-market",
  273. marketplacePlugin: {
  274. pluginName: "formatter",
  275. marketplaceSourceArchive: {
  276. marketplaceId: "team-market",
  277. source: { source: "directory", path: "/Users/me/plugins/team-market" },
  278. },
  279. },
  280. },
  281. ]);
  282. expect(candidates[0]?.marketplacePlugin).not.toHaveProperty("marketplaceSourceInput");
  283. });
  284. it("selects missing marketplace plugins and filters existing ones", () => {
  285. const candidates = buildMarketplaceRemotePluginSyncCandidates(
  286. makeOverview([
  287. {
  288. id: "formatter@team-market",
  289. name: "formatter",
  290. marketplace: "team-market",
  291. enabled: true,
  292. scope: "user",
  293. },
  294. {
  295. id: "runner@team-market",
  296. name: "runner",
  297. marketplace: "team-market",
  298. enabled: true,
  299. scope: "workspace",
  300. },
  301. ]),
  302. [],
  303. );
  304. const statuses = buildMarketplaceRemotePluginStatuses(
  305. candidates,
  306. makeOverview([
  307. {
  308. id: "runner@team-market",
  309. name: "runner",
  310. marketplace: "team-market",
  311. enabled: true,
  312. scope: "workspace",
  313. installPath: "/remote/cache/runner",
  314. },
  315. ]),
  316. );
  317. const rows = buildRemotePluginSyncRowsForCandidates(candidates, statuses);
  318. expect(Array.from(resolveDefaultRemotePluginSyncSelection(rows))).toEqual([
  319. "marketplace:formatter@team-market",
  320. ]);
  321. expect(filterRemotePluginSyncRows(rows, false).map((row) => row.candidate.id)).toEqual([
  322. "marketplace:formatter@team-market",
  323. ]);
  324. });
  325. it("mirrors a local marketplace source before adding and installing on the remote", async () => {
  326. const calls: string[] = [];
  327. const archive = new Uint8Array([1, 2, 3]);
  328. const result = await syncSelectedRemotePlugins({
  329. localPluginSyncService: {
  330. exportMarketplaceSourceArchive: async (params) => {
  331. calls.push(`export:${params.marketplaceId}:${params.pluginNames.join(",")}`);
  332. expect(params.source).toEqual({
  333. source: "directory",
  334. path: "/Users/me/plugins/team-market",
  335. });
  336. return {
  337. archive,
  338. archiveBytes: archive.byteLength,
  339. marketplaceId: "team-market",
  340. pluginNames: params.pluginNames,
  341. };
  342. },
  343. } as never,
  344. remotePluginSyncService: {
  345. importMarketplaceSourceArchive: async (params) => {
  346. calls.push("import");
  347. expect(params.archive).toBe(archive);
  348. return {
  349. marketplaceId: "team-market",
  350. path: "/remote/.zcode/plugins/marketplace-sources/team-market-hash",
  351. status: "synced",
  352. };
  353. },
  354. } as never,
  355. remoteWorkspacePath: "/remote/workspace",
  356. remoteZCodeAgentService: {
  357. addPluginMarketplace: async (params) => {
  358. calls.push(`add:${params.source}`);
  359. return { diagnostics: [] };
  360. },
  361. installPlugin: async (params) => {
  362. calls.push(`install:${params.pluginName}@${params.marketplace}`);
  363. return {
  364. dependencyClosure: [],
  365. diagnostics: [],
  366. installedPlugins: [
  367. {
  368. id: "formatter@team-market",
  369. name: "formatter",
  370. marketplace: "team-market",
  371. enabled: true,
  372. scope: "user",
  373. installPath: "/remote/cache/formatter",
  374. },
  375. ],
  376. };
  377. },
  378. setPluginEnabled: async (params) => {
  379. calls.push(`enabled:${params.pluginId}:${params.enabled}`);
  380. return {
  381. enabled: params.enabled,
  382. plugin: {
  383. id: params.pluginId,
  384. name: "formatter",
  385. enabled: params.enabled,
  386. source: "cache",
  387. marketplace: "team-market",
  388. skillRootCount: 0,
  389. commandRootCount: 0,
  390. mcpServerNames: [],
  391. rootPath: "/remote/cache/formatter",
  392. },
  393. };
  394. },
  395. } as never,
  396. rows: [
  397. {
  398. candidate: {
  399. id: "marketplace:formatter@team-market",
  400. kind: "marketplace",
  401. name: "formatter",
  402. pluginId: "formatter@team-market",
  403. marketplace: "team-market",
  404. enabled: true,
  405. componentTypes: ["skill"],
  406. marketplacePlugin: {
  407. marketplaceSourceArchive: {
  408. marketplaceId: "team-market",
  409. source: { source: "directory", path: "/Users/me/plugins/team-market" },
  410. },
  411. pluginName: "formatter",
  412. },
  413. },
  414. exists: false,
  415. remoteMarketplaceExists: false,
  416. },
  417. ],
  418. });
  419. expect(result.results).toMatchObject([
  420. {
  421. pluginId: "formatter@team-market",
  422. status: "synced",
  423. },
  424. ]);
  425. expect(calls).toEqual([
  426. "export:team-market:formatter",
  427. "import",
  428. "add:/remote/.zcode/plugins/marketplace-sources/team-market-hash",
  429. "install:formatter@team-market",
  430. "enabled:formatter@team-market:true",
  431. ]);
  432. });
  433. it("syncs only portable configured plugin options and preserves remote options", async () => {
  434. const configureCalls: Array<Record<string, unknown>> = [];
  435. const progress: string[] = [];
  436. const result = await syncSelectedRemotePlugins({
  437. localPluginSyncService: {} as never,
  438. remotePluginSyncService: {} as never,
  439. remoteWorkspacePath: "/remote/workspace",
  440. remoteZCodeAgentService: {
  441. installPlugin: async () => ({
  442. dependencyClosure: [],
  443. diagnostics: [],
  444. installedPlugins: [
  445. {
  446. id: "formatter@team-market",
  447. name: "formatter",
  448. marketplace: "team-market",
  449. enabled: true,
  450. scope: "user",
  451. installPath: "/remote/cache/formatter",
  452. },
  453. ],
  454. }),
  455. setPluginEnabled: async (params) => ({
  456. enabled: params.enabled,
  457. plugin: makePluginInfo({
  458. id: params.pluginId,
  459. rootPath: "/remote/cache/formatter",
  460. }),
  461. }),
  462. listPlugins: async () => ({
  463. diagnostics: [],
  464. plugins: [
  465. makePluginInfo({
  466. id: "formatter@team-market",
  467. rootPath: "/remote/cache/formatter",
  468. userConfig: {
  469. region: { type: "string" },
  470. retries: { type: "number" },
  471. token: { type: "string", sensitive: true },
  472. workspaceDir: { type: "directory" },
  473. },
  474. configuredOptions: {
  475. remoteOnly: true,
  476. },
  477. }),
  478. ],
  479. }),
  480. configurePlugin: async (params) => {
  481. configureCalls.push(params.options);
  482. return { diagnostics: [], pluginId: params.pluginId };
  483. },
  484. } as never,
  485. rows: [
  486. {
  487. candidate: {
  488. id: "marketplace:formatter@team-market",
  489. kind: "marketplace",
  490. name: "formatter",
  491. pluginId: "formatter@team-market",
  492. marketplace: "team-market",
  493. enabled: true,
  494. componentTypes: ["command"],
  495. pluginOptions: {
  496. userConfig: {
  497. region: { type: "string" },
  498. retries: { type: "number" },
  499. token: { type: "string", sensitive: true },
  500. workspaceDir: { type: "directory" },
  501. stale: { type: "string" },
  502. },
  503. configuredOptions: {
  504. region: "cn",
  505. retries: 2,
  506. token: "local-secret",
  507. workspaceDir: "/Users/me/project",
  508. stale: "old",
  509. },
  510. },
  511. marketplacePlugin: {
  512. marketplaceSourceInput: "example/team-plugins",
  513. pluginName: "formatter",
  514. },
  515. },
  516. exists: false,
  517. remoteMarketplaceExists: true,
  518. },
  519. ],
  520. onItemProgress: (event) => {
  521. progress.push(event.log);
  522. },
  523. });
  524. expect(result.results).toMatchObject([
  525. {
  526. pluginId: "formatter@team-market",
  527. status: "synced",
  528. },
  529. ]);
  530. expect(configureCalls).toEqual([
  531. {
  532. remoteOnly: true,
  533. region: "cn",
  534. retries: 2,
  535. },
  536. ]);
  537. expect(progress.join("\n")).toContain("RPC plugins/configure 2 portable option(s)");
  538. expect(progress.join("\n")).toContain("skipped 3 non-portable plugin option(s)");
  539. });
  540. it("uses User config scope for all marketplace sync operations", async () => {
  541. const calls: Array<{ operation: string; scope: string | undefined }> = [];
  542. const result = await syncSelectedRemotePlugins({
  543. localPluginSyncService: {} as never,
  544. remotePluginSyncService: {} as never,
  545. remoteWorkspacePath: "/remote/workspace",
  546. remoteZCodeAgentService: {
  547. installPlugin: async (params) => {
  548. calls.push({ operation: "install", scope: params.scope });
  549. return {
  550. dependencyClosure: [],
  551. diagnostics: [],
  552. installedPlugins: [
  553. {
  554. id: "formatter@team-market",
  555. name: "formatter",
  556. marketplace: "team-market",
  557. enabled: true,
  558. scope: "user",
  559. installPath: "/remote/cache/formatter",
  560. },
  561. ],
  562. };
  563. },
  564. setPluginEnabled: async (params) => {
  565. calls.push({ operation: "setEnabled", scope: params.scope });
  566. return {
  567. enabled: params.enabled,
  568. plugin: makePluginInfo({ id: params.pluginId, rootPath: "/remote/cache/formatter" }),
  569. };
  570. },
  571. listPlugins: async () => ({
  572. diagnostics: [],
  573. plugins: [
  574. makePluginInfo({
  575. configuredOptions: {},
  576. id: "formatter@team-market",
  577. userConfig: { region: { type: "string" } },
  578. }),
  579. ],
  580. }),
  581. configurePlugin: async (params) => {
  582. calls.push({ operation: "configure", scope: params.scope });
  583. return { diagnostics: [], pluginId: params.pluginId };
  584. },
  585. } as never,
  586. rows: [
  587. {
  588. candidate: {
  589. id: "marketplace:formatter@team-market",
  590. kind: "marketplace",
  591. name: "formatter",
  592. pluginId: "formatter@team-market",
  593. marketplace: "team-market",
  594. enabled: true,
  595. componentTypes: ["skill"],
  596. pluginOptions: {
  597. userConfig: { region: { type: "string" } },
  598. configuredOptions: { region: "cn" },
  599. },
  600. marketplacePlugin: {
  601. marketplaceSourceInput: "example/team-plugins",
  602. pluginName: "formatter",
  603. },
  604. },
  605. exists: false,
  606. remoteMarketplaceExists: true,
  607. },
  608. ],
  609. });
  610. expect(result.results).toMatchObject([
  611. { pluginId: "formatter@team-market", status: "synced" },
  612. ]);
  613. expect(calls).toEqual([
  614. { operation: "install", scope: "user" },
  615. { operation: "setEnabled", scope: "user" },
  616. { operation: "configure", scope: "user" },
  617. ]);
  618. });
  619. it("stops waiting for one marketplace plugin and continues syncing the next plugin", async () => {
  620. const slowInstall = deferred<RemoteInstallResult>();
  621. const slowStarted = deferred<void>();
  622. const fastStarted = deferred<void>();
  623. const stoppedIds = new Set<string>();
  624. const stopWaiters = new Map<string, () => void>();
  625. const progress: Array<{ candidateId: string; log: string; status: string }> = [];
  626. const cancelCalls: string[] = [];
  627. const installOperationIds: string[] = [];
  628. const stopPlugin = (candidateId: string) => {
  629. stoppedIds.add(candidateId);
  630. stopWaiters.get(candidateId)?.();
  631. };
  632. const resultPromise = syncSelectedRemotePlugins({
  633. localPluginSyncService: {} as never,
  634. remotePluginSyncService: {} as never,
  635. remoteWorkspacePath: "/remote/workspace",
  636. remoteZCodeAgentService: {
  637. installPlugin: async (params) => {
  638. installOperationIds.push(params.operationId ?? "");
  639. if (params.pluginName === "slow") {
  640. slowStarted.resolve();
  641. return await slowInstall.promise;
  642. }
  643. fastStarted.resolve();
  644. return {
  645. dependencyClosure: [],
  646. diagnostics: [],
  647. installedPlugins: [
  648. {
  649. id: "fast@team-market",
  650. name: "fast",
  651. marketplace: "team-market",
  652. enabled: true,
  653. scope: "user",
  654. installPath: "/remote/cache/fast",
  655. },
  656. ],
  657. };
  658. },
  659. setPluginEnabled: async (params) => ({
  660. enabled: params.enabled,
  661. plugin: {
  662. id: params.pluginId,
  663. name: params.pluginId.split("@")[0] ?? params.pluginId,
  664. enabled: params.enabled,
  665. source: "cache",
  666. marketplace: "team-market",
  667. skillRootCount: 0,
  668. commandRootCount: 0,
  669. mcpServerNames: [],
  670. rootPath: `/remote/cache/${params.pluginId}`,
  671. },
  672. }),
  673. cancelPluginOperation: async (params) => {
  674. cancelCalls.push(params.operationId);
  675. return { cancelled: true, operationId: params.operationId };
  676. },
  677. } as never,
  678. rows: [makeMarketplaceRow("slow"), makeMarketplaceRow("fast")],
  679. onItemProgress: (event) => {
  680. progress.push({
  681. candidateId: event.candidateId,
  682. log: event.log,
  683. status: event.status,
  684. });
  685. },
  686. stopControl: {
  687. isStopped: (candidateId) => stoppedIds.has(candidateId),
  688. waitForStop: (candidateId) =>
  689. new Promise<void>((resolve) => {
  690. stopWaiters.set(candidateId, resolve);
  691. }),
  692. cancelOperation: async (operationId) => {
  693. await (
  694. {
  695. cancelPluginOperation: async (params: { operationId: string }) => {
  696. cancelCalls.push(params.operationId);
  697. return { cancelled: true, operationId: params.operationId };
  698. },
  699. }
  700. ).cancelPluginOperation({ operationId });
  701. },
  702. },
  703. });
  704. await slowStarted.promise;
  705. stopPlugin("marketplace:slow@team-market");
  706. await expect(
  707. Promise.race([fastStarted.promise.then(() => "fast-started" as const), delay(25)]),
  708. ).resolves.toBe("fast-started");
  709. expect(installOperationIds[0]).toMatch(/^remote-plugin-sync-/);
  710. expect(cancelCalls).toEqual([installOperationIds[0]]);
  711. slowInstall.resolve({
  712. dependencyClosure: [],
  713. diagnostics: [],
  714. installedPlugins: [
  715. {
  716. id: "slow@team-market",
  717. name: "slow",
  718. marketplace: "team-market",
  719. enabled: true,
  720. scope: "user",
  721. installPath: "/remote/cache/slow",
  722. },
  723. ],
  724. });
  725. await expect(resultPromise).resolves.toMatchObject({
  726. results: [
  727. {
  728. pluginId: "slow@team-market",
  729. status: "stopped",
  730. },
  731. {
  732. pluginId: "fast@team-market",
  733. status: "synced",
  734. },
  735. ],
  736. });
  737. expect(progress).toEqual(
  738. expect.arrayContaining([
  739. expect.objectContaining({
  740. candidateId: "marketplace:slow@team-market",
  741. status: "syncing",
  742. log: expect.stringContaining("install"),
  743. }),
  744. expect.objectContaining({
  745. candidateId: "marketplace:slow@team-market",
  746. status: "stopped",
  747. log: expect.stringContaining("stop"),
  748. }),
  749. expect.objectContaining({
  750. candidateId: "marketplace:fast@team-market",
  751. status: "synced",
  752. }),
  753. ]),
  754. );
  755. });
  756. it("aborts an active remote plugin operation when its target becomes invalid", async () => {
  757. const controller = new AbortController();
  758. const slowInstall = deferred<RemoteInstallResult>();
  759. const slowStarted = deferred<void>();
  760. const cancelCalls: string[] = [];
  761. const installOperationIds: string[] = [];
  762. const resultPromise = syncSelectedRemotePlugins({
  763. localPluginSyncService: {} as never,
  764. remotePluginSyncService: {} as never,
  765. remoteWorkspacePath: "/remote/workspace",
  766. remoteZCodeAgentService: {
  767. installPlugin: async (params) => {
  768. installOperationIds.push(params.operationId ?? "");
  769. slowStarted.resolve();
  770. return await slowInstall.promise;
  771. },
  772. } as never,
  773. rows: [makeMarketplaceRow("slow")],
  774. signal: controller.signal,
  775. stopControl: {
  776. isStopped: () => false,
  777. cancelOperation: async (operationId) => {
  778. cancelCalls.push(operationId);
  779. },
  780. },
  781. });
  782. await slowStarted.promise;
  783. controller.abort();
  784. const outcome = await Promise.race([resultPromise, delay(25)]);
  785. expect(outcome).not.toBe("timeout");
  786. expect(outcome).toMatchObject({
  787. results: [
  788. {
  789. pluginId: "slow@team-market",
  790. status: "stopped",
  791. },
  792. ],
  793. });
  794. expect(cancelCalls).toEqual([installOperationIds[0]]);
  795. slowInstall.resolve({
  796. dependencyClosure: [],
  797. diagnostics: [],
  798. installedPlugins: [],
  799. });
  800. });
  801. it("does not start a remote install when a plugin was already stopped", async () => {
  802. const installCalls: string[] = [];
  803. const result = await syncSelectedRemotePlugins({
  804. localPluginSyncService: {} as never,
  805. remotePluginSyncService: {} as never,
  806. remoteWorkspacePath: "/remote/workspace",
  807. remoteZCodeAgentService: {
  808. installPlugin: async (params) => {
  809. installCalls.push(params.pluginName);
  810. return {
  811. dependencyClosure: [],
  812. diagnostics: [],
  813. installedPlugins: [],
  814. };
  815. },
  816. } as never,
  817. rows: [makeMarketplaceRow("stopped")],
  818. stopControl: {
  819. isStopped: (candidateId) => candidateId === "marketplace:stopped@team-market",
  820. },
  821. });
  822. expect(installCalls).toEqual([]);
  823. expect(result.results).toMatchObject([
  824. {
  825. pluginId: "stopped@team-market",
  826. status: "stopped",
  827. },
  828. ]);
  829. });
  830. });