fix: 修复插件包路径策略DI配置

This commit is contained in:
sunlei 2026-06-18 08:49:43 +08:00
parent 500f5c69f1
commit 5b22703949
2 changed files with 25 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable, Optional } from '@nestjs/common';
import { existsSync, statSync } from 'node:fs';
import { isAbsolute, relative, resolve, sep } from 'node:path';
@ -9,6 +9,9 @@ const DEFAULT_BUILTIN_PACKAGE_ROOT = resolve(
'qqbot',
'plugins',
);
export const QQBOT_PLUGIN_PACKAGE_CONTROLLED_ROOTS = Symbol(
'QQBOT_PLUGIN_PACKAGE_CONTROLLED_ROOTS',
);
/**
* Restricts QQBot plugin package discovery and entry resolution to controlled package roots.
@ -19,10 +22,16 @@ export class QqbotPluginPackagePathPolicyService {
/**
* Creates a path policy for package roots that the plugin platform may scan.
* @param controlledRoots - Directory roots that may contain one-level QQBot plugin package folders.
* @param controlledRoots - Optional DI-provided package roots; production omits this token so the platform scans the standard built-in plugin root.
*/
constructor(controlledRoots = [DEFAULT_BUILTIN_PACKAGE_ROOT]) {
this.controlledRoots = controlledRoots.map((root) => resolve(root));
constructor(
@Optional()
@Inject(QQBOT_PLUGIN_PACKAGE_CONTROLLED_ROOTS)
controlledRoots?: string[],
) {
this.controlledRoots = (
controlledRoots?.length ? controlledRoots : [DEFAULT_BUILTIN_PACKAGE_ROOT]
).map((root) => resolve(root));
}
/**

View File

@ -1,6 +1,8 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { Test } from '@nestjs/testing';
import { QqbotPluginPlatformService } from '../../../../src/modules/qqbot/plugin-platform/application/plugin-platform.service';
import { QqbotPluginPackagePathPolicyService } from '../../../../src/modules/qqbot/plugin-platform/infrastructure/integration/package/plugin-package-path-policy.service';
import { QqbotPluginTaskWorkerProcessor } from '../../../../src/modules/qqbot/plugin-platform/application/task/qqbot-plugin-task-worker.processor';
describe('QQBot plugin platform DI tokens', () => {
@ -27,4 +29,14 @@ describe('QQBot plugin platform DI tokens', () => {
expect(paramTypes[1]).toBe(QqbotPluginPlatformService);
});
it('lets Nest instantiate the package path policy without a config-array provider', async () => {
const moduleRef = await Test.createTestingModule({
providers: [QqbotPluginPackagePathPolicyService],
}).compile();
expect(moduleRef.get(QqbotPluginPackagePathPolicyService)).toBeInstanceOf(
QqbotPluginPackagePathPolicyService,
);
});
});