1+ /**
2+ * Tests for XDR types and type guards
3+ */
4+
5+ import {
6+ XdrSchema ,
7+ XdrIntSchema ,
8+ XdrStringSchema ,
9+ XdrStructSchema ,
10+ XdrEnumSchema ,
11+ XdrUnionSchema ,
12+ XdrVoidSchema ,
13+ XdrBooleanSchema ,
14+ XdrFixedArraySchema ,
15+ isXdrPrimitiveSchema ,
16+ isXdrCompositeSchema ,
17+ isXdrNamedSchema ,
18+ XDR_BLOCK_SIZE ,
19+ XDR_MAX_STRING_LENGTH ,
20+ XDR_MAX_ARRAY_LENGTH
21+ } from '../types' ;
22+
23+ describe ( 'XDR Types' , ( ) => {
24+ describe ( 'primitive schemas' , ( ) => {
25+ it ( 'creates valid void schema' , ( ) => {
26+ const voidSchema : XdrVoidSchema = {
27+ type : 'void' ,
28+ doc : 'No data'
29+ } ;
30+ expect ( voidSchema . type ) . toBe ( 'void' ) ;
31+ expect ( isXdrPrimitiveSchema ( voidSchema ) ) . toBe ( true ) ;
32+ expect ( isXdrCompositeSchema ( voidSchema ) ) . toBe ( false ) ;
33+ } ) ;
34+
35+ it ( 'creates valid boolean schema' , ( ) => {
36+ const boolSchema : XdrBooleanSchema = {
37+ type : 'boolean'
38+ } ;
39+ expect ( boolSchema . type ) . toBe ( 'boolean' ) ;
40+ expect ( isXdrPrimitiveSchema ( boolSchema ) ) . toBe ( true ) ;
41+ } ) ;
42+
43+ it ( 'creates valid int schema' , ( ) => {
44+ const intSchema : XdrIntSchema = {
45+ type : 'int' ,
46+ doc : 'A 32-bit signed integer'
47+ } ;
48+ expect ( intSchema . type ) . toBe ( 'int' ) ;
49+ expect ( intSchema . doc ) . toBe ( 'A 32-bit signed integer' ) ;
50+ expect ( isXdrPrimitiveSchema ( intSchema ) ) . toBe ( true ) ;
51+ } ) ;
52+
53+ it ( 'creates valid enum schema' , ( ) => {
54+ const enumSchema : XdrEnumSchema = {
55+ type : 'enum' ,
56+ name : 'Color' ,
57+ values : {
58+ RED : 0 ,
59+ GREEN : 1 ,
60+ BLUE : 2
61+ }
62+ } ;
63+ expect ( enumSchema . type ) . toBe ( 'enum' ) ;
64+ expect ( enumSchema . name ) . toBe ( 'Color' ) ;
65+ expect ( enumSchema . values . RED ) . toBe ( 0 ) ;
66+ expect ( isXdrPrimitiveSchema ( enumSchema ) ) . toBe ( true ) ;
67+ expect ( isXdrNamedSchema ( enumSchema ) ) . toBe ( true ) ;
68+ } ) ;
69+ } ) ;
70+
71+ describe ( 'composite schemas' , ( ) => {
72+ it ( 'creates valid string schema' , ( ) => {
73+ const stringSchema : XdrStringSchema = {
74+ type : 'string' ,
75+ maxLength : 255
76+ } ;
77+ expect ( stringSchema . type ) . toBe ( 'string' ) ;
78+ expect ( stringSchema . maxLength ) . toBe ( 255 ) ;
79+ expect ( isXdrCompositeSchema ( stringSchema ) ) . toBe ( true ) ;
80+ expect ( isXdrPrimitiveSchema ( stringSchema ) ) . toBe ( false ) ;
81+ } ) ;
82+
83+ it ( 'creates valid fixed array schema' , ( ) => {
84+ const arraySchema : XdrFixedArraySchema = {
85+ type : 'fixed_array' ,
86+ elements : { type : 'int' } ,
87+ length : 10
88+ } ;
89+ expect ( arraySchema . type ) . toBe ( 'fixed_array' ) ;
90+ expect ( arraySchema . length ) . toBe ( 10 ) ;
91+ expect ( isXdrCompositeSchema ( arraySchema ) ) . toBe ( true ) ;
92+ } ) ;
93+
94+ it ( 'creates valid struct schema' , ( ) => {
95+ const structSchema : XdrStructSchema = {
96+ type : 'struct' ,
97+ name : 'Person' ,
98+ fields : [
99+ {
100+ name : 'id' ,
101+ type : { type : 'int' } ,
102+ doc : 'Unique identifier'
103+ } ,
104+ {
105+ name : 'name' ,
106+ type : { type : 'string' }
107+ }
108+ ]
109+ } ;
110+ expect ( structSchema . type ) . toBe ( 'struct' ) ;
111+ expect ( structSchema . name ) . toBe ( 'Person' ) ;
112+ expect ( structSchema . fields ) . toHaveLength ( 2 ) ;
113+ expect ( structSchema . fields [ 0 ] . name ) . toBe ( 'id' ) ;
114+ expect ( isXdrCompositeSchema ( structSchema ) ) . toBe ( true ) ;
115+ expect ( isXdrNamedSchema ( structSchema ) ) . toBe ( true ) ;
116+ } ) ;
117+
118+ it ( 'creates valid union schema' , ( ) => {
119+ const unionSchema : XdrUnionSchema = {
120+ type : 'union' ,
121+ name : 'Result' ,
122+ discriminant : { type : 'int' } ,
123+ cases : [
124+ {
125+ value : 0 ,
126+ type : { type : 'void' }
127+ } ,
128+ {
129+ value : 1 ,
130+ type : { type : 'string' }
131+ }
132+ ]
133+ } ;
134+ expect ( unionSchema . type ) . toBe ( 'union' ) ;
135+ expect ( unionSchema . name ) . toBe ( 'Result' ) ;
136+ expect ( unionSchema . cases ) . toHaveLength ( 2 ) ;
137+ expect ( isXdrCompositeSchema ( unionSchema ) ) . toBe ( true ) ;
138+ expect ( isXdrNamedSchema ( unionSchema ) ) . toBe ( true ) ;
139+ } ) ;
140+ } ) ;
141+
142+ describe ( 'type guards' , ( ) => {
143+ it ( 'correctly identifies primitive schemas' , ( ) => {
144+ expect ( isXdrPrimitiveSchema ( { type : 'int' } ) ) . toBe ( true ) ;
145+ expect ( isXdrPrimitiveSchema ( { type : 'boolean' } ) ) . toBe ( true ) ;
146+ expect ( isXdrPrimitiveSchema ( { type : 'float' } ) ) . toBe ( true ) ;
147+ expect ( isXdrPrimitiveSchema ( { type : 'string' } ) ) . toBe ( false ) ;
148+ expect ( isXdrPrimitiveSchema ( 'int' ) ) . toBe ( false ) ;
149+ } ) ;
150+
151+ it ( 'correctly identifies composite schemas' , ( ) => {
152+ expect ( isXdrCompositeSchema ( { type : 'string' } ) ) . toBe ( true ) ;
153+ expect ( isXdrCompositeSchema ( { type : 'struct' , name : 'Test' , fields : [ ] } ) ) . toBe ( true ) ;
154+ expect ( isXdrCompositeSchema ( { type : 'int' } ) ) . toBe ( false ) ;
155+ expect ( isXdrCompositeSchema ( 'string' ) ) . toBe ( false ) ;
156+ } ) ;
157+
158+ it ( 'correctly identifies named schemas' , ( ) => {
159+ expect ( isXdrNamedSchema ( { type : 'struct' , name : 'Test' , fields : [ ] } ) ) . toBe ( true ) ;
160+ expect ( isXdrNamedSchema ( { type : 'enum' , name : 'Test' , values : { } } ) ) . toBe ( true ) ;
161+ expect ( isXdrNamedSchema ( { type : 'union' , name : 'Test' , discriminant : { type : 'int' } , cases : [ ] } ) ) . toBe ( true ) ;
162+ expect ( isXdrNamedSchema ( { type : 'string' } ) ) . toBe ( false ) ;
163+ expect ( isXdrNamedSchema ( 'struct' ) ) . toBe ( false ) ;
164+ } ) ;
165+ } ) ;
166+
167+ describe ( 'constants' , ( ) => {
168+ it ( 'defines correct XDR constants' , ( ) => {
169+ expect ( XDR_BLOCK_SIZE ) . toBe ( 4 ) ;
170+ expect ( XDR_MAX_STRING_LENGTH ) . toBe ( 2 ** 32 - 1 ) ;
171+ expect ( XDR_MAX_ARRAY_LENGTH ) . toBe ( 2 ** 32 - 1 ) ;
172+ } ) ;
173+ } ) ;
174+
175+ describe ( 'schema arrays' , ( ) => {
176+ it ( 'accepts mixed schema types' , ( ) => {
177+ const schemas : XdrSchema [ ] = [
178+ { type : 'int' } ,
179+ { type : 'string' } ,
180+ 'int' ,
181+ { type : 'struct' , name : 'Test' , fields : [ ] }
182+ ] ;
183+ expect ( schemas ) . toHaveLength ( 4 ) ;
184+ } ) ;
185+ } ) ;
186+ } ) ;
0 commit comments