11// Plugin registry loader tests cover CLI plugin registry loading and cache reset behavior.
2+ import { readFile } from "node:fs/promises" ;
3+ import { join } from "node:path" ;
24import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
6+ import { measureCliCommandStartup } from "./command-startup-timing.js" ;
37
48const ensurePluginRegistryLoadedMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
9+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
510
611vi . mock ( "./plugin-registry.js" , ( ) => ( {
712 ensurePluginRegistryLoaded : ensurePluginRegistryLoadedMock ,
@@ -25,6 +30,7 @@ describe("plugin-registry-loader", () => {
2530
2631 afterEach ( ( ) => {
2732 loggingState . forceConsoleToStderr = originalForceStderr ;
33+ vi . unstubAllEnvs ( ) ;
2834 } ) ;
2935
3036 it ( "routes plugin load logs to stderr and restores state" , async ( ) => {
@@ -85,4 +91,32 @@ describe("plugin-registry-loader", () => {
8591 scope : "configured-channels" ,
8692 } ) ;
8793 } ) ;
94+
95+ it ( "attributes module import separately from runtime loading" , async ( ) => {
96+ const dir = tempDirs . make ( "openclaw-plugin-registry-startup-" ) ;
97+ const timelinePath = join ( dir , "timeline.jsonl" ) ;
98+ vi . stubEnv ( "OPENCLAW_DIAGNOSTICS" , "timeline" ) ;
99+ vi . stubEnv ( "OPENCLAW_DIAGNOSTICS_TIMELINE_PATH" , timelinePath ) ;
100+
101+ await measureCliCommandStartup ( "plugin-registry" , ( ) =>
102+ ensureCliPluginRegistryLoaded ( {
103+ scope : "all" ,
104+ } ) ,
105+ ) ;
106+
107+ const events = ( await readFile ( timelinePath , "utf8" ) )
108+ . trim ( )
109+ . split ( "\n" )
110+ . map ( ( line ) => JSON . parse ( line ) as Record < string , unknown > ) ;
111+ const starts = events . filter ( ( event ) => event . type === "span.start" ) ;
112+ const outer = starts . find (
113+ ( event ) => ( event . attributes as { stage ?: string } | undefined ) ?. stage === "plugin-registry" ,
114+ ) ;
115+ expect ( outer ) . toBeDefined ( ) ;
116+ expect (
117+ starts
118+ . filter ( ( event ) => event . parentSpanId === outer ?. spanId )
119+ . map ( ( event ) => ( event . attributes as { stage ?: string } | undefined ) ?. stage ) ,
120+ ) . toEqual ( [ "plugin-registry-module-import" , "plugin-registry-runtime-load" ] ) ;
121+ } ) ;
88122} ) ;
0 commit comments