Newer
Older
Labyrinth / src / GameServer.spec.ts
// to get rid of "[ts] could not find module ..." in vs code realod the window: f1 -> type "reload window"
// official issue for this: https://github.com/Microsoft/TypeScript/issues/10346

import {expect} from "chai";
import {GameServer} from "./GameServer";
import { Direction, MazeMap, MazeTile } from "./MazeMap";
// import { describe, it } from "mocha";

function createTrivialMap(): MazeMap
{
    const maze = new MazeMap("lvl1");
    let tile = new MazeTile("1");
    tile.paths.push(Direction.right);
    tile.paths.push(Direction.bottom);
    maze.layout.push([tile]);
    tile = new MazeTile("2");
    tile.paths.push(Direction.left);
    tile.paths.push(Direction.bottom);
    maze.layout[0].push(tile);

    tile = new MazeTile("3");
    tile.paths.push(Direction.right);
    tile.paths.push(Direction.top);
    maze.layout.push([tile]);
    tile = new MazeTile("4");
    tile.paths.push(Direction.left);
    tile.paths.push(Direction.top);
    maze.layout[1].push(tile);

    return maze;
}

function createGameServer(): GameServer
{
    const server = new GameServer();
    server.addMap(createTrivialMap());
    return server;
}

describe("Scenario: Starting the game", function(){
    const userID = "UserID";
    const mapID = "lvl1";
    describe("when a user starts an existing valid level", function(){
        const games = createGameServer();
        const position = games.startGame(userID, mapID);

        it("then the start position must exist", function(){
            expect(position).to.be.not.null;
        });

        it("then the start position must be remembered for the user", function(){
            const current = games.currentPosition(userID);
            expect(current.id).to.equal(position.id);
        });
    });
});

describe("Scenario: Navigating in ongoing game", function(){
    const userID = "UserID";
    const mapID = "lvl1";
    const games = createGameServer();
    const startPosition = games.startGame(userID, mapID);

    describe("When player navigates in existing direction", function(){
        const naviagtionDirection = startPosition.paths[0];
        const nextPosition = games.navigate(userID, startPosition.id, naviagtionDirection);
        it("then the new position has to have a different id", function(){
            expect(nextPosition.id).to.not.equal(startPosition.id);
        });

        it("then the new position must have pathway back", function(){
            let hasOpposite:boolean = false;
            for (const dir of nextPosition.paths) {
                switch (dir) {
                    case Direction.top:
                        if(naviagtionDirection === Direction.bottom)
                        {
                            hasOpposite = true;
                        }
                        break;
                    case Direction.left:
                        if(naviagtionDirection === Direction.right)
                        {
                            hasOpposite = true;
                        }
                        break;
                    case Direction.bottom:
                        if(naviagtionDirection === Direction.top)
                        {
                            hasOpposite = true;
                        }
                        break;
                    case Direction.right:
                        if(naviagtionDirection === Direction.left)
                        {
                            hasOpposite = true;
                        }
                        break;
                    default:
                        throw new Error(`Direction "${dir}" not recognized.`);
                }
            }
            expect(hasOpposite).to.be.true;
        });
    });
});