This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
undernames.test.ts
213 lines (196 loc) · 6.24 KB
/
undernames.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { Contract, JWKInterface } from 'warp-contracts';
import {
INVALID_INPUT_MESSAGE,
MAX_ALLOWED_UNDERNAMES,
} from '../src/constants';
import { IOState } from '../src/types';
import { getLocalArNSContractKey, getLocalWallet } from './utils/helper';
import { warp } from './utils/services';
describe('undernames', () => {
let contract: Contract<IOState>;
let srcContractId: string;
let nonContractOwner: JWKInterface;
let prevState: IOState;
const arnsName = 'name-1';
beforeAll(async () => {
srcContractId = getLocalArNSContractKey('id');
nonContractOwner = getLocalWallet(1);
contract = warp.contract<IOState>(srcContractId).connect(nonContractOwner);
});
beforeEach(async () => {
// tick so we are always working off freshest state
await contract.writeInteraction({ function: 'tick' });
prevState = (await contract.readState()).cachedValue.state as IOState;
});
describe('Submits undername increase', () => {
it.each([
'',
'*&*##$%#',
'-leading',
'this-is-a-looong-name-a-verrrryyyyy-loooooong-name-that-is-too-long',
'test.subdomain.name',
false,
true,
0,
1,
3.5,
])(
'should throw an error when an invalid name is submitted: %s',
async (badName) => {
const undernameInput = {
name: badName,
qty: 1,
};
const writeInteraction = await contract.writeInteraction(
{
function: 'increaseUndernameCount',
...undernameInput,
},
{
disableBundling: true,
},
);
expect(writeInteraction?.originalTxId).not.toBe(undefined);
const { cachedValue } = await contract.readState();
expect(Object.keys(cachedValue.errorMessages)).toContain(
writeInteraction?.originalTxId,
);
expect(
cachedValue.errorMessages[writeInteraction?.originalTxId],
).toEqual(expect.stringContaining(INVALID_INPUT_MESSAGE));
expect(cachedValue.state).toEqual(prevState);
},
);
it.each([
'',
'*&*##$%#',
'-leading',
'this-is-a-looong-name-a-verrrryyyyy-loooooong-name-that-is-too-long',
'test.subdomain.name',
false,
true,
0.5,
0,
Infinity,
-Infinity,
-1,
-1000,
])(
'should throw an error when an invalid quantity is provided: %s',
async (badQty) => {
const undernameInput = {
name: arnsName,
qty: badQty,
};
const writeInteraction = await contract.writeInteraction(
{
function: 'increaseUndernameCount',
...undernameInput,
},
{
disableBundling: true,
},
);
expect(writeInteraction?.originalTxId).not.toBe(undefined);
const { cachedValue } = await contract.readState();
expect(Object.keys(cachedValue.errorMessages)).toContain(
writeInteraction?.originalTxId,
);
expect(
cachedValue.errorMessages[writeInteraction?.originalTxId],
).toEqual(expect.stringContaining(INVALID_INPUT_MESSAGE));
expect(cachedValue.state).toEqual(prevState);
},
);
it.each([
MAX_ALLOWED_UNDERNAMES,
MAX_ALLOWED_UNDERNAMES + 1,
Number.MAX_SAFE_INTEGER,
])(
'should throw an error when a quantity over the max allowed undernames is provided: %s',
async (badQty) => {
const undernameInput = {
name: arnsName,
qty: badQty,
};
const writeInteraction = await contract.writeInteraction(
{
function: 'increaseUndernameCount',
...undernameInput,
},
{
disableBundling: true,
},
);
expect(writeInteraction?.originalTxId).not.toBe(undefined);
const { cachedValue } = await contract.readState();
expect(Object.keys(cachedValue.errorMessages)).toContain(
writeInteraction?.originalTxId,
);
expect(
cachedValue.errorMessages[writeInteraction?.originalTxId],
).toEqual(expect.stringContaining(INVALID_INPUT_MESSAGE));
expect(cachedValue.state).toEqual(prevState);
},
);
});
describe('with valid input', () => {
const arnsName = 'name-1';
it.each([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000])(
'should successfully increase undernames with valid quantity provided: %s',
async (goodQty) => {
const undernameInput = {
name: arnsName,
qty: goodQty,
};
const initialUndernameCount = prevState.records[arnsName].undernames;
const writeInteraction = await contract.writeInteraction(
{
function: 'increaseUndernameCount',
...undernameInput,
},
{
disableBundling: true,
},
);
expect(writeInteraction?.originalTxId).not.toBe(undefined);
const { cachedValue } = await contract.readState();
expect(Object.keys(cachedValue.errorMessages)).not.toContain(
writeInteraction?.originalTxId,
);
expect(cachedValue.state.records[arnsName].undernames).toEqual(
initialUndernameCount + goodQty,
);
// TODO: balance checks
},
);
it.each(['name-1', 'name-2', 'name-3'])(
'should successfully increase undernames with valid name provided: %s',
async (validName) => {
const undernameInput = {
name: validName,
qty: 1,
};
const initialUndernameCount = prevState.records[validName].undernames;
const writeInteraction = await contract.writeInteraction(
{
function: 'increaseUndernameCount',
...undernameInput,
},
{
disableBundling: true,
},
);
expect(writeInteraction?.originalTxId).not.toBe(undefined);
const { cachedValue } = await contract.readState();
expect(Object.keys(cachedValue.errorMessages)).not.toContain(
writeInteraction?.originalTxId,
);
expect(cachedValue.state.records[validName].undernames).toEqual(
initialUndernameCount + 1,
);
// TODO: balance checks
},
);
});
});