agones.dev/agones@v1.53.0/sdks/nodejs/spec/alphaAgonesSDK.spec.js (about)

     1  // Copyright 2020 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  const grpc = require('@grpc/grpc-js');
    16  
    17  const messages = require('../lib/alpha/alpha_pb');
    18  const Alpha = require('../src/alpha');
    19  
    20  describe('Alpha', () => {
    21  	let alpha;
    22  
    23  	beforeEach(() => {
    24  		const address = 'localhost:9357';
    25  		const credentials = grpc.credentials.createInsecure();
    26  		alpha = new Alpha(address, credentials);
    27  	});
    28  
    29  	describe('playerConnect', () => {
    30  		it('calls the server and handles success when the player is not connected', async () => {
    31  			spyOn(alpha.client, 'playerConnect').and.callFake((request, callback) => {
    32  				let result = new messages.Bool();
    33  				result.setBool(true);
    34  				callback(undefined, result);
    35  			});
    36  
    37  			let result = await alpha.playerConnect('playerID');
    38  			expect(alpha.client.playerConnect).toHaveBeenCalled();
    39  			expect(result).toEqual(true);
    40  		});
    41  
    42  		it('calls the server and handles success when the player is already connected', async () => {
    43  			spyOn(alpha.client, 'playerConnect').and.callFake((request, callback) => {
    44  				let result = new messages.Bool();
    45  				result.setBool(false);
    46  				callback(undefined, result);
    47  			});
    48  
    49  			let result = await alpha.playerConnect('playerID');
    50  			expect(alpha.client.playerConnect).toHaveBeenCalled();
    51  			expect(result).toEqual(false);
    52  		});
    53  
    54  		it('calls the server and handles failure', async () => {
    55  			spyOn(alpha.client, 'playerConnect').and.callFake((request, callback) => {
    56  				callback('error', undefined);
    57  			});
    58  			try {
    59  				await alpha.playerConnect('playerID');
    60  				fail();
    61  			} catch (error) {
    62  				expect(alpha.client.playerConnect).toHaveBeenCalled();
    63  				expect(error).toEqual('error');
    64  			}
    65  		});
    66  	});
    67  
    68  	describe('playerDisconnect', () => {
    69  		it('calls the server and handles success when the player is connected', async () => {
    70  			spyOn(alpha.client, 'playerDisconnect').and.callFake((request, callback) => {
    71  				let result = new messages.Bool();
    72  				result.setBool(true);
    73  				callback(undefined, result);
    74  			});
    75  
    76  			let result = await alpha.playerDisconnect('playerID');
    77  			expect(alpha.client.playerDisconnect).toHaveBeenCalled();
    78  			expect(result).toEqual(true);
    79  		});
    80  
    81  		it('calls the server and handles success when the player is not connected', async () => {
    82  			spyOn(alpha.client, 'playerDisconnect').and.callFake((request, callback) => {
    83  				let result = new messages.Bool();
    84  				result.setBool(false);
    85  				callback(undefined, result);
    86  			});
    87  
    88  			let result = await alpha.playerDisconnect('playerID');
    89  			expect(alpha.client.playerDisconnect).toHaveBeenCalled();
    90  			expect(result).toEqual(false);
    91  		});
    92  
    93  		it('calls the server and handles failure', async () => {
    94  			spyOn(alpha.client, 'playerDisconnect').and.callFake((request, callback) => {
    95  				callback('error', undefined);
    96  			});
    97  			try {
    98  				await alpha.playerDisconnect('playerID');
    99  				fail();
   100  			} catch (error) {
   101  				expect(alpha.client.playerDisconnect).toHaveBeenCalled();
   102  				expect(error).toEqual('error');
   103  			}
   104  		});
   105  	});
   106  
   107  	describe('setPlayerCapacity', () => {
   108  		it('passes arguments to the server and handles success', async () => {
   109  			spyOn(alpha.client, 'setPlayerCapacity').and.callFake((request, callback) => {
   110  				let result = new messages.Empty();
   111  				callback(undefined, result);
   112  			});
   113  
   114  			let result = await alpha.setPlayerCapacity(64);
   115  			expect(result).toEqual({});
   116  			expect(alpha.client.setPlayerCapacity).toHaveBeenCalled();
   117  			let request = alpha.client.setPlayerCapacity.calls.argsFor(0)[0];
   118  			expect(request.getCount()).toEqual(64);
   119  		});
   120  
   121  		it('calls the server and handles failure', async () => {
   122  			spyOn(alpha.client, 'setPlayerCapacity').and.callFake((request, callback) => {
   123  				callback('error', undefined);
   124  			});
   125  			try {
   126  				await alpha.setPlayerCapacity(64);
   127  				fail();
   128  			} catch (error) {
   129  				expect(alpha.client.setPlayerCapacity).toHaveBeenCalled();
   130  				expect(error).toEqual('error');
   131  			}
   132  		});
   133  	});
   134  
   135  	describe('getPlayerCapacity', () => {
   136  		it('calls the server and handles the response', async () => {
   137  			spyOn(alpha.client, 'getPlayerCapacity').and.callFake((request, callback) => {
   138  				let capacity = new messages.Count();
   139  				capacity.setCount(64);
   140  				callback(undefined, capacity);
   141  			});
   142  
   143  			let capacity = await alpha.getPlayerCapacity();
   144  			expect(alpha.client.getPlayerCapacity).toHaveBeenCalled();
   145  			expect(capacity).toEqual(64);
   146  		});
   147  
   148  		it('calls the server and handles failure', async () => {
   149  			spyOn(alpha.client, 'getPlayerCapacity').and.callFake((request, callback) => {
   150  				callback('error', undefined);
   151  			});
   152  			try {
   153  				await alpha.getPlayerCapacity();
   154  				fail();
   155  			} catch (error) {
   156  				expect(alpha.client.getPlayerCapacity).toHaveBeenCalled();
   157  				expect(error).toEqual('error');
   158  			}
   159  		});
   160  	});
   161  
   162  	describe('getPlayerCount', () => {
   163  		it('calls the server and handles the response', async () => {
   164  			spyOn(alpha.client, 'getPlayerCount').and.callFake((request, callback) => {
   165  				let capacity = new messages.Count();
   166  				capacity.setCount(16);
   167  				callback(undefined, capacity);
   168  			});
   169  
   170  			let capacity = await alpha.getPlayerCount();
   171  			expect(alpha.client.getPlayerCount).toHaveBeenCalled();
   172  			expect(capacity).toEqual(16);
   173  		});
   174  
   175  		it('calls the server and handles failure', async () => {
   176  			spyOn(alpha.client, 'getPlayerCount').and.callFake((request, callback) => {
   177  				callback('error', undefined);
   178  			});
   179  			try {
   180  				await alpha.getPlayerCount();
   181  				fail();
   182  			} catch (error) {
   183  				expect(alpha.client.getPlayerCount).toHaveBeenCalled();
   184  				expect(error).toEqual('error');
   185  			}
   186  		});
   187  	});
   188  
   189  	describe('isPlayerConnected', () => {
   190  		it('calls the server and handles success when the player is connected', async () => {
   191  			spyOn(alpha.client, 'isPlayerConnected').and.callFake((request, callback) => {
   192  				let result = new messages.Bool();
   193  				result.setBool(true);
   194  				callback(undefined, result);
   195  			});
   196  
   197  			let result = await alpha.isPlayerConnected('playerID');
   198  			expect(alpha.client.isPlayerConnected).toHaveBeenCalled();
   199  			expect(result).toEqual(true);
   200  		});
   201  
   202  		it('calls the server and handles success when the player is not connected', async () => {
   203  			spyOn(alpha.client, 'isPlayerConnected').and.callFake((request, callback) => {
   204  				let result = new messages.Bool();
   205  				result.setBool(false);
   206  				callback(undefined, result);
   207  			});
   208  
   209  			let result = await alpha.isPlayerConnected('playerID');
   210  			expect(alpha.client.isPlayerConnected).toHaveBeenCalled();
   211  			expect(result).toEqual(false);
   212  		});
   213  
   214  		it('calls the server and handles failure', async () => {
   215  			spyOn(alpha.client, 'isPlayerConnected').and.callFake((request, callback) => {
   216  				callback('error', undefined);
   217  			});
   218  			try {
   219  				await alpha.isPlayerConnected('playerID');
   220  				fail();
   221  			} catch (error) {
   222  				expect(alpha.client.isPlayerConnected).toHaveBeenCalled();
   223  				expect(error).toEqual('error');
   224  			}
   225  		});
   226  	});
   227  
   228  	describe('getConnectedPlayers', () => {
   229  		it('calls the server and handles the response', async () => {
   230  			spyOn(alpha.client, 'getConnectedPlayers').and.callFake((request, callback) => {
   231  				let connectedPlayers = new messages.PlayerIDList();
   232  				connectedPlayers.setListList(['firstPlayerID', 'secondPlayerID']);
   233  				callback(undefined, connectedPlayers);
   234  			});
   235  
   236  			let connectedPlayers = await alpha.getConnectedPlayers();
   237  			expect(alpha.client.getConnectedPlayers).toHaveBeenCalled();
   238  			expect(connectedPlayers).toEqual(['firstPlayerID', 'secondPlayerID']);
   239  		});
   240  
   241  		it('calls the server and handles failure', async () => {
   242  			spyOn(alpha.client, 'getConnectedPlayers').and.callFake((request, callback) => {
   243  				callback('error', undefined);
   244  			});
   245  			try {
   246  				await alpha.getConnectedPlayers();
   247  				fail();
   248  			} catch (error) {
   249  				expect(alpha.client.getConnectedPlayers).toHaveBeenCalled();
   250  				expect(error).toEqual('error');
   251  			}
   252  		});
   253  	});
   254  });