1- import { type ExpectType , DP } from "@duplojs/utils" ;
1+ import { type ExpectType , DPE , DP , S } from "@duplojs/utils" ;
22import { DServerCommand , TESTImplementation , setEnvironment } from "@scripts" ;
33
44describe ( "create" , ( ) => {
@@ -20,6 +20,16 @@ describe("create", () => {
2020 string ,
2121 "strict"
2222 > ;
23+ type _CheckDescription = ExpectType <
24+ typeof command . description ,
25+ string | null ,
26+ "strict"
27+ > ;
28+ type _CheckSubject = ExpectType <
29+ typeof command . subject ,
30+ DServerCommand . Subject | null | readonly DServerCommand . Command [ ] ,
31+ "strict"
32+ > ;
2333
2434 expect ( command . name ) . toBe ( "root" ) ;
2535 expect ( command . description ) . toBeNull ( ) ;
@@ -55,11 +65,27 @@ describe("create", () => {
5565 {
5666 options : [
5767 DServerCommand . createBooleanOption ( "verbose" , { aliases : [ "v" ] } ) ,
58- DServerCommand . createOption ( "name" , DP . string ( ) , { required : true } ) ,
68+ DServerCommand . createOption ( "name" , DPE . string ( ) , { required : true } ) ,
5969 ] ,
6070 subject : DP . tuple ( [ DP . string ( ) ] ) ,
6171 } ,
62- executeSpy ,
72+ ( params ) => {
73+ type _CheckOptions = ExpectType <
74+ typeof params . options ,
75+ {
76+ verbose : boolean ;
77+ name : string ;
78+ } ,
79+ "strict"
80+ > ;
81+ type _CheckSubject = ExpectType <
82+ typeof params . subject ,
83+ [ string ] ,
84+ "strict"
85+ > ;
86+
87+ return executeSpy ( params ) ;
88+ } ,
6389 ) ;
6490
6591 await command . execute ( [ "--verbose" , "--name=duplo" , "subject" ] ) ;
@@ -81,9 +107,17 @@ describe("create", () => {
81107 const command = DServerCommand . create (
82108 "root" ,
83109 {
84- subject : DP . string ( ) ,
110+ subject : DPE . string ( ) . optional ( ) ,
111+ } ,
112+ ( { subject } ) => {
113+ type check = ExpectType <
114+ typeof subject ,
115+ string | undefined ,
116+ "strict"
117+ > ;
118+
119+ return undefined ;
85120 } ,
86- ( ) => undefined ,
87121 ) ;
88122
89123 await expect ( command . execute ( [ "one" , "two" ] ) ) . rejects . toThrowError ( DServerCommand . CommandManyArgumentsError ) ;
@@ -126,9 +160,20 @@ describe("create", () => {
126160 } ) ,
127161 } as never ,
128162 ] ,
129- subject : DP . string ( ) ,
163+ subject : DPE . string ( ) ,
164+ } ,
165+ ( { options, subject } ) => {
166+ type _CheckSubject = ExpectType <
167+ typeof subject ,
168+ string ,
169+ "strict"
170+ > ;
171+
172+ executeSpy ( {
173+ options,
174+ subject,
175+ } ) ;
130176 } ,
131- executeSpy ,
132177 ) ;
133178
134179 await command . execute ( [ "subject" ] ) ;
@@ -142,6 +187,86 @@ describe("create", () => {
142187 expect ( exitSpy ) . toHaveBeenCalledWith ( 0 ) ;
143188 } ) ;
144189
190+ it ( "infers typed params with array subject" , async ( ) => {
191+ setEnvironment ( "TEST" ) ;
192+ const exitSpy = vi . fn ( ) ;
193+ const executeSpy = vi . fn ( ) ;
194+ TESTImplementation . set ( "exitProcess" , exitSpy ) ;
195+
196+ const command = DServerCommand . create (
197+ "root" ,
198+ {
199+ subject : DPE . string ( ) . array ( ) ,
200+ } ,
201+ ( params ) => {
202+ type _CheckSubject = ExpectType <
203+ typeof params . subject ,
204+ string [ ] ,
205+ "strict"
206+ > ;
207+
208+ executeSpy ( params ) ;
209+ } ,
210+ ) ;
211+
212+ await command . execute ( [ "one" , "two" ] ) ;
213+
214+ expect ( executeSpy ) . toHaveBeenCalledWith ( {
215+ options : { } ,
216+ subject : [ "one" , "two" ] ,
217+ } ) ;
218+ expect ( exitSpy ) . toHaveBeenCalledWith ( 0 ) ;
219+ } ) ;
220+
221+ it ( "executes optional option with pipe and transform parser without runtime bug" , async ( ) => {
222+ setEnvironment ( "TEST" ) ;
223+ const exitSpy = vi . fn ( ) ;
224+ const executeSpy = vi . fn ( ) ;
225+ TESTImplementation . set ( "exitProcess" , exitSpy ) ;
226+
227+ const command = DServerCommand . create (
228+ "root" ,
229+ {
230+ options : [
231+ DServerCommand . createOption (
232+ "name" ,
233+ DPE . string ( ) . pipe (
234+ DPE . transform (
235+ DPE . string ( ) ,
236+ S . toUpperCase ,
237+ ) ,
238+ ) ,
239+ ) ,
240+ ] ,
241+ } ,
242+ ( { options } ) => {
243+ type _CheckOptions = ExpectType <
244+ typeof options . name ,
245+ Uppercase < string > | undefined ,
246+ "strict"
247+ > ;
248+
249+ executeSpy ( { options } ) ;
250+ } ,
251+ ) ;
252+
253+ await command . execute ( [ ] ) ;
254+ await command . execute ( [ "--name=guest" ] ) ;
255+
256+ expect ( executeSpy ) . toHaveBeenNthCalledWith ( 1 , {
257+ options : {
258+ name : undefined ,
259+ } ,
260+ } ) ;
261+ expect ( executeSpy ) . toHaveBeenNthCalledWith ( 2 , {
262+ options : {
263+ name : "GUEST" ,
264+ } ,
265+ } ) ;
266+ expect ( exitSpy ) . toHaveBeenNthCalledWith ( 1 , 0 ) ;
267+ expect ( exitSpy ) . toHaveBeenNthCalledWith ( 2 , 0 ) ;
268+ } ) ;
269+
145270 it ( "executes matching child command when subject is a command list" , async ( ) => {
146271 setEnvironment ( "TEST" ) ;
147272 const exitSpy = vi . fn ( ) ;
0 commit comments