Browse Source

commit tests

master
peter 5 years ago
parent
commit
8804fe2ebe
1 changed files with 370 additions and 0 deletions
  1. 370
    0
      test/backendTest.ts

+ 370
- 0
test/backendTest.ts View File

@@ -0,0 +1,370 @@
1
+import { Injector } from "../src/backend/Injector/Injector";
2
+import { FrontworkAdmin } from "../src/backend/Admin/Admin";
3
+import { T1 } from "../src/backend/Types/Items";
4
+
5
+import { RPCSocket } from "rpclibrary";
6
+import { FrontcraftIfc, Auth, User, FrontcraftFeatureIfc, Raid, Character, Rank, Class, Race } from "../src/backend/Types/Types";
7
+import { SpecT } from "../src/backend/Types/PlayerSpecs";
8
+
9
+
10
+type protoAccount<C extends Class = Class> = {
11
+    name : string,
12
+    rank : Rank,
13
+    race: Race,
14
+    class: C,
15
+    spec: SpecT[C]
16
+}
17
+
18
+const trialsAndUp = {
19
+    ADMIN: true,
20
+    Guildmaster: true,
21
+    Officer: true,
22
+    Classleader: true,
23
+    Raider: true,
24
+    Trial: true,
25
+    Social: false,
26
+    Guest: false
27
+}
28
+
29
+const adminsOnly = {
30
+    ADMIN: true,
31
+    Guildmaster: true,
32
+    Officer: true,
33
+    Classleader: false,
34
+    Raider: false,
35
+    Trial: false,
36
+    Social: false,
37
+    Guest: false
38
+}
39
+
40
+const defaultPermissions = [
41
+{   rpcname: 'signup', ...trialsAndUp
42
+},{ rpcname: 'reset', ...adminsOnly 
43
+},{ rpcname: 'modifyPermissions', ...adminsOnly 
44
+},{ rpcname: 'manageGuild', ...adminsOnly
45
+},{ rpcname: 'managePriorities', ...adminsOnly
46
+},{ rpcname: 'softreserveCurrency', ...adminsOnly
47
+},{ rpcname: 'manageRaid', ...adminsOnly
48
+}]
49
+
50
+const testAccounts : protoAccount[] = [
51
+    {
52
+        name: 'Rain',
53
+        race: 'Human',
54
+        class: 'Warrior',
55
+        spec: 'Protection',
56
+        rank: 'Guildmaster'
57
+    },{
58
+        name: 'Celinda',
59
+        class: 'Warrior',
60
+        race: 'Night Elf',
61
+        spec: 'Protection',
62
+        rank: 'Officer'
63
+    },{
64
+        name: 'Dagger',
65
+        race: 'Dwarf',
66
+        class: 'Rogue',
67
+        spec: 'Assassination',
68
+        rank: 'Classleader'
69
+    },{
70
+        name: 'Hope',
71
+        class: 'Paladin',
72
+        race: 'Human',
73
+        spec: 'Holy',
74
+        rank: 'Classleader'
75
+    },{
76
+        name: 'Shrekd',
77
+        class: 'Warrior',
78
+        race: 'Dwarf',
79
+        spec: 'Fury',
80
+        rank: 'Classleader'
81
+    },{
82
+        name: 'Teenieweenie',
83
+        class: 'Warlock',
84
+        race: 'Gnome',
85
+        spec: 'Demonology',
86
+        rank: 'Classleader'
87
+    },{
88
+        name: 'Hagibaba',
89
+        class: 'Priest',
90
+        race: 'Human',
91
+        spec: 'Discipline',
92
+        rank: 'Classleader'
93
+    },{
94
+        name: 'Muffinbreak',
95
+        class: 'Mage',
96
+        race: 'Gnome',
97
+        spec: 'Arcane',
98
+        rank: 'Classleader'
99
+    },
100
+]
101
+
102
+
103
+describe('Frontcraft', () => {
104
+    let auth: Auth,
105
+        adminUser : User,
106
+        server: FrontworkAdmin,
107
+        client : RPCSocket & FrontcraftIfc,
108
+        adminClient : RPCSocket & FrontcraftFeatureIfc,
109
+        raids: Raid[] = [],
110
+        users : {[username in string] : { account: User, character: Character, auth: Auth, item?:string }} = {}
111
+
112
+    const createAccount = (user: User) => {
113
+        return client.UserManager.createUser(user)
114
+    }
115
+
116
+    const createAccountAndUser = async (acc : protoAccount) => {
117
+        const account = await createAccount({
118
+            pwhash: 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', //sha256("a")
119
+            rank: acc.rank,
120
+            username: acc.name
121
+        })
122
+        const auth = await client.UserManager.login(acc.name, 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb')
123
+        const id = await client.CharacterManager.getSpecId(acc.class, acc.spec)
124
+        const char = await client.CharacterManager.createCharacter(auth.token.value, {
125
+            charactername: acc.name,
126
+            specid: id,
127
+            userid: account.id!,
128
+            race: acc.race
129
+        })
130
+        return {
131
+            account: account,
132
+            character: char,
133
+            auth: auth
134
+        }
135
+    }
136
+
137
+    before(function (done){
138
+        this.timeout(10000); 
139
+
140
+        server = Injector.resolve<FrontworkAdmin>(FrontworkAdmin)
141
+        server.start().then((_server) => {
142
+            
143
+            RPCSocket.makeSocket<FrontcraftIfc>(20000, 'localhost').then(_client => {
144
+                client = _client
145
+                
146
+
147
+                createAccount({
148
+                    pwhash: 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', //hash("a")
149
+                    rank: 'ADMIN',
150
+                    username: 'a',
151
+                    currency: 2
152
+                }).then(adminUser => {
153
+
154
+                    client.UserManager.login(adminUser.username, 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb').then(auth => {
155
+                        const sock = new RPCSocket(
156
+                            auth.port, 
157
+                            "localhost"
158
+                        )
159
+
160
+                        sock.connect<any>(auth.token.value).then(cli => {
161
+                            adminClient = cli
162
+                            sock.hook('kick', () => {
163
+                                console.log("I got kicked");
164
+                            })
165
+                            sock.hook('getUserData', () => auth)
166
+                            sock.hook('navigate', (where:string) => {
167
+                                console.log("Nagivate client to "+where);
168
+                            })
169
+                            sock.on('error', (e) => {
170
+                                console.log('Socket error',  e)
171
+                            })
172
+                            done()
173
+                        })
174
+                    })
175
+                })
176
+            })
177
+        }).catch(done)
178
+    })
179
+
180
+    after(()=>{
181
+        client.destroy()
182
+        adminClient.destroy()
183
+        server.stop()
184
+    })
185
+
186
+    it('create raids', (done) => {
187
+        let insertRaid = <Raid>{
188
+            description: "Test raid 1",
189
+            title: 'MC',
190
+            start: Date.now().toString()
191
+        }
192
+
193
+        adminClient.manageRaid.createRaid(insertRaid).then(() => {
194
+            client.RaidManager.getRaids().then((r)=>{
195
+                if(r[0].title === insertRaid.title 
196
+                && r[0].description === insertRaid.description){
197
+                    raids.push(r[0])
198
+                    done()
199
+                }
200
+            }).catch(done)
201
+        }).catch(done)
202
+    })
203
+
204
+    it('create users', (done)=>{
205
+        Promise.all(testAccounts.map(acc => createAccountAndUser(acc))).then(accs => {
206
+            if(accs.length === testAccounts.length){
207
+                accs.forEach(acc => {
208
+                    users[acc.account.username] = acc
209
+                })
210
+                done()
211
+            }
212
+        }).catch(done)
213
+    })
214
+
215
+    it('should sign up', (done)=>{
216
+        Promise.all(Object.values(users).map((user) =>
217
+            adminClient.signup.sign(user.auth.token.value, user.character, raids[0], false).catch(done)
218
+        )).then(x => {
219
+            adminClient.signup.getSignups(raids[0]).then(s => {
220
+                if(s.length == testAccounts.length)
221
+                    done()
222
+                else{
223
+                    done("Unexpected number of signups: "+s.length)
224
+                }
225
+            })
226
+        })
227
+    })
228
+
229
+    it('calculate priorities', (done)=>{
230
+
231
+        const user = Object.values(users)[0]
232
+
233
+        adminClient.managePriorities.setPriority(T1[0], {
234
+            race: user.character.race,
235
+            specid: user.character.specid,
236
+            modifier: 1,
237
+            description:'AAA'
238
+        }).then(() => {
239
+            adminClient.managePriorities.setPriority(T1[0], {
240
+                race: user.character.race,
241
+                specid: undefined,
242
+                modifier: 2,
243
+                description:'BBB'
244
+            }).then(()=>{
245
+                adminClient.managePriorities.setPriority(T1[0], {
246
+                    race: undefined,
247
+                    specid: user.character.specid,
248
+                    modifier: 3,
249
+                    description:'CCC'
250
+                }).then(()=>{
251
+                    client.ItemManager.calculatePriorities(T1[0], user.character).then(sum => {
252
+                        if(sum === 6)
253
+                            done()
254
+                    })
255
+                })
256
+            })
257
+        })
258
+    })
259
+
260
+    it('buy token', (done)=>{
261
+        Promise.all(Object.values(users).map(async (user) =>{
262
+            const itemname = T1[0]//T1[Math.floor(T1.length*Math.random())]
263
+            const modifier = await client.ItemManager.calculatePriorities(itemname, user.character)
264
+
265
+            const token = await client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname)
266
+            users[user.account.username].item = itemname
267
+            
268
+            if(!token) return false
269
+            return modifier+1 === token.level
270
+        })).then(success => {
271
+            if(success.reduce((prev, curr)=>prev&&curr, true)) done()
272
+        })
273
+    })
274
+
275
+    it('not buy token without currency', (done)=>{
276
+        const user = Object.values(users)[0]
277
+        const itemname = T1[0]
278
+        client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname).then(token => {
279
+            if(!token) 
280
+                done()
281
+            else {
282
+                console.log("Unexpected token", token);
283
+            }
284
+        })
285
+    })
286
+
287
+    it('upgrade token', (done)=>{
288
+        const user = Object.values(users)[0]
289
+        const itemname = T1[0]
290
+
291
+        adminClient.softreserveCurrency.incrementCurrency(user.account, 2).then(async ()=>{
292
+            const item = await client.ItemManager.getItem(itemname)
293
+            const before = await client.ItemManager.getToken(user.character, item)
294
+            
295
+            if(!before || before.level !== 7) return
296
+            
297
+            await client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname)
298
+            const after = await client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname)
299
+
300
+            if(!after || after.level !== 9) return
301
+            done()
302
+        })
303
+    })
304
+
305
+    it('should buy more tokens', (done) => {
306
+        Promise.all(Object.values(users).map(async (user) => {
307
+            await adminClient.softreserveCurrency.incrementCurrency(user.account, 1)
308
+            await client.ItemManager
309
+            .buyToken(
310
+                user.auth.token.value, 
311
+                user.character.charactername, 
312
+                T1[Math.floor(T1.length*Math.random())]
313
+            )
314
+        })).then(_ => {
315
+            done()
316
+        })
317
+    })
318
+
319
+    it('can set permissions', (done) => {
320
+        Promise.all(
321
+            defaultPermissions.map(perm => adminClient.modifyPermissions.setPermission(perm)),
322
+        ).then(_ => {
323
+            done()
324
+        })
325
+    })
326
+
327
+    it('start raid', (done) => {
328
+        client.RaidManager.getRaids().then((r)=>{
329
+            adminClient.manageRaid.startRaid(raids[0]).then(async data => {
330
+                const dbRaids = await client.RaidManager.getRaids()
331
+                if(dbRaids.length === 0){
332
+                    await client.UserManager.getUser(testAccounts[0].name).then(dbUser => {
333
+                        if(dbUser && dbUser.currency === 1){
334
+                            adminClient.signup.getSignups(raids[0]).then(signups => {
335
+                                if(signups.length === 0){
336
+                                    done()
337
+                                }else{
338
+                                    console.log(signups);
339
+                                }
340
+                            })
341
+                        }
342
+                        else{
343
+                            console.log("Bad user currency", dbUser);
344
+                            
345
+                        }
346
+                    })
347
+                }
348
+            })
349
+        })
350
+    })
351
+
352
+    it('reset system', (done) => {
353
+        adminClient.reset.wipeCurrencyAndItems().then(() => {
354
+            client.UserManager.getUser(testAccounts[0].name).then(user => {
355
+                if(user && user.currency === 0){
356
+                    client.ItemManager.getTokens(users[testAccounts[0].name.toLowerCase()].character).then(tokens => {
357
+                        if(tokens.length === 0){
358
+                            done()
359
+                        }else{
360
+                            console.log(tokens);
361
+                        }
362
+                    })
363
+                }else{
364
+                    console.log(user)
365
+                }
366
+            })
367
+        })
368
+        
369
+    })
370
+})

Loading…
Cancel
Save