2026-08-11 06:46:41 -07:00
|
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
|
|
|
import { Context, type Plugin } from '@deepseek-ai/cordis'
|
|
|
|
|
import Loader from '@deepseek-ai/cordis-plugin-loader'
|
2026-08-13 00:36:22 +08:00
|
|
|
import { remoteMethods } from '@deepseek-ai/dsh-typert-protocol'
|
2026-08-14 13:53:24 +08:00
|
|
|
import PluginInventoryGateway, { type PluginEntryId } from '../src/index.ts'
|
2026-08-11 06:46:41 -07:00
|
|
|
|
|
|
|
|
const contexts: Context[] = []
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const activePlugin: Plugin.Function = () => {}
|
|
|
|
|
const pendingPlugin: Plugin.Object = {
|
|
|
|
|
inject: ['neverReady'],
|
|
|
|
|
apply() {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function harness(): Promise<{
|
|
|
|
|
ctx: Context
|
2026-08-13 00:36:22 +08:00
|
|
|
inventory: PluginInventoryGateway
|
2026-08-11 06:46:41 -07:00
|
|
|
}> {
|
|
|
|
|
const ctx = new Context()
|
|
|
|
|
contexts.push(ctx)
|
|
|
|
|
await ctx.plugin(Loader)
|
|
|
|
|
ctx.loader.builtins.active = activePlugin
|
|
|
|
|
ctx.loader.builtins.pending = pendingPlugin
|
2026-08-14 14:10:38 +08:00
|
|
|
// `cordis:` builtins the toggle tests create; they resolve without a package install.
|
|
|
|
|
ctx.loader.builtins['user-toggleable'] = activePlugin
|
|
|
|
|
ctx.loader.builtins['host-webserver'] = activePlugin
|
2026-08-13 00:36:22 +08:00
|
|
|
await ctx.plugin(PluginInventoryGateway)
|
|
|
|
|
const inventory = ctx.get('pluginInventory') as PluginInventoryGateway
|
2026-08-11 06:46:41 -07:00
|
|
|
return { ctx, inventory }
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-13 00:36:22 +08:00
|
|
|
describe('PluginInventoryGateway', () => {
|
2026-08-11 06:46:41 -07:00
|
|
|
it('publishes one direct list method under the pluginInventory namespace', async () => {
|
|
|
|
|
const { inventory } = await harness()
|
2026-08-13 00:36:22 +08:00
|
|
|
expect(inventory.typertRemote).toMatchObject({
|
2026-08-11 06:46:41 -07:00
|
|
|
serviceKey: 'pluginInventory',
|
|
|
|
|
namespace: 'pluginInventory',
|
|
|
|
|
})
|
|
|
|
|
expect(remoteMethods(inventory)).toEqual([
|
|
|
|
|
{ method: 'list', invocation: { kind: 'direct' } },
|
2026-08-14 13:53:24 +08:00
|
|
|
{ method: 'setEnabled', invocation: { kind: 'direct' } },
|
2026-08-11 06:46:41 -07:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-14 14:10:38 +08:00
|
|
|
it('setEnabled toggles a user-toggleable Loader entry live', async () => {
|
2026-08-14 13:53:24 +08:00
|
|
|
const { ctx, inventory } = await harness()
|
2026-08-14 14:10:38 +08:00
|
|
|
const id = await ctx.loader.create({ name: 'cordis:user-toggleable' }) as PluginEntryId
|
2026-08-14 13:53:24 +08:00
|
|
|
await inventory.setEnabled(id, false)
|
|
|
|
|
expect(inventory.list().entries.find(entry => entry.entryId === id)).toEqual({
|
|
|
|
|
entryId: id,
|
2026-08-14 14:10:38 +08:00
|
|
|
moduleName: 'cordis:user-toggleable',
|
2026-08-14 13:53:24 +08:00
|
|
|
enabled: false,
|
2026-08-14 14:10:38 +08:00
|
|
|
protected: false,
|
2026-08-14 13:53:24 +08:00
|
|
|
fiberPhase: null,
|
|
|
|
|
})
|
|
|
|
|
await inventory.setEnabled(id, true)
|
|
|
|
|
expect(inventory.list().entries.find(entry => entry.entryId === id)?.enabled).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-14 14:10:38 +08:00
|
|
|
it('setEnabled refuses a required plugin', async () => {
|
|
|
|
|
const { ctx, inventory } = await harness()
|
|
|
|
|
const id = await ctx.loader.create({ name: 'cordis:host-webserver' }) as PluginEntryId
|
|
|
|
|
expect(inventory.list().entries.find(entry => entry.entryId === id)?.protected).toBe(true)
|
|
|
|
|
await expect(inventory.setEnabled(id, false)).rejects.toThrow(/required by the application/)
|
|
|
|
|
expect(inventory.list().entries.find(entry => entry.entryId === id)?.enabled).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-11 06:46:41 -07:00
|
|
|
it('projects current non-group Loader entries without a second cache', async () => {
|
|
|
|
|
const { ctx, inventory } = await harness()
|
|
|
|
|
const activeId = await ctx.loader.create({ name: 'cordis:active' })
|
|
|
|
|
const pendingId = await ctx.loader.create({ name: 'cordis:pending' })
|
|
|
|
|
const disabledId = await ctx.loader.create({
|
|
|
|
|
name: 'cordis:not-installed',
|
|
|
|
|
disabled: true,
|
|
|
|
|
})
|
|
|
|
|
await ctx.loader.create({ name: 'cordis:active', group: true })
|
|
|
|
|
|
2026-08-12 04:23:03 -07:00
|
|
|
const snapshot = inventory.list()
|
|
|
|
|
expect(snapshot.entries).toHaveLength(3)
|
|
|
|
|
expect(snapshot.entries).toEqual(expect.arrayContaining([
|
|
|
|
|
{
|
|
|
|
|
entryId: activeId,
|
|
|
|
|
moduleName: 'cordis:active',
|
|
|
|
|
enabled: true,
|
2026-08-14 14:10:38 +08:00
|
|
|
protected: true,
|
2026-08-12 04:23:03 -07:00
|
|
|
fiberPhase: 'active',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
entryId: pendingId,
|
|
|
|
|
moduleName: 'cordis:pending',
|
|
|
|
|
enabled: true,
|
2026-08-14 14:10:38 +08:00
|
|
|
protected: true,
|
2026-08-12 04:23:03 -07:00
|
|
|
fiberPhase: 'pending',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
entryId: disabledId,
|
|
|
|
|
moduleName: 'cordis:not-installed',
|
|
|
|
|
enabled: false,
|
2026-08-14 14:10:38 +08:00
|
|
|
protected: true,
|
2026-08-12 04:23:03 -07:00
|
|
|
fiberPhase: null,
|
|
|
|
|
},
|
|
|
|
|
]))
|
2026-08-11 06:46:41 -07:00
|
|
|
|
|
|
|
|
await ctx.loader.update(activeId, { disabled: true })
|
|
|
|
|
expect(inventory.list().entries.find(entry => entry.entryId === activeId)).toEqual({
|
|
|
|
|
entryId: activeId,
|
2026-08-11 08:54:29 -07:00
|
|
|
moduleName: 'cordis:active',
|
2026-08-11 06:46:41 -07:00
|
|
|
enabled: false,
|
2026-08-14 14:10:38 +08:00
|
|
|
protected: true,
|
2026-08-11 06:46:41 -07:00
|
|
|
fiberPhase: null,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await ctx.loader.remove(pendingId)
|
|
|
|
|
expect(inventory.list().entries.some(entry => entry.entryId === pendingId)).toBe(false)
|
|
|
|
|
})
|
|
|
|
|
})
|