|
| 1 | +const assert = require('assert'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const vm = require('vm'); |
| 5 | + |
| 6 | +const root = path.resolve(__dirname, '..'); |
| 7 | + |
| 8 | +function loadScript(ctx, relativePath) { |
| 9 | + const code = fs.readFileSync(path.join(root, relativePath), 'utf8'); |
| 10 | + vm.runInContext(code, ctx, { filename: relativePath }); |
| 11 | +} |
| 12 | + |
| 13 | +const context = vm.createContext({ |
| 14 | + console, |
| 15 | + Date, |
| 16 | + Math, |
| 17 | + setTimeout, |
| 18 | + clearTimeout, |
| 19 | + VizMagicConfig: null, |
| 20 | + GuildSystem: null, |
| 21 | + ActionValidator: null, |
| 22 | + CheckpointSystem: { |
| 23 | + init(callback) { callback(null); }, |
| 24 | + loadLatestCheckpoint(account, callback) { callback(null, null); }, |
| 25 | + saveCheckpoint(account, block, state, callback) { callback(null); } |
| 26 | + }, |
| 27 | + QuestSystem: undefined, |
| 28 | + TerritorySystem: undefined, |
| 29 | + CharacterSystem: undefined, |
| 30 | + InventorySystem: undefined, |
| 31 | + MarketplaceSystem: undefined, |
| 32 | + LociSystem: undefined, |
| 33 | + WorldBossSystem: undefined, |
| 34 | + SocialSystem: undefined, |
| 35 | + DuelStateManager: undefined |
| 36 | +}); |
| 37 | +context.window = context; |
| 38 | + |
| 39 | +loadScript(context, 'app/js/config.js'); |
| 40 | +loadScript(context, 'app/js/engine/guild.js'); |
| 41 | +loadScript(context, 'app/js/engine/validator.js'); |
| 42 | +loadScript(context, 'app/js/engine/state-engine.js'); |
| 43 | + |
| 44 | +const AT = context.VizMagicConfig.ACTION_TYPES; |
| 45 | +const StateEngine = context.StateEngine; |
| 46 | + |
| 47 | +function processAction(sender, type, data, blockNum) { |
| 48 | + return StateEngine.processBlock({ |
| 49 | + vmActions: [{ sender, action: { type, data: data || {} } }], |
| 50 | + voicePosts: [], |
| 51 | + veEvents: [], |
| 52 | + awards: [], |
| 53 | + blockHash: 'block-' + blockNum, |
| 54 | + blockNum, |
| 55 | + timestamp: '' |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +const inviteEvents = processAction('guildleader', AT.GUILD_INVITE, { |
| 60 | + guild_id: 'ancients', |
| 61 | + target: 'targetmage' |
| 62 | +}, 101); |
| 63 | + |
| 64 | +const stateAfterInvite = StateEngine.getState(); |
| 65 | +assert.strictEqual(inviteEvents.length, 1, 'invite event should be emitted'); |
| 66 | +assert.ok(stateAfterInvite.guilds.ancients, 'placeholder guild should be materialized'); |
| 67 | +assert.strictEqual(stateAfterInvite.guilds.ancients.isPlaceholder, true, 'guild should be marked as placeholder'); |
| 68 | +assert.ok(stateAfterInvite.guilds.ancients.invites.targetmage, 'target invite should be stored'); |
| 69 | +assert.ok(stateAfterInvite.guilds.ancients.members.guildleader, 'inviter should be added as placeholder founder'); |
| 70 | + |
| 71 | +const joinEvents = processAction('targetmage', AT.GUILD_ACCEPT, { |
| 72 | + guild_id: 'ancients' |
| 73 | +}, 102); |
| 74 | + |
| 75 | +const stateAfterJoin = StateEngine.getState(); |
| 76 | +assert.strictEqual(joinEvents.length, 1, 'join event should be emitted'); |
| 77 | +assert.ok(stateAfterJoin.guilds.ancients.members.targetmage, 'target should be able to join placeholder guild'); |
| 78 | +assert.strictEqual(stateAfterJoin.guilds.ancients.invites.targetmage, undefined, 'invite should be consumed on join'); |
| 79 | + |
| 80 | +console.log('guild invite shell flow: ok'); |
0 commit comments