plugin-manager.spec.ts 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 roster, and the agent registry off the context per call.
  8. */
  9. import { EventEmitter } from 'node:events'
  10. import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  11. import { mkdtemp } from 'node:fs/promises'
  12. import { tmpdir } from 'node:os'
  13. import { join } from 'node:path'
  14. import { PassThrough } from 'node:stream'
  15. import type { ChildProcess } from 'node:child_process'
  16. import { afterEach, describe, expect, it } from 'vitest'
  17. import { Context, type Plugin } from '@deepseek-ai/cordis'
  18. import Loader from '@deepseek-ai/cordis-plugin-loader'
  19. import {
  20. boot, composeProfileStack, loadOptionalPatches, loadProfile, ProfileRuntime, rootIncludeEntry,
  21. type ComposedStack, type Profile, readPackageMetadata,
  22. } from '@deepseek-ai/dsh-app-boot'
  23. import {
  24. PluginManager, type PluginInstallLogChunk, type PluginToolingConfig, type SpawnLike,
  25. } from '@deepseek-ai/dsh-plugin-manager'
  26. import type {} from '@deepseek-ai/dsh-agent'
  27. import type {} from '@deepseek-ai/dsh-agent-presets'
  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, 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. plugins?: { name: string; title?: string; config?: unknown }[]
  95. files?: Record<string, string>
  96. }
  97. /** Stage one package under the profile's node_modules, the way pnpm leaves it. */
  98. function stagePackage(profileDir: string, name: string, staged: StagedPackage): void {
  99. const dir = join(profileDir, 'node_modules', name)
  100. mkdirSync(dir, { recursive: true })
  101. writeFileSync(join(dir, 'package.json'), JSON.stringify({
  102. name,
  103. version: staged.version ?? '1.0.0',
  104. description: `staged ${name}`,
  105. type: 'module',
  106. ...staged.main === undefined ? {} : { main: 'index.js' },
  107. dsh: {
  108. title: `Title of ${name}`,
  109. ...staged.patch === undefined ? {} : { bundle: { patch: './cordis.patch.yml' } },
  110. ...staged.plugins === undefined ? {} : { plugins: staged.plugins },
  111. },
  112. }, null, 2))
  113. if (staged.patch !== undefined) writeFileSync(join(dir, 'cordis.patch.yml'), staged.patch)
  114. if (staged.main !== undefined) writeFileSync(join(dir, 'index.js'), staged.main)
  115. for (const [file, text] of Object.entries(staged.files ?? {})) {
  116. mkdirSync(join(dir, file, '..'), { recursive: true })
  117. writeFileSync(join(dir, file), text)
  118. }
  119. }
  120. /** Add a dependency to the profile manifest, as `pnpm add` does. */
  121. function addDependency(profileDir: string, name: string, spec = '1.0.0'): void {
  122. const path = join(profileDir, 'package.json')
  123. const manifest = JSON.parse(readFileSync(path, 'utf8')) as { dependencies: Record<string, string> }
  124. manifest.dependencies[name] = spec
  125. writeFileSync(path, JSON.stringify(manifest, null, 2))
  126. }
  127. function manifestOf(profileDir: string): { dependencies: Record<string, string>; dsh: { profile: { bundles: string[] } } } {
  128. return JSON.parse(readFileSync(join(profileDir, 'package.json'), 'utf8')) as ReturnType<typeof manifestOf>
  129. }
  130. /** What the fake pnpm does for one invocation. */
  131. type PnpmBehavior = (args: readonly string[]) => { code: number | null; stdout?: string; stderr?: string; hang?: boolean; error?: unknown }
  132. /** A fake `spawn` that runs `behavior` on the next tick and reports through a child-like emitter. */
  133. function fakePnpm(profileDir: string, behavior: PnpmBehavior, calls: string[][] = []): SpawnLike {
  134. return (command, args, options) => {
  135. calls.push([command, ...args])
  136. expect(options.cwd).toBe(profileDir)
  137. const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough; kill: (signal?: string) => boolean }
  138. child.stdout = new PassThrough()
  139. child.stderr = new PassThrough()
  140. let killed = false
  141. child.kill = () => { killed = true; return true }
  142. setTimeout(() => {
  143. const outcome = behavior(args)
  144. if (outcome.error !== undefined) {
  145. child.emit('error', outcome.error)
  146. return
  147. }
  148. if (outcome.stdout !== undefined) child.stdout.write(outcome.stdout)
  149. if (outcome.stderr !== undefined) child.stderr.write(outcome.stderr)
  150. if (outcome.hang === true) {
  151. // Report the kill the timeout sends, as a real child would.
  152. const poll = setInterval(() => {
  153. if (!killed) return
  154. clearInterval(poll)
  155. child.emit('close', null)
  156. }, 10)
  157. return
  158. }
  159. setTimeout(() => { child.emit('close', outcome.code) }, 5)
  160. }, 5)
  161. return child as unknown as ChildProcess
  162. }
  163. }
  164. /** The pnpm every install test shares: `add <name>` stages nothing (the test did) and records the dependency. */
  165. function recordingPnpm(profileDir: string, calls: string[][] = []): SpawnLike {
  166. return fakePnpm(profileDir, (args) => {
  167. const [verb, target] = args
  168. if (verb === 'add' && target !== undefined) {
  169. addDependency(profileDir, target)
  170. return { code: 0, stdout: `+ ${target} 1.0.0\n` }
  171. }
  172. if (verb === 'remove' && target !== undefined) {
  173. const path = join(profileDir, 'package.json')
  174. const manifest = JSON.parse(readFileSync(path, 'utf8')) as { dependencies: Record<string, string> }
  175. const { [target]: _removed, ...remaining } = manifest.dependencies
  176. writeFileSync(path, JSON.stringify({ ...manifest, dependencies: remaining }, null, 2))
  177. rmSync(join(profileDir, 'node_modules', target), { recursive: true, force: true })
  178. return { code: 0, stdout: `- ${target}\n` }
  179. }
  180. return { code: 1, stderr: 'unexpected pnpm invocation\n' }
  181. }, calls)
  182. }
  183. interface Booted {
  184. ctx: Context
  185. manager: PluginManager
  186. runtime: ProfileRuntime
  187. changes: { reason: string; packageName?: string }[]
  188. log: PluginInstallLogChunk[]
  189. }
  190. /** Boot the profile the way the launcher does, then build the manager over it. */
  191. async function bootProfile(staged: StagedHome, internals: Internals = {}, config: Partial<PluginToolingConfig> = {}): Promise<Booted> {
  192. const load = (): Profile => loadProfile(NAME, 'web', staged.anchor, staged.home)
  193. const composeFor = (profile: Profile): ComposedStack => {
  194. const stack = composeProfileStack(profile.layers, [
  195. { label: profile.patchPath, patches: loadOptionalPatches(NAME, profile.patchPath) ?? [] },
  196. ])
  197. return { ...stack, patches: structuredClone(stack.patches) }
  198. }
  199. const profile = load()
  200. const ctx = await boot(NAME, join(staged.profileDir, 'cordis.yml'), composeFor(profile).patches, async (ctx) => {
  201. prepare(ctx)
  202. await ctx.plugin(ProfileRuntime, {
  203. profile, stack: composeFor(profile), installAnchor: staged.anchor,
  204. loadProfile: load, compose: composeFor, rootEntry: () => rootIncludeEntry(ctx),
  205. })
  206. })
  207. contexts.push(ctx)
  208. const changes: Booted['changes'] = []
  209. const log: PluginInstallLogChunk[] = []
  210. ctx.on('plugins/changed', (change) => { changes.push(change) })
  211. ctx.on('plugins/install-log', (chunk) => { log.push(chunk) })
  212. const host = ctx.plugin({ inject: ['loader'], apply() {} })
  213. await host.await()
  214. const manager = managerOver(host.ctx, internals, config)
  215. return { ctx, manager, runtime: ctx.profileRuntime, changes, log }
  216. }
  217. /** The manager as the Web host's adapter builds it: runtime, roster, and agent count read off the context per call. */
  218. function managerOver(ctx: Context, internals: Internals = {}, config: Partial<PluginToolingConfig> = {}): PluginManager {
  219. return new PluginManager(ctx, {
  220. config: managerConfig(config),
  221. runtime: () => ctx.get('profileRuntime'),
  222. presets: () => ctx.get('agentPresets'),
  223. runningAgents: () => (ctx.get('agents')?.list() ?? []).filter(agent => agent.status === 'running').length,
  224. ...internals,
  225. })
  226. }
  227. const entryIds = (ctx: Context): string[] => [...ctx.loader.entries()].map(entry => entry.id)
  228. const BUNDLE_ONE_ROW = '- insert:\n - id: hello\n name: cordis:good\n'
  229. describe('PluginManager', () => {
  230. it('keeps an installed package with malformed metadata manageable', async () => {
  231. const staged = await stageHome()
  232. stagePackage(staged.profileDir, 'broken', {})
  233. addDependency(staged.profileDir, 'broken')
  234. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  235. writeFileSync(join(staged.profileDir, 'node_modules/broken/package.json'), '{ invalid json')
  236. const view = (await manager.list())[0]!
  237. expect(view.name).toBe('broken')
  238. expect(view.installed).toBe(true)
  239. expect(view.reason).toBeTruthy()
  240. await manager.uninstall('broken')
  241. expect(await manager.list()).toEqual([])
  242. })
  243. it('shows failed group imports while omitting healthy structural groups', async () => {
  244. const staged = await stageHome()
  245. stagePackage(staged.profileDir, 'groups', { patch: `- insert:
  246. - id: healthy
  247. name: cordis:group
  248. group: true
  249. config: []
  250. - id: broken-group
  251. name: ./missing.mjs
  252. group: true
  253. config: []
  254. ` })
  255. addDependency(staged.profileDir, 'groups')
  256. const { manager } = await bootProfile(staged)
  257. await manager.enable('groups')
  258. const view = (await manager.list())[0]!
  259. expect(view.status).toBe('failed')
  260. expect(view.rows.map(row => row.rowId)).toEqual(['broken-group'])
  261. expect(view.rows[0]?.phase).toBe('failed')
  262. expect(view.rows[0]?.failure?.stage).toBe('import')
  263. })
  264. it('shows a failed config update beside the still-active old instance and attributes overrides to their owner', async () => {
  265. const staged = await stageHome()
  266. stagePackage(staged.profileDir, 'base', {
  267. patch: '- insert:\n - id: core-config\n name: cordis:configured\n config: { value: 1 }\n',
  268. })
  269. const manifest = manifestOf(staged.profileDir)
  270. manifest.dsh.profile.bundles.push('base')
  271. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest))
  272. stagePackage(staged.profileDir, 'ext', { patch: '- insert:\n - id: ext-ok\n name: cordis:good\n- id: core-config\n config: { value: -1 }\n' })
  273. addDependency(staged.profileDir, 'ext')
  274. const { ctx, manager } = await bootProfile(staged)
  275. const result = await manager.enable('ext')
  276. expect(result.issues?.[0]?.entryId).toBe('include:core-config')
  277. const core = ctx.loader.resolve('include:core-config')
  278. expect(core.fiber?.state).toBe(2)
  279. expect(core.fiber?.config).toEqual({ value: 1 })
  280. expect(core.options.config).toEqual({ value: -1 })
  281. const views = await manager.list()
  282. const ext = views.find(view => view.name === 'ext')!
  283. expect(ext.status).toBe('partial')
  284. expect(ext.issues?.[0]?.stage).toBe('update')
  285. expect(ext.rows.map(row => row.rowId)).toEqual(['ext-ok'])
  286. expect(ctx.profileRuntime.originOfEntry(core)?.packageName).toBe('base')
  287. expect(views.find(view => view.name === 'base')?.rows[0]).toMatchObject({ phase: 'active', failure: { stage: 'update' } })
  288. writeFileSync(join(staged.profileDir, 'node_modules/ext/cordis.patch.yml'), '- id: core-config\n config: { value: -1 }\n')
  289. await ctx.profileRuntime.recompose({ reloadBundles: true })
  290. expect((await manager.list()).find(view => view.name === 'ext')?.status).toBe('failed')
  291. await manager.disable('ext')
  292. expect(core.fiber?.config).toEqual({ value: 1 })
  293. expect((await manager.list()).find(view => view.name === 'base')?.status).toBe('running')
  294. })
  295. it('keeps a rejected preparation out of the layer list without promising rollback of live plugin effects', async () => {
  296. const staged = await stageHome()
  297. stagePackage(staged.profileDir, 'base', { patch: BUNDLE_ONE_ROW })
  298. const manifest = manifestOf(staged.profileDir)
  299. manifest.dsh.profile.bundles.push('base')
  300. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest))
  301. stagePackage(staged.profileDir, 'clash', { patch: BUNDLE_ONE_ROW })
  302. addDependency(staged.profileDir, 'clash')
  303. const { ctx, manager } = await bootProfile(staged)
  304. const changedManifest = manifestOf(staged.profileDir)
  305. changedManifest.dsh.profile.bundles.push('missing-base')
  306. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(changedManifest))
  307. await expect(manager.enable('clash')).rejects.toMatchObject({ code: 'plugins/enable-failed' })
  308. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['base', 'missing-base'])
  309. expect(ctx.loader.resolve('include:hello').fiber?.state).toBe(2)
  310. })
  311. it('does not execute a declared main module during install or list and refuses an undeclared entry', async () => {
  312. const staged = await stageHome()
  313. const marker = join(staged.profileDir, 'executed')
  314. stagePackage(staged.profileDir, 'declared', {
  315. plugins: [{ name: '.', config: { custom: 1 } }],
  316. main: `import { writeFileSync } from 'node:fs'; writeFileSync(${JSON.stringify(marker)}, 'bad'); export function apply() {}`,
  317. })
  318. stagePackage(staged.profileDir, 'unknown', { main: 'export function apply() {}' })
  319. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  320. await manager.add('declared')
  321. await manager.add('unknown')
  322. const views = await manager.list()
  323. expect(views.find(view => view.name === 'declared')?.addable[0]).toMatchObject({ moduleName: 'declared', config: { custom: 1 } })
  324. expect(existsSync(marker)).toBe(false)
  325. for (const id of ['', 'nested:child']) {
  326. await expect(manager.addRow('declared', { kind: 'global' }, { id })).rejects.toMatchObject({ code: 'plugins/bad-request' })
  327. }
  328. const added = await manager.addRow('declared', { kind: 'global' }, { config: null })
  329. expect(readFileSync(added.file, 'utf8')).toContain('config: null')
  330. expect(views.find(view => view.name === 'unknown')?.kind).toBe('unknown')
  331. await expect(manager.addRow('unknown', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/not-enableable' })
  332. })
  333. it('reports plugins/unavailable without a profile runtime', async () => {
  334. const ctx = new Context()
  335. contexts.push(ctx)
  336. await ctx.plugin(Loader)
  337. await expect(managerOver(ctx).list()).rejects.toMatchObject({ code: 'plugins/unavailable', details: { reason: 'no profile runtime' } })
  338. })
  339. describe('list', () => {
  340. it('folds installed, enabled, declared and running facts into one view per package', async () => {
  341. const staged = await stageHome()
  342. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW, plugins: [{ name: './extra.js', title: 'Extra' }], files: { 'extra.js': 'export const name = "extra"\nexport function apply() {}\n' } })
  343. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  344. stagePackage(staged.profileDir, 'ext-plugin', { plugins: [{ name: '.' }], main: 'export const name = "p"\nexport function apply() {}\n' })
  345. addDependency(staged.profileDir, 'ext-bundle')
  346. addDependency(staged.profileDir, 'ext-lib')
  347. addDependency(staged.profileDir, 'ext-plugin')
  348. const { manager } = await bootProfile(staged)
  349. const views = await manager.list()
  350. expect(views.map(view => [view.name, view.kind, view.status, view.installed, view.enabled])).toEqual([
  351. ['ext-bundle', 'bundle', 'disabled', true, false],
  352. ['ext-lib', 'unknown', 'plain', true, false],
  353. ['ext-plugin', 'plugin', 'plain', true, false],
  354. ])
  355. const bundle = views[0]
  356. expect(bundle).toMatchObject({ version: '1.0.0', title: 'Title of ext-bundle', description: 'staged ext-bundle', liveReload: true })
  357. // Rows come from static declarations while the bundle is not composed, under the ids the patch declares.
  358. expect(bundle?.rows).toEqual([{ entryId: 'hello', rowId: 'hello', moduleName: 'cordis:good', enabled: true, phase: null }])
  359. expect(bundle?.addable).toEqual([{ moduleName: 'ext-bundle/extra.js', declaredName: './extra.js', title: 'Extra' }])
  360. })
  361. it('reads a composed bundle\'s rows from the live tree with their failures', async () => {
  362. const staged = await stageHome()
  363. stagePackage(staged.profileDir, 'ext-mixed', { patch: '- insert:\n - id: ok\n name: cordis:good\n - id: bad\n name: cordis:throws\n' })
  364. addDependency(staged.profileDir, 'ext-mixed')
  365. const manifest = manifestOf(staged.profileDir)
  366. manifest.dsh.profile.bundles.push('ext-mixed')
  367. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  368. const { manager } = await bootProfile(staged)
  369. const [view] = await manager.list()
  370. expect(view).toMatchObject({ name: 'ext-mixed', status: 'partial', enabled: true })
  371. expect(view?.reason).toContain('boom at apply')
  372. expect(view?.rows.map(row => [row.entryId, row.phase, row.failure?.stage])).toEqual([
  373. ['include:ok', 'active', undefined],
  374. ['include:bad', 'failed', 'activation'],
  375. ])
  376. })
  377. it('lists a bundle left out by an id conflict as failed, with the conflict as its row', async () => {
  378. const staged = await stageHome()
  379. stagePackage(staged.profileDir, 'ext-one', { patch: BUNDLE_ONE_ROW })
  380. addDependency(staged.profileDir, 'ext-one')
  381. stagePackage(staged.profileDir, 'ext-two', { patch: BUNDLE_ONE_ROW })
  382. addDependency(staged.profileDir, 'ext-two')
  383. const manifest = manifestOf(staged.profileDir)
  384. manifest.dsh.profile.bundles.push('ext-one', 'ext-two')
  385. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  386. const { ctx, manager } = await bootProfile(staged)
  387. const views = await manager.list()
  388. expect(entryIds(ctx)).toContain('include:hello')
  389. expect(views.find(view => view.name === 'ext-one')).toMatchObject({ status: 'running' })
  390. const two = views.find(view => view.name === 'ext-two')
  391. expect(two).toMatchObject({ status: 'failed', enabled: true })
  392. expect(two?.reason).toContain('already declared by ext-one')
  393. expect(two?.rows).toEqual([{
  394. entryId: 'conflict:ext-two:hello', rowId: 'hello', moduleName: 'cordis:good', enabled: true, phase: 'failed',
  395. failure: { stage: 'conflict', message: 'row "hello" is already declared by ext-one' },
  396. }])
  397. })
  398. it('does not import a disabled bundle and reports a stale layer as restart-required', async () => {
  399. const staged = await stageHome('startup')
  400. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  401. addDependency(staged.profileDir, 'ext-broken')
  402. stagePackage(staged.profileDir, 'ext-later', { patch: BUNDLE_ONE_ROW })
  403. addDependency(staged.profileDir, 'ext-later')
  404. const { manager } = await bootProfile(staged)
  405. // Enabled after boot on a startup-reload profile: the manifest says yes, the tree says no.
  406. const manifest = manifestOf(staged.profileDir)
  407. manifest.dsh.profile.bundles.push('ext-later')
  408. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  409. const views = await manager.list()
  410. expect(views.find(view => view.name === 'ext-broken')).toMatchObject({ status: 'disabled' })
  411. expect(views.find(view => view.name === 'ext-later')).toMatchObject({ status: 'restart-required', liveReload: false })
  412. })
  413. it('reports unapplied live selections as failures instead of requiring a restart', async () => {
  414. const staged = await stageHome()
  415. stagePackage(staged.profileDir, 'ext-live', { patch: BUNDLE_ONE_ROW })
  416. addDependency(staged.profileDir, 'ext-live')
  417. const initial = manifestOf(staged.profileDir)
  418. initial.dsh.profile.bundles = ['ext-live']
  419. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(initial))
  420. const { ctx, manager } = await bootProfile(staged)
  421. try {
  422. initial.dsh.profile.bundles = []
  423. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(initial))
  424. const [view] = await manager.list()
  425. expect(view).toMatchObject({ enabled: false, liveReload: true, status: 'failed', reason: expect.stringContaining('not been applied') as string })
  426. expect(view?.rows[0]?.phase).toBe('active')
  427. } finally {
  428. await ctx.fiber.dispose()
  429. rmSync(staged.home, { recursive: true, force: true })
  430. }
  431. })
  432. it('reports unreadable declarations and treats physical peer identity as advisory', async () => {
  433. const staged = await stageHome()
  434. stagePackage(staged.profileDir, 'ext-odd', { patch: BUNDLE_ONE_ROW })
  435. addDependency(staged.profileDir, 'ext-odd')
  436. stagePackage(staged.profileDir, 'ext-refused', { patch: BUNDLE_ONE_ROW })
  437. addDependency(staged.profileDir, 'ext-refused')
  438. const { manager } = await bootProfile(staged, {
  439. metadata: ({ packageName }) => {
  440. if (packageName === 'ext-odd') throw new Error('invalid declarations')
  441. return { packageName, kind: 'bundle', cordisSameCopy: false, rows: [], overrides: [], addable: [] }
  442. },
  443. })
  444. const views = await manager.list()
  445. expect(views.find(view => view.name === 'ext-odd')).toMatchObject({ status: 'not-enableable', reason: 'invalid declarations', cordisSameCopy: null, rows: [] })
  446. expect(views.find(view => view.name === 'ext-refused')).toMatchObject({ status: 'disabled', cordisSameCopy: false })
  447. await expect(manager.enable('ext-odd')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: 'cannot read package declarations: invalid declarations' } })
  448. await expect(manager.enable('ext-refused')).resolves.toMatchObject({ effect: 'live' })
  449. })
  450. it('reads a hand-written manifest: no dependencies, a template bundle, a ghost, and a builtin layer added after boot', async () => {
  451. const staged = await stageHome('startup')
  452. stagePackage(staged.profileDir, 'tpl', { patch: BUNDLE_ONE_ROW })
  453. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: ['tpl'], patchReload: 'startup' } } }))
  454. const { manager } = await bootProfile(staged)
  455. expect((await manager.list()).map(view => [view.name, view.status, view.installed, view.rows.length])).toEqual([['tpl', 'running', false, 1]])
  456. // A manifest with no dsh section at all knows no bundles.
  457. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web' }))
  458. expect(await manager.list()).toEqual([])
  459. // Bundles the manifest names after boot: one staged, one that resolves to nothing.
  460. stagePackage(staged.profileDir, 'tpl-later', { patch: BUNDLE_ONE_ROW })
  461. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: ['tpl', 'tpl-later', 'ghost'], patchReload: 'startup' } } }))
  462. const views = await manager.list()
  463. expect(views.map(view => [view.name, view.kind, view.status])).toEqual([
  464. ['tpl', 'bundle', 'running'],
  465. ['tpl-later', 'bundle', 'restart-required'],
  466. ['ghost', 'unknown', 'plain'],
  467. ])
  468. await expect(manager.uninstall('ghost')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  469. // Retrying takes the bundle out first, which a template bundle refuses.
  470. await expect(manager.retry('ghost')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  471. })
  472. it('folds disabled, user-disabled, and waiting rows', async () => {
  473. const staged = await stageHome()
  474. 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' })
  475. addDependency(staged.profileDir, 'ext-off')
  476. stagePackage(staged.profileDir, 'ext-waiting', { patch: '- insert:\n - id: w\n name: cordis:consumer\n' })
  477. addDependency(staged.profileDir, 'ext-waiting')
  478. const { manager } = await bootProfile(staged)
  479. await manager.enable('ext-off')
  480. await manager.enable('ext-waiting')
  481. await manager.setRowDisabled({ kind: 'global' }, 'b', true)
  482. const views = await manager.list()
  483. const off = views.find(view => view.name === 'ext-off')
  484. expect(off).toMatchObject({ status: 'running' })
  485. expect(off?.rows.map(row => [row.rowId, row.enabled, row.disabledBy, row.phase])).toEqual([
  486. ['a', false, 'composition', null],
  487. ['b', false, 'user', null],
  488. ])
  489. await manager.setRowDisabled({ kind: 'global' }, 'b', true)
  490. const waiting = views.find(view => view.name === 'ext-waiting')
  491. expect(waiting).toMatchObject({ status: 'failed', reason: expect.stringContaining('fixtureSvc') as string })
  492. expect(waiting?.rows).toEqual([expect.objectContaining({ entryId: 'include:w', phase: 'pending', failure: expect.objectContaining({ stage: 'inject-pending' }) as object })])
  493. // Another bundle's recorded failure is not this one's row.
  494. expect(off?.rows.some(row => row.failure !== undefined)).toBe(false)
  495. })
  496. it('reads anonymous and gated static patch rows', async () => {
  497. const staged = await stageHome()
  498. stagePackage(staged.profileDir, 'ext-anon', { patch: '- insert:\n - name: cordis:good\n - id: gated\n name: cordis:good\n disabled: true\n' })
  499. addDependency(staged.profileDir, 'ext-anon')
  500. stagePackage(staged.profileDir, 'installed-bundle', { patch: BUNDLE_ONE_ROW })
  501. addDependency(staged.profileDir, 'installed-bundle')
  502. const { manager } = await bootProfile(staged)
  503. const views = await manager.list()
  504. expect(views.find(view => view.name === 'ext-anon')?.rows).toEqual([
  505. { entryId: 'cordis:good', rowId: 'cordis:good', moduleName: 'cordis:good', enabled: true, phase: null },
  506. { entryId: 'gated', rowId: 'gated', moduleName: 'cordis:good', enabled: false, disabledBy: 'composition', phase: null },
  507. ])
  508. await manager.enable('installed-bundle')
  509. const installed = (await manager.list()).find(view => view.name === 'installed-bundle')
  510. expect(installed).toMatchObject({ status: 'running' })
  511. expect(installed?.rows.map(row => row.entryId)).toEqual(['include:hello'])
  512. await manager.disable('installed-bundle')
  513. expect((await manager.list()).find(view => view.name === 'installed-bundle')?.rows.map(row => row.entryId)).toEqual(['hello'])
  514. })
  515. })
  516. describe('install', () => {
  517. it('removes a package that is neither a bundle nor a plugin module and says why', async () => {
  518. const staged = await stageHome()
  519. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const answer = 42\n' })
  520. const calls: string[][] = []
  521. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  522. const result = await manager.add('ext-lib')
  523. expect(result).toMatchObject({
  524. installed: ['ext-lib'], plain: ['ext-lib'], installedOnly: [], removed: [],
  525. })
  526. expect(calls).toEqual([['pnpm', 'add', 'ext-lib']])
  527. expect(manifestOf(staged.profileDir).dependencies).toHaveProperty('ext-lib')
  528. expect((await manager.list()).some(view => view.name === 'ext-lib')).toBe(true)
  529. })
  530. it('removes an installed bundle whose row id another layer already owns', async () => {
  531. const staged = await stageHome()
  532. stagePackage(staged.profileDir, 'ext-one', { patch: BUNDLE_ONE_ROW })
  533. addDependency(staged.profileDir, 'ext-one')
  534. const manifest = manifestOf(staged.profileDir)
  535. manifest.dsh.profile.bundles.push('ext-one')
  536. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  537. stagePackage(staged.profileDir, 'ext-two', { patch: BUNDLE_ONE_ROW })
  538. const calls: string[][] = []
  539. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  540. const result = await manager.add('ext-two', { enable: true })
  541. expect(result).toMatchObject({
  542. installed: [], enabled: [], installedOnly: [],
  543. removed: [{ name: 'ext-two', reason: 'row "hello" is already declared by ext-one' }],
  544. })
  545. expect(calls).toEqual([['pnpm', 'add', 'ext-two'], ['pnpm', 'remove', 'ext-two']])
  546. expect(manifestOf(staged.profileDir)).toMatchObject({ dsh: { profile: { bundles: expect.not.arrayContaining(['ext-two']) as string[] } } })
  547. expect(manifestOf(staged.profileDir).dependencies).not.toHaveProperty('ext-two')
  548. })
  549. it('reports a bundle whose patch disappears after metadata discovery', async () => {
  550. const staged = await stageHome()
  551. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  552. const calls: string[][] = []
  553. const { manager } = await bootProfile(staged, {
  554. spawn: recordingPnpm(staged.profileDir, calls),
  555. metadata: (options) => {
  556. const metadata = readPackageMetadata(options)
  557. rmSync(join(staged.profileDir, 'node_modules/ext-new/cordis.patch.yml'))
  558. return metadata
  559. },
  560. })
  561. const result = await manager.add('ext-new')
  562. expect(result.removed).toEqual([{ name: 'ext-new', reason: expect.stringContaining('cordis.patch.yml') as string }])
  563. expect(calls).toEqual([['pnpm', 'add', 'ext-new'], ['pnpm', 'remove', 'ext-new']])
  564. })
  565. it('keeps an unexecuted package installed with its bundle disabled', async () => {
  566. const staged = await stageHome()
  567. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  568. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  569. const result = await manager.add('ext-broken')
  570. expect(result).toMatchObject({ installed: ['ext-broken'], removed: [] })
  571. expect((await manager.list()).find(view => view.name === 'ext-broken')).toMatchObject({ status: 'disabled' })
  572. })
  573. it('restores the manifest when pnpm fails after writing it', async () => {
  574. const staged = await stageHome()
  575. const manifestPath = join(staged.profileDir, 'package.json')
  576. const before = readFileSync(manifestPath, 'utf8')
  577. const { manager } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, (args) => {
  578. addDependency(staged.profileDir, args[1] ?? 'ext-ghost')
  579. return { code: 1, stderr: 'ERR_PNPM_FETCH_404\n' }
  580. }) })
  581. await expect(manager.add('ext-ghost')).rejects.toMatchObject({ code: 'plugins/install-failed' })
  582. expect(readFileSync(manifestPath, 'utf8')).toBe(before)
  583. })
  584. it('refuses a second mutation while one is still running', async () => {
  585. const staged = await stageHome()
  586. stagePackage(staged.profileDir, 'ext-slow', { patch: BUNDLE_ONE_ROW })
  587. let release = (): void => {}
  588. const gate = new Promise<void>((resolve) => { release = resolve })
  589. const spawn: SpawnLike = (_command, args) => {
  590. const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough; kill: () => boolean }
  591. child.stdout = new PassThrough()
  592. child.stderr = new PassThrough()
  593. child.kill = () => true
  594. void gate.then(() => {
  595. addDependency(staged.profileDir, args[1] ?? 'ext-slow')
  596. child.emit('close', 0)
  597. })
  598. return child as unknown as ChildProcess
  599. }
  600. const { manager } = await bootProfile(staged, { spawn })
  601. const first = manager.add('ext-slow')
  602. await expect(manager.enable('ext-slow')).rejects.toMatchObject({
  603. code: 'plugins/busy', details: { operation: 'enable', active: { operation: 'add', subject: 'ext-slow' } },
  604. })
  605. release()
  606. await expect(first).resolves.toMatchObject({ installed: ['ext-slow'] })
  607. // The lock is released with the run: the refused call now goes through.
  608. await expect(manager.enable('ext-slow')).resolves.toMatchObject({ changed: true })
  609. })
  610. it('refuses to change node_modules while a session is running', async () => {
  611. const staged = await stageHome()
  612. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  613. addDependency(staged.profileDir, 'ext-bundle')
  614. const calls: string[][] = []
  615. const { ctx, manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  616. ctx.provide('agents', { list: () => [{ status: 'running' }, { status: 'idle' }] } as never)
  617. await expect(manager.add('ext-new')).rejects.toMatchObject({ code: 'plugins/agents-running', details: { operation: 'add', running: 1 } })
  618. await expect(manager.uninstall('ext-bundle')).rejects.toMatchObject({ code: 'plugins/agents-running', details: { operation: 'uninstall' } })
  619. expect(calls).toEqual([])
  620. // Enabling recomposes the tree without touching node_modules.
  621. await expect(manager.enable('ext-bundle')).resolves.toMatchObject({ changed: true })
  622. })
  623. it('runs pnpm add, records the dependency and leaves its bundle disabled', async () => {
  624. const staged = await stageHome()
  625. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  626. const calls: string[][] = []
  627. const { manager, changes, log } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  628. const result = await manager.add('github:acme/ext-new')
  629. expect(calls).toEqual([['pnpm', 'add', 'github:acme/ext-new']])
  630. // The fake pnpm records the spec itself as the dependency name, which
  631. // resolves to nothing: an installed dependency whose declarations cannot be read.
  632. expect(result).toEqual({ installed: ['github:acme/ext-new'], removed: [], enabled: [], installedOnly: [], plain: ['github:acme/ext-new'], jobId: expect.any(String) as string })
  633. expect(log.map(chunk => [chunk.argv, chunk.cwd, chunk.stream, chunk.text, chunk.exitCode])).toEqual([
  634. [['pnpm', 'add', 'github:acme/ext-new'], staged.profileDir, 'stdout', '+ github:acme/ext-new 1.0.0\n', undefined],
  635. [['pnpm', 'add', 'github:acme/ext-new'], staged.profileDir, 'stdout', '', 0],
  636. ])
  637. expect(changes).toEqual([{ reason: 'install' }])
  638. })
  639. it('has pnpm colour its output and streams the escapes as they come', async () => {
  640. const staged = await stageHome()
  641. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  642. const colours: (string | undefined)[] = []
  643. const pnpm = fakePnpm(staged.profileDir, (args) => {
  644. addDependency(staged.profileDir, args[1] as string)
  645. return { code: 0, stdout: '\u001b[32m+\u001b[39m ext-new \u001b[90m1.0.0\u001b[39m\n' }
  646. })
  647. const { manager, log } = await bootProfile(staged, {
  648. spawn: (command, args, options) => {
  649. colours.push(options.env?.FORCE_COLOR)
  650. return pnpm(command, args, options)
  651. },
  652. })
  653. await manager.add('ext-new')
  654. expect(colours).toEqual(['1'])
  655. expect(log[0]?.text).toBe('\u001b[32m+\u001b[39m ext-new \u001b[90m1.0.0\u001b[39m\n')
  656. })
  657. it('reconciles by the installed name, and enables the new bundle when asked', async () => {
  658. const staged = await stageHome()
  659. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  660. const { ctx, manager, changes } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  661. const result = await manager.add('ext-new', { enable: true })
  662. expect(result).toMatchObject({ installed: ['ext-new'], enabled: ['ext-new'], installedOnly: [], plain: [] })
  663. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-new'])
  664. expect(entryIds(ctx)).toEqual(expect.arrayContaining(['include:hello']))
  665. expect(changes.map(change => change.reason)).toEqual(['enable', 'install'])
  666. expect((await manager.list()).find(view => view.name === 'ext-new')?.status).toBe('running')
  667. })
  668. it('reports a plugin module as plain and uninstalls it', async () => {
  669. const staged = await stageHome()
  670. stagePackage(staged.profileDir, 'ext-lib', { main: 'export function apply() {}\n' })
  671. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  672. expect(await manager.add('ext-lib')).toMatchObject({ installed: ['ext-lib'], plain: ['ext-lib'], installedOnly: [], removed: [] })
  673. expect((await manager.list()).find(view => view.name === 'ext-lib')?.status).toBe('plain')
  674. await manager.uninstall('ext-lib')
  675. expect((await manager.list()).some(view => view.name === 'ext-lib')).toBe(false)
  676. })
  677. it('installs into a manifest that declares no dependencies yet', async () => {
  678. const staged = await stageHome()
  679. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: [], patchReload: 'live' } } }))
  680. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  681. const { manager } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => {
  682. const path = join(staged.profileDir, 'package.json')
  683. const manifest = JSON.parse(readFileSync(path, 'utf8')) as Record<string, unknown>
  684. writeFileSync(path, JSON.stringify({ ...manifest, dependencies: { 'ext-new': '1.0.0' } }))
  685. return { code: 0 }
  686. }) })
  687. expect(await manager.add('ext-new')).toMatchObject({ installed: ['ext-new'], installedOnly: ['ext-new'] })
  688. })
  689. it('fails loud on a non-zero exit, a spawn error, a timeout, and an empty spec', async () => {
  690. const staged = await stageHome()
  691. const exits = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => ({ code: 1, stderr: 'ERR_PNPM_NO_MATCHING_VERSION\n' })) })
  692. await expect(exits.manager.add('nope')).rejects.toMatchObject({
  693. code: 'plugins/install-failed', details: { spec: 'nope', exitCode: 1, log: 'ERR_PNPM_NO_MATCHING_VERSION\n' },
  694. })
  695. expect(exits.log.at(-1)).toMatchObject({ exitCode: 1 })
  696. await expect(exits.manager.add(' ')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  697. const erroringHome = await stageHome()
  698. const erroring = await bootProfile(erroringHome, { spawn: fakePnpm(erroringHome.profileDir, () => ({ code: null, error: 'spawn pnpm ENOENT' })) })
  699. await expect(erroring.manager.add('x')).rejects.toMatchObject({ code: 'plugins/install-failed', details: { exitCode: null } })
  700. expect(erroring.log.some(chunk => chunk.text.includes('ENOENT'))).toBe(true)
  701. const hangingHome = await stageHome()
  702. const hanging = await bootProfile(hangingHome, { spawn: fakePnpm(hangingHome.profileDir, () => ({ code: null, hang: true })) })
  703. await expect(hanging.manager.add('x')).rejects.toMatchObject({ code: 'plugins/install-failed' })
  704. expect(hanging.log.some(chunk => chunk.text.includes('timed out'))).toBe(true)
  705. })
  706. it('keeps only the tail of a long log in the failure', async () => {
  707. const staged = await stageHome()
  708. const { manager } = await bootProfile(staged, {
  709. spawn: fakePnpm(staged.profileDir, () => ({ code: 2, stdout: 'a'.repeat(300), stderr: 'b'.repeat(300) })),
  710. }, { installLogTailBytes: 256 })
  711. await expect(manager.add('x')).rejects.toMatchObject({ details: { log: 'b'.repeat(300) } })
  712. })
  713. })
  714. describe('enable, disable, and retry', () => {
  715. it('composes an installed bundle live, and takes it out again', async () => {
  716. const staged = await stageHome()
  717. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  718. addDependency(staged.profileDir, 'ext-bundle')
  719. const { ctx, manager, changes } = await bootProfile(staged)
  720. expect(await manager.enable('ext-bundle')).toEqual({ changed: true, effect: 'live' })
  721. expect(entryIds(ctx)).toContain('include:hello')
  722. expect(await manager.enable('ext-bundle')).toEqual({ changed: false, effect: 'live' })
  723. expect(await manager.disable('ext-bundle')).toEqual({ changed: true, effect: 'live' })
  724. expect(entryIds(ctx)).not.toContain('include:hello')
  725. expect(await manager.disable('ext-bundle')).toEqual({ changed: false, effect: 'live' })
  726. expect(changes.map(change => [change.reason, change.packageName])).toEqual([
  727. ['enable', 'ext-bundle'], ['enable', 'ext-bundle'], ['disable', 'ext-bundle'], ['disable', 'ext-bundle'],
  728. ])
  729. })
  730. it('only writes the manifest on a startup-reload profile', async () => {
  731. const staged = await stageHome('startup')
  732. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  733. addDependency(staged.profileDir, 'ext-bundle')
  734. const { ctx, manager } = await bootProfile(staged)
  735. expect(await manager.enable('ext-bundle')).toEqual({ changed: true, effect: 'restart' })
  736. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-bundle'])
  737. expect(entryIds(ctx)).not.toContain('include:hello')
  738. expect(await manager.disable('ext-bundle')).toEqual({ changed: true, effect: 'restart' })
  739. })
  740. it('refuses what cannot be enabled or disabled', async () => {
  741. const staged = await stageHome()
  742. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  743. addDependency(staged.profileDir, 'ext-lib')
  744. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  745. addDependency(staged.profileDir, 'ext-broken')
  746. const { manager } = await bootProfile(staged)
  747. await expect(manager.enable('absent')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  748. await expect(manager.enable('ext-lib')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: expect.stringContaining('declares no dsh.bundle') as string } })
  749. await expect(manager.enable('ext-broken')).resolves.toMatchObject({ effect: 'live' })
  750. // A template bundle is not a dependency and cannot be disabled.
  751. const manifest = manifestOf(staged.profileDir)
  752. manifest.dsh.profile.bundles.push('template')
  753. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  754. await expect(manager.disable('template')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  755. await expect(manager.retry('ext-lib')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  756. })
  757. it('retains live enablement and reports failed rows after startup', async () => {
  758. const staged = await stageHome()
  759. stagePackage(staged.profileDir, 'ext-fatal', { patch: '- insert:\n - id: bad\n name: cordis:throws\n' })
  760. addDependency(staged.profileDir, 'ext-fatal')
  761. stagePackage(staged.profileDir, 'ext-fine', { patch: BUNDLE_ONE_ROW })
  762. addDependency(staged.profileDir, 'ext-fine')
  763. const { ctx, manager } = await bootProfile(staged)
  764. await manager.enable('ext-fine')
  765. const result = await manager.enable('ext-fatal')
  766. expect(result.effect).toBe('live')
  767. expect(result.issues?.[0]?.message).toContain('boom at apply')
  768. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-fine', 'ext-fatal'])
  769. expect(entryIds(ctx)).toContain('include:hello')
  770. expect(entryIds(ctx)).toContain('include:bad')
  771. })
  772. it('retries a bundle by removing and re-adding its layer', async () => {
  773. const staged = await stageHome()
  774. stagePackage(staged.profileDir, 'ext-flaky', { patch: '- insert:\n - id: once\n name: cordis:flaky\n' })
  775. addDependency(staged.profileDir, 'ext-flaky')
  776. flakyCalls = 0
  777. const { manager, changes } = await bootProfile(staged)
  778. await manager.enable('ext-flaky')
  779. expect((await manager.list())[0]).toMatchObject({ status: 'failed', reason: expect.stringContaining('flaky first start') as string })
  780. expect(await manager.retry('ext-flaky')).toEqual({ changed: true, effect: 'live' })
  781. expect((await manager.list())[0]).toMatchObject({ status: 'running' })
  782. expect(changes.map(change => change.reason)).toEqual(['enable', 'disable', 'enable', 'retry'])
  783. })
  784. })
  785. describe('rows in user layers', () => {
  786. it('adds, disables, re-enables, and removes a row in the live global layer', async () => {
  787. const staged = await stageHome()
  788. stagePackage(staged.profileDir, '@acme/ext-plugin', { plugins: [{ name: '.' }], main: 'export const name = "p"\nexport function apply() {}\n' })
  789. addDependency(staged.profileDir, '@acme/ext-plugin')
  790. const { ctx, manager, changes } = await bootProfile(staged)
  791. const added = await manager.addRow('@acme/ext-plugin', { kind: 'global' })
  792. expect(added).toEqual({ target: { kind: 'global' }, rowId: 'acme/ext-plugin', file: join(staged.profileDir, 'cordis.patch.yml') })
  793. expect(readFileSync(added.file, 'utf8')).toBe('- insert:\n - id: acme/ext-plugin\n name: "@acme/ext-plugin"\n config: {}\n')
  794. // Composed live: the row is in the tree, though its module cannot import from the temp home.
  795. expect(entryIds(ctx)).toContain('include:acme/ext-plugin')
  796. await expect(manager.addRow('@acme/ext-plugin', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/row-conflict' })
  797. await manager.setRowDisabled({ kind: 'global' }, 'acme/ext-plugin', true)
  798. expect(ctx.loader.resolve('include:acme/ext-plugin')?.disabled).toBe(true)
  799. expect(readFileSync(added.file, 'utf8')).toContain('- id: acme/ext-plugin\n disabled: true\n')
  800. await manager.setRowDisabled({ kind: 'global' }, 'acme/ext-plugin', false)
  801. expect(ctx.loader.resolve('include:acme/ext-plugin')?.disabled).toBe(false)
  802. expect(readFileSync(added.file, 'utf8')).not.toContain('disabled')
  803. await manager.removeRow({ kind: 'global' }, 'acme/ext-plugin')
  804. expect(entryIds(ctx)).not.toContain('include:acme/ext-plugin')
  805. await expect(manager.removeRow({ kind: 'global' }, 'acme/ext-plugin')).rejects.toMatchObject({ code: 'plugins/bad-request' })
  806. expect(changes.map(change => change.reason)).toEqual(['row', 'row', 'row', 'row'])
  807. })
  808. it('detects a conflict through the layer file when the tree was not recomposed', async () => {
  809. const staged = await stageHome('startup')
  810. stagePackage(staged.profileDir, 'ext-plugin', { plugins: [{ name: '.' }], main: 'export const name = "p"\nexport function apply() {}\n' })
  811. addDependency(staged.profileDir, 'ext-plugin')
  812. const { ctx, manager } = await bootProfile(staged)
  813. await manager.addRow('ext-plugin', { kind: 'global' })
  814. expect(entryIds(ctx)).not.toContain('include:ext-plugin')
  815. await expect(manager.addRow('ext-plugin', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/row-conflict' })
  816. })
  817. it('adds a declared addable module with its default config and an explicit id, and refuses the rest', async () => {
  818. const staged = await stageHome()
  819. stagePackage(staged.profileDir, 'ext-bundle', {
  820. patch: BUNDLE_ONE_ROW,
  821. plugins: [
  822. { name: './tools/sql.js', title: 'SQL', config: { dsn: 'sqlite://' } },
  823. { name: './missing.js' },
  824. ],
  825. files: { 'tools/sql.js': 'export const name = "sql"\nexport function apply() {}\n' },
  826. })
  827. addDependency(staged.profileDir, 'ext-bundle')
  828. const { manager } = await bootProfile(staged)
  829. const added = await manager.addRow('ext-bundle', { kind: 'global' }, { module: './tools/sql.js', id: 'sql' })
  830. expect(added.rowId).toBe('sql')
  831. expect(readFileSync(added.file, 'utf8')).toContain('- id: sql\n name: ext-bundle/tools/sql.js\n config:\n dsn: sqlite://\n')
  832. await expect(manager.addRow('ext-bundle', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/not-enableable' })
  833. await expect(manager.addRow('ext-bundle', { kind: 'global' }, { module: './missing.js' })).resolves.toMatchObject({ rowId: 'ext-bundle/missing.js' })
  834. await expect(manager.addRow('absent', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/not-installed' })
  835. })
  836. it('writes a preset\'s layer through the roster, and refuses a preset target without one', async () => {
  837. const staged = await stageHome()
  838. stagePackage(staged.profileDir, 'ext-plugin', { plugins: [{ name: '.' }], main: 'export const name = "p"\nexport function apply() {}\n' })
  839. addDependency(staged.profileDir, 'ext-plugin')
  840. const { ctx, manager } = await bootProfile(staged)
  841. await expect(manager.addRow('ext-plugin', { kind: 'preset', preset: 'standard' })).rejects.toMatchObject({ code: 'plugins/unavailable' })
  842. const overlay = join(staged.home, '.agent-presets', 'standard', 'cordis.patch.yml')
  843. const roster = {
  844. overlayPathFor: (id: string) => Promise.resolve(join(staged.home, '.agent-presets', id, 'cordis.patch.yml')),
  845. compositionInventory: () => Promise.resolve([{ id: 'standard', rows: [{ entryId: 'tool-web' }] }]),
  846. list: () => Promise.resolve([{ id: 'standard', overlayPath: existsSync(overlay) ? overlay : undefined }]),
  847. }
  848. ctx.provide('agentPresets', roster)
  849. // No layer exists yet: nothing references the package.
  850. expect((await manager.dependents('ext-plugin')).references).toEqual([])
  851. // A preset the inventory does not know has no rows to conflict with.
  852. const elsewhere = await manager.addRow('ext-plugin', { kind: 'preset', preset: 'other' })
  853. expect(elsewhere.file).toBe(join(staged.home, '.agent-presets', 'other', 'cordis.patch.yml'))
  854. const added = await manager.addRow('ext-plugin', { kind: 'preset', preset: 'standard' })
  855. expect(added).toEqual({ target: { kind: 'preset', preset: 'standard' }, rowId: 'ext-plugin', file: overlay })
  856. await expect(manager.addRow('ext-plugin', { kind: 'preset', preset: 'standard' }, { id: 'tool-web' })).rejects.toMatchObject({ code: 'plugins/row-conflict' })
  857. await manager.setRowDisabled({ kind: 'preset', preset: 'standard' }, 'tool-web', true)
  858. expect(readFileSync(overlay, 'utf8')).toBe('- insert:\n - id: ext-plugin\n name: ext-plugin\n config: {}\n- id: tool-web\n disabled: true\n')
  859. // The preset's layer is not part of the host tree.
  860. expect(entryIds(ctx)).not.toContain('include:ext-plugin')
  861. const dependents = await manager.dependents('ext-plugin')
  862. expect(dependents.references).toEqual([{ target: { kind: 'preset', preset: 'standard' }, rowId: 'ext-plugin', moduleName: 'ext-plugin' }])
  863. await manager.removeRow({ kind: 'preset', preset: 'standard' }, 'ext-plugin')
  864. expect(readFileSync(overlay, 'utf8')).toBe('- id: tool-web\n disabled: true\n')
  865. })
  866. })
  867. describe('dependents and uninstall', () => {
  868. it('names the services other rows inject and the user-layer rows that reference the package', async () => {
  869. const staged = await stageHome()
  870. // Beside the provider: a row nobody injects, and a row switched off (no fiber to read).
  871. 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' })
  872. addDependency(staged.profileDir, 'ext-provider')
  873. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), [
  874. '- insert:',
  875. ' - name: ext-provider/anonymous.js',
  876. ' disabled: true',
  877. ' - id: ref',
  878. ' name: ext-provider/tools/x.js',
  879. ' disabled: true',
  880. ' - id: grp',
  881. ' name: cordis:group',
  882. ' group: true',
  883. ' config:',
  884. ' - id: nested-ref',
  885. ' name: ext-provider',
  886. ' disabled: true',
  887. '',
  888. ].join('\n'))
  889. const { manager, runtime } = await bootProfile(staged)
  890. await manager.enable('ext-provider')
  891. // A built-in row injecting the bundle's service, composed once the
  892. // provider is up (a boot would refuse a row left waiting).
  893. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), `${readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')}- insert:\n - id: needs-svc\n name: cordis:consumer\n`)
  894. await runtime.recompose()
  895. const dependents = await manager.dependents('ext-provider')
  896. expect(dependents.services).toEqual([{ service: 'fixtureSvc', providedBy: 'include:svc', injectedBy: ['include:needs-svc'] }])
  897. expect(dependents.references.map(reference => reference.rowId)).toEqual(['ref', 'nested-ref'])
  898. expect(dependents.references[0]).toEqual({ target: { kind: 'global' }, rowId: 'ref', moduleName: 'ext-provider/tools/x.js' })
  899. })
  900. it('ignores an unreadable user layer while collecting references', async () => {
  901. const staged = await stageHome()
  902. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  903. addDependency(staged.profileDir, 'ext-lib')
  904. const { manager } = await bootProfile(staged)
  905. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'a: [\n')
  906. expect((await manager.dependents('ext-lib')).references).toEqual([])
  907. })
  908. it('disables, drops references, removes the package, and forgets its probe', async () => {
  909. const staged = await stageHome()
  910. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW, plugins: [{ name: './extra.js' }], files: { 'extra.js': 'export function apply() {}\n' } })
  911. addDependency(staged.profileDir, 'ext-bundle')
  912. const calls: string[][] = []
  913. const { ctx, manager, changes } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  914. await manager.enable('ext-bundle')
  915. await manager.addRow('ext-bundle', { kind: 'global' }, { module: './extra.js' })
  916. expect(entryIds(ctx)).toEqual(expect.arrayContaining(['include:hello', 'include:ext-bundle/extra.js']))
  917. await manager.uninstall('ext-bundle')
  918. expect(calls).toEqual([['pnpm', 'remove', 'ext-bundle']])
  919. expect(manifestOf(staged.profileDir)).toMatchObject({ dependencies: {}, dsh: { profile: { bundles: [] } } })
  920. expect(entryIds(ctx)).not.toContain('include:hello')
  921. expect(entryIds(ctx)).not.toContain('include:ext-bundle/extra.js')
  922. expect(readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')).toBe('[]\n')
  923. expect(changes.map(change => change.reason)).toEqual(['enable', 'row', 'disable', 'uninstall'])
  924. await expect(manager.uninstall('ext-bundle')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  925. })
  926. })
  927. })