@@ -5,7 +5,11 @@ import { afterEach, describe, expect, it } from "vitest";
55import { writePersistedInstalledPluginIndexSync } from "./installed-plugin-index-store.js" ;
66import { loadInstalledPluginIndex } from "./installed-plugin-index.js" ;
77import { refreshPluginRegistry } from "./plugin-registry.js" ;
8- import { buildPluginRegistrySnapshotReport , buildPluginSnapshotReport } from "./status.js" ;
8+ import {
9+ buildPluginDiagnosticsReport ,
10+ buildPluginRegistrySnapshotReport ,
11+ buildPluginSnapshotReport ,
12+ } from "./status.js" ;
913import {
1014 createColdPluginConfig ,
1115 createColdPluginFixture ,
@@ -207,6 +211,18 @@ describe("buildPluginRegistrySnapshotReport", () => {
207211 } ) ;
208212
209213 const plugin = requirePlugin ( report . plugins , "dependency-demo" ) ;
214+ expectFields ( plugin , {
215+ status : "error" ,
216+ error :
217+ 'Plugin "dependency-demo" cannot load because required dependencies are missing: missing-required. Install the plugin dependencies or reinstall/update the plugin, then restart the Gateway.' ,
218+ } ) ;
219+ expect ( report . diagnostics ) . toContainEqual ( {
220+ level : "error" ,
221+ pluginId : "dependency-demo" ,
222+ source : fs . realpathSync ( fixture . runtimeSource ) ,
223+ message :
224+ 'Plugin "dependency-demo" cannot load because required dependencies are missing: missing-required. Install the plugin dependencies or reinstall/update the plugin, then restart the Gateway.' ,
225+ } ) ;
210226 const dependencyStatus = requireRecord ( plugin . dependencyStatus ) ;
211227 expectFields ( dependencyStatus , {
212228 hasDependencies : true ,
@@ -241,6 +257,101 @@ describe("buildPluginRegistrySnapshotReport", () => {
241257 expect ( isColdPluginRuntimeLoaded ( fixture ) ) . toBe ( false ) ;
242258 } ) ;
243259
260+ it ( "honors npm optional dependency precedence without reporting a false required failure" , ( ) => {
261+ const fixture = createColdPluginFixture ( {
262+ rootDir : makeTempDir ( ) ,
263+ pluginId : "optional-dependency-demo" ,
264+ packageJson : {
265+ dependencies : { "optional-runtime" : "1.0.0" } ,
266+ optionalDependencies : { "optional-runtime" : "2.0.0" } ,
267+ } ,
268+ } ) ;
269+
270+ const report = buildPluginRegistrySnapshotReport ( {
271+ config : createColdPluginConfig ( fixture . rootDir , fixture . pluginId ) ,
272+ } ) ;
273+ const plugin = requirePlugin ( report . plugins , fixture . pluginId ) ;
274+
275+ expectFields ( plugin , { status : "loaded" } ) ;
276+ expectFields ( requireRecord ( plugin . dependencyStatus ) , {
277+ requiredInstalled : true ,
278+ optionalInstalled : false ,
279+ missing : [ ] ,
280+ missingOptional : [ "optional-runtime" ] ,
281+ dependencies : [ ] ,
282+ } ) ;
283+ expect ( report . diagnostics ) . not . toContainEqual (
284+ expect . objectContaining ( { pluginId : fixture . pluginId , level : "error" } ) ,
285+ ) ;
286+ expect ( isColdPluginRuntimeLoaded ( fixture ) ) . toBe ( false ) ;
287+ } ) ;
288+
289+ it ( "keeps disabled plugins with missing required dependencies diagnostic-free" , ( ) => {
290+ const fixture = createColdPluginFixture ( {
291+ rootDir : makeTempDir ( ) ,
292+ pluginId : "disabled-dependency-demo" ,
293+ packageJson : { dependencies : { "missing-required" : "1.0.0" } } ,
294+ } ) ;
295+
296+ const report = buildPluginRegistrySnapshotReport ( {
297+ config : {
298+ plugins : {
299+ load : { paths : [ fixture . rootDir ] } ,
300+ entries : { [ fixture . pluginId ] : { enabled : false } } ,
301+ } ,
302+ } ,
303+ } ) ;
304+ const plugin = requirePlugin ( report . plugins , fixture . pluginId ) ;
305+
306+ expectFields ( plugin , { enabled : false , status : "disabled" } ) ;
307+ expect ( plugin . error ) . toBeUndefined ( ) ;
308+ expect ( requireRecord ( plugin . dependencyStatus ) . missing ) . toEqual ( [ "missing-required" ] ) ;
309+ expect ( report . diagnostics ) . not . toContainEqual (
310+ expect . objectContaining ( { pluginId : fixture . pluginId , level : "error" } ) ,
311+ ) ;
312+ expect ( isColdPluginRuntimeLoaded ( fixture ) ) . toBe ( false ) ;
313+ } ) ;
314+
315+ it ( "preserves the real runtime import error while explaining missing dependencies" , ( ) => {
316+ const rootDir = makeTempDir ( ) ;
317+ const bundledRoot = makeTempDir ( ) ;
318+ const fixture = createColdPluginFixture ( {
319+ rootDir,
320+ pluginId : "failed-runtime-dependency-demo" ,
321+ packageJson : {
322+ dependencies : { "missing-runtime" : "1.0.0" , "optional-runtime" : "1.0.0" } ,
323+ optionalDependencies : { "optional-runtime" : "2.0.0" } ,
324+ } ,
325+ } ) ;
326+ fs . writeFileSync (
327+ fixture . runtimeSource ,
328+ `require("node:fs").writeFileSync(${ JSON . stringify ( fixture . runtimeMarker ) } , "loaded");\n` +
329+ 'require("missing-runtime");\n' ,
330+ "utf8" ,
331+ ) ;
332+
333+ const report = buildPluginDiagnosticsReport ( {
334+ config : createColdPluginConfig ( rootDir , fixture . pluginId ) ,
335+ workspaceDir : rootDir ,
336+ env : createColdPluginHermeticEnv ( rootDir , { bundledPluginsDir : bundledRoot } ) ,
337+ logger : { info ( ) { } , warn ( ) { } , error ( ) { } , debug ( ) { } } ,
338+ } ) ;
339+ const plugin = requirePlugin ( report . plugins , fixture . pluginId ) ;
340+ const diagnostics = report . diagnostics . filter ( ( entry ) => entry . pluginId === fixture . pluginId ) ;
341+
342+ expectFields ( plugin , { status : "error" } ) ;
343+ expect ( String ( plugin . error ) ) . toContain ( "Cannot find module" ) ;
344+ expect ( String ( plugin . error ) ) . toContain ( "Install the plugin dependencies" ) ;
345+ expectFields ( requireRecord ( plugin . dependencyStatus ) , {
346+ missing : [ "missing-runtime" ] ,
347+ missingOptional : [ "optional-runtime" ] ,
348+ } ) ;
349+ expect ( diagnostics ) . toHaveLength ( 1 ) ;
350+ expect ( diagnostics [ 0 ] ?. message ) . toContain ( "Cannot find module" ) ;
351+ expect ( diagnostics [ 0 ] ?. message ) . toContain ( "Install the plugin dependencies" ) ;
352+ expect ( isColdPluginRuntimeLoaded ( fixture ) ) . toBe ( true ) ;
353+ } ) ;
354+
244355 it ( "replays persisted list metadata without importing plugin runtime" , async ( ) => {
245356 const fixture = createColdPluginFixture ( {
246357 rootDir : makeTempDir ( ) ,
0 commit comments