import { mkdir, mkdtemp, realpath, rm, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; import { readPluginConfigurationMap } from '@zhin.js/plugin-runtime'; import { APPLICATION_PLUGIN_RESERVED_KEYS, NodePackageResolver, ProjectGraphService, parsePackageJson, pluginInstanceConfigPath, readApplicationPluginDeclaration, type ManifestWarning, } from '../src/index.js'; const temporary: string[] = []; afterEach(async () => { await Promise.all(temporary.splice(0).map(path => rm(path, { recursive: true }))); }); describe('application Plugin declarations', () => { it('requires an explicit v2 application and rejects old and unknown formats with migration guidance', () => { for (const document of [{}, { plugins: {} }, { configVersion: 1 }, { configVersion: 3 }]) { expect(() => readApplicationPluginDeclaration(document)).toThrow('docs/migrations/plugin-instance-declarations.md'); } expect(() => readPluginConfigurationMap({})).toThrow('configVersion must be 2'); }); it('separates package declarations, disabled intent and instance business values', () => { const document = { configVersion: 2, plugins: { home: '@test/reminder', work: '@test/reminder' }, disable_plugins: ['home'], home: {}, work: { channel: 'work' }, }; expect(readApplicationPluginDeclaration(document)).toEqual({ version: 2, disabled: ['home'], instances: [ { instanceKey: 'home', package: '@test/reminder', enabled: false }, { instanceKey: 'work', package: '@test/reminder', enabled: true }, ], }); expect(readPluginConfigurationMap(document)).toEqual({ home: {}, work: { channel: 'work' } }); expect(pluginInstanceConfigPath(document, 'work')).toEqual(['work']); }); it('uses source order for numeric aliases and refuses stale ordering metadata', () => { const document = { configVersion: 2, plugins: { '10': './plugins/a', '2': './plugins/a' } }; expect(readApplicationPluginDeclaration(document, 'fixture', ['10', '2']).instances.map(item => item.instanceKey)).toEqual(['10', '2']); expect(() => readApplicationPluginDeclaration(document, 'fixture', ['10'])).toThrow('source order'); }); it('rejects collisions, orphan business blocks, invalid references and disabled aliases before package loading', () => { expect(APPLICATION_PLUGIN_RESERVED_KEYS).toEqual(expect.arrayContaining(['ai', 'database', 'plugin', 'constructor', 'plugins'])); for (const key of ['ai', 'database', 'plugin', 'constructor', 'prototype']) { expect(() => readApplicationPluginDeclaration({ configVersion: 2, plugins: { [key]: '@test/x' } })).toThrow('reserved'); } expect(readApplicationPluginDeclaration({ configVersion: 2, plugins: { root: '@test/x' } }).instances[0]?.instanceKey).toBe('root'); for (const plugins of [null, [], { x: {} }, { x: './plugins/../escape' }, { x: './' }]) { expect(() => readApplicationPluginDeclaration({ configVersion: 2, plugins })).toThrow(); } expect(() => readApplicationPluginDeclaration({ configVersion: 2, orphan: {} })).toThrow('undeclared'); expect(() => readApplicationPluginDeclaration({ configVersion: 2, plugins: { x: '@test/x' }, x: null })).toThrow('business configuration object'); expect(() => readApplicationPluginDeclaration({ configVersion: 2, disable_plugins: ['missing'] })).toThrow('undeclared'); expect(() => readApplicationPluginDeclaration({ configVersion: 2, plugins: { x: '@test/x' }, disable_plugins: ['x', 'x'] })).toThrow('duplicate'); expect(() => readApplicationPluginDeclaration({ configVersion: 2, plugins: { x: { package: '@test/x', optional: true } } })).toThrow(); }); it('builds only explicit root instances and does not resolve disabled missing packages', async () => { const root = await fixture({ name: '@test/root', dependencies: { '@test/child': 'workspace:*' } }); await json(join(root, 'plugins/child/package.json'), { name: '@test/child', zhin: { ...plugin, plugins: [{ package: '@test/not-installed', instanceKey: 'hidden' }] }, }); const graph = await new ProjectGraphService(await NodePackageResolver.create(root)).inspect(root, { configVersion: 2, plugins: { first: '@test/child', second: '@test/child', missing: '@test/uninstalled' }, disable_plugins: ['missing'], missing: { incomplete: true }, }); expect(graph.root.children.map(node => node.id)).toEqual(['root/first', 'root/second']); expect(graph.root.children.every(node => node.children.length === 0)).toBe(true); expect(graph.root.children[0]?.package).toBe(graph.root.children[1]?.package); }); it('keeps platform Feature inheritance while ignoring facade instance defaults', async () => { const root = await fixture({ name: '@test/root', dependencies: { 'zhin.js': 'workspace:*' } }); await json(join(root, 'packages/facade/package.json'), { name: 'zhin.js', dependencies: { '@zhin.js/core': 'workspace:*' }, zhin: { ...plugin, plugins: [{ package: '@test/not-installed', instanceKey: 'implicit' }] }, }); await json(join(root, 'packages/core/package.json'), { name: '@zhin.js/core', dependencies: { '@test/feature': 'workspace:*' }, zhin: { ...plugin, features: [{ package: '@test/feature', api: '^1.0.0' }] }, }); await json(join(root, 'packages/feature/package.json'), { name: '@test/feature', zhin: { protocol: 1, type: 'feature', entry: './provider.js', featureApi: '1.0.0' }, }); const graph = await new ProjectGraphService(await NodePackageResolver.create(root)).inspect(root, { configVersion: 2 }); expect(graph.root.children).toEqual([]); expect(graph.root.features.map(item => item.package.name)).toEqual(['@test/feature']); }); it('rejects old Root mounts and legacy disabled state instead of merging authorities', async () => { const root = await fixture({ name: '@test/root', zhin: { ...plugin, plugins: [{ package: '@test/child', instanceKey: 'child' }] } }); await expect(new ProjectGraphService(await NodePackageResolver.create(root)).inspect(root, { configVersion: 2 })) .rejects.toThrow('docs/migrations/plugin-instance-declarations.md'); await expect(new ProjectGraphService(await NodePackageResolver.create(root), { disabledPluginInstanceKeys: ['child'] }) .inspect(root, { configVersion: 2 })).rejects.toThrow('disable_plugins'); }); it('validates only the two new static display metadata fields without changing the authoring contract', () => { const parsed = parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayName: 'Label', icon: 'Bell' } } }, 'fixture'); expect(parsed.zhin).toMatchObject({ metadata: { displayName: 'Label', icon: 'Bell' } }); expect(() => parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { order: 1 } } }, 'fixture')).toThrow('metadata.order'); }); it('accepts locale-keyed display names and rejects other locales or empty text', () => { const parsed = parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayName: 'Repeater', displayNameI18n: { 'zh-CN': '复读机', en: 'Repeater' } } } }, 'fixture'); expect(parsed.zhin).toMatchObject({ metadata: { displayName: 'Repeater', displayNameI18n: { 'zh-CN': '复读机', en: 'Repeater' } } }); expect(() => parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayNameI18n: { ja: 'リピーター' } } } }, 'fixture')).toThrow('displayNameI18n.ja'); expect(() => parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayNameI18n: { en: '' } } } }, 'fixture')).toThrow('displayNameI18n.en'); expect(() => parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayNameI18n: 'Repeater' } } }, 'fixture')).toThrow('displayNameI18n'); }); it('accepts marketplace descriptions and a known category, validated like display names', () => { const metadata = { descriptionI18n: { 'zh-CN': '群内复读', en: 'Repeats group messages' }, category: 'entertainment', }; const parsed = parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata } }, 'fixture'); expect(parsed.zhin).toMatchObject({ metadata }); const reject = (value: Record) => () => parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: value } }, 'fixture'); expect(reject({ descriptionI18n: { fr: 'Répéteur' } })).toThrow('descriptionI18n.fr'); expect(reject({ descriptionI18n: { 'zh-CN': '' } })).toThrow('descriptionI18n.zh-CN'); expect(reject({ descriptionI18n: 'Repeats' })).toThrow('descriptionI18n must be an object'); expect(reject({ category: 'fun' })).toThrow('metadata.category must be one of adapter'); expect(reject({ category: 'util' })).toThrow('metadata.category'); expect(reject({ category: ['game'] })).toThrow('metadata.category'); }); it('lenient (instance load) mode warns and drops invalid marketplace metadata instead of throwing', () => { const warnings: ManifestWarning[] = []; const parsed = parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayName: 'Repeater', displayNameI18n: { 'zh-CN': '复读机', ja: 'リピーター' }, descriptionI18n: { en: 'Repeats', fr: 'Répéteur' }, category: 'fun', } } }, 'fixture', { metadata: 'lenient', onWarning: warning => warnings.push(warning) }); expect(parsed.zhin.type === 'plugin' && parsed.zhin.metadata).toEqual({ displayName: 'Repeater', displayNameI18n: { 'zh-CN': '复读机' }, descriptionI18n: { en: 'Repeats' }, }); expect(warnings.map(({ package: name, field, code }) => ({ name, field, code }))).toEqual([ { name: '@test/plugin', field: 'zhin.metadata.displayNameI18n', code: 'manifest-metadata-dropped' }, { name: '@test/plugin', field: 'zhin.metadata.descriptionI18n', code: 'manifest-metadata-dropped' }, { name: '@test/plugin', field: 'zhin.metadata.category', code: 'manifest-metadata-dropped' }, ]); expect(warnings[2]?.message).toContain('metadata.category must be one of'); // Whole-field drops: a non-object description, or a display-name map with only unknown locales. const dropped: ManifestWarning[] = []; const whole = parsePackageJson({ name: '@test/plugin', zhin: { ...plugin, metadata: { displayNameI18n: { ja: 'リピーター' }, descriptionI18n: 'Repeats', category: ['game'], } } }, 'fixture', { metadata: 'lenient', onWarning: warning => dropped.push(warning) }); expect(whole.zhin.type === 'plugin' && whole.zhin.metadata).toEqual({}); expect(dropped).toHaveLength(3); }); it('lenient mode keeps runtime-affecting metadata problems fatal', () => { const lenient = (metadata: Record) => () => parsePackageJson( { name: '@test/plugin', zhin: { ...plugin, metadata } }, 'fixture', { metadata: 'lenient', onWarning: () => undefined }, ); expect(lenient({ order: 1 })).toThrow('metadata.order'); expect(lenient({ displayNameI18n: 'Repeater' })).toThrow('displayNameI18n must be an object'); expect(lenient({ displayNameI18n: { en: '' } })).toThrow('displayNameI18n.en'); expect(lenient({ displayName: '' })).toThrow('metadata.displayName'); }); it('loads a package with a bad marketplace category through the resolver and reports it once', async () => { const root = await fixture({ name: '@test/root', dependencies: { '@test/child': 'workspace:*' } }); await json(join(root, 'plugins/child/package.json'), { name: '@test/child', zhin: { ...plugin, metadata: { category: 'fun', descriptionI18n: { ja: '子' } } }, }); const warnings: ManifestWarning[] = []; const resolver = await NodePackageResolver.create(root, { onManifestWarning: warning => warnings.push(warning) }); const graph = await new ProjectGraphService(resolver).inspect(root, { configVersion: 2, plugins: { child: '@test/child' } }); expect(graph.root.children.map(node => node.id)).toEqual(['root/child']); const child = resolver.workspacePackages().find(item => item.name === '@test/child'); expect(child?.packageJson.zhin.type === 'plugin' && child.packageJson.zhin.metadata).toEqual({}); expect(warnings.map(warning => [warning.package, warning.field])).toEqual([ ['@test/child', 'zhin.metadata.descriptionI18n'], ['@test/child', 'zhin.metadata.category'], ]); }); }); const plugin = { protocol: 1, type: 'plugin', entry: './plugin.ts' }; async function fixture(rootJson: Record): Promise { const root = await realpath(await mkdtemp(join(tmpdir(), 'zhin-v2-declarations-'))); temporary.push(root); await json(join(root, 'package.json'), { zhin: plugin, ...rootJson }); return root; } async function json(file: string, value: unknown): Promise { await mkdir(join(file, '..'), { recursive: true }); await writeFile(file, JSON.stringify(value)); }