Skip to content

Commit 39b0dca

Browse files
committed
add more tests
1 parent 04bea86 commit 39b0dca

File tree

5 files changed

+114
-31
lines changed

5 files changed

+114
-31
lines changed

packages/xrpl/src/models/transactions/common.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,27 +1053,31 @@ export function encodeMPTokenMetadata(
10531053
let input = mptokenMetadata as unknown as Record<string, unknown>
10541054

10551055
if (!isRecord(input)) {
1056-
throw new Error('MPTokenMetadata must be JSON object')
1056+
throw new Error('MPTokenMetadata must be JSON object.')
10571057
}
10581058

10591059
input = shortenKeys(input, MPT_META_ALL_FIELDS)
10601060

10611061
if (Array.isArray(input.uris)) {
1062-
input.uris = input.uris.map((uri: unknown): unknown => {
1063-
if (isRecord(uri)) {
1064-
return shortenKeys(uri, MPT_META_URI_FIELDS)
1065-
}
1066-
return uri
1067-
})
1062+
input.uris = input.uris.map(
1063+
(uri: Record<string, unknown>): Record<string, unknown> => {
1064+
if (isRecord(uri)) {
1065+
return shortenKeys(uri, MPT_META_URI_FIELDS)
1066+
}
1067+
return uri
1068+
},
1069+
)
10681070
}
10691071

10701072
if (Array.isArray(input.us)) {
1071-
input.us = input.us.map((uri: unknown): unknown => {
1072-
if (isRecord(uri)) {
1073-
return shortenKeys(uri, MPT_META_URI_FIELDS)
1074-
}
1075-
return uri
1076-
})
1073+
input.us = input.us.map(
1074+
(uri: Record<string, unknown>): Record<string, unknown> => {
1075+
if (isRecord(uri)) {
1076+
return shortenKeys(uri, MPT_META_URI_FIELDS)
1077+
}
1078+
return uri
1079+
},
1080+
)
10771081
}
10781082

10791083
return stringToHex(JSON.stringify(input)).toUpperCase()
@@ -1150,21 +1154,25 @@ export function decodeMPTokenMetadata(input: string): MPTokenMetadata {
11501154
output = expandKeys(output, MPT_META_ALL_FIELDS)
11511155

11521156
if (Array.isArray(output.uris)) {
1153-
output.uris = output.uris.map((uri: unknown): unknown => {
1154-
if (isRecord(uri)) {
1155-
return expandKeys(uri, MPT_META_URI_FIELDS)
1156-
}
1157-
return uri
1158-
})
1157+
output.uris = output.uris.map(
1158+
(uri: Record<string, unknown>): Record<string, unknown> => {
1159+
if (isRecord(uri)) {
1160+
return expandKeys(uri, MPT_META_URI_FIELDS)
1161+
}
1162+
return uri
1163+
},
1164+
)
11591165
}
11601166

11611167
if (Array.isArray(output.us)) {
1162-
output.us = output.us.map((uri: unknown): unknown => {
1163-
if (isRecord(uri)) {
1164-
return expandKeys(uri, MPT_META_URI_FIELDS)
1165-
}
1166-
return uri
1167-
})
1168+
output.us = output.us.map(
1169+
(uri: Record<string, unknown>): Record<string, unknown> => {
1170+
if (isRecord(uri)) {
1171+
return expandKeys(uri, MPT_META_URI_FIELDS)
1172+
}
1173+
return uri
1174+
},
1175+
)
11681176
}
11691177

11701178
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Required here as output is now properly formatted

packages/xrpl/test/fixtures/transactions/mptokenMetadataEncodeDecodeData.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,55 @@
220220
}
221221
},
222222
"hex": "7B2274223A2243525950544F222C226E223A2243727970746F20546F6B656E222C2269223A2268747470733A2F2F6578616D706C652E6F72672F63727970746F2D69636F6E2E706E67222C226163223A2267616D696E67222C2264223A22412067616D696E6720746F6B656E20666F72207669727475616C20776F726C64732E222C22696E223A2247616D696E672053747564696F7320496E632E222C226173223A22657175697479222C227573223A5B7B2275223A2268747470733A2F2F67616D696E6773747564696F732E636F6D222C2263223A2277656273697465222C2274223A224D61696E2057656273697465227D2C7B2275223A2268747470733A2F2F67616D696E6773747564696F732E636F6D222C2263223A2277656273697465222C2274223A224D61696E2057656273697465227D5D2C226169223A2247616D696E672065636F73797374656D20746F6B656E222C226578747261223A7B226578747261223A226578747261227D7D"
223+
},
224+
{
225+
"testName": "with unkown null fields",
226+
"mptMetadata": {
227+
"t": "CRYPTO",
228+
"extra": null
229+
},
230+
"expectedLongForm": {
231+
"ticker": "CRYPTO",
232+
"extra": null
233+
},
234+
"hex": "7B2274223A2243525950544F222C226578747261223A6E756C6C7D"
235+
},
236+
{
237+
"testName": "multiple uris and us",
238+
"mptMetadata": {
239+
"t": "CRYPTO",
240+
"uris": [
241+
{
242+
"u": "https://gamingstudios.com",
243+
"c": "website",
244+
"t": "Main Website"
245+
}
246+
],
247+
"us": [
248+
{
249+
"uri": "https://gamingstudios.com",
250+
"category": "website",
251+
"title": "Main Website"
252+
}
253+
]
254+
},
255+
"expectedLongForm": {
256+
"ticker": "CRYPTO",
257+
"uris": [
258+
{
259+
"uri": "https://gamingstudios.com",
260+
"category": "website",
261+
"title": "Main Website"
262+
}
263+
],
264+
"us": [
265+
{
266+
"uri": "https://gamingstudios.com",
267+
"category": "website",
268+
"title": "Main Website"
269+
}
270+
]
271+
},
272+
"hex": "7B2274223A2243525950544F222C2275726973223A5B7B2275223A2268747470733A2F2F67616D696E6773747564696F732E636F6D222C2263223A2277656273697465222C2274223A224D61696E2057656273697465227D5D2C227573223A5B7B2275223A2268747470733A2F2F67616D696E6773747564696F732E636F6D222C2263223A2277656273697465222C2274223A224D61696E2057656273697465227D5D7D"
223273
}
224274
]

