1+ import * as t from '../test-utils/meta' ;
2+ import { A_Expr , List , ParseResult , ResTarget , SelectStmt } from '@pgsql/types' ;
3+ import { generateTsAstCodeFromPgAstWithSchema } from '../src/utils' ;
4+ import generate from '@babel/generator' ;
5+ import { runtimeSchema } from '../test-utils/meta/runtime-schema' ;
6+
7+ describe ( 'AST to AST with runtime schema' , ( ) => {
8+ it ( 'should generate t.ast.* for wrapped nodes and t.nodes.* for non-wrapped nodes' , ( ) => {
9+ // Create a simple AST using the wrapped helpers
10+ const selectStmt : { SelectStmt : SelectStmt } = t . nodes . selectStmt ( {
11+ targetList : [
12+ t . nodes . resTarget ( {
13+ val : t . nodes . columnRef ( {
14+ fields : [ t . nodes . aStar ( ) ]
15+ } )
16+ } )
17+ ] ,
18+ fromClause : [
19+ t . nodes . rangeVar ( {
20+ relname : 'users_table' ,
21+ inh : true ,
22+ relpersistence : 'p'
23+ } )
24+ ] ,
25+ limitOption : 'LIMIT_OPTION_DEFAULT' ,
26+ op : 'SETOP_NONE'
27+ } ) ;
28+
29+ // Generate code using the new function with runtime schema
30+ const astForAst = generateTsAstCodeFromPgAstWithSchema ( selectStmt , runtimeSchema ) ;
31+ const generatedCode = generate ( astForAst ) . code ;
32+ expect ( generatedCode ) . toMatchSnapshot ( ) ;
33+ } ) ;
34+
35+ it ( 'should handle mixed wrapped and non-wrapped nodes' , ( ) => {
36+ // Create an AST with both wrapped and non-wrapped nodes
37+ const testAst : { A_Expr : A_Expr } = t . nodes . aExpr ( {
38+ kind : 'AEXPR_OP' ,
39+ name : [ t . nodes . string ( { sval : '=' } ) ] ,
40+ lexpr : t . nodes . columnRef ( {
41+ fields : [ t . nodes . string ( { sval : 'id' } ) ]
42+ } ) ,
43+ rexpr : t . nodes . aConst ( {
44+ ival : t . ast . integer ( { ival : 42 } )
45+ } )
46+ } ) ;
47+
48+ const astForAst = generateTsAstCodeFromPgAstWithSchema ( testAst , runtimeSchema ) ;
49+ const generatedCode = generate ( astForAst ) . code ;
50+ expect ( generatedCode ) . toMatchSnapshot ( ) ;
51+ } ) ;
52+
53+ it ( 'should handle arrays of nodes' , ( ) => {
54+ const testAst : { List : List } = t . nodes . list ( {
55+ items : [
56+ t . nodes . string ( { sval : 'first' } ) ,
57+ t . nodes . string ( { sval : 'second' } ) ,
58+ t . nodes . string ( { sval : 'third' } )
59+ ]
60+ } ) ;
61+
62+ const astForAst = generateTsAstCodeFromPgAstWithSchema ( testAst , runtimeSchema ) ;
63+ const generatedCode = generate ( astForAst ) . code ;
64+ expect ( generatedCode ) . toMatchSnapshot ( ) ;
65+ } ) ;
66+
67+ it ( 'should handle empty objects and null values' , ( ) => {
68+ const testAst : { ResTarget : ResTarget } = t . nodes . resTarget ( {
69+ name : null ,
70+ val : t . nodes . columnRef ( {
71+ fields : [ t . nodes . aStar ( ) ]
72+ } )
73+ } ) ;
74+
75+ const astForAst = generateTsAstCodeFromPgAstWithSchema ( testAst , runtimeSchema ) ;
76+ const generatedCode = generate ( astForAst ) . code ;
77+ expect ( generatedCode ) . toMatchSnapshot ( ) ;
78+ } ) ;
79+
80+ it ( 'should use t.nodes.* for non-wrapped nodes' , ( ) => {
81+ // Test that unwrapped nodes (plain objects) are converted to t.nodes.* calls
82+ const testAst : { ParseResult : ParseResult } = t . nodes . parseResult ( {
83+ version : 160001 ,
84+ stmts : [
85+ t . ast . rawStmt ( {
86+ stmt : {
87+ // This is an unwrapped SelectStmt - just a plain object with SelectStmt fields
88+ targetList : [
89+ {
90+ ResTarget : {
91+ val : {
92+ ColumnRef : {
93+ fields : [
94+ { A_Star : { } }
95+ ]
96+ }
97+ }
98+ }
99+ }
100+ ] ,
101+ limitOption : 'LIMIT_OPTION_DEFAULT' ,
102+ op : 'SETOP_NONE'
103+ }
104+ } )
105+ ]
106+ } ) ;
107+
108+ const astForAst = generateTsAstCodeFromPgAstWithSchema ( testAst , runtimeSchema ) ;
109+ const generatedCode = generate ( astForAst ) . code ;
110+ expect ( generatedCode ) . toMatchSnapshot ( ) ;
111+ } ) ;
112+ } ) ;
0 commit comments