11import { DiscoveryDocument , PaymentInfo , ServiceInfo } from './Discovery.js'
22
33describe ( 'PaymentInfo' , ( ) => {
4- test ( 'parses a valid charge payment info ' , ( ) => {
4+ test ( 'normalizes legacy shorthand to offers ' , ( ) => {
55 const result = PaymentInfo . safeParse ( {
66 amount : '1000' ,
77 intent : 'charge' ,
88 method : 'tempo' ,
99 } )
1010 expect ( result . success ) . toBe ( true )
11- expect ( result . data ) . toEqual ( { amount : '1000' , intent : 'charge' , method : 'tempo' } )
11+ expect ( result . data ) . toEqual ( {
12+ offers : [ { amount : '1000' , intent : 'charge' , method : 'tempo' } ] ,
13+ } )
14+ } )
15+
16+ test ( 'parses offers format without modification' , ( ) => {
17+ const result = PaymentInfo . safeParse ( {
18+ offers : [ { amount : '1000' , intent : 'charge' , method : 'tempo' } ] ,
19+ } )
20+ expect ( result . success ) . toBe ( true )
21+ expect ( result . data ) . toEqual ( {
22+ offers : [ { amount : '1000' , intent : 'charge' , method : 'tempo' } ] ,
23+ } )
1224 } )
1325
1426 test ( 'parses a session with null amount' , ( ) => {
@@ -18,7 +30,7 @@ describe('PaymentInfo', () => {
1830 method : 'tempo' ,
1931 } )
2032 expect ( result . success ) . toBe ( true )
21- expect ( result . data ?. amount ) . toBeNull ( )
33+ expect ( result . data ?. offers [ 0 ] ?. amount ) . toBeNull ( )
2234 } )
2335
2436 test ( 'accepts custom intents' , ( ) => {
@@ -28,7 +40,7 @@ describe('PaymentInfo', () => {
2840 method : 'tempo' ,
2941 } )
3042 expect ( result . success ) . toBe ( true )
31- expect ( result . data ?. intent ) . toBe ( 'subscribe' )
43+ expect ( result . data ?. offers [ 0 ] ?. intent ) . toBe ( 'subscribe' )
3244 } )
3345
3446 test ( 'rejects invalid amount pattern' , ( ) => {
@@ -40,13 +52,36 @@ describe('PaymentInfo', () => {
4052 expect ( result . success ) . toBe ( false )
4153 } )
4254
55+ test ( 'rejects mixed shorthand and offers shapes' , ( ) => {
56+ const result = PaymentInfo . safeParse ( {
57+ amount : '100' ,
58+ offers : [ { amount : '100' , intent : 'charge' , method : 'tempo' } ] ,
59+ } )
60+ expect ( result . success ) . toBe ( false )
61+ } )
62+
63+ test ( 'rejects empty offers arrays' , ( ) => {
64+ const result = PaymentInfo . safeParse ( { offers : [ ] } )
65+ expect ( result . success ) . toBe ( false )
66+ } )
67+
68+ test ( 'rejects malformed offers' , ( ) => {
69+ const result = PaymentInfo . safeParse ( {
70+ offers : [ { amount : '01' , intent : 'charge' , method : 'tempo' } ] ,
71+ } )
72+ expect ( result . success ) . toBe ( false )
73+ } )
74+
4375 test ( 'accepts x402 format with unknown fields' , ( ) => {
4476 const result = PaymentInfo . safeParse ( {
4577 price : '0.54' ,
4678 pricingMode : 'fixed' ,
4779 protocols : [ 'x402' , 'mpp' ] ,
4880 } )
4981 expect ( result . success ) . toBe ( true )
82+ expect ( result . data ) . toEqual ( {
83+ offers : [ { price : '0.54' , pricingMode : 'fixed' , protocols : [ 'x402' , 'mpp' ] } ] ,
84+ } )
5085 } )
5186} )
5287
@@ -118,6 +153,33 @@ describe('DiscoveryDocument', () => {
118153 } ,
119154 } )
120155 expect ( result . success ) . toBe ( true )
156+ expect ( result . data ?. paths ?. [ '/search' ] ?. post ?. [ 'x-payment-info' ] ) . toEqual ( {
157+ offers : [ { amount : '100' , intent : 'charge' , method : 'tempo' } ] ,
158+ } )
159+ } )
160+
161+ test ( 'normalizes offers-based discovery documents' , ( ) => {
162+ const result = DiscoveryDocument . safeParse ( {
163+ info : { title : 'Test' , version : '1.0.0' } ,
164+ openapi : '3.1.0' ,
165+ paths : {
166+ '/search' : {
167+ post : {
168+ 'x-payment-info' : {
169+ offers : [ { amount : '100' , intent : 'charge' , method : 'tempo' } ] ,
170+ } ,
171+ responses : {
172+ '200' : { description : 'OK' } ,
173+ '402' : { description : 'Payment Required' } ,
174+ } ,
175+ } ,
176+ } ,
177+ } ,
178+ } )
179+ expect ( result . success ) . toBe ( true )
180+ expect ( result . data ?. paths ?. [ '/search' ] ?. post ?. [ 'x-payment-info' ] ) . toEqual ( {
181+ offers : [ { amount : '100' , intent : 'charge' , method : 'tempo' } ] ,
182+ } )
121183 } )
122184
123185 test ( 'accepts path items with summary, parameters, and extensions' , ( ) => {
0 commit comments