This repository was archived by the owner on Aug 28, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathGateway.ts
233 lines (180 loc) · 7.24 KB
/
Gateway.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* eslint-disable dot-notation */
import unknownTest, { TestInterface } from 'ava';
import { RequestHandler } from '@klasa/request-handler';
import { UserStore } from '@klasa/core';
import { createClient } from './lib/SettingsClient';
import {
Gateway,
KlasaClient,
Provider,
Settings,
SettingsExistenceStatus,
Schema
} from '../src';
const ava = unknownTest as TestInterface<{
client: KlasaClient,
schema: Schema
}>;
ava.beforeEach(async (test): Promise<void> => {
test.context = {
client: createClient(),
schema: new Schema()
};
});
ava('Gateway Properties', (test): void => {
test.plan(9);
const gateway = new Gateway(test.context.client, 'MockGateway');
test.is(gateway.client, test.context.client);
test.is(gateway.name, 'MockGateway');
test.is(gateway.provider, test.context.client.providers.get('json'));
test.is(gateway.ready, false);
test.true(gateway.requestHandler instanceof RequestHandler);
test.true(gateway.requestHandler.available);
test.true(gateway.schema instanceof Schema);
test.is(gateway.schema.size, 0);
test.deepEqual(gateway.toJSON(), {
name: 'MockGateway',
provider: 'json',
schema: {}
});
});
ava('Gateway#schema', (test): void => {
const gateway = new Gateway(test.context.client, 'MockGateway', { schema: test.context.schema });
test.is(gateway.schema, test.context.schema);
});
ava('Gateway#init', async (test): Promise<void> => {
test.plan(7);
const gateway = new Gateway(test.context.client, 'MockGateway', { schema: test.context.schema });
const provider = gateway.provider as Provider;
// Uninitialized gateway
test.false(gateway.ready);
test.false(gateway.schema.ready);
test.false(await provider.hasTable(gateway.name));
// Initialize gateway
test.is(await gateway.init(), undefined);
// Initialized gateway
test.true(gateway.ready);
test.true(gateway.schema.ready);
test.true(await provider.hasTable(gateway.name));
});
ava('Gateway#init (No Provider)', async (test): Promise<void> => {
test.plan(2);
const gateway = new Gateway(test.context.client, 'MockGateway', { schema: test.context.schema });
test.context.client.providers.clear();
test.is(gateway.provider, null);
await test.throwsAsync(() => gateway.init(), { message: 'The gateway "MockGateway" could not find the provider "json".' });
});
ava('Gateway#init (Ready)', async (test): Promise<void> => {
const gateway = new Gateway(test.context.client, 'MockGateway', { schema: test.context.schema });
await gateway.init();
await test.throwsAsync(() => gateway.init(), { message: 'The gateway "MockGateway" has already been initialized.' });
});
ava('Gateway#init (Broken Schema)', async (test): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
test.context.schema.add('key', 'String', { array: null });
const gateway = new Gateway(test.context.client, 'MockGateway', { schema: test.context.schema });
await test.throwsAsync(() => gateway.init(), {
message: [
'[SCHEMA] There is an error with your schema.',
"[KEY] key - Parameter 'array' must be a boolean."
].join('\n')
});
});
ava('Gateway#sync', async (test): Promise<void> => {
const gateway = new Gateway(test.context.client, 'MockGateway', { schema: test.context.schema });
test.is(await gateway.sync(), gateway);
});
ava('Gateway (Reverse Proxy Sync)', (test): void => {
test.plan(2);
const gateway = new Gateway(test.context.client, 'users');
test.true(gateway.cache instanceof UserStore);
test.is(gateway.cache.size, 0);
});
ava('Gateway#get', (test): void => {
const gateway = new Gateway(test.context.client, 'test');
test.is(gateway.get('id'), null);
});
ava('Gateway#create', (test): void => {
test.plan(2);
const gateway = new Gateway(test.context.client, 'test');
const created = gateway.create({ id: 'id' });
test.true(created instanceof Settings);
test.is(created.id, 'id');
});
ava('Gateway#acquire', (test): void => {
test.plan(2);
const gateway = new Gateway(test.context.client, 'test');
const acquired = gateway.acquire({ id: 'id' });
test.true(acquired instanceof Settings);
test.is(acquired.id, 'id');
});
ava('Gateway#init (Table Existence In Database)', async (test): Promise<void> => {
test.plan(2);
const gateway = new Gateway(test.context.client, 'test');
const provider = gateway.provider as Provider;
test.false(await provider.hasTable(gateway.name));
await gateway.init();
test.true(await provider.hasTable(gateway.name));
});
ava('Gateway (Direct Sync | No Provider)', async (test): Promise<void> => {
test.plan(2);
const { client } = test.context;
client.providers.clear();
const gateway = client.gateways.get('users') as Gateway;
test.is(gateway.provider, null);
const settings = new Settings(gateway, { id: 'Mock' }, 'Mock');
await test.throwsAsync(() => settings.sync(), { message: 'Cannot run requests without a provider available.' });
});
ava('Gateway (Multiple Direct Sync | No Provider)', async (test): Promise<void> => {
test.plan(2);
const { client } = test.context;
client.providers.clear();
const gateway = client.gateways.get('users') as Gateway;
test.is(gateway.provider, null);
const settings = [
new Settings(gateway, { id: 'Mock1' }, 'Mock1'),
new Settings(gateway, { id: 'Mock2' }, 'Mock2'),
new Settings(gateway, { id: 'Mock3' }, 'Mock3')
];
await test.throwsAsync(() => Promise.all(settings.map(instance => instance.sync())), { message: 'Cannot run requests without a provider available.' });
});
ava('Gateway (Reverse Proxy Sync | With Data)', (test): void => {
test.plan(2);
const { client } = test.context;
const gateway = client.gateways.get('users') as Gateway;
client.users['_add']({
id: '339942739275677727',
username: 'Dirigeants',
avatar: null,
discriminator: '0000'
});
const retrieved = gateway.get('339942739275677727') as Settings;
test.true(retrieved instanceof Settings);
test.is(retrieved.id, '339942739275677727');
});
ava('Gateway (Multiple Reverse Proxy Sync | With Data)', async (test): Promise<void> => {
test.plan(6);
const { client } = test.context;
const gateway = client.gateways.get('users') as Gateway;
const provider = gateway.provider as Provider;
gateway.schema.add('value', 'String');
await provider.createTable('users');
await Promise.all([
provider.create('users', 'foo', { value: 'bar' }),
provider.create('users', 'hello', { value: 'world' })
]);
const user1 = client.users['_add']({ id: 'foo', username: 'Dirigeants', avatar: null, discriminator: '0000' });
const user2 = client.users['_add']({ id: 'hello', username: 'Dirigeants', avatar: null, discriminator: '0001' });
const user3 = client.users['_add']({ id: 'bar', username: 'Dirigeants', avatar: null, discriminator: '0002' });
const settings1 = user1.settings as unknown as Settings;
const settings2 = user2.settings as unknown as Settings;
const settings3 = user3.settings as unknown as Settings;
test.is(settings1.existenceStatus, SettingsExistenceStatus.Unsynchronized);
test.is(settings2.existenceStatus, SettingsExistenceStatus.Unsynchronized);
test.is(settings3.existenceStatus, SettingsExistenceStatus.Unsynchronized);
await gateway.sync();
test.is(settings1.existenceStatus, SettingsExistenceStatus.Exists);
test.is(settings2.existenceStatus, SettingsExistenceStatus.Exists);
test.is(settings3.existenceStatus, SettingsExistenceStatus.NotExists);
});