Skip to content

Commit fa9e54f

Browse files
committed
Add unit tests for TokenModuleReference and TokenId
1 parent 30e36e5 commit fa9e54f

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

packages/sdk/src/plt/TokenId.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export type JSON = string;
88
const MAX_LENGTH = 256;
99

1010
/**
11-
* Enum representing the types of errors that can occur with token amounts.
11+
* Enum representing the types of errors that can occur with token IDs.
1212
*/
1313
export enum ErrorType {
14-
/** Error type indicating the length of module reference is incorrect. */
14+
/** Error type indicating the length length exceeds the max allowed. */
1515
EXCEEDS_MAX_LENGTH = 'EXCEEDS_MAX_LENGTH',
1616
}
1717

1818
/**
19-
* Custom error to represent issues with token amounts.
19+
* Custom error to represent issues with token IDs.
2020
*/
2121
export class Err extends Error {
2222
private constructor(
@@ -29,9 +29,9 @@ export class Err extends Error {
2929
}
3030

3131
/**
32-
* Creates a TokenId.Err indicating the length of module reference is incorrect.
32+
* Creates a TokenId.Err indicating the length exceeds the max allowed.
3333
*/
34-
public static incorrectLength(): Err {
34+
public static exceedsMaxLength(): Err {
3535
return new Err(
3636
ErrorType.EXCEEDS_MAX_LENGTH,
3737
`Token ID's cannot be longer than ${MAX_LENGTH} utf-8 encoded bytes`
@@ -58,7 +58,7 @@ class TokenId {
5858
) {
5959
// Check if the value exceeds 256 UTF-8 bytes
6060
if (new TextEncoder().encode(symbol).length > MAX_LENGTH) {
61-
throw Err.incorrectLength();
61+
throw Err.exceedsMaxLength();
6262
}
6363
}
6464

packages/sdk/src/plt/TokenModuleReference.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ const MODULE_REF_BYTE_LENGTH = 32;
1010
export type JSON = HexString;
1111

1212
/**
13-
* Enum representing the types of errors that can occur with token amounts.
13+
* Enum representing the types of errors that can occur with token module references.
1414
*/
1515
export enum ErrorType {
1616
/** Error type indicating the length of module reference is incorrect. */
1717
INCORRECT_LENGTH = 'INCORRECT_LENGTH',
1818
}
1919

2020
/**
21-
* Custom error to represent issues with token amounts.
21+
* Custom error to represent issues with token module references.
2222
*/
2323
export class Err extends Error {
2424
private constructor(
@@ -33,10 +33,11 @@ export class Err extends Error {
3333
/**
3434
* Creates a TokenModuleReference.Err indicating the length of module reference is incorrect.
3535
*/
36-
public static incorrectLength(moduleRef: Type): Err {
36+
public static incorrectLength(bytes: Uint8Array): Err {
37+
const hex = Buffer.from(bytes).toString('hex');
3738
return new Err(
3839
ErrorType.INCORRECT_LENGTH,
39-
`Token module reference ${moduleRef.toString()} is invalid, as it must contain ${MODULE_REF_BYTE_LENGTH} bytes`
40+
`Token module reference ${hex} is invalid, as it must contain ${MODULE_REF_BYTE_LENGTH} bytes`
4041
);
4142
}
4243
}
@@ -59,7 +60,7 @@ class ModuleReference {
5960
public readonly bytes: Uint8Array
6061
) {
6162
if (bytes.byteLength !== MODULE_REF_BYTE_LENGTH) {
62-
throw Err.incorrectLength(this);
63+
throw Err.incorrectLength(bytes);
6364
}
6465
}
6566

@@ -114,7 +115,7 @@ export function fromBuffer(buffer: ArrayBuffer): ModuleReference {
114115
* @throws {Err} If the value is not exactly 32 bytes.
115116
*/
116117
export function fromHexString(moduleRef: HexString): ModuleReference {
117-
return new ModuleReference(new Uint8Array(Buffer.from(moduleRef, 'hex')));
118+
return fromBuffer(Buffer.from(moduleRef, 'hex'));
118119
}
119120

120121
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { TokenId } from '../../../src/plt/types.ts';
2+
3+
describe('PLT TokenId', () => {
4+
test('Creates token IDs as expected', () => {
5+
const str = 'DKK';
6+
const expected = TokenId.fromString(str);
7+
8+
expect(TokenId.fromString(str).symbol).toEqual(str);
9+
10+
const bytes = new TextEncoder().encode(str);
11+
expect(TokenId.fromBytes(bytes)).toEqual(expected);
12+
13+
const json = str;
14+
expect(TokenId.fromJSON(json)).toEqual(expected);
15+
16+
expect(() => TokenId.fromString('T'.repeat(256))).not.toThrow()
17+
});
18+
19+
test('JSON id test', () => {
20+
const json = 'TOKEN';
21+
expect(TokenId.fromJSON(json).toJSON()).toEqual(json);
22+
});
23+
24+
test('Throws errors on invalid values', () => {
25+
expect(() => TokenId.fromString('T'.repeat(257))).toThrow(TokenId.Err.exceedsMaxLength())
26+
})
27+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { TokenModuleReference } from '../../../src/plt/types.ts';
2+
3+
describe('PLT TokenModuleReference', () => {
4+
test('Creates token module references as expected', () => {
5+
const hex = '0ea8121fdc427c9b23ae5e26cfea3e8cbb544c84aa0c82db26a85949ce1706c3';
6+
const bytes = Buffer.from(hex, 'hex');
7+
const expected = TokenModuleReference.fromBuffer(bytes);
8+
9+
expect(expected.bytes).toEqual(new Uint8Array(bytes));
10+
expect(TokenModuleReference.fromHexString(hex)).toEqual(expected);
11+
});
12+
13+
test('JSON id test', () => {
14+
const json = '0ea8121fdc427c9b23ae5e26cfea3e8cbb544c84aa0c82db26a85949ce1706c3';
15+
expect(TokenModuleReference.fromJSON(json).toJSON()).toEqual(json);
16+
});
17+
18+
test('Throws errors on invalid values', () => {
19+
let bytes = Buffer.from('0EA8121FDC427C9B23AE5E26CFEA3E8CBB544C84AA0C82DB26A85949CE1706', 'hex');
20+
expect(() => TokenModuleReference.fromBuffer(bytes)).toThrow(TokenModuleReference.Err.incorrectLength(bytes));
21+
22+
bytes = Buffer.from('0EA8121FDC427C9B23AE5E26CFEA3E8CBB544C84AA0C82DB26A85949CE1706E1706', 'hex');
23+
expect(() => TokenModuleReference.fromBuffer(bytes)).toThrow(TokenModuleReference.Err.incorrectLength(bytes));
24+
25+
const invalidHex = '0EA8121FDC427C9B23AE5E26CFEA3E8CBB544C84AA0C82DB26A85949CE1706E170'
26+
expect(() => TokenModuleReference.fromHexString(invalidHex)).toThrow();
27+
});
28+
});

0 commit comments

Comments
 (0)