-
-
Notifications
You must be signed in to change notification settings - Fork 939
/
externalTest.js
152 lines (138 loc) · 4.86 KB
/
externalTest.js
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
/* eslint-env mocha */
const assert = require('assert')
const mineflayer = require('../')
const commonTest = require('./externalTests/plugins/testCommon')
const mc = require('minecraft-protocol')
const fs = require('fs')
const path = require('path')
const { getPort } = require('./common/util')
// set this to false if you want to test without starting a server automatically
const START_THE_SERVER = true
// if you want to have time to look what's happening increase this (milliseconds)
const TEST_TIMEOUT_MS = 95000
const excludedTests = ['digEverything', 'book', 'anvil', 'placeEntity']
const propOverrides = {
'level-type': 'FLAT',
'spawn-npcs': 'true',
'spawn-animals': 'false',
'online-mode': 'false',
gamemode: '1',
'spawn-monsters': 'false',
'generate-structures': 'false',
'enable-command-block': 'true',
'use-native-transport': 'false' // java 16 throws errors without this, https://www.spigotmc.org/threads/unable-to-access-address-of-buffer.311602
}
const Wrap = require('minecraft-wrap').Wrap
const download = require('minecraft-wrap').download
const MC_SERVER_PATH = path.join(__dirname, 'server')
for (const supportedVersion of mineflayer.testedVersions) {
let PORT = 25565
const registry = require('prismarine-registry')(supportedVersion)
const version = registry.version
const MC_SERVER_JAR_DIR = process.env.MC_SERVER_JAR_DIR || `${process.cwd()}/server_jars`
const MC_SERVER_JAR = `${MC_SERVER_JAR_DIR}/minecraft_server.${version.minecraftVersion}.jar`
const wrap = new Wrap(MC_SERVER_JAR, `${MC_SERVER_PATH}_${supportedVersion}`)
wrap.on('line', (line) => {
console.log(line)
})
describe(`mineflayer_external ${supportedVersion}v`, function () {
let bot
this.timeout(10 * 60 * 1000)
before(async function () {
PORT = await getPort()
console.log(`Port chosen: ${PORT}`)
})
before(function (done) {
this.timeout(1000 * 60)
function begin () {
bot = mineflayer.createBot({
username: 'flatbot',
viewDistance: 'tiny',
port: PORT,
host: '127.0.0.1',
version: supportedVersion
})
commonTest(bot)
bot.test.port = PORT
console.log('starting bot')
bot.once('spawn', () => {
wrap.writeServer('op flatbot\n')
bot.once('messagestr', msg => {
if (msg.includes('Made flatbot a server operator') || msg === '[Server: Opped flatbot]') {
done()
}
})
})
}
if (START_THE_SERVER) {
console.log('downloading and starting server')
download(version.minecraftVersion, MC_SERVER_JAR, (err) => {
if (err) {
console.log(err)
done(err)
return
}
propOverrides['server-port'] = PORT
wrap.startServer(propOverrides, (err) => {
if (err) return done(err)
console.log(`pinging ${version.minecraftVersion} port : ${PORT}`)
mc.ping({
port: PORT,
host: '127.0.0.1',
version: supportedVersion
}, (err, results) => {
if (err) return done(err)
console.log('pong')
assert.ok(results.latency >= 0)
assert.ok(results.latency <= 1000)
begin()
})
})
})
} else begin()
})
beforeEach(async () => {
console.log('reset state')
await bot.test.resetState()
})
after((done) => {
bot.quit()
wrap.stopServer((err) => {
if (err) {
console.log(err)
}
wrap.deleteServerData((err) => {
if (err) {
console.log(err)
}
done(err)
})
})
})
const externalTestsFolder = path.resolve(__dirname, './externalTests')
fs.readdirSync(externalTestsFolder)
.filter(file => fs.statSync(path.join(externalTestsFolder, file)).isFile())
.forEach((test) => {
test = path.basename(test, '.js')
const testFunctions = require(`./externalTests/${test}`)(supportedVersion)
const runTest = (testName, testFunction) => {
return function (done) {
this.timeout(TEST_TIMEOUT_MS)
bot.test.sayEverywhere(`### Starting ${testName}`)
testFunction(bot, done).then(res => done()).catch(e => done(e))
}
}
if (excludedTests.indexOf(test) === -1) {
if (typeof testFunctions === 'object') {
for (const testFunctionName in testFunctions) {
if (testFunctions[testFunctionName] !== undefined) {
it(`${test} ${testFunctionName}`, (testFunctionName => runTest(`${test} ${testFunctionName}`, testFunctions[testFunctionName]))(testFunctionName))
}
}
} else {
it(test, runTest(test, testFunctions))
}
}
})
})
}