remotePluginSyncDialog.test.ts 28 KB

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