Skip to content

Commit

Permalink
pass most tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk committed May 21, 2019
1 parent fe7be73 commit e475597
Showing 12 changed files with 82 additions and 43 deletions.
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2019, Matthew Kaufer, Thomas Quirk

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ const Messer = require("./src/messer");
const packageJson = require("./package.json");
const settings = require("./settings");

const COMMANDS = [require("./src/commands/message")];
const EVENT_HANDLERS = [require("./src/event-handlers/message")];
const COMMANDS = require("./src/commands");
const EVENT_HANDLERS = require("./src/event-handlers");

const messer = new Messer();
COMMANDS.forEach(command => {
8 changes: 4 additions & 4 deletions src/commands/command.template.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = messer => {
return {
commands: ["mycommand"],
primaryCommand: "mycommand",

regexp: /mycommand/,
shortcutCommand: "mc",

help: "mycommand",
help: "mycommand <my-argument>",

handler() {
handler(command) {
return new Promise((resolve, reject) => {
return resolve();
});
15 changes: 9 additions & 6 deletions src/commands/help.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const chalk = require("chalk");
const { objectValues } = require("../util/helpers");

module.exports = messer => {
return {
primaryCommand: "help",

help: "help",

handler(command) {
const helpPretty = `Commands:\n${helpers
.objectValues(commandTypes)
.filter(command => command.help)
.map(type => {
return chalk.blue(type.command);
const help = Object.values(messer._commandRegistry.commands)
.map(command => {
return `${command.primaryCommand}\n\t${chalk.blue(command.help)}`;
})
.join("\n")}`;
.join("\n");

const helpPretty = `Commands:\n\n${help}`;

return Promise.resolve(helpPretty);
},
16 changes: 16 additions & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = [
require("./clear"),
require("./contacts"),
require("./delete"),
require("./file"),
require("./help"),
require("./history"),
require("./lock"),
require("./logout"),
require("./message"),
require("./recent"),
require("./reply"),
require("./settings"),
require("./threads"),
require("./unlock"),
];
1 change: 1 addition & 0 deletions src/commands/lock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const patterns = require("./util/patterns");
const { getThreadByName } = require("./util/helpers");

module.exports = messer => {
return {
6 changes: 3 additions & 3 deletions src/commands/reply.js
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@ const patterns = require("./util/patterns");

module.exports = messer => {
return {
primaryCommand: "message",
primaryCommand: "reply",

shortcutCommand: "m",
shortcutCommand: "r",

help: '(message | m) "<thread-name>" <message>',
help: "reply <message>",

handler(command) {
return new Promise((resolve, reject) => {
1 change: 1 addition & 0 deletions src/event-handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = [require("./message")];
12 changes: 8 additions & 4 deletions src/messer/index.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ const { Messen } = require("messen");
const repl = require("./repl");
const settings = require("./settings");
const helpers = require("../util/helpers");
const lock = require("../util/lock");
const lock = require("./lock");
const messageEventHandler = require("../event-handlers/message");
const eventEventHandler = require("../event-handlers/event");

@@ -143,12 +143,16 @@ Messer.prototype.processCommand = function processCommand(rawCommand) {
// ignore if rawCommand is only spaces
if (rawCommand.trim().length === 0) return Promise.resolve();

const args = rawCommand.replace("\n", "").split(" ");
const argv = rawCommand.match(/([A-z]+).*/);

let commandEntry = this._commandRegistry.commands[args[0]];
if (!argv || !argv[1]) {
return Promise.reject(Error("Invalid command - check your syntax"));
}

let commandEntry = this._commandRegistry.commands[argv[1]];
if (!commandEntry) {
commandEntry = this._commandRegistry.commands[
this._commandRegistry.shortcutMap[args[0]]
this._commandRegistry.shortcutMap[argv[1]]
];
}

File renamed without changes.
12 changes: 0 additions & 12 deletions src/util/fb-assets.js

This file was deleted.

35 changes: 23 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ const assert = require("assert");
const fs = require("fs");
const path = require("path");

const commandTypes = require("../src/commands/command-types");
const Messer = require("../src/messer");
const { getMessen, threads } = require("./messer.mock");

@@ -23,8 +22,19 @@ function getMockThread() {
* Factory to mock an instance of Messer
*/
function MockMesser() {
const COMMANDS = require("../src/commands");
const EVENT_HANDLERS = require("../src/event-handlers");

const messer = new Messer();
messer.messen = getMessen();

COMMANDS.forEach(command => {
messer.registerCommand(command(messer));
});
EVENT_HANDLERS.forEach(handler => {
messer.registerEventHandler(handler(messer));
});

return messer;
}

@@ -36,6 +46,7 @@ describe("Messer", function() {
before(async function() {
await messer.messen.login();
});

/**
* Test processCommand
*/
@@ -59,14 +70,14 @@ describe("Messer", function() {
/**
* Test the "message" command
*/
describe(`#${commandTypes.MESSAGE.command}`, function() {
describe("#message", function() {
it("should send message to valid threadname", async function() {
await messer.processCommand('message "test" hey dude').then(res => {
assert.ok(res);
});
});

it("should send message to valid threadname using abbreviated command", async function() {
it("should send message to valid threadname using shortcut command", async function() {
await messer.processCommand('m "test" hey dude').then(res => {
assert.ok(res);
});
@@ -91,7 +102,7 @@ describe("Messer", function() {
/**
* Test the "reply" command
*/
describe(`#${commandTypes.REPLY.command}`, function() {
describe("#reply", function() {
it("should fail if no message has been recieved", async function() {
await messer.processCommand("reply hey dude").catch(err => {
assert.ok(err);
@@ -105,7 +116,7 @@ describe("Messer", function() {
});
});

it("should reply using abbreviated command", async function() {
it("should reply using shortcut command", async function() {
await messer.processCommand("r yea i agree").then(() => {
assert.ok(true);
});
@@ -115,7 +126,7 @@ describe("Messer", function() {
/**
* Test the "history" command
*/
describe(`#${commandTypes.HISTORY.command}`, function() {
describe("#history", function() {
it("should gracefully fail if no thread found", async function() {
await messer.processCommand('history "bill"').catch(err => {
assert.ok(err);
@@ -232,9 +243,9 @@ describe("Messer", function() {
/**
* Test the "contacts" command
*/
describe(`#${commandTypes.CONTACTS.command}`, function() {
describe("#contacts", function() {
it("should return list of friends sep. by newline", async function() {
await messer.processCommand(commandTypes.CONTACTS.command).then(res => {
await messer.processCommand("contacts").then(res => {
assert.equal(res, "Test Friend\nTom Quirk");
});
});
@@ -250,9 +261,9 @@ describe("Messer", function() {
/**
* Test the "help" command
*/
describe(`#${commandTypes.HELP.command}`, function() {
describe("#help", function() {
it("should return some truthy value", async function() {
await messer.processCommand(commandTypes.HELP.command).then(res => {
await messer.processCommand("help").then(res => {
assert.ok(res);
});
});
@@ -261,7 +272,7 @@ describe("Messer", function() {
/**
* Test the "lock" command
*/
describe(`#${commandTypes.LOCK.command}`, function() {
describe("#lock", function() {
it("should lock on to a valid thread name and process every input line as a message command", async function() {
await messer
.processCommand('lock "test"')
@@ -300,7 +311,7 @@ describe("Messer", function() {
/**
* Test the "unlock" command
*/
describe(`#${commandTypes.UNLOCK.command}`, function() {
describe("#unlock", function() {
it("should free up the input to type regular commands", async function() {
await messer
.processCommand('lock "test"')

0 comments on commit e475597

Please sign in to comment.