@@ -3,8 +3,9 @@ import fs from 'node:fs';
33import path from 'node:path' ;
44import { afterEach , test , vi } from 'vitest' ;
55import { runCli } from '../../../cli.ts' ;
6+ import { readCurrentOwnerIdentity } from '@agent-device/host-kit/process' ;
67import { pluginHome , selectPlugin } from '@agent-device/test-kit' ;
7- import { parseRawArgs , usageForCommand } from '../../parser/args.ts' ;
8+ import { parseArgs , parseRawArgs , usageForCommand } from '../../parser/args.ts' ;
89import { beginStdoutCapture } from '../../../__tests__/test-utils/stream-capture.ts' ;
910
1011vi . mock ( '../../../provider-device-runtimes.ts' , ( ) => {
@@ -57,5 +58,65 @@ test('plugin schema exposes operator help and preserves scoped npm package specs
5758 assert . deepEqual ( args . positionals , [ 'add' , '@example/provider@^1.0.0' ] ) ;
5859 const help = await usageForCommand ( 'plugins' ) ;
5960 assert . ok ( help ) ;
60- assert . match ( help , / p l u g i n s l i s t \| a d d / ) ;
61+ assert . match ( help , / p l u g i n s l i s t \[ - - s t a t e - d i r < p a t h > \] \| a d d / ) ;
62+ } ) ;
63+
64+ test ( 'plugins list reports the running daemon inventory next to the store selections' , async ( ) => {
65+ const { home } = pluginHome ( ) ;
66+ const stateDir = path . join ( home , 'state' ) ;
67+ vi . stubEnv ( 'AGENT_DEVICE_HOME' , home ) ;
68+ vi . stubEnv ( 'AGENT_DEVICE_STATE_DIR' , stateDir ) ;
69+ vi . stubEnv ( 'AGENT_DEVICE_NO_UPDATE_NOTIFIER' , '1' ) ;
70+ const owner = readCurrentOwnerIdentity ( ) ;
71+ const failed = {
72+ provider : 'example' ,
73+ source : 'store' ,
74+ package : 'example' ,
75+ version : '1.2.3' ,
76+ state : 'failed' ,
77+ error : { code : 'INVALID_ARGS' , message : 'bad plugin' , reason : 'invalid_plugin' } ,
78+ credentials : { variables : [ ] , byBackend : { } } ,
79+ } ;
80+ fs . mkdirSync ( stateDir , { recursive : true } ) ;
81+ fs . writeFileSync (
82+ path . join ( stateDir , 'daemon.json' ) ,
83+ JSON . stringify ( {
84+ token : 't' ,
85+ port : 1 ,
86+ pid : owner . pid ,
87+ processStartTime : owner . startTime ,
88+ plugins : [ failed ] ,
89+ } ) ,
90+ ) ;
91+ selectPlugin ( home , 'example' , 'example' , 'export default () => {};' ) ;
92+ const stdoutCapture = beginStdoutCapture ( ) ;
93+ try {
94+ await runCli ( [ 'plugins' , 'list' , '--json' ] , {
95+ sendToDaemon : vi . fn ( async ( ) => {
96+ throw new Error ( 'unexpected daemon access' ) ;
97+ } ) ,
98+ } ) ;
99+ const { data } = JSON . parse ( stdoutCapture . read ( ) ) ;
100+ assert . deepEqual ( data . daemon , { pid : owner . pid , stateDir, plugins : [ failed ] } ) ;
101+ assert . equal ( data . plugins [ 0 ] . name , 'example' ) ;
102+ } finally {
103+ stdoutCapture . restore ( ) ;
104+ }
105+ } ) ;
106+
107+ test ( '--state-dir belongs to plugins list and daemon status, not to the commands that change the store' , async ( ) => {
108+ assert . equal (
109+ parseArgs ( [ 'plugins' , 'list' , '--state-dir' , '/tmp/state' ] ) . flags . stateDir ,
110+ '/tmp/state' ,
111+ ) ;
112+ assert . throws ( ( ) => parseArgs ( [ 'plugins' , 'add' , 'example' , '--state-dir' , '/tmp/state' ] ) , {
113+ code : 'INVALID_ARGS' ,
114+ } ) ;
115+ assert . equal (
116+ parseArgs ( [ 'daemon' , 'status' , '--state-dir' , '/tmp/state' ] ) . flags . stateDir ,
117+ '/tmp/state' ,
118+ ) ;
119+ assert . throws ( ( ) => parseArgs ( [ 'daemon' , 'status' , '--clean' ] ) , { code : 'INVALID_ARGS' } ) ;
120+ const help = await usageForCommand ( 'daemon' ) ;
121+ assert . match ( help ?? '' , / d a e m o n s t a t u s \[ - - s t a t e - d i r < p a t h > \] / ) ;
61122} ) ;
0 commit comments