-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
536 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,4 +111,6 @@ dist | |
|
||
.screeps-world-printer/ | ||
|
||
.unit-test-* | ||
.unit-test-* | ||
|
||
.coveralls.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { defaultRoomDrawer } from './drawRoom'; | ||
import { DrawMaterial, RoomStatus } from './type'; | ||
|
||
test('输入 undefined 返回 undefined', async () => { | ||
const result = await defaultRoomDrawer(undefined); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
const getSvgBuffer = async (): Promise<Buffer> => Buffer.from( | ||
'<svg xmlns="http://www.w3.org/2000/svg" width="150" height="150"></svg>' | ||
); | ||
|
||
test('可以正确使用头像访问器', async () => { | ||
const getBadge = jest.fn(getSvgBuffer); | ||
const getBadgeBorder = jest.fn(getSvgBuffer); | ||
|
||
// 没有玩家占领,不应该调用头像访问器 | ||
const material: DrawMaterial = { | ||
roomName: '', | ||
roomInfo: { status: RoomStatus.Normal }, | ||
getMask: getSvgBuffer, | ||
getRoom: getSvgBuffer, | ||
getBadge, | ||
getBadgeBorder | ||
}; | ||
|
||
await defaultRoomDrawer(material); | ||
|
||
expect(getBadge).not.toHaveBeenCalled(); | ||
expect(getBadgeBorder).not.toHaveBeenCalled(); | ||
|
||
// 玩家占领,应该调用头像访问器 | ||
material.roomInfo.own = { user: '', level: 0 }; | ||
|
||
await defaultRoomDrawer(material); | ||
|
||
expect(getBadge).toBeCalledTimes(1); | ||
expect(getBadgeBorder).toBeCalledTimes(1); | ||
}); | ||
|
||
test('可以正常使用蒙版访问器', async () => { | ||
const getMask = jest.fn(getSvgBuffer); | ||
|
||
const material: DrawMaterial = { | ||
roomName: '', | ||
roomInfo: { status: RoomStatus.Normal }, | ||
getMask, | ||
getRoom: getSvgBuffer | ||
}; | ||
|
||
await defaultRoomDrawer(material); | ||
expect(getMask).not.toHaveBeenCalledWith(); | ||
|
||
const maskTypes = [ | ||
RoomStatus.Inactivated, | ||
RoomStatus.Novice, | ||
RoomStatus.Respawn | ||
]; | ||
|
||
for (const maskType of maskTypes) { | ||
getMask.mockClear(); | ||
material.roomInfo.status = maskType; | ||
await defaultRoomDrawer(material); | ||
expect(getMask).toHaveBeenCalledWith(maskType); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* eslint-disable @typescript-eslint/consistent-type-assertions */ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
|
||
import { MapStatsResp, PlayerBadge, RoomStatus } from './type'; | ||
import { ScreepsWorldPrinter } from './printer'; | ||
import { ScreepsService } from './service'; | ||
|
||
// 默认的 mock 地图信息 | ||
const DEFAULT_MAP_STATS: MapStatsResp = { | ||
stats: { | ||
E0N0: { status: RoomStatus.Normal, own: { user: 'asd123', level: 2 } }, | ||
W0N0: { status: RoomStatus.Inactivated }, | ||
W0S0: { status: RoomStatus.Novice }, | ||
E0S0: { status: RoomStatus.Respawn } | ||
}, | ||
users: { | ||
asd123: { _id: 'asd123', username: 'testUser', badge: {} as PlayerBadge } | ||
} | ||
}; | ||
|
||
const getSvgBuffer = async (): Promise<Buffer> => Buffer.from( | ||
'<svg xmlns="http://www.w3.org/2000/svg" width="150" height="150"></svg>' | ||
); | ||
|
||
const mockService = (service: ScreepsService) => { | ||
const connect = jest.fn(); | ||
const getBadge = jest.fn(getSvgBuffer); | ||
const getRoomTile = jest.fn(getSvgBuffer); | ||
const getMapSize = jest.fn(async () => ({ width: 2, height: 2 })); | ||
const getMapStats = jest.fn(async () => JSON.parse(JSON.stringify(DEFAULT_MAP_STATS))); | ||
service.connect = connect; | ||
service.getBadge = getBadge; | ||
service.getMapSize = getMapSize; | ||
service.getMapStats = getMapStats; | ||
service.getRoomTile = getRoomTile; | ||
|
||
return { connect, getBadge, getMapStats, getMapSize, getRoomTile }; | ||
}; | ||
|
||
test('可以正常下载内容', async () => { | ||
const printer = new ScreepsWorldPrinter({ | ||
host: 'testHost', | ||
shard: 'shard3', | ||
token: 'testToken' | ||
}); | ||
|
||
mockService(printer.service); | ||
printer.silence(); | ||
|
||
const worldDataSet = await printer.fetchWorld(); | ||
expect(worldDataSet).toHaveProperty('roomMaterialMatrix'); | ||
expect(worldDataSet).toHaveProperty('mapSize'); | ||
expect(worldDataSet).toHaveProperty('roomStats'); | ||
}); | ||
|
||
test('可以设置房间名解析器', async () => { | ||
const printer = new ScreepsWorldPrinter({ | ||
host: 'testHost', | ||
shard: 'shard3', | ||
token: 'testToken' | ||
}, async () => [['W0N0']]); | ||
|
||
mockService(printer.service); | ||
printer.silence(); | ||
|
||
const worldDataSet = await printer.fetchWorld(); | ||
expect(worldDataSet.roomMaterialMatrix.length).toBe(1); | ||
expect(worldDataSet.roomMaterialMatrix[0].length).toBe(1); | ||
|
||
printer.setRoomNameGetter(async () => [[undefined, 'W0N0', 'W0S0']]); | ||
const newWorldDataSet = await printer.fetchWorld(); | ||
expect(newWorldDataSet.roomMaterialMatrix.length).toBe(1); | ||
expect(newWorldDataSet.roomMaterialMatrix[0].length).toBe(3); | ||
// 因为新设置的房间名第一个是个 undefined,所以这里也会返回 undefined | ||
expect(newWorldDataSet.roomMaterialMatrix[0][0]).toBeUndefined(); | ||
}); | ||
|
||
test('可以正常执行绘制', async () => { | ||
const printer = new ScreepsWorldPrinter({ | ||
host: 'testHost', | ||
shard: 'shard3', | ||
token: 'testToken' | ||
}); | ||
|
||
mockService(printer.service); | ||
|
||
const saver = jest.fn(async () => 'save path'); | ||
printer.setResultSaver(saver); | ||
|
||
await printer.drawWorld(); | ||
|
||
expect(saver).toBeCalledTimes(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
|
||
import { AxiosInstance } from 'axios'; | ||
import { ServerConnectInfo } from '.'; | ||
import { ScreepsService } from './service'; | ||
|
||
const createService = function (opt: ServerConnectInfo) { | ||
const service = new ScreepsService(opt); | ||
const getSpy = jest.fn(); | ||
const postSpy = jest.fn(); | ||
const http = (service as any).http as AxiosInstance; | ||
http.get = getSpy; | ||
http.post = postSpy; | ||
|
||
return { service, http, getSpy, postSpy }; | ||
}; | ||
|
||
test('可以登录', async () => { | ||
const { service, http } = createService({ | ||
host: 'testHost', | ||
shard: 'shard3', | ||
token: 'testToken' | ||
}); | ||
|
||
// 正确配置了服务器 url | ||
expect(http.defaults.baseURL).toBe('testHost'); | ||
|
||
await service.connect(); | ||
expect(http.defaults.headers['X-Token']).toBe('testToken'); | ||
expect(http.defaults.headers['X-Username']).toBe('testToken'); | ||
|
||
const { service: privateService, http: privateHttp, postSpy } = createService({ | ||
host: 'testPrivate', | ||
username: 'test', | ||
password: '123' | ||
}); | ||
postSpy.mockResolvedValueOnce({ data: { token: 'returnedToken' } }); | ||
|
||
await privateService.connect(); | ||
expect(postSpy.mock.calls[0][1]).toEqual({ email: 'test', password: '123' }); | ||
expect(privateHttp.defaults.headers['X-Token']).toBe('returnedToken'); | ||
expect(privateHttp.defaults.headers['X-Username']).toBe('returnedToken'); | ||
}); | ||
|
||
test('可以下载地图瓦片', async () => { | ||
const { service, getSpy } = createService({ | ||
host: 'testHost', | ||
shard: 'shard3', | ||
token: 'testToken' | ||
}); | ||
|
||
const tile = Buffer.from( | ||
'<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"></svg>' | ||
); | ||
|
||
getSpy.mockResolvedValue({ data: tile }); | ||
const result = await service.getRoomTile('roomName'); | ||
// 发起的请求中包含房间名 | ||
expect(getSpy.mock.calls[0][0]).toContain('roomName'); | ||
// 可以正常接受到瓦片 | ||
expect(result).toBe(tile); | ||
}); | ||
|
||
test('可以下载头像', async () => { | ||
const { service, getSpy } = createService({ | ||
host: 'testHost', | ||
shard: 'shard3', | ||
token: 'testToken' | ||
}); | ||
|
||
// 原始头像 | ||
const badBadge = `<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"> | ||
<circle cx="50" cy="50" r="52" /> | ||
</svg>`; | ||
|
||
// 修复后的头像 | ||
const goodBadge = `<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"> | ||
<circle cx="50" cy="50" r="50" /> | ||
</svg>`; | ||
|
||
getSpy.mockResolvedValue({ data: badBadge }); | ||
const result = await service.getBadge('username'); | ||
// 发起的请求中包含玩家名 | ||
expect(getSpy.mock.calls[0][0]).toContain('username'); | ||
// 可以正常接收到头像并且进行了修复 | ||
expect(result.toString()).toBe(goodBadge); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.