Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afrokick committed Apr 1, 2019
1 parent a528867 commit 5f3774f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 210 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Michelle Bu, Eric Zhang",
"license": "MIT",
"scripts": {
"test": "mocha test",
"test": "mocha test/**/*.js",
"start": "node ./src/index.js"
},
"dependencies": {
Expand Down
16 changes: 1 addition & 15 deletions src/api/v1/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@ const config = require('../../../../config');

const app = module.exports = express.Router();

const randomId = () => {
return (Math.random().toString(36) + '0000000000000000000').substr(2, 16);
};

const generateClientId = () => {
let clientId = randomId();

while (realm.getClientById(clientId)) {
clientId = randomId();
}

return clientId;
};

// Retrieve guaranteed random ID.
app.get('/id', (req, res, next) => {
res.contentType = 'text/html';
res.send(generateClientId());
res.send(realm.generateClientId());
});

// Get a list of all peers for a key, enabled by the `allowDiscovery` flag.
Expand Down
56 changes: 56 additions & 0 deletions src/models/realm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Realm {
constructor () {
this.clients = new Map();
this.messageQueue = new Map();
}

getClientsIds () {
return [...this.clients.keys()];
}

getClientById (clientId) {
return this.clients.get(clientId);
}

setClient (client, id) {
this.clients.set(id, client);
}

removeClientById (id) {
const client = this.getClientById(id);

if (!client) return false;

this.clients.delete(id);
}

getMessageQueueById (id) {
return this.messageQueue.get(id);
}

addMessageToQueue (id, message) {
if (!this.getMessageQueueById(id)) {
this.messageQueue.set(id, []);
}

this.getMessageQueueById(id).push(message);
}

clearMessageQueue (id) {
this.messageQueue.delete(id);
}

generateClientId () {
const randomId = () => (Math.random().toString(36) + '0000000000000000000').substr(2, 16);

let clientId = randomId();

while (this.getClientById(clientId)) {
clientId = randomId();
}

return clientId;
}
}

module.exports = Realm;
43 changes: 1 addition & 42 deletions src/services/realm/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,3 @@
class Realm {
constructor () {
this.clients = new Map();
this.messageQueue = new Map();
}

getClientsIds () {
return [...this.clients.keys()];
}

getClientById (clientId) {
return this.clients.get(clientId);
}

setClient (client, id) {
this.clients.set(id, client);
}

removeClientById (id) {
const client = this.getClientById(id);

if (!client) return false;

this.clients.delete(id);
}

getMessageQueueById (id) {
return this.messageQueue.get(id);
}

addMessageToQueue (id, message) {
if (!this.getMessageQueueById(id)) {
this.messageQueue.set(id, []);
}

this.getMessageQueueById(id).push(message);
}

clearMessageQueue (id) {
this.messageQueue.delete(id);
}
}
const Realm = require('../../models/realm');

module.exports = new Realm();
49 changes: 49 additions & 0 deletions test/models/realm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { expect } = require('chai');
const Realm = require('../../src/models/realm');
const Client = require('../../src/models/client');

describe('Realm', () => {
describe('#generateClientId', () => {
it('should generate a 16-character ID', () => {
const realm = new Realm();
expect(realm.generateClientId().length).to.eq(16);
});
});

describe('#setClient', () => {
it('should add client to realm', () => {
const realm = new Realm();
const client = new Client({ id: 'id', token: '' });

realm.setClient(client, 'id');
expect(realm.getClientsIds()).to.deep.eq(['id']);
});
});

describe('#removeClientById', () => {
it('should remove client from realm', () => {
const realm = new Realm();
const client = new Client({ id: 'id', token: '' });

realm.setClient(client, 'id');
realm.removeClientById('id');

expect(realm.getClientById('id')).to.be.undefined;
});
});

describe('#getClientsIds', () => {
it('should reflects on add/remove childs', () => {
const realm = new Realm();
const client = new Client({ id: 'id', token: '' });

realm.setClient(client, 'id');
expect(realm.getClientsIds()).to.deep.eq(['id']);

expect(realm.getClientById('id')).to.eq(client);

realm.removeClientById('id');
expect(realm.getClientsIds()).to.deep.eq([]);
});
});
});
152 changes: 0 additions & 152 deletions test/server.js

This file was deleted.

0 comments on commit 5f3774f

Please sign in to comment.