plugin-manager.spec.ts 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /**
  2. * The plugin manager over a real profile: a temporary harness home with one
  3. * profile, packages staged the way pnpm leaves them, the host tree booted
  4. * through `boot()` with the profile runtime the launcher provides, and a
  5. * fake pnpm that edits the profile the way the real one does. The manager
  6. * is built the way the Web host's adapter builds it, reading the runtime,
  7. * the agent registry off the context per call.
  8. */
  9. import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  10. import { mkdtemp } from 'node:fs/promises'
  11. import { tmpdir } from 'node:os'
  12. import { join } from 'node:path'
  13. import { PassThrough } from 'node:stream'
  14. import { execFileSync } from 'node:child_process'
  15. import { afterEach, describe, expect, it, vi } from 'vitest'
  16. import { Context, type Plugin } from '@deepseek-ai/cordis'
  17. import Loader from '@deepseek-ai/cordis-plugin-loader'
  18. import {
  19. boot, composeProfileStack, loadOptionalPatches, loadProfile, ProfileRuntime, rootIncludeEntry,
  20. type ComposedStack, type Profile, readPackageMetadata,
  21. } from '@deepseek-ai/dsh-app-boot'
  22. import {
  23. PluginManager, PluginInstaller, PluginOperationError, pluginOperationFailureOf,
  24. type PluginInstallLogChunk, type PluginInstallRequestId, type PluginInstallProgress,
  25. type PluginToolingConfig, type SpawnLike,
  26. } from '@deepseek-ai/dsh-plugin-manager'
  27. import type {} from '@deepseek-ai/dsh-agent'
  28. const NAME = 'dsh-test'
  29. /** A complete tooling config: the host's schema fills these defaults at load, the type does not. */
  30. function managerConfig(overrides: Partial<PluginToolingConfig> = {}): PluginToolingConfig {
  31. return { pnpmCommand: 'pnpm', installTimeoutMs: 1_000, installKillGraceMs: 50, installLogTailBytes: 16_384, ...overrides }
  32. }
  33. /** Test seams: the child spawner and the static metadata reader. */
  34. interface Internals {
  35. spawn?: SpawnLike
  36. metadata?: typeof readPackageMetadata
  37. }
  38. const contexts: Context[] = []
  39. afterEach(async () => {
  40. await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
  41. })
  42. /** Builtins the staged bundles name; registered on every boot. */
  43. const good: Plugin.Function = () => {}
  44. const throws: Plugin.Function = () => { throw new Error('boom at apply') }
  45. let flakyCalls = 0
  46. const flaky: Plugin.Function = () => {
  47. flakyCalls += 1
  48. if (flakyCalls === 1) throw new Error('flaky first start')
  49. }
  50. const provider: Plugin.Function = (ctx) => { ctx.effect(() => ctx.reflect.provide('fixtureSvc', { ready: true })) }
  51. const lonelyProvider: Plugin.Function = (ctx) => { ctx.effect(() => ctx.reflect.provide('lonelySvc', { ready: true })) }
  52. const consumer: Plugin.Object = { inject: ['fixtureSvc'], apply() {} }
  53. const prepare = (ctx: Context): void => {
  54. ctx.loader.builtins.configured = {
  55. Config: { '~standard': { version: 1, vendor: 'manager-test', validate(input: unknown) {
  56. const config = input as { value: number }
  57. return config.value < 0 ? { issues: [{ message: 'negative config' }] } : { value: config }
  58. } } },
  59. apply() {},
  60. }
  61. ctx.loader.builtins.good = good
  62. ctx.loader.builtins.throws = throws
  63. ctx.loader.builtins.flaky = flaky
  64. ctx.loader.builtins.provider = provider
  65. ctx.loader.builtins['lonely-provider'] = lonelyProvider
  66. ctx.loader.builtins.consumer = consumer
  67. }
  68. interface StagedHome {
  69. home: string
  70. profileDir: string
  71. anchor: string
  72. }
  73. /** A harness home with one empty live profile and an install anchor that carries nothing. */
  74. async function stageHome(patchReload: 'live' | 'startup' = 'live'): Promise<StagedHome> {
  75. const home = await mkdtemp(join(tmpdir(), 'dsh-plugin-manager-'))
  76. const profileDir = join(home, 'profiles', 'web')
  77. mkdirSync(profileDir, { recursive: true })
  78. writeFileSync(join(profileDir, 'package.json'), JSON.stringify({
  79. name: 'dsh-profile-web', private: true, dependencies: {}, dsh: { profile: { bundles: [], patchReload } },
  80. }, null, 2))
  81. writeFileSync(join(profileDir, 'cordis.yml'), '[]\n')
  82. const anchorDir = join(home, 'anchor')
  83. mkdirSync(anchorDir, { recursive: true })
  84. const anchor = join(anchorDir, 'package.json')
  85. writeFileSync(anchor, JSON.stringify({ name: 'dsh-anchor', version: '0.0.0', dependencies: {} }))
  86. return { home, profileDir, anchor }
  87. }
  88. interface StagedPackage {
  89. /** The bundle patch text; omitted stages a bundle-less package. */
  90. patch?: string
  91. /** `index.js` text, exported as the package main. */
  92. main?: string
  93. version?: string
  94. files?: Record<string, string>
  95. }
  96. /** Stage one package under the profile's node_modules, the way pnpm leaves it. */
  97. function stagePackage(profileDir: string, name: string, staged: StagedPackage): void {
  98. const dir = join(profileDir, 'node_modules', name)
  99. mkdirSync(dir, { recursive: true })
  100. writeFileSync(join(dir, 'package.json'), JSON.stringify({
  101. name,
  102. version: staged.version ?? '1.0.0',
  103. description: `staged ${name}`,
  104. type: 'module',
  105. ...staged.main === undefined ? {} : { main: 'index.js' },
  106. dsh: {
  107. title: `Title of ${name}`,
  108. ...staged.patch === undefined ? {} : { bundle: { patch: './cordis.patch.yml' } },
  109. },
  110. }, null, 2))
  111. if (staged.patch !== undefined) writeFileSync(join(dir, 'cordis.patch.yml'), staged.patch)
  112. if (staged.main !== undefined) writeFileSync(join(dir, 'index.js'), staged.main)
  113. for (const [file, text] of Object.entries(staged.files ?? {})) {
  114. mkdirSync(join(dir, file, '..'), { recursive: true })
  115. writeFileSync(join(dir, file), text)
  116. }
  117. }
  118. /** Add a dependency to the profile manifest, as `pnpm add` does. */
  119. function addDependency(profileDir: string, name: string, spec = '1.0.0'): void {
  120. const path = join(profileDir, 'package.json')
  121. const manifest = JSON.parse(readFileSync(path, 'utf8')) as { dependencies: Record<string, string> }
  122. manifest.dependencies[name] = spec
  123. writeFileSync(path, JSON.stringify(manifest, null, 2))
  124. }
  125. function manifestOf(profileDir: string): { dependencies: Record<string, string>; dsh: { profile: { bundles: string[] } } } {
  126. return JSON.parse(readFileSync(join(profileDir, 'package.json'), 'utf8')) as ReturnType<typeof manifestOf>
  127. }
  128. /** What the fake pnpm does for one invocation. */
  129. type PnpmBehavior = (args: readonly string[]) => { code: number | null; stdout?: string; stderr?: string; hang?: boolean; error?: unknown }
  130. /** A fake `spawn` that runs `behavior` on the next tick and reports through a child-like emitter. */
  131. function fakePnpm(profileDir: string, behavior: PnpmBehavior, calls: string[][] = [], gate = Promise.resolve()): SpawnLike {
  132. return (spec) => {
  133. const [, ...args] = spec.argv
  134. calls.push([...spec.argv])
  135. expect(spec.cwd).toBe(profileDir)
  136. const stdout = new PassThrough()
  137. const stderr = new PassThrough()
  138. const exit = Promise.withResolvers<{ exitCode: number | null; signal: NodeJS.Signals | null }>()
  139. const finish = (exitCode: number | null, signal: NodeJS.Signals | null = null): void => {
  140. stdout.end(); stderr.end(); exit.resolve({ exitCode, signal })
  141. }
  142. const terminate = (): void => { finish(null, 'SIGTERM') }
  143. spec.signal?.addEventListener('abort', terminate, { once: true })
  144. const clean = (): void => { spec.signal?.removeEventListener('abort', terminate) }
  145. void exit.promise.then(clean, clean)
  146. void gate.then(() => {
  147. if (spec.signal?.aborted) { terminate(); return }
  148. const outcome = behavior(args)
  149. if (outcome.error !== undefined) {
  150. stdout.end(); stderr.end(); exit.reject(outcome.error)
  151. return
  152. }
  153. if (outcome.stdout !== undefined) stdout.write(outcome.stdout)
  154. if (outcome.stderr !== undefined) stderr.write(outcome.stderr)
  155. if (!outcome.hang) finish(outcome.code)
  156. })
  157. return {
  158. stdin: undefined, stdout, stderr, control: undefined, collected: {}, done: exit.promise, terminate,
  159. waitForExit: () => exit.promise.then(() => true, () => true),
  160. }
  161. }
  162. }
  163. /** The pnpm every install test shares: `add <name>` stages nothing (the test did) and records the dependency. */
  164. function recordingPnpm(profileDir: string, calls: string[][] = []): SpawnLike {
  165. return fakePnpm(profileDir, (args) => {
  166. const [verb, target] = args
  167. if (verb === 'add' && target !== undefined) {
  168. addDependency(profileDir, target)
  169. return { code: 0, stdout: `+ ${target} 1.0.0\n` }
  170. }
  171. if (verb === 'remove' && target !== undefined) {
  172. const path = join(profileDir, 'package.json')
  173. const manifest = JSON.parse(readFileSync(path, 'utf8')) as { dependencies: Record<string, string> }
  174. const { [target]: _removed, ...remaining } = manifest.dependencies
  175. writeFileSync(path, JSON.stringify({ ...manifest, dependencies: remaining }, null, 2))
  176. rmSync(join(profileDir, 'node_modules', target), { recursive: true, force: true })
  177. return { code: 0, stdout: `- ${target}\n` }
  178. }
  179. return { code: 1, stderr: 'unexpected pnpm invocation\n' }
  180. }, calls)
  181. }
  182. interface Booted {
  183. ctx: Context
  184. manager: PluginManager
  185. runtime: ProfileRuntime
  186. changes: { reason: string; packageName?: string }[]
  187. log: PluginInstallLogChunk[]
  188. }
  189. /** Boot the profile the way the launcher does, then build the manager over it. */
  190. async function bootProfile(staged: StagedHome, internals: Internals = {}, config: Partial<PluginToolingConfig> = {}): Promise<Booted> {
  191. const load = (): Profile => loadProfile(NAME, 'web', staged.anchor, staged.home)
  192. const composeFor = (profile: Profile): ComposedStack => {
  193. const stack = composeProfileStack(profile.layers, [
  194. { label: profile.patchPath, patches: loadOptionalPatches(NAME, profile.patchPath) ?? [] },
  195. ])
  196. return { ...stack, patches: structuredClone(stack.patches) }
  197. }
  198. const profile = load()
  199. const ctx = await boot(NAME, join(staged.profileDir, 'cordis.yml'), composeFor(profile).patches, async (ctx) => {
  200. prepare(ctx)
  201. await ctx.plugin(ProfileRuntime, {
  202. profile, stack: composeFor(profile), installAnchor: staged.anchor,
  203. loadProfile: load, compose: composeFor, rootEntry: () => rootIncludeEntry(ctx),
  204. })
  205. })
  206. contexts.push(ctx)
  207. const changes: Booted['changes'] = []
  208. const log: PluginInstallLogChunk[] = []
  209. ctx.on('plugins/changed', (change) => { changes.push(change) })
  210. ctx.on('plugins/install-log', (chunk) => { log.push(chunk) })
  211. const host = ctx.plugin({ inject: ['loader'], apply() {} })
  212. await host.await()
  213. const manager = managerOver(host.ctx, internals, config)
  214. return { ctx, manager, runtime: ctx.profileRuntime, changes, log }
  215. }
  216. /** The manager as the Web host's adapter builds it: runtime and agent count read off the context per call. */
  217. function managerOver(ctx: Context, internals: Internals = {}, config: Partial<PluginToolingConfig> = {}): PluginManager {
  218. return new PluginManager(ctx, {
  219. config: managerConfig(config),
  220. runtime: () => ctx.get('profileRuntime'),
  221. runningAgents: () => (ctx.get('agents')?.list() ?? []).filter(agent => agent.status === 'running').length,
  222. ...internals,
  223. })
  224. }
  225. const entryIds = (ctx: Context): string[] => [...ctx.loader.entries()].map(entry => entry.id)
  226. const BUNDLE_ONE_ROW = '- insert:\n - id: hello\n name: cordis:good\n'
  227. describe('PluginManager', () => {
  228. it('keeps an installed package with malformed metadata manageable', async () => {
  229. const staged = await stageHome()
  230. stagePackage(staged.profileDir, 'broken', {})
  231. addDependency(staged.profileDir, 'broken')
  232. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  233. writeFileSync(join(staged.profileDir, 'node_modules/broken/package.json'), '{ invalid json')
  234. const view = (await manager.list())[0]!
  235. expect(view.name).toBe('broken')
  236. expect(view.installed).toBe(true)
  237. expect(view.reason).toBeTruthy()
  238. await manager.uninstall('broken')
  239. expect(await manager.list()).toEqual([])
  240. })
  241. it('shows failed group imports while omitting healthy structural groups', async () => {
  242. const staged = await stageHome()
  243. stagePackage(staged.profileDir, 'groups', { patch: `- insert:
  244. - id: healthy
  245. name: cordis:group
  246. group: true
  247. config: []
  248. - id: broken-group
  249. name: ./missing.mjs
  250. group: true
  251. config: []
  252. ` })
  253. addDependency(staged.profileDir, 'groups')
  254. const { manager } = await bootProfile(staged)
  255. await manager.enable('groups')
  256. const view = (await manager.list())[0]!
  257. expect(view.status).toBe('failed')
  258. expect(view.rows.map(row => row.rowId)).toEqual(['broken-group'])
  259. expect(view.rows[0]?.phase).toBe('failed')
  260. expect(view.rows[0]?.failure?.stage).toBe('import')
  261. })
  262. it('shows a failed config update beside the still-active old instance and attributes overrides to their owner', async () => {
  263. const staged = await stageHome()
  264. stagePackage(staged.profileDir, 'base', {
  265. patch: '- insert:\n - id: core-config\n name: cordis:configured\n config: { value: 1 }\n',
  266. })
  267. const manifest = manifestOf(staged.profileDir)
  268. manifest.dsh.profile.bundles.push('base')
  269. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest))
  270. stagePackage(staged.profileDir, 'ext', { patch: '- insert:\n - id: ext-ok\n name: cordis:good\n- id: core-config\n config: { value: -1 }\n' })
  271. addDependency(staged.profileDir, 'ext')
  272. const { ctx, manager } = await bootProfile(staged)
  273. const result = await manager.enable('ext')
  274. expect(result.issues?.[0]?.entryId).toBe('include:core-config')
  275. const core = ctx.loader.resolve('include:core-config')
  276. expect(core.fiber?.state).toBe(2)
  277. expect(core.fiber?.config).toEqual({ value: 1 })
  278. expect(core.options.config).toEqual({ value: -1 })
  279. const views = await manager.list()
  280. const ext = views.find(view => view.name === 'ext')!
  281. expect(ext.status).toBe('partial')
  282. expect(ext.issues?.[0]?.stage).toBe('update')
  283. expect(ext.rows.map(row => row.rowId)).toEqual(['ext-ok'])
  284. expect(ctx.profileRuntime.originOfEntry(core)?.packageName).toBe('base')
  285. expect(views.find(view => view.name === 'base')?.rows[0]).toMatchObject({ phase: 'active', failure: { stage: 'update' } })
  286. writeFileSync(join(staged.profileDir, 'node_modules/ext/cordis.patch.yml'), '- id: core-config\n config: { value: -1 }\n')
  287. await ctx.profileRuntime.recompose({ reloadBundles: true })
  288. expect((await manager.list()).find(view => view.name === 'ext')?.status).toBe('failed')
  289. await manager.disable('ext')
  290. expect(core.fiber?.config).toEqual({ value: 1 })
  291. expect((await manager.list()).find(view => view.name === 'base')?.status).toBe('running')
  292. })
  293. it('keeps a rejected preparation out of the layer list without promising rollback of live plugin effects', async () => {
  294. const staged = await stageHome()
  295. stagePackage(staged.profileDir, 'base', { patch: BUNDLE_ONE_ROW })
  296. const manifest = manifestOf(staged.profileDir)
  297. manifest.dsh.profile.bundles.push('base')
  298. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest))
  299. stagePackage(staged.profileDir, 'clash', { patch: BUNDLE_ONE_ROW })
  300. addDependency(staged.profileDir, 'clash')
  301. const { ctx, manager } = await bootProfile(staged)
  302. const changedManifest = manifestOf(staged.profileDir)
  303. changedManifest.dsh.profile.bundles.push('missing-base')
  304. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(changedManifest))
  305. await expect(manager.enable('clash')).rejects.toMatchObject({ code: 'plugins/enable-failed' })
  306. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['base', 'missing-base'])
  307. expect(ctx.loader.resolve('include:hello').fiber?.state).toBe(2)
  308. })
  309. it('installs and lists a non-bundle package without executing its main module', async () => {
  310. const staged = await stageHome()
  311. const marker = join(staged.profileDir, 'executed')
  312. stagePackage(staged.profileDir, 'plain', {
  313. main: `import { writeFileSync } from 'node:fs'; writeFileSync(${JSON.stringify(marker)}, 'bad'); export function apply() {}`,
  314. })
  315. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  316. await manager.add('plain')
  317. expect((await manager.list()).find(view => view.name === 'plain')?.kind).toBe('unknown')
  318. expect(existsSync(marker)).toBe(false)
  319. })
  320. it('reports plugins/unavailable without a profile runtime', async () => {
  321. const ctx = new Context()
  322. contexts.push(ctx)
  323. await ctx.plugin(Loader)
  324. await expect(managerOver(ctx).list()).rejects.toMatchObject({ code: 'plugins/unavailable', details: { reason: 'no profile runtime' } })
  325. })
  326. describe('list', () => {
  327. it('folds installed, enabled, declared and running facts into one view per package', async () => {
  328. const staged = await stageHome()
  329. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW, files: { 'extra.js': 'export const name = "extra"\nexport function apply() {}\n' } })
  330. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  331. stagePackage(staged.profileDir, 'ext-plugin', { main: 'export const name = "p"\nexport function apply() {}\n' })
  332. addDependency(staged.profileDir, 'ext-bundle')
  333. addDependency(staged.profileDir, 'ext-lib')
  334. addDependency(staged.profileDir, 'ext-plugin')
  335. const { manager } = await bootProfile(staged)
  336. const views = await manager.list()
  337. expect(views.map(view => [view.name, view.kind, view.status, view.installed, view.enabled])).toEqual([
  338. ['ext-bundle', 'bundle', 'disabled', true, false],
  339. ['ext-lib', 'unknown', 'plain', true, false],
  340. ['ext-plugin', 'unknown', 'plain', true, false],
  341. ])
  342. const bundle = views[0]
  343. expect(bundle).toMatchObject({ version: '1.0.0', title: 'Title of ext-bundle', description: 'staged ext-bundle', liveReload: true })
  344. // Rows come from static declarations while the bundle is not composed, under the ids the patch declares.
  345. expect(bundle?.rows).toEqual([{ entryId: 'hello', rowId: 'hello', moduleName: 'cordis:good', enabled: true, phase: null }])
  346. })
  347. it('reads a composed bundle\'s rows from the live tree with their failures', async () => {
  348. const staged = await stageHome()
  349. stagePackage(staged.profileDir, 'ext-mixed', { patch: '- insert:\n - id: ok\n name: cordis:good\n - id: bad\n name: cordis:throws\n' })
  350. addDependency(staged.profileDir, 'ext-mixed')
  351. const manifest = manifestOf(staged.profileDir)
  352. manifest.dsh.profile.bundles.push('ext-mixed')
  353. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  354. const { manager } = await bootProfile(staged)
  355. const [view] = await manager.list()
  356. expect(view).toMatchObject({ name: 'ext-mixed', status: 'partial', enabled: true })
  357. expect(view?.reason).toContain('boom at apply')
  358. expect(view?.rows.map(row => [row.entryId, row.phase, row.failure?.stage])).toEqual([
  359. ['include:ok', 'active', undefined],
  360. ['include:bad', 'failed', 'activation'],
  361. ])
  362. })
  363. it('lists a bundle left out by an id conflict as failed, with the conflict as its row', async () => {
  364. const staged = await stageHome()
  365. stagePackage(staged.profileDir, 'ext-one', { patch: BUNDLE_ONE_ROW })
  366. addDependency(staged.profileDir, 'ext-one')
  367. stagePackage(staged.profileDir, 'ext-two', { patch: BUNDLE_ONE_ROW })
  368. addDependency(staged.profileDir, 'ext-two')
  369. const manifest = manifestOf(staged.profileDir)
  370. manifest.dsh.profile.bundles.push('ext-one', 'ext-two')
  371. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  372. const { ctx, manager } = await bootProfile(staged)
  373. const views = await manager.list()
  374. expect(entryIds(ctx)).toContain('include:hello')
  375. expect(views.find(view => view.name === 'ext-one')).toMatchObject({ status: 'running' })
  376. const two = views.find(view => view.name === 'ext-two')
  377. expect(two).toMatchObject({ status: 'failed', enabled: true })
  378. expect(two?.reason).toContain('already declared by ext-one')
  379. expect(two?.rows).toEqual([{
  380. entryId: 'conflict:ext-two:hello', rowId: 'hello', moduleName: 'cordis:good', enabled: true, phase: 'failed',
  381. failure: { stage: 'conflict', message: 'row "hello" is already declared by ext-one' },
  382. }])
  383. })
  384. it('does not import a disabled bundle and reports a stale layer as restart-required', async () => {
  385. const staged = await stageHome('startup')
  386. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  387. addDependency(staged.profileDir, 'ext-broken')
  388. stagePackage(staged.profileDir, 'ext-later', { patch: BUNDLE_ONE_ROW })
  389. addDependency(staged.profileDir, 'ext-later')
  390. const { manager } = await bootProfile(staged)
  391. // Enabled after boot on a startup-reload profile: the manifest says yes, the tree says no.
  392. const manifest = manifestOf(staged.profileDir)
  393. manifest.dsh.profile.bundles.push('ext-later')
  394. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  395. const views = await manager.list()
  396. expect(views.find(view => view.name === 'ext-broken')).toMatchObject({ status: 'disabled' })
  397. expect(views.find(view => view.name === 'ext-later')).toMatchObject({ status: 'restart-required', liveReload: false })
  398. })
  399. it('reports unapplied live selections as failures instead of requiring a restart', async () => {
  400. const staged = await stageHome()
  401. stagePackage(staged.profileDir, 'ext-live', { patch: BUNDLE_ONE_ROW })
  402. addDependency(staged.profileDir, 'ext-live')
  403. const initial = manifestOf(staged.profileDir)
  404. initial.dsh.profile.bundles = ['ext-live']
  405. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(initial))
  406. const { ctx, manager } = await bootProfile(staged)
  407. try {
  408. initial.dsh.profile.bundles = []
  409. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(initial))
  410. const [view] = await manager.list()
  411. expect(view).toMatchObject({ enabled: false, liveReload: true, status: 'failed', reason: expect.stringContaining('not been applied') as string })
  412. expect(view?.rows[0]?.phase).toBe('active')
  413. } finally {
  414. await ctx.fiber.dispose()
  415. rmSync(staged.home, { recursive: true, force: true })
  416. }
  417. })
  418. it('reports unreadable declarations and treats physical peer identity as advisory', async () => {
  419. const staged = await stageHome()
  420. stagePackage(staged.profileDir, 'ext-odd', { patch: BUNDLE_ONE_ROW })
  421. addDependency(staged.profileDir, 'ext-odd')
  422. stagePackage(staged.profileDir, 'ext-refused', { patch: BUNDLE_ONE_ROW })
  423. addDependency(staged.profileDir, 'ext-refused')
  424. const { manager } = await bootProfile(staged, {
  425. metadata: ({ packageName }) => {
  426. if (packageName === 'ext-odd') throw new Error('invalid declarations')
  427. return { packageName, kind: 'bundle', cordisSameCopy: false, rows: [], overrides: [] }
  428. },
  429. })
  430. const views = await manager.list()
  431. expect(views.find(view => view.name === 'ext-odd')).toMatchObject({ status: 'not-enableable', reason: 'invalid declarations', cordisSameCopy: null, rows: [] })
  432. expect(views.find(view => view.name === 'ext-refused')).toMatchObject({ status: 'disabled', cordisSameCopy: false })
  433. await expect(manager.enable('ext-odd')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: 'cannot read package declarations: invalid declarations' } })
  434. await expect(manager.enable('ext-refused')).resolves.toMatchObject({ effect: 'live' })
  435. })
  436. it('reads a hand-written manifest: no dependencies, a template bundle, a ghost, and a builtin layer added after boot', async () => {
  437. const staged = await stageHome('startup')
  438. stagePackage(staged.profileDir, 'tpl', { patch: BUNDLE_ONE_ROW })
  439. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: ['tpl'], patchReload: 'startup' } } }))
  440. const { manager } = await bootProfile(staged)
  441. expect((await manager.list()).map(view => [view.name, view.status, view.installed, view.rows.length])).toEqual([['tpl', 'running', false, 1]])
  442. // A manifest with no dsh section at all knows no bundles.
  443. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web' }))
  444. expect(await manager.list()).toEqual([])
  445. // Bundles the manifest names after boot: one staged, one that resolves to nothing.
  446. stagePackage(staged.profileDir, 'tpl-later', { patch: BUNDLE_ONE_ROW })
  447. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: ['tpl', 'tpl-later', 'ghost'], patchReload: 'startup' } } }))
  448. const views = await manager.list()
  449. expect(views.map(view => [view.name, view.kind, view.status])).toEqual([
  450. ['tpl', 'bundle', 'running'],
  451. ['tpl-later', 'bundle', 'restart-required'],
  452. ['ghost', 'unknown', 'plain'],
  453. ])
  454. await expect(manager.uninstall('ghost')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  455. // Retrying takes the bundle out first, which a template bundle refuses.
  456. await expect(manager.retry('ghost')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  457. })
  458. it('folds disabled, user-disabled, and waiting rows', async () => {
  459. const staged = await stageHome()
  460. stagePackage(staged.profileDir, 'ext-off', { patch: '- insert:\n - id: a\n name: cordis:good\n disabled: true\n - id: b\n name: cordis:good\n' })
  461. addDependency(staged.profileDir, 'ext-off')
  462. stagePackage(staged.profileDir, 'ext-waiting', { patch: '- insert:\n - id: w\n name: cordis:consumer\n' })
  463. addDependency(staged.profileDir, 'ext-waiting')
  464. const { manager } = await bootProfile(staged)
  465. await manager.enable('ext-off')
  466. await manager.enable('ext-waiting')
  467. await manager.setRowDisabled('b', true)
  468. const views = await manager.list()
  469. const off = views.find(view => view.name === 'ext-off')
  470. expect(off).toMatchObject({ status: 'running' })
  471. expect(off?.rows.map(row => [row.rowId, row.enabled, row.disabledBy, row.phase])).toEqual([
  472. ['a', false, 'composition', null],
  473. ['b', false, 'user', null],
  474. ])
  475. await manager.setRowDisabled('b', true)
  476. const waiting = views.find(view => view.name === 'ext-waiting')
  477. expect(waiting).toMatchObject({ status: 'failed', reason: expect.stringContaining('fixtureSvc') as string })
  478. expect(waiting?.rows).toEqual([expect.objectContaining({ entryId: 'include:w', phase: 'pending', failure: expect.objectContaining({ stage: 'inject-pending' }) as object })])
  479. // Another bundle's recorded failure is not this one's row.
  480. expect(off?.rows.some(row => row.failure !== undefined)).toBe(false)
  481. })
  482. it('reads anonymous and gated static patch rows', async () => {
  483. const staged = await stageHome()
  484. stagePackage(staged.profileDir, 'ext-anon', { patch: '- insert:\n - name: cordis:good\n - id: gated\n name: cordis:good\n disabled: true\n' })
  485. addDependency(staged.profileDir, 'ext-anon')
  486. stagePackage(staged.profileDir, 'installed-bundle', { patch: BUNDLE_ONE_ROW })
  487. addDependency(staged.profileDir, 'installed-bundle')
  488. const { manager } = await bootProfile(staged)
  489. const views = await manager.list()
  490. expect(views.find(view => view.name === 'ext-anon')?.rows).toEqual([
  491. { entryId: 'cordis:good', rowId: 'cordis:good', moduleName: 'cordis:good', enabled: true, phase: null },
  492. { entryId: 'gated', rowId: 'gated', moduleName: 'cordis:good', enabled: false, disabledBy: 'composition', phase: null },
  493. ])
  494. await manager.enable('installed-bundle')
  495. const installed = (await manager.list()).find(view => view.name === 'installed-bundle')
  496. expect(installed).toMatchObject({ status: 'running' })
  497. expect(installed?.rows.map(row => row.entryId)).toEqual(['include:hello'])
  498. await manager.disable('installed-bundle')
  499. expect((await manager.list()).find(view => view.name === 'installed-bundle')?.rows.map(row => row.entryId)).toEqual(['hello'])
  500. })
  501. })
  502. describe('add', () => {
  503. it('removes a package that is neither a bundle nor a plugin module and says why', async () => {
  504. const staged = await stageHome()
  505. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const answer = 42\n' })
  506. const calls: string[][] = []
  507. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  508. const result = await manager.add('ext-lib')
  509. expect(result).toMatchObject({
  510. installed: ['ext-lib'], plain: ['ext-lib'], installedOnly: [], removed: [],
  511. })
  512. expect(calls).toEqual([['pnpm', 'add', 'ext-lib']])
  513. expect(manifestOf(staged.profileDir).dependencies).toHaveProperty('ext-lib')
  514. expect((await manager.list()).some(view => view.name === 'ext-lib')).toBe(true)
  515. })
  516. it('removes an installed bundle whose row id another layer already owns', async () => {
  517. const staged = await stageHome()
  518. stagePackage(staged.profileDir, 'ext-one', { patch: BUNDLE_ONE_ROW })
  519. addDependency(staged.profileDir, 'ext-one')
  520. const manifest = manifestOf(staged.profileDir)
  521. manifest.dsh.profile.bundles.push('ext-one')
  522. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  523. stagePackage(staged.profileDir, 'ext-two', { patch: BUNDLE_ONE_ROW })
  524. const calls: string[][] = []
  525. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  526. const result = await manager.add('ext-two', { enable: true })
  527. expect(result).toMatchObject({
  528. installed: [], enabled: [], installedOnly: [],
  529. removed: [{ name: 'ext-two', reason: 'row "hello" is already declared by ext-one' }],
  530. })
  531. expect(calls).toEqual([['pnpm', 'add', 'ext-two'], ['pnpm', 'remove', 'ext-two']])
  532. expect(manifestOf(staged.profileDir)).toMatchObject({ dsh: { profile: { bundles: expect.not.arrayContaining(['ext-two']) as string[] } } })
  533. expect(manifestOf(staged.profileDir).dependencies).not.toHaveProperty('ext-two')
  534. })
  535. it('reports a bundle whose patch disappears after metadata discovery', async () => {
  536. const staged = await stageHome()
  537. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  538. const calls: string[][] = []
  539. const { manager } = await bootProfile(staged, {
  540. spawn: recordingPnpm(staged.profileDir, calls),
  541. metadata: (options) => {
  542. const metadata = readPackageMetadata(options)
  543. rmSync(join(staged.profileDir, 'node_modules/ext-new/cordis.patch.yml'))
  544. return metadata
  545. },
  546. })
  547. const result = await manager.add('ext-new')
  548. expect(result.removed).toEqual([{ name: 'ext-new', reason: expect.stringContaining('cordis.patch.yml') as string }])
  549. expect(calls).toEqual([['pnpm', 'add', 'ext-new'], ['pnpm', 'remove', 'ext-new']])
  550. })
  551. it('keeps an unexecuted package installed with its bundle disabled', async () => {
  552. const staged = await stageHome()
  553. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  554. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  555. const result = await manager.add('ext-broken')
  556. expect(result).toMatchObject({ installed: ['ext-broken'], removed: [] })
  557. expect((await manager.list()).find(view => view.name === 'ext-broken')).toMatchObject({ status: 'disabled' })
  558. })
  559. it('restores the manifest when pnpm fails after writing it', async () => {
  560. const staged = await stageHome()
  561. const manifestPath = join(staged.profileDir, 'package.json')
  562. const before = readFileSync(manifestPath, 'utf8')
  563. const { manager } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, (args) => {
  564. addDependency(staged.profileDir, args[1] ?? 'ext-ghost')
  565. return { code: 1, stderr: 'ERR_PNPM_FETCH_404\n' }
  566. }) })
  567. await expect(manager.add('ext-ghost')).rejects.toMatchObject({ code: 'plugins/install-failed' })
  568. expect(readFileSync(manifestPath, 'utf8')).toBe(before)
  569. })
  570. it.each([undefined, 'original lockfile\n'])('cancels a silent install, waits for its process range and restores lockfile %s', async (lockfile) => {
  571. const staged = await stageHome()
  572. const requestId = 'f2340b6d-40bb-46b7-8b94-217bdf5010bd' as PluginInstallRequestId
  573. const manifestPath = join(staged.profileDir, 'package.json')
  574. const lockPath = join(staged.profileDir, 'pnpm-lock.yaml')
  575. const manifest = readFileSync(manifestPath, 'utf8')
  576. if (lockfile !== undefined) writeFileSync(lockPath, lockfile)
  577. const started = Promise.withResolvers<undefined>()
  578. const gone = Promise.withResolvers<undefined>()
  579. const pnpm = fakePnpm(staged.profileDir, () => {
  580. addDependency(staged.profileDir, 'partial')
  581. writeFileSync(lockPath, 'partially written lockfile')
  582. started.resolve(undefined)
  583. return { code: null, hang: true }
  584. })
  585. const { ctx, manager, log } = await bootProfile(staged, {
  586. spawn: spec => ({ ...pnpm(spec), waitForExit: () => gone.promise.then(() => true) }),
  587. })
  588. const states: PluginInstallProgress[] = []
  589. ctx.on('plugins/install-state', (progress) => { states.push(progress) })
  590. const answer = manager.add('partial', { enable: true, requestId }).catch((error: unknown) => error)
  591. await started.promise
  592. expect(states).toEqual([{ requestId, phase: 'installing' }])
  593. expect(log).toEqual([])
  594. expect(await manager.cancelInstall('other' as PluginInstallRequestId)).toEqual({ status: 'not-running' })
  595. const cancel = manager.cancelInstall(requestId)
  596. const repeated = manager.cancelInstall(requestId)
  597. try {
  598. expect(states.at(-1)).toEqual({ requestId, phase: 'cancelling' })
  599. await expect(manager.add('another')).rejects.toMatchObject({ code: 'plugins/busy' })
  600. expect(readFileSync(lockPath, 'utf8')).toBe('partially written lockfile')
  601. } finally { gone.resolve(undefined) }
  602. expect(await cancel).toEqual({ status: 'cancelled' })
  603. expect(await repeated).toEqual({ status: 'cancelled' })
  604. expect(await answer).toMatchObject({ code: 'plugins/install-cancelled', details: { requestId } })
  605. expect(readFileSync(manifestPath, 'utf8')).toBe(manifest)
  606. expect(existsSync(lockPath) ? readFileSync(lockPath, 'utf8') : undefined).toBe(lockfile)
  607. expect(states.some(state => state.phase === 'applying')).toBe(false)
  608. expect(log.every(chunk => chunk.requestId === requestId)).toBe(true)
  609. expect(await manager.cancelInstall(requestId)).toEqual({ status: 'not-running' })
  610. await expect(manager.add('')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  611. })
  612. it.skipIf(process.platform === 'win32')('terminates a real process group even when its parent exits zero and its child ignores TERM', async () => {
  613. // POSIX process groups and SIGTERM are the behavior under test; Windows owns taskkill semantics.
  614. const staged = await stageHome()
  615. const requestId = 'f2340b6d-40bb-46b7-8b94-217bdf5010bd' as PluginInstallRequestId
  616. const script = join(staged.profileDir, 'add')
  617. writeFileSync(script, `
  618. import { spawn } from 'node:child_process';
  619. import { writeFileSync } from 'node:fs';
  620. const child = spawn(process.execPath, ['-e', 'process.on("SIGTERM", () => {}); console.log(String(process.pid)); setInterval(() => {}, 1000)'], { stdio: ['ignore', 'pipe', 'inherit'] });
  621. child.stdout.once('data', bytes => {
  622. writeFileSync('pnpm-lock.yaml', 'partial');
  623. console.log(String(bytes).trim());
  624. });
  625. process.on('SIGTERM', () => process.exit(0));
  626. setInterval(() => {}, 1000);
  627. `)
  628. const ready = Promise.withResolvers<number>()
  629. const { ctx, manager, log } = await bootProfile(staged, {}, {
  630. pnpmCommand: process.execPath, installTimeoutMs: 30_000, installKillGraceMs: 50,
  631. })
  632. ctx.on('plugins/install-log', (chunk) => {
  633. if (chunk.stream === 'stdout' && chunk.text.trim()) ready.resolve(Number(chunk.text.trim()))
  634. })
  635. const result = manager.add('slow', { requestId, enable: true }).catch((error: unknown) => error)
  636. try {
  637. const pid = await ready.promise
  638. expect(pid).toBeGreaterThan(0)
  639. expect(await manager.cancelInstall(requestId)).toEqual({ status: 'cancelled' })
  640. expect(await result).toMatchObject({ code: 'plugins/install-cancelled' })
  641. expect(log.at(-1)?.exitCode).toBe(0)
  642. // A reparented Linux child may remain a zombie until init reaps it; it cannot execute or write.
  643. let status = ''
  644. try { status = execFileSync('ps', ['-p', String(pid), '-o', 'stat='], { encoding: 'utf8' }).trim() } catch (error) {
  645. if ((error as { status?: number }).status !== 1) throw error
  646. }
  647. expect(status === '' || status.startsWith('Z')).toBe(true)
  648. expect(existsSync(join(staged.profileDir, 'pnpm-lock.yaml'))).toBe(false)
  649. } finally { await manager.cancelInstall(requestId) }
  650. })
  651. it('reports a preparation failure instead of claiming cancellation completed', async () => {
  652. const staged = await stageHome()
  653. mkdirSync(join(staged.profileDir, 'pnpm-lock.yaml'))
  654. const { ctx, manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  655. const requestId = 'f2340b6d-40bb-46b7-8b94-217bdf5010bd' as PluginInstallRequestId
  656. let cancelled: Promise<unknown> | undefined
  657. ctx.on('plugins/install-state', (progress) => {
  658. if (progress.phase === 'installing') cancelled = manager.cancelInstall(requestId).catch((error: unknown) => error)
  659. })
  660. const failed = await manager.add('pkg', { requestId }).catch((error: unknown) => error)
  661. expect(failed).toBeInstanceOf(Error)
  662. expect(await cancelled).toBe(failed)
  663. expect(failed).not.toMatchObject({ code: 'plugins/install-cancelled' })
  664. })
  665. it('keeps application authoritative when cancellation arrives after preparation', async () => {
  666. const staged = await stageHome()
  667. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  668. const { ctx, manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  669. const requestId = 'f2340b6d-40bb-46b7-8b94-217bdf5010bd' as PluginInstallRequestId
  670. let cancel: ReturnType<PluginManager['cancelInstall']> | undefined
  671. ctx.on('plugins/install-state', (progress) => {
  672. if (progress.phase === 'applying') cancel = manager.cancelInstall(requestId)
  673. })
  674. const answer = await manager.add('ext-new', { requestId, enable: true })
  675. expect(await cancel).toEqual({ status: 'too-late' })
  676. expect(answer.enabled).toEqual(['ext-new'])
  677. })
  678. it('stops an in-flight installer when its owning plugin is disposed', async () => {
  679. const staged = await stageHome()
  680. const started = Promise.withResolvers<undefined>()
  681. const { ctx, manager } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => {
  682. started.resolve(undefined)
  683. return { code: null, hang: true }
  684. }) })
  685. const answer = manager.add('slow').catch((error: unknown) => error)
  686. await started.promise
  687. await ctx.fiber.dispose()
  688. expect(await answer).toMatchObject({ code: 'plugins/install-cancelled' })
  689. })
  690. it('uses the standalone installer without a runtime or cancellation controller', async () => {
  691. const staged = await stageHome()
  692. stagePackage(staged.profileDir, 'ext-plain', {})
  693. const log: PluginInstallLogChunk[] = []
  694. const installer = new PluginInstaller({
  695. profileDir: staged.profileDir, profileName: 'web', installAnchor: staged.anchor,
  696. loadProfile: () => loadProfile(NAME, 'web', staged.anchor, staged.home),
  697. config: managerConfig(), color: false, spawn: recordingPnpm(staged.profileDir), installLog: (chunk) => { log.push(chunk) },
  698. })
  699. expect(await installer.add('ext-plain')).toMatchObject({ installed: ['ext-plain'] })
  700. expect(manifestOf(staged.profileDir).dependencies).toHaveProperty('ext-plain')
  701. expect(log.every(chunk => chunk.requestId === undefined)).toBe(true)
  702. await installer.remove('ext-plain')
  703. expect(manifestOf(staged.profileDir).dependencies).not.toHaveProperty('ext-plain')
  704. })
  705. it('does not start package removal after removing its user row disposes the manager', async () => {
  706. const staged = await stageHome()
  707. stagePackage(staged.profileDir, 'ext-old', { main: 'export function apply() {}' })
  708. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), '- insert:\n - id: old-row\n name: ext-old\n')
  709. addDependency(staged.profileDir, 'ext-old')
  710. const calls: string[][] = []
  711. const { ctx, manager, runtime } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  712. const recompose = runtime.recompose.bind(runtime)
  713. vi.spyOn(runtime, 'recompose').mockImplementationOnce(async (options) => {
  714. const result = await recompose(options)
  715. await ctx.fiber.dispose()
  716. return result
  717. })
  718. await expect(manager.uninstall('ext-old')).rejects.toMatchObject({ code: 'plugins/unavailable', details: { reason: 'manager disposed' } })
  719. expect(calls).toEqual([])
  720. })
  721. it('joins a package removal process during host disposal without exposing installation cancellation for it', async () => {
  722. const staged = await stageHome()
  723. stagePackage(staged.profileDir, 'ext-old', {})
  724. addDependency(staged.profileDir, 'ext-old')
  725. const started = Promise.withResolvers<undefined>()
  726. const { ctx, manager, log } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => {
  727. started.resolve(undefined)
  728. return { code: null, hang: true, stdout: 'removing' }
  729. }) })
  730. const result = manager.uninstall('ext-old').catch((error: unknown) => error)
  731. await started.promise
  732. expect(await manager.cancelInstall(log[0]?.requestId as PluginInstallRequestId)).toEqual({ status: 'not-running' })
  733. await ctx.fiber.dispose()
  734. expect(await result).toMatchObject({ code: 'plugins/install-cancelled' })
  735. })
  736. it('refuses a second mutation while one is still running', async () => {
  737. const staged = await stageHome()
  738. stagePackage(staged.profileDir, 'ext-slow', { patch: BUNDLE_ONE_ROW })
  739. let release = (): void => {}
  740. const gate = new Promise<void>((resolve) => { release = resolve })
  741. const spawn = fakePnpm(staged.profileDir, (args) => {
  742. addDependency(staged.profileDir, args[1] ?? 'ext-slow')
  743. return { code: 0 }
  744. }, [], gate)
  745. const { manager } = await bootProfile(staged, { spawn })
  746. const first = manager.add('ext-slow')
  747. await expect(manager.enable('ext-slow')).rejects.toMatchObject({
  748. code: 'plugins/busy', details: { operation: 'enable', active: { operation: 'add', subject: 'ext-slow' } },
  749. })
  750. release()
  751. await expect(first).resolves.toMatchObject({ installed: ['ext-slow'] })
  752. // The lock is released with the run: the refused call now goes through.
  753. await expect(manager.enable('ext-slow')).resolves.toMatchObject({ changed: true })
  754. })
  755. it('refuses to change node_modules while a session is running', async () => {
  756. const staged = await stageHome()
  757. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  758. addDependency(staged.profileDir, 'ext-bundle')
  759. const calls: string[][] = []
  760. const { ctx, manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  761. ctx.provide('agents', { list: () => [{ status: 'running' }, { status: 'idle' }] } as never)
  762. await expect(manager.add('ext-new')).rejects.toMatchObject({ code: 'plugins/agents-running', details: { operation: 'add', running: 1 } })
  763. await expect(manager.uninstall('ext-bundle')).rejects.toMatchObject({ code: 'plugins/agents-running', details: { operation: 'uninstall' } })
  764. expect(calls).toEqual([])
  765. // Enabling recomposes the tree without touching node_modules.
  766. await expect(manager.enable('ext-bundle')).resolves.toMatchObject({ changed: true })
  767. })
  768. it('runs pnpm add, records the dependency and leaves its bundle disabled', async () => {
  769. const staged = await stageHome()
  770. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  771. const calls: string[][] = []
  772. const { manager, changes, log } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  773. const result = await manager.add('github:acme/ext-new')
  774. expect(calls).toEqual([['pnpm', 'add', 'github:acme/ext-new']])
  775. // The fake pnpm records the spec itself as the dependency name, which
  776. // resolves to nothing: an installed dependency whose declarations cannot be read.
  777. expect(result).toEqual({ installed: ['github:acme/ext-new'], removed: [], enabled: [], installedOnly: [], plain: ['github:acme/ext-new'], jobId: expect.any(String) as string })
  778. expect(log.map(chunk => [chunk.argv, chunk.cwd, chunk.stream, chunk.text, chunk.exitCode])).toEqual([
  779. [['pnpm', 'add', 'github:acme/ext-new'], staged.profileDir, 'stdout', '+ github:acme/ext-new 1.0.0\n', undefined],
  780. [['pnpm', 'add', 'github:acme/ext-new'], staged.profileDir, 'stdout', '', 0],
  781. ])
  782. expect(changes).toEqual([{ reason: 'install' }])
  783. })
  784. it('has pnpm colour its output and streams the escapes as they come', async () => {
  785. const staged = await stageHome()
  786. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  787. const colours: (string | undefined)[] = []
  788. const pnpm = fakePnpm(staged.profileDir, (args) => {
  789. addDependency(staged.profileDir, args[1] as string)
  790. return { code: 0, stdout: '\u001b[32m+\u001b[39m ext-new \u001b[90m1.0.0\u001b[39m\n' }
  791. })
  792. const { manager, log } = await bootProfile(staged, {
  793. spawn: (spec) => {
  794. colours.push(spec.env?.FORCE_COLOR)
  795. return pnpm(spec)
  796. },
  797. })
  798. await manager.add('ext-new')
  799. expect(colours).toEqual(['1'])
  800. expect(log[0]?.text).toBe('\u001b[32m+\u001b[39m ext-new \u001b[90m1.0.0\u001b[39m\n')
  801. })
  802. it('reconciles by the installed name, and enables the new bundle when asked', async () => {
  803. const staged = await stageHome()
  804. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  805. const { ctx, manager, changes } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  806. const result = await manager.add('ext-new', { enable: true })
  807. expect(result).toMatchObject({ installed: ['ext-new'], enabled: ['ext-new'], installedOnly: [], plain: [] })
  808. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-new'])
  809. expect(entryIds(ctx)).toEqual(expect.arrayContaining(['include:hello']))
  810. expect(changes.map(change => change.reason)).toEqual(['enable', 'install'])
  811. expect((await manager.list()).find(view => view.name === 'ext-new')?.status).toBe('running')
  812. })
  813. it('reports a plugin module as plain and uninstalls it', async () => {
  814. const staged = await stageHome()
  815. stagePackage(staged.profileDir, 'ext-lib', { main: 'export function apply() {}\n' })
  816. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  817. expect(await manager.add('ext-lib')).toMatchObject({ installed: ['ext-lib'], plain: ['ext-lib'], installedOnly: [], removed: [] })
  818. expect((await manager.list()).find(view => view.name === 'ext-lib')?.status).toBe('plain')
  819. await manager.uninstall('ext-lib')
  820. expect((await manager.list()).some(view => view.name === 'ext-lib')).toBe(false)
  821. })
  822. it('installs into a manifest that declares no dependencies yet', async () => {
  823. const staged = await stageHome()
  824. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: [], patchReload: 'live' } } }))
  825. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  826. const { manager } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => {
  827. const path = join(staged.profileDir, 'package.json')
  828. const manifest = JSON.parse(readFileSync(path, 'utf8')) as Record<string, unknown>
  829. writeFileSync(path, JSON.stringify({ ...manifest, dependencies: { 'ext-new': '1.0.0' } }))
  830. return { code: 0 }
  831. }) })
  832. expect(await manager.add('ext-new')).toMatchObject({ installed: ['ext-new'], installedOnly: ['ext-new'] })
  833. })
  834. it('fails loud on a non-zero exit, a spawn error, a timeout, and an empty spec', async () => {
  835. const staged = await stageHome()
  836. const exits = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => ({ code: 1, stderr: 'ERR_PNPM_NO_MATCHING_VERSION\n' })) })
  837. await expect(exits.manager.add('nope')).rejects.toMatchObject({
  838. code: 'plugins/install-failed', details: { spec: 'nope', exitCode: 1, log: 'ERR_PNPM_NO_MATCHING_VERSION\n' },
  839. })
  840. expect(exits.log.at(-1)).toMatchObject({ exitCode: 1 })
  841. await expect(exits.manager.add(' ')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  842. const erroringHome = await stageHome()
  843. const erroring = await bootProfile(erroringHome, { spawn: fakePnpm(erroringHome.profileDir, () => ({ code: null, error: 'spawn pnpm ENOENT' })) })
  844. await expect(erroring.manager.add('x')).rejects.toMatchObject({ code: 'plugins/install-failed', details: { exitCode: null } })
  845. expect(erroring.log.some(chunk => chunk.text.includes('ENOENT'))).toBe(true)
  846. const hangingHome = await stageHome()
  847. const hanging = await bootProfile(hangingHome, { spawn: fakePnpm(hangingHome.profileDir, () => ({ code: null, hang: true })) })
  848. await expect(hanging.manager.add('x')).rejects.toMatchObject({ code: 'plugins/install-failed' })
  849. expect(hanging.log.some(chunk => chunk.text.includes('timed out'))).toBe(true)
  850. })
  851. it('keeps only the tail of a long log in the failure', async () => {
  852. const staged = await stageHome()
  853. const { manager } = await bootProfile(staged, {
  854. spawn: fakePnpm(staged.profileDir, () => ({ code: 2, stdout: 'a'.repeat(300), stderr: 'b'.repeat(300) })),
  855. }, { installLogTailBytes: 256 })
  856. await expect(manager.add('x')).rejects.toMatchObject({ details: { log: 'b'.repeat(300) } })
  857. })
  858. })
  859. describe('enable, disable, and retry', () => {
  860. it('composes an installed bundle live, and takes it out again', async () => {
  861. const staged = await stageHome()
  862. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  863. addDependency(staged.profileDir, 'ext-bundle')
  864. const { ctx, manager, changes } = await bootProfile(staged)
  865. expect(await manager.enable('ext-bundle')).toEqual({ changed: true, effect: 'live' })
  866. expect(entryIds(ctx)).toContain('include:hello')
  867. expect(await manager.enable('ext-bundle')).toEqual({ changed: false, effect: 'live' })
  868. expect(await manager.disable('ext-bundle')).toEqual({ changed: true, effect: 'live' })
  869. expect(entryIds(ctx)).not.toContain('include:hello')
  870. expect(await manager.disable('ext-bundle')).toEqual({ changed: false, effect: 'live' })
  871. expect(changes.map(change => [change.reason, change.packageName])).toEqual([
  872. ['enable', 'ext-bundle'], ['enable', 'ext-bundle'], ['disable', 'ext-bundle'], ['disable', 'ext-bundle'],
  873. ])
  874. })
  875. it('only writes the manifest on a startup-reload profile', async () => {
  876. const staged = await stageHome('startup')
  877. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  878. addDependency(staged.profileDir, 'ext-bundle')
  879. const { ctx, manager } = await bootProfile(staged)
  880. expect(await manager.enable('ext-bundle')).toEqual({ changed: true, effect: 'restart' })
  881. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-bundle'])
  882. expect(entryIds(ctx)).not.toContain('include:hello')
  883. expect(await manager.disable('ext-bundle')).toEqual({ changed: true, effect: 'restart' })
  884. })
  885. it('refuses what cannot be enabled or disabled', async () => {
  886. const staged = await stageHome()
  887. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  888. addDependency(staged.profileDir, 'ext-lib')
  889. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  890. addDependency(staged.profileDir, 'ext-broken')
  891. const { manager } = await bootProfile(staged)
  892. await expect(manager.enable('absent')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  893. await expect(manager.enable('ext-lib')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: expect.stringContaining('declares no dsh.bundle') as string } })
  894. await expect(manager.enable('ext-broken')).resolves.toMatchObject({ effect: 'live' })
  895. // A template bundle is not a dependency and cannot be disabled.
  896. const manifest = manifestOf(staged.profileDir)
  897. manifest.dsh.profile.bundles.push('template')
  898. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  899. await expect(manager.disable('template')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  900. await expect(manager.retry('ext-lib')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  901. })
  902. it('retains live enablement and reports failed rows after startup', async () => {
  903. const staged = await stageHome()
  904. stagePackage(staged.profileDir, 'ext-fatal', { patch: '- insert:\n - id: bad\n name: cordis:throws\n' })
  905. addDependency(staged.profileDir, 'ext-fatal')
  906. stagePackage(staged.profileDir, 'ext-fine', { patch: BUNDLE_ONE_ROW })
  907. addDependency(staged.profileDir, 'ext-fine')
  908. const { ctx, manager } = await bootProfile(staged)
  909. await manager.enable('ext-fine')
  910. const result = await manager.enable('ext-fatal')
  911. expect(result.effect).toBe('live')
  912. expect(result.issues?.[0]?.message).toContain('boom at apply')
  913. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-fine', 'ext-fatal'])
  914. expect(entryIds(ctx)).toContain('include:hello')
  915. expect(entryIds(ctx)).toContain('include:bad')
  916. })
  917. it('retries a bundle by removing and re-adding its layer', async () => {
  918. const staged = await stageHome()
  919. stagePackage(staged.profileDir, 'ext-flaky', { patch: '- insert:\n - id: once\n name: cordis:flaky\n' })
  920. addDependency(staged.profileDir, 'ext-flaky')
  921. flakyCalls = 0
  922. const { manager, changes } = await bootProfile(staged)
  923. await manager.enable('ext-flaky')
  924. expect((await manager.list())[0]).toMatchObject({ status: 'failed', reason: expect.stringContaining('flaky first start') as string })
  925. expect(await manager.retry('ext-flaky')).toEqual({ changed: true, effect: 'live' })
  926. expect((await manager.list())[0]).toMatchObject({ status: 'running' })
  927. expect(changes.map(change => change.reason)).toEqual(['enable', 'disable', 'enable', 'retry'])
  928. })
  929. })
  930. describe('rows in user layers', () => {
  931. it('persists a startup profile row toggle without changing its mounted tree', async () => {
  932. const staged = await stageHome('startup')
  933. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  934. addDependency(staged.profileDir, 'ext-bundle')
  935. const manifest = manifestOf(staged.profileDir)
  936. manifest.dsh.profile.bundles.push('ext-bundle')
  937. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest))
  938. const { ctx, manager } = await bootProfile(staged)
  939. await manager.setRowDisabled('hello', true)
  940. expect(readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')).toContain('disabled: true')
  941. expect(ctx.loader.resolve('include:hello').disabled).toBe(false)
  942. })
  943. it('disables and restores a bundle row through the live user layer', async () => {
  944. const staged = await stageHome()
  945. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  946. addDependency(staged.profileDir, 'ext-bundle')
  947. const { ctx, manager, changes } = await bootProfile(staged)
  948. await manager.enable('ext-bundle')
  949. const file = join(staged.profileDir, 'cordis.patch.yml')
  950. await manager.setRowDisabled('hello', true)
  951. expect(ctx.loader.resolve('include:hello').disabled).toBe(true)
  952. expect(readFileSync(file, 'utf8')).toContain('disabled: true')
  953. await manager.setRowDisabled('hello', false)
  954. expect(ctx.loader.resolve('include:hello').disabled).toBe(false)
  955. expect(readFileSync(file, 'utf8')).not.toContain('disabled')
  956. expect(changes.map(change => change.reason)).toEqual(['enable', 'row', 'row'])
  957. })
  958. })
  959. describe('dependents and uninstall', () => {
  960. it('names the services other rows inject and the user-layer rows that reference the package', async () => {
  961. const staged = await stageHome()
  962. // Beside the provider: a row nobody injects, and a row switched off (no fiber to read).
  963. stagePackage(staged.profileDir, 'ext-provider', { patch: '- insert:\n - id: svc\n name: cordis:provider\n - id: lonely\n name: cordis:lonely-provider\n - id: off\n name: cordis:good\n disabled: true\n' })
  964. addDependency(staged.profileDir, 'ext-provider')
  965. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), [
  966. '- insert:',
  967. ' - name: ext-provider/anonymous.js',
  968. ' disabled: true',
  969. ' - id: ref',
  970. ' name: ext-provider/tools/x.js',
  971. ' disabled: true',
  972. ' - id: grp',
  973. ' name: cordis:group',
  974. ' group: true',
  975. ' config:',
  976. ' - id: nested-ref',
  977. ' name: ext-provider',
  978. ' disabled: true',
  979. '',
  980. ].join('\n'))
  981. const { manager, runtime } = await bootProfile(staged)
  982. await manager.enable('ext-provider')
  983. // A built-in row injecting the bundle's service, composed once the
  984. // provider is up (a boot would refuse a row left waiting).
  985. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), `${readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')}- id: svc\n disabled: false\n- insert:\n - id: needs-svc\n name: cordis:consumer\n`)
  986. await runtime.recompose()
  987. const dependents = await manager.dependents('ext-provider')
  988. expect(dependents.services).toEqual([{ service: 'fixtureSvc', providedBy: 'include:svc', injectedBy: ['include:needs-svc'] }])
  989. expect(dependents.references.map(reference => reference.rowId)).toEqual(['ref', 'nested-ref'])
  990. expect(dependents.references[0]).toEqual({ rowId: 'ref', moduleName: 'ext-provider/tools/x.js' })
  991. })
  992. it('ignores an unreadable user layer while collecting references', async () => {
  993. const staged = await stageHome()
  994. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  995. addDependency(staged.profileDir, 'ext-lib')
  996. const { manager } = await bootProfile(staged)
  997. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'a: [\n')
  998. expect((await manager.dependents('ext-lib')).references).toEqual([])
  999. })
  1000. it('disables a bundle and removes references from a handwritten user patch before uninstalling', async () => {
  1001. const staged = await stageHome()
  1002. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW, files: { 'extra.js': 'export function apply() {}\n' } })
  1003. addDependency(staged.profileDir, 'ext-bundle')
  1004. const calls: string[][] = []
  1005. const { ctx, manager, changes } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  1006. await manager.enable('ext-bundle')
  1007. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), '- insert:\n - id: ext-bundle/extra.js\n name: ext-bundle/extra.js\n')
  1008. await ctx.profileRuntime.recompose()
  1009. expect(entryIds(ctx)).toEqual(expect.arrayContaining(['include:hello', 'include:ext-bundle/extra.js']))
  1010. await manager.uninstall('ext-bundle')
  1011. expect(calls).toEqual([['pnpm', 'remove', 'ext-bundle']])
  1012. expect(manifestOf(staged.profileDir)).toMatchObject({ dependencies: {}, dsh: { profile: { bundles: [] } } })
  1013. expect(entryIds(ctx)).not.toContain('include:hello')
  1014. expect(entryIds(ctx)).not.toContain('include:ext-bundle/extra.js')
  1015. expect(readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')).toBe('[]\n')
  1016. expect(changes.map(change => change.reason)).toEqual(['enable', 'disable', 'uninstall'])
  1017. await expect(manager.uninstall('ext-bundle')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  1018. })
  1019. })
  1020. })
  1021. describe('PluginOperationError', () => {
  1022. it('carries its code and details, and is what pluginOperationFailureOf narrows to', () => {
  1023. const failure = new PluginOperationError('plugins/not-installed', 'absent', { packageName: 'x' })
  1024. expect(failure).toMatchObject({ name: 'PluginOperationError', code: 'plugins/not-installed', message: 'absent', details: { packageName: 'x' } })
  1025. expect(pluginOperationFailureOf(failure)).toBe(failure)
  1026. expect(pluginOperationFailureOf(new Error('plain'))).toBeUndefined()
  1027. })
  1028. })