Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test on a running MC server #85

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions .github/workflows/build.yml

This file was deleted.

53 changes: 53 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Test
on: [push]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
- name: Install packages
run: yarn install
- name: Type check
run: yarn type-check
- name: Test
run: yarn test --testPathIgnorePatterns 'MinecraftServer'
- name: Build
run: yarn build

server:
runs-on: ubuntu-latest
needs: test
timeout-minutes: 5
strategy:
matrix:
include:
# - javaVersion: 17
# mcVersion: '1.18.2'
- javaVersion: 17
mcVersion: '1.19'
- javaVersion: 17
mcVersion: '1.19.2'
fail-fast: false

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
- name: Setup Java JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.javaVersion }}
java-package: jre
- name: Install packages
run: yarn install
- name: Test
run: yarn test -t 'MinecraftServer'
env:
MC_VERSION: ${{ matrix.mcVersion }}
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'minecraft-wrap';
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "node build/index.js",
"build": "tsc",
"type-check": "tsc --noEmit",
"test": "jest"
"test": "jest --verbose"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -38,6 +38,7 @@
"@types/tail": "^2.2.1",
"dotenv": "^16.0.0",
"jest": "^27.5.1",
"minecraft-wrap": "^1.5.1",
"ts-jest": "^27.1.3",
"typescript": "^4.6.2"
}
Expand Down
10 changes: 9 additions & 1 deletion src/MinecraftHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from 'fs'
import path from 'path'
import type { Server } from 'http'

import { Tail } from 'tail'
import express from 'express'

Expand All @@ -18,6 +20,7 @@ class MinecraftHandler {
config: Config

app: express.Application
server?: Server
tail: Tail

constructor(config: Config) {
Expand Down Expand Up @@ -152,7 +155,7 @@ class MinecraftHandler {

const port: number = Number(process.env.PORT) || this.config.PORT

this.app.listen(port, () => {
this.server = this.app.listen(port, () => {
console.log('[INFO] Bot listening on *:' + port)

if (!this.config.IS_LOCAL_FILE && this.config.SHOW_INIT_MESSAGE) {
Expand Down Expand Up @@ -203,6 +206,11 @@ class MinecraftHandler {
})
}

public _teardown() {
if (this.config.IS_LOCAL_FILE) this.tail.unwatch()
else if (this.server) this.server.close()
}

public init (callback: Callback) {
if (this.config.IS_LOCAL_FILE) {
this.initTail(callback)
Expand Down
118 changes: 118 additions & 0 deletions tests/MinecraftServer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Wrap, download } from 'minecraft-wrap'
import path from 'path'

import Rcon from '../src/Rcon'
import MinecraftHandler, { LogLine } from '../src/MinecraftHandler'
import { defaultConfig } from './constants'

const MC_VERSION = process.env['MC_VERSION']
const MC_SERVER_PATH = path.resolve(`./tests/server/server-${MC_VERSION}`)
const MC_SERVER_JAR = path.join(MC_SERVER_PATH, `${MC_VERSION}.jar`)

const RCON_PORT = 25575
const RCON_PASSWORD = 'test'
const configWithServer = {
...defaultConfig,
DEBUG: true,
IS_LOCAL_FILE: true,
LOCAL_FILE_PATH: path.join(MC_SERVER_PATH, 'logs/latest.log'),
MINECRAFT_SERVER_RCON_IP: '127.0.0.1',
MINECRAFT_SERVER_RCON_PORT: RCON_PORT,
MINECRAFT_SERVER_RCON_PASSWORD: RCON_PASSWORD
}

const wrap = new Wrap(MC_SERVER_JAR, MC_SERVER_PATH)

const serverProperties = {
'online-mode': 'false',
'level-type': 'FLAT',
'enable-rcon': 'true',
'rcon.password': RCON_PASSWORD,
'rcon.port': RCON_PORT.toString(),
}

describe(`MinecraftServer v${MC_VERSION}`, () => {
jest.setTimeout(1000 * 60) // 1 minutes
const serverLog = jest.fn((_line: string) => undefined)
// const logSpy = jest.spyOn(console, 'log')

beforeAll((done) => {
console.log(`Downloading Minecraft ${MC_VERSION} server...`)

wrap.on('line', (line: string) => {
console.log(`[${MC_VERSION} SERVER] ${line}`)
serverLog(line)
})

download(MC_VERSION, MC_SERVER_JAR, (err: any) => {
if (err) {
console.error(err)
done(err)
return
}

console.log(`Starting Minecraft ${MC_VERSION} server...`)
wrap.startServer(serverProperties, (err: any) => {
if (err) {
console.error(err)
done(err)
return
}
done()
})
})
})

afterAll((done) => {
wrap.stopServer((err: any) => {
if (err) {
console.error(err)
}
// done()
wrap.deleteServerData((err: any) => {
if (err) {
console.log(err)
}
done(err)
})
})
})

// with 1.19.2 the name is surrounded with brackets rather than <>

it('reads logs from Minecraft server', (done) => {
const handler = new MinecraftHandler(configWithServer)
// (handler as any) since private
const parseLogLineSpy = jest.spyOn(handler as any, 'parseLogLine')

handler.init((data: LogLine) => {
console.log(`[${MC_VERSION} SHULKER]:`, data)

// both the server and the handler should have received the line
expect(data).toBeNull()
expect(parseLogLineSpy).toHaveBeenCalledWith(expect.stringContaining('[Server] hello world!'))
expect(serverLog).toHaveBeenCalledWith(expect.stringContaining('[Server] hello world!'))

handler._teardown()
done()
})

setTimeout(() => {
// simulate a chat message after a few seconds
wrap.writeServer('say hello world!\n')
}, 1000 * 10)
})

it('connects to Minecraft server via rcon', async () => {
const rcon = new Rcon(configWithServer.MINECRAFT_SERVER_RCON_IP, configWithServer.MINECRAFT_SERVER_RCON_PORT, configWithServer.DEBUG)
await rcon.auth(configWithServer.MINECRAFT_SERVER_RCON_PASSWORD)

await rcon.command('say hello world from rcon!')

await new Promise((resolve) => setTimeout(resolve, 1000 * 2))

expect(serverLog).toHaveBeenCalledWith(expect.stringContaining('[Rcon] hello world from rcon!'))

rcon.close()
})
})
Loading