@@ -14,40 +14,32 @@ function defineRun(run: CommandDef["run"]): CommandDef {
1414}
1515
1616describe ( "trackCommandFailures" , ( ) => {
17- it ( "reports the error and re-throws when run() rejects" , async ( ) => {
18- const onFailure = vi . fn ( ) ;
17+ it ( "re-throws when run() rejects so the executable boundary can report it" , async ( ) => {
1918 const boom = new Error ( "ffmpeg not found" ) ;
20- const wrapped = trackCommandFailures (
21- ( ) => Promise . resolve ( defineRun ( ( ) => Promise . reject ( boom ) ) ) ,
22- onFailure ,
19+ const wrapped = trackCommandFailures ( ( ) =>
20+ Promise . resolve ( defineRun ( ( ) => Promise . reject ( boom ) ) ) ,
2321 ) ;
2422
2523 const cmd = await wrapped ( ) ;
2624 await expect ( ( cmd . run as ( ) => Promise < unknown > ) ( ) ) . rejects . toBe ( boom ) ;
27- expect ( onFailure ) . toHaveBeenCalledWith ( boom ) ;
2825 } ) ;
2926
30- it ( "does not report when run() succeeds, and returns its value" , async ( ) => {
31- const onFailure = vi . fn ( ) ;
32- const wrapped = trackCommandFailures (
33- ( ) => Promise . resolve ( defineRun ( ( ) => Promise . resolve ( "ok" as unknown as void ) ) ) ,
34- onFailure ,
27+ it ( "returns the command value when run() succeeds" , async ( ) => {
28+ const wrapped = trackCommandFailures ( ( ) =>
29+ Promise . resolve ( defineRun ( ( ) => Promise . resolve ( "ok" as unknown as void ) ) ) ,
3530 ) ;
3631
3732 const cmd = await wrapped ( ) ;
3833 await expect ( ( cmd . run as ( ) => Promise < unknown > ) ( ) ) . resolves . toBe ( "ok" ) ;
39- expect ( onFailure ) . not . toHaveBeenCalled ( ) ;
4034 } ) ;
4135
4236 it ( "rejects an unknown flag on a leaf command" , async ( ) => {
43- const wrapped = trackCommandFailures (
44- ( ) =>
45- Promise . resolve ( {
46- meta : { name : "leaf" } ,
47- args : { out : { type : "string" } } ,
48- run : ( ) => Promise . resolve ( ) ,
49- } as CommandDef ) ,
50- vi . fn ( ) ,
37+ const wrapped = trackCommandFailures ( ( ) =>
38+ Promise . resolve ( {
39+ meta : { name : "leaf" } ,
40+ args : { out : { type : "string" } } ,
41+ run : ( ) => Promise . resolve ( ) ,
42+ } as CommandDef ) ,
5143 ) ;
5244 const cmd = await wrapped ( ) ;
5345 await expect (
@@ -59,14 +51,12 @@ describe("trackCommandFailures", () => {
5951 // `figma component <ref> --name x`: --name belongs to the subcommand's
6052 // table; the group (subCommands + fallback-help run) must not reject it.
6153 const run = vi . fn ( ( ) => Promise . resolve ( ) ) ;
62- const wrapped = trackCommandFailures (
63- ( ) =>
64- Promise . resolve ( {
65- meta : { name : "figma" } ,
66- subCommands : { component : ( ) => Promise . resolve ( { meta : { name : "component" } } ) } ,
67- run,
68- } as unknown as CommandDef ) ,
69- vi . fn ( ) ,
54+ const wrapped = trackCommandFailures ( ( ) =>
55+ Promise . resolve ( {
56+ meta : { name : "figma" } ,
57+ subCommands : { component : ( ) => Promise . resolve ( { meta : { name : "component" } } ) } ,
58+ run,
59+ } as unknown as CommandDef ) ,
7060 ) ;
7161 const cmd = await wrapped ( ) ;
7262 await expect (
@@ -78,14 +68,12 @@ describe("trackCommandFailures", () => {
7868 } ) ;
7969
8070 it ( "still rejects an unknown flag when the group is NOT delegating" , async ( ) => {
81- const wrapped = trackCommandFailures (
82- ( ) =>
83- Promise . resolve ( {
84- meta : { name : "figma" } ,
85- subCommands : { component : ( ) => Promise . resolve ( { meta : { name : "component" } } ) } ,
86- run : ( ) => Promise . resolve ( ) ,
87- } as unknown as CommandDef ) ,
88- vi . fn ( ) ,
71+ const wrapped = trackCommandFailures ( ( ) =>
72+ Promise . resolve ( {
73+ meta : { name : "figma" } ,
74+ subCommands : { component : ( ) => Promise . resolve ( { meta : { name : "component" } } ) } ,
75+ run : ( ) => Promise . resolve ( ) ,
76+ } as unknown as CommandDef ) ,
8977 ) ;
9078 const cmd = await wrapped ( ) ;
9179 await expect (
@@ -94,65 +82,44 @@ describe("trackCommandFailures", () => {
9482 } ) ;
9583
9684 it ( "passes through a command with no run() untouched" , async ( ) => {
97- const onFailure = vi . fn ( ) ;
9885 const parent : CommandDef = { meta : { name : "parent" } } ;
99- const wrapped = trackCommandFailures ( ( ) => Promise . resolve ( parent ) , onFailure ) ;
86+ const wrapped = trackCommandFailures ( ( ) => Promise . resolve ( parent ) ) ;
10087
10188 const cmd = await wrapped ( ) ;
10289 expect ( cmd ) . toBe ( parent ) ;
103- expect ( onFailure ) . not . toHaveBeenCalled ( ) ;
10490 } ) ;
10591
106- it ( "awaits onFailure and re-throws the ORIGINAL error even if onFailure rejects" , async ( ) => {
107- const boom = new Error ( "original failure" ) ;
108- const wrapped = trackCommandFailures (
109- ( ) => Promise . resolve ( defineRun ( ( ) => Promise . reject ( boom ) ) ) ,
110- ( ) => Promise . reject ( new Error ( "telemetry is down" ) ) ,
111- ) ;
112-
113- const cmd = await wrapped ( ) ;
114- await expect ( ( cmd . run as ( ) => Promise < unknown > ) ( ) ) . rejects . toBe ( boom ) ;
115- } ) ;
116-
117- it ( "REPORTS an unknown-flag rejection to onFailure (HF#2033: assertion inside the try)" , async ( ) => {
118- // The flag assertion used to run before the try/catch, so an unknown-flag
119- // throw skipped telemetry. It must now be reported like any other failure.
120- const onFailure = vi . fn ( ) ;
92+ it ( "re-throws an unknown-flag rejection to the executable boundary" , async ( ) => {
12193 const cmd = {
12294 meta : { name : "render" } ,
12395 args : { output : { type : "string" , alias : "o" } } ,
12496 run : vi . fn ( ( ) => Promise . resolve ( ) ) ,
12597 } as unknown as CommandDef ;
126- const wrapped = trackCommandFailures ( ( ) => Promise . resolve ( cmd ) , onFailure ) ;
98+ const wrapped = trackCommandFailures ( ( ) => Promise . resolve ( cmd ) ) ;
12799
128100 const resolved = await wrapped ( ) ;
129101 await expect (
130102 ( resolved . run as ( ctx : unknown ) => Promise < unknown > ) ( { rawArgs : [ "--nope" , "x" ] } ) ,
131103 ) . rejects . toThrow ( / u n k n o w n f l a g / i) ;
132- expect ( onFailure ) . toHaveBeenCalledTimes ( 1 ) ;
133104 expect ( cmd . run ) . not . toHaveBeenCalled ( ) ; // body never ran — flag rejected first
134105 } ) ;
135106
136- it ( "recursively wraps nested subcommands so their failures report too (HF#2033)" , async ( ) => {
137- // cli.ts wraps only top-level loaders; a group's leaves (cloud/*, auth/*, …)
138- // were never wrapped, silently dropping unknown-flag + failure telemetry.
139- const onFailure = vi . fn ( ) ;
107+ it ( "recursively wraps nested subcommands so their failures reach the boundary" , async ( ) => {
140108 const boom = new Error ( "nested boom" ) ;
141109 const group : CommandDef = {
142110 meta : { name : "cloud" } ,
143111 subCommands : {
144112 render : defineRun ( ( ) => Promise . reject ( boom ) ) ,
145113 } ,
146114 } ;
147- const wrapped = trackCommandFailures ( ( ) => Promise . resolve ( group ) , onFailure ) ;
115+ const wrapped = trackCommandFailures ( ( ) => Promise . resolve ( group ) ) ;
148116
149117 const resolvedGroup = await wrapped ( ) ;
150118 const subLoader = ( resolvedGroup . subCommands as Record < string , ( ) => Promise < CommandDef > > )
151119 . render ;
152120 if ( ! subLoader ) throw new Error ( "expected a wrapped 'render' subcommand loader" ) ;
153121 const leaf = await subLoader ( ) ;
154122 await expect ( ( leaf . run as ( ) => Promise < unknown > ) ( ) ) . rejects . toBe ( boom ) ;
155- expect ( onFailure ) . toHaveBeenCalledWith ( boom ) ;
156123 } ) ;
157124} ) ;
158125
0 commit comments