Skip to content

Commit bea4236

Browse files
a6b8claude
andcommitted
Add unit tests for >80% coverage #11
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ffd6fa5 commit bea4236

20 files changed

Lines changed: 3478 additions & 0 deletions

tests/helpers/config.mjs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// --- Contract Catalog ---
2+
3+
const VALID_CONTRACT_CATALOG = {
4+
'usdc-base': {
5+
paymentNetworkId: 'eip155:8453',
6+
address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
7+
decimals: 6,
8+
domainName: 'USD Coin',
9+
domainVersion: '2'
10+
},
11+
'usdc-avax': {
12+
paymentNetworkId: 'eip155:43114',
13+
address: '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E',
14+
decimals: 6,
15+
domainName: 'USD Coin',
16+
domainVersion: '2'
17+
}
18+
}
19+
20+
21+
// --- Payment Option Catalog ---
22+
23+
const VALID_PAYMENT_OPTION_CATALOG = {
24+
'option-base-usdc': {
25+
contractId: 'usdc-base',
26+
amount: '1000000',
27+
payTo: '0x1234567890abcdef1234567890abcdef12345678'
28+
},
29+
'option-avax-usdc': {
30+
contractId: 'usdc-avax',
31+
amount: '2000000',
32+
payTo: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
33+
}
34+
}
35+
36+
37+
// --- Server Pay To Address Map ---
38+
39+
const VALID_SERVER_PAY_TO_ADDRESS_MAP = {
40+
'merchant-wallet': '0x1234567890abcdef1234567890abcdef12345678'
41+
}
42+
43+
44+
// --- Sample Accepts Entry ---
45+
46+
const SAMPLE_ACCEPTS_ENTRY = {
47+
scheme: 'exact',
48+
network: 'eip155:8453',
49+
amount: '1000000',
50+
asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
51+
payTo: '0x1234567890abcdef1234567890abcdef12345678',
52+
maxTimeoutSeconds: 300,
53+
extra: {
54+
name: 'USDC',
55+
version: '2'
56+
}
57+
}
58+
59+
60+
// --- Sample Payment Payload ---
61+
62+
const SAMPLE_PAYMENT_PAYLOAD = {
63+
x402Version: 2,
64+
scheme: 'exact',
65+
network: 'eip155:8453',
66+
payload: {
67+
signature: '0xdeadbeef',
68+
authorization: {
69+
from: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
70+
to: '0x1234567890abcdef1234567890abcdef12345678',
71+
value: '1000000',
72+
validAfter: '0',
73+
validBefore: '9999999999',
74+
nonce: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
75+
}
76+
}
77+
}
78+
79+
80+
// --- Sample Settlement Response ---
81+
82+
const SAMPLE_SETTLEMENT = {
83+
success: true,
84+
network: 'eip155:8453',
85+
transactionHash: '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890',
86+
payer: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
87+
}
88+
89+
90+
export {
91+
VALID_CONTRACT_CATALOG,
92+
VALID_PAYMENT_OPTION_CATALOG,
93+
VALID_SERVER_PAY_TO_ADDRESS_MAP,
94+
SAMPLE_ACCEPTS_ENTRY,
95+
SAMPLE_PAYMENT_PAYLOAD,
96+
SAMPLE_SETTLEMENT
97+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { describe, test, expect } from '@jest/globals'
2+
import { Base64JsonCodec } from '../../src/v2/transports/http/base64JsonCodec.mjs'
3+
4+
5+
describe( 'Base64JsonCodec', () => {
6+
describe( 'encodeBase64Json', () => {
7+
test( 'encodes object to base64 string', () => {
8+
const { base64String, encodeError } = Base64JsonCodec
9+
.encodeBase64Json( { plainObjectToEncode: { hello: 'world' } } )
10+
11+
expect( encodeError ).toBeNull()
12+
expect( typeof base64String ).toBe( 'string' )
13+
expect( base64String.length ).toBeGreaterThan( 0 )
14+
} )
15+
16+
17+
test( 'returns error for undefined input', () => {
18+
const { base64String, encodeError } = Base64JsonCodec
19+
.encodeBase64Json( { plainObjectToEncode: undefined } )
20+
21+
expect( base64String ).toBeNull()
22+
expect( encodeError.errorCode ).toBe( 'invalid_payload' )
23+
} )
24+
25+
26+
test( 'returns error for null input', () => {
27+
const { base64String, encodeError } = Base64JsonCodec
28+
.encodeBase64Json( { plainObjectToEncode: null } )
29+
30+
expect( base64String ).toBeNull()
31+
expect( encodeError.errorCode ).toBe( 'invalid_payload' )
32+
} )
33+
34+
35+
test( 'encodes complex nested object', () => {
36+
const complex = { a: [ 1, 2 ], b: { c: true } }
37+
const { base64String, encodeError } = Base64JsonCodec
38+
.encodeBase64Json( { plainObjectToEncode: complex } )
39+
40+
expect( encodeError ).toBeNull()
41+
expect( base64String ).toBeDefined()
42+
} )
43+
} )
44+
45+
46+
describe( 'decodeBase64Json', () => {
47+
test( 'decodes base64 string to object', () => {
48+
const original = { hello: 'world', number: 42 }
49+
const { base64String } = Base64JsonCodec
50+
.encodeBase64Json( { plainObjectToEncode: original } )
51+
52+
const { decodedPlainObject, decodeError } = Base64JsonCodec
53+
.decodeBase64Json( { base64StringToDecode: base64String } )
54+
55+
expect( decodeError ).toBeNull()
56+
expect( decodedPlainObject ).toEqual( original )
57+
} )
58+
59+
60+
test( 'returns error for undefined input', () => {
61+
const { decodedPlainObject, decodeError } = Base64JsonCodec
62+
.decodeBase64Json( { base64StringToDecode: undefined } )
63+
64+
expect( decodedPlainObject ).toBeNull()
65+
expect( decodeError.errorCode ).toBe( 'invalid_payload' )
66+
} )
67+
68+
69+
test( 'returns error for non-string input', () => {
70+
const { decodedPlainObject, decodeError } = Base64JsonCodec
71+
.decodeBase64Json( { base64StringToDecode: 12345 } )
72+
73+
expect( decodedPlainObject ).toBeNull()
74+
expect( decodeError.errorMessage ).toBe( 'Input must be a string' )
75+
} )
76+
77+
78+
test( 'returns error for invalid base64 json', () => {
79+
const { decodedPlainObject, decodeError } = Base64JsonCodec
80+
.decodeBase64Json( { base64StringToDecode: 'not-valid-base64-json!!!' } )
81+
82+
expect( decodedPlainObject ).toBeNull()
83+
expect( decodeError.errorMessage ).toContain( 'Base64/JSON decode failed' )
84+
} )
85+
} )
86+
} )

0 commit comments

Comments
 (0)