packages/xrpl/test/models/MPTokenIssuanceCreate.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ describe('MPTokenMetadata warnings', function () {
164164
asset_class: 'rwa',
165165
asset_subclass: 'treasury',
166166
issuer_name: 'Issuer',
167-
}
167+
uris: ['apple'],
168+
} as unknown as MPTokenMetadata
168169
const tx = {
169170
TransactionType: 'MPTokenIssuanceCreate',
170171
Account: 'rWYkbWkCeg8dP6rXALnjgZSjjLyih5NXm',
@@ -175,7 +176,7 @@ describe('MPTokenMetadata warnings', function () {
175176

176177
const expectedMessage = [
177178
MPT_META_WARNING_HEADER,
178-
'- icon should be a valid https url.',
179+
'- uris/us: should be an array of objects each with uri/u, category/c, and title/t properties.',
179180
].join('\n')
180181

181182
expect(console.warn).toHaveBeenCalledWith(

packages/xrpl/test/models/utils.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,34 @@ describe('Models Utils', function () {
603603
)
604604
assert.equal(encodedHex, testCase.hex)
605605

606-
const decoded = decodeMPTokenMetadata(encodedHex)
606+
const decoded = decodeMPTokenMetadata(encodedHex) as unknown as Record<
607+
string,
608+
unknown
609+
>
607610
assert.deepStrictEqual(decoded, testCase.expectedLongForm)
608611
})
609612
}
613+
614+
it('throws error for invalid JSON', function () {
615+
assert.throws(
616+
() =>
617+
encodeMPTokenMetadata('invalid json' as unknown as MPTokenMetadata),
618+
`MPTokenMetadata must be JSON object.`,
619+
)
620+
})
621+
622+
it('throws error for invalid hex', function () {
623+
assert.throws(
624+
() => decodeMPTokenMetadata('invalid'),
625+
`MPTokenMetadata must be in hex format.`,
626+
)
627+
})
628+
629+
it('throws error for invalid JSON underneath hex', function () {
630+
assert.throws(
631+
() => decodeMPTokenMetadata('464F4F'),
632+
`MPTokenMetadata is not properly formatted as JSON - SyntaxError: Unexpected token 'F', "FOO" is not valid JSON`,
633+
)
634+
})
610635
})
611636
})

packages/xrpl/test/models/vaultCreate.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,10 @@ describe('MPTokenMetadata warnings', function () {
126126
const mptMetaData: MPTokenMetadata = {
127127
ticker: 'TBILL',
128128
name: 'T-Bill Token',
129-
icon: 'http://example.com/icon.png',
130129
asset_class: 'rwa',
131130
asset_subclass: 'treasury',
132131
issuer_name: 'Issuer',
133-
}
132+
} as MPTokenMetadata
134133
const tx = {
135134
TransactionType: 'VaultCreate',
136135
Account: 'rfmDuhDyLGgx94qiwf3YF8BUV5j6KSvE8',
@@ -143,7 +142,7 @@ describe('MPTokenMetadata warnings', function () {
143142

144143
const expectedMessage = [
145144
MPT_META_WARNING_HEADER,
146-
'- icon should be a valid https url.',
145+
'- icon/i: should be a non-empty string.',
147146
].join('\n')
148147

149148
expect(console.warn).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)