@@ -74,7 +74,14 @@ describe('registerCodegen responsive error handling', () => {
7474
7575 expect ( consoleErrorMock ) . toHaveBeenCalled ( )
7676 expect ( runMock ) . toHaveBeenCalled ( )
77+ // Pure Code is generated via a separate Codegen instance whose run()/getCode()
78+ // hit the same prototype mocks, so its code is also 'base-code'.
7779 expect ( result ) . toEqual ( [
80+ {
81+ title : 'Pure Code' ,
82+ language : 'TYPESCRIPT' ,
83+ code : 'base-code' ,
84+ } ,
7885 {
7986 title : 'Main' ,
8087 language : 'TYPESCRIPT' ,
@@ -83,3 +90,73 @@ describe('registerCodegen responsive error handling', () => {
8390 ] )
8491 } )
8592} )
93+
94+ describe ( 'registerCodegen pure code error handling' , ( ) => {
95+ // Throws ONLY for the Pure Code Codegen instance (inlineAllInstances=true),
96+ // letting the main codegen run normally so we can isolate the catch branch.
97+ const pureCodeRunMock = mock ( async function ( this : {
98+ options ?: { inlineAllInstances ?: boolean }
99+ } ) {
100+ if ( this . options ?. inlineAllInstances ) {
101+ throw new Error ( 'pure-code-boom' )
102+ }
103+ } )
104+
105+ beforeEach ( ( ) => {
106+ Codegen . prototype . run =
107+ pureCodeRunMock as unknown as typeof Codegen . prototype . run
108+ Codegen . prototype . getComponentsCodes = getComponentsCodesMock
109+ Codegen . prototype . getCode = getCodeMock
110+
111+ console . error = consoleErrorMock as typeof console . error
112+ resetFigma ( )
113+ } )
114+
115+ afterEach ( ( ) => {
116+ Codegen . prototype . run = originalRun
117+ Codegen . prototype . getComponentsCodes = originalGetComponentsCodes
118+ Codegen . prototype . getCode = originalGetCode
119+
120+ console . error = originalError
121+ resetFigma ( )
122+ mock . restore ( )
123+ } )
124+
125+ test ( 'swallows pure code errors and omits the Pure Code entry' , async ( ) => {
126+ const handlerCalls : ( ( event : CodegenEvent ) => Promise < CodegenResult [ ] > ) [ ] =
127+ [ ]
128+ const ctx = {
129+ editorType : 'dev' ,
130+ mode : 'codegen' ,
131+ command : 'noop' ,
132+ codegen : {
133+ on : mock ( ( _event , handler ) => {
134+ handlerCalls . push ( handler )
135+ } ) ,
136+ } ,
137+ } as unknown as typeof figma
138+
139+ const node = {
140+ type : 'FRAME' ,
141+ name : 'PureFail' ,
142+ } as unknown as SceneNode
143+
144+ registerCodegen ( ctx )
145+
146+ const generate = handlerCalls [ 0 ]
147+ const result = await generate ( { node, language : 'devup-ui' } )
148+
149+ // Pure Code generation threw → console.error captured the failure.
150+ expect ( consoleErrorMock ) . toHaveBeenCalled ( )
151+
152+ // Pure Code entry must be ABSENT in the result.
153+ expect (
154+ result . find ( ( r ) => ( r as { title ?: string } ) . title === 'Pure Code' ) ,
155+ ) . toBeUndefined ( )
156+
157+ // Main code remains present (FRAME → showMainCode true).
158+ expect (
159+ result . find ( ( r ) => ( r as { title ?: string } ) . title === 'PureFail' ) ,
160+ ) . toBeDefined ( )
161+ } )
162+ } )
0 commit comments