Fixing unit tests in back

This commit is contained in:
David Négrier 2020-12-08 18:37:15 +01:00
parent 5d188e51b2
commit bd38dd5ede
2 changed files with 89 additions and 53 deletions

View File

@ -1,14 +1,35 @@
import "jasmine";
import {GameRoom, ConnectCallback, DisconnectCallback } from "../src/Model/GameRoom";
import {ConnectCallback, DisconnectCallback, GameRoom} from "../src/Model/GameRoom";
import {Point} from "../src/Model/Websocket/MessageUserPosition";
import { Group } from "../src/Model/Group";
import {ExSocketInterface} from "_Model/Websocket/CharacterLayer";
import {User} from "_Model/User";
import {Group} from "../src/Model/Group";
import {User, UserSocket} from "_Model/User";
import {JoinRoomMessage, PositionMessage} from "../src/Messages/generated/messages_pb";
import Direction = PositionMessage.Direction;
function createMockUser(userId: number): ExSocketInterface {
function createMockUser(userId: number): User {
return {
userId
} as ExSocketInterface;
} as unknown as User;
}
function createMockUserSocket(): UserSocket {
return {
} as unknown as UserSocket;
}
function createJoinRoomMessage(uuid: string, x: number, y: number): JoinRoomMessage
{
const positionMessage = new PositionMessage();
positionMessage.setX(x);
positionMessage.setY(y);
positionMessage.setDirection(Direction.DOWN);
positionMessage.setMoving(false);
const joinRoomMessage = new JoinRoomMessage();
joinRoomMessage.setUseruuid('1');
joinRoomMessage.setName('foo');
joinRoomMessage.setRoomid('_/global/test.json');
joinRoomMessage.setPositionmessage(positionMessage);
return joinRoomMessage;
}
describe("GameRoom", () => {
@ -23,19 +44,21 @@ describe("GameRoom", () => {
const world = new GameRoom('_/global/test.json', connect, disconnect, 160, 160, () => {}, () => {}, () => {});
world.join(createMockUser(1), new Point(100, 100));
world.join(createMockUser(2), new Point(500, 100));
world.updatePosition({ userId: 2 }, new Point(261, 100));
const user1 = world.join(createMockUserSocket(), createJoinRoomMessage('1', 100, 100));
const user2 = world.join(createMockUserSocket(), createJoinRoomMessage('2', 500, 100));
world.updatePosition(user2, new Point(261, 100));
expect(connectCalledNumber).toBe(0);
world.updatePosition({ userId: 2 }, new Point(101, 100));
world.updatePosition(user2, new Point(101, 100));
expect(connectCalledNumber).toBe(2);
world.updatePosition({ userId: 2 }, new Point(102, 100));
world.updatePosition(user2, new Point(102, 100));
expect(connectCalledNumber).toBe(2);
});
@ -50,19 +73,19 @@ describe("GameRoom", () => {
const world = new GameRoom('_/global/test.json', connect, disconnect, 160, 160, () => {}, () => {}, () => {});
world.join(createMockUser(1), new Point(100, 100));
const user1 = world.join(createMockUserSocket(), createJoinRoomMessage('1', 100, 100));
world.join(createMockUser(2), new Point(200, 100));
const user2 = world.join(createMockUserSocket(), createJoinRoomMessage('2', 200, 100));
expect(connectCalled).toBe(true);
connectCalled = false;
// baz joins at the outer limit of the group
world.join(createMockUser(3), new Point(311, 100));
const user3 = world.join(createMockUserSocket(), createJoinRoomMessage('2', 311, 100));
expect(connectCalled).toBe(false);
world.updatePosition({ userId: 3 }, new Point(309, 100));
world.updatePosition(user3, new Point(309, 100));
expect(connectCalled).toBe(true);
});
@ -79,18 +102,18 @@ describe("GameRoom", () => {
const world = new GameRoom('_/global/test.json', connect, disconnect, 160, 160, () => {}, () => {}, () => {});
world.join(createMockUser(1), new Point(100, 100));
const user1 = world.join(createMockUserSocket(), createJoinRoomMessage('1', 100, 100));
world.join(createMockUser(2), new Point(259, 100));
const user2 = world.join(createMockUserSocket(), createJoinRoomMessage('2', 259, 100));
expect(connectCalled).toBe(true);
expect(disconnectCallNumber).toBe(0);
world.updatePosition({ userId: 2 }, new Point(100+160+160+1, 100));
world.updatePosition(user2, new Point(100+160+160+1, 100));
expect(disconnectCallNumber).toBe(2);
world.updatePosition({ userId: 2 }, new Point(262, 100));
world.updatePosition(user2, new Point(262, 100));
expect(disconnectCallNumber).toBe(2);
});

View File

@ -3,12 +3,12 @@ import {GameRoom, ConnectCallback, DisconnectCallback } from "_Model/GameRoom";
import {Point} from "../src/Model/Websocket/MessageUserPosition";
import { Group } from "../src/Model/Group";
import {PositionNotifier} from "../src/Model/PositionNotifier";
import {User} from "../src/Model/User";
import {User, UserSocket} from "../src/Model/User";
import {PointInterface} from "../src/Model/Websocket/PointInterface";
import {Zone} from "_Model/Zone";
import {Movable} from "_Model/Movable";
import {PositionInterface} from "_Model/PositionInterface";
import {ExSocketInterface} from "_Model/Websocket/CharacterLayer";
import {ZoneSocket} from "../src/RoomManager";
describe("PositionNotifier", () => {
@ -30,21 +30,25 @@ describe("PositionNotifier", () => {
y: 500,
moving: false,
direction: 'down'
}, false, positionNotifier, {} as ExSocketInterface);
}, false, positionNotifier, {} as UserSocket, [], 'foo', []);
const user2 = new User(2, 'test', {
x: -9999,
y: -9999,
moving: false,
direction: 'down'
}, false, positionNotifier, {} as ExSocketInterface);
}, false, positionNotifier, {} as UserSocket, [], 'foo', []);
positionNotifier.setViewport(user1, {
positionNotifier.addZoneListener({} as ZoneSocket, 0, 0);
positionNotifier.addZoneListener({} as ZoneSocket, 0, 1);
positionNotifier.addZoneListener({} as ZoneSocket, 1, 1);
positionNotifier.addZoneListener({} as ZoneSocket, 1, 0);
/*positionNotifier.setViewport(user1, {
left: 200,
right: 600,
top: 100,
bottom: 500
});
});*/
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
@ -74,16 +78,9 @@ describe("PositionNotifier", () => {
expect(leaveTriggered).toBe(false);
enterTriggered = false;
// Move out of the zone in a zone that we do track
user2.setPosition({x: 200, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(true);
expect(leaveTriggered).toBe(false);
moveTriggered = false;
// Leave the room
positionNotifier.leave(user2);
positionNotifier.removeViewport(user2);
//positionNotifier.removeViewport(user2);
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(true);
@ -95,7 +92,7 @@ describe("PositionNotifier", () => {
let moveTriggered = false;
let leaveTriggered = false;
const positionNotifier = new PositionNotifier(300, 300, (thing: Movable) => {
const positionNotifier = new PositionNotifier(300, 300, (thing: Movable, fromZone: Zone|null ) => {
enterTriggered = true;
}, (thing: Movable, position: PositionInterface) => {
moveTriggered = true;
@ -108,69 +105,85 @@ describe("PositionNotifier", () => {
y: 500,
moving: false,
direction: 'down'
}, false, positionNotifier, {} as ExSocketInterface);
}, false, positionNotifier, {} as UserSocket, [], 'foo', []);
const user2 = new User(2, 'test', {
x: 0,
y: 0,
moving: false,
direction: 'down'
}, false, positionNotifier, {} as ExSocketInterface);
}, false, positionNotifier, {} as UserSocket, [], 'foo', []);
let newUsers = positionNotifier.setViewport(user1, {
const listener = {} as ZoneSocket;
positionNotifier.addZoneListener(listener, 0, 0);
positionNotifier.addZoneListener(listener, 0, 1);
positionNotifier.addZoneListener(listener, 1, 1);
positionNotifier.addZoneListener(listener, 1, 0);
/*let newUsers = positionNotifier.setViewport(user1, {
left: 200,
right: 600,
top: 100,
bottom: 500
});
});*/
positionNotifier.enter(user1);
positionNotifier.enter(user2);
expect(newUsers.length).toBe(2);
//expect(newUsers.length).toBe(2);
expect(enterTriggered).toBe(true);
enterTriggered = false;
//positionNotifier.updatePosition(user2, {x:500, y:500}, {x:0, y: 0})
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(true);
moveTriggered = false;
expect(enterTriggered).toBe(true);
expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(true);
enterTriggered = false;
leaveTriggered = false;
// Move the viewport but the user stays inside.
positionNotifier.setViewport(user1, {
// Add a listener, but the user in not in this zone.
positionNotifier.addZoneListener(listener, 10, 10);
/*positionNotifier.setViewport(user1, {
left: 201,
right: 601,
top: 100,
bottom: 500
});
});*/
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(false);
// Stop listening to zone
positionNotifier.removeZoneListener(listener, 1, 1);
// Move the viewport out of the user.
positionNotifier.setViewport(user1, {
/*positionNotifier.setViewport(user1, {
left: 901,
right: 1001,
top: 100,
bottom: 500
});
});*/
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(true);
leaveTriggered = false;
expect(leaveTriggered).toBe(false);
// Move the viewport back on the user.
newUsers = positionNotifier.setViewport(user1, {
positionNotifier.addZoneListener(listener, 1, 1);
/*newUsers = positionNotifier.setViewport(user1, {
left: 200,
right: 600,
top: 100,
bottom: 500
});
});*/
expect(enterTriggered).toBe(true);
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(false);
enterTriggered = false;
expect(newUsers.length).toBe(2);
//expect(newUsers.length).toBe(2);
});
})