Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replace command to update memories #384

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions scripts/remember.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# Remembers a key and value
#
# Commands:
# hubot what is|remember <key> - Returns a string
# hubot remember <key> is <value>. - Returns nothing. Remembers the text for next time!
# hubot what do you remember - Returns everything hubot remembers.
# hubot rem|remember|what is <key> - Returns a string.
# hubot rem|remember <key> is <value> - Returns nothing. Remembers the text for next time!
# hubot replace <key> with <value> - Returns nothing. Replaces an existing memory with new text.
# hubot forget <key> - Removes key from hubots brain.
# hubot what are your favorite memories? - Returns a list of the most remembered memories.
# hubot random memory - Returns a random string
# hubot what are your favorite memories? - Returns a list of the most remembered memories.
# hubot me|random memory - Returns a random memory.
# hubot me|random memory <prefix> - Returns a random memory whose key matches the prefix.
#
# Dependencies:
# "underscore": "*"
Expand All @@ -30,7 +31,7 @@ module.exports = (robot) ->
value = match[3]
currently = memories()[key]
if currently
msg.send "But #{key} is already #{currently}. Forget #{key} first."
msg.send "But #{key} is already #{currently}. Use `replace` to update existing memories."
else
memories()[key] = value
msg.send "OK, I'll remember #{key}."
Expand Down Expand Up @@ -61,12 +62,28 @@ module.exports = (robot) ->

msg.send value

robot.respond /replace\s+(.*)/i, (msg) ->
words = msg.match[1].trim()
if match = words.match /(.*?)(\s+with\s+([\s\S]*))$/i
msg.finish()
key = match[1].toLowerCase()
value = match[3]
currently = memories()[key]
memories()[key] = value
if currently
msg.send "OK, #{key} has been updated."
else
msg.send "I don't remember #{key}, but I'll remember it now!"

robot.respond /forget\s+(.*)/i, (msg) ->
key = msg.match[1].toLowerCase()
value = memories()[key]
delete memories()[key]
delete memoriesByRecollection()[key]
msg.send "I've forgotten #{key} is #{value}."
if value
delete memories()[key]
delete memoriesByRecollection()[key]
msg.send "I've forgotten #{key} is #{value}."
else
msg.send "I don't remember anything matching `#{key}`... so we're probably all good?"

robot.respond /what are your favorite memories/i, (msg) ->
msg.finish()
Expand Down
49 changes: 49 additions & 0 deletions tests/rem_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Helper = require('hubot-test-helper')

helper = new Helper('../scripts/remember.coffee')
expect = require('chai').expect

describe '.rem/.replace', ->
room = null

lastResponse = () -> room.messages[room.messages.length - 1]

beforeEach ->
room = helper.createRoom()

it 'should remember things', ->
# Test that we can create a new memory.
room.user.say 'alice', '@hubot rem key is value'

expect(lastResponse()).to.eql ['hubot', 'OK, I\'ll remember key.']
expect(room.robot.brain.data.remember['key']).to.eql 'value'

# Test recalling the new memory.
room.user.say 'alice', '@hubot rem key'
expect(lastResponse()).to.eql ['hubot', 'value']

it 'should be able to update memories', ->
# Create a new memory `key` and then update its value.
room.user.say 'alice', '@hubot rem key is value'
room.user.say 'alice', '@hubot replace key with a different value'

expect(lastResponse()).to.eql ['hubot', 'OK, key has been updated.']
expect(room.robot.brain.data.remember['key']).to.eql 'a different value'

it 'should be able to forget memories', ->
# Create a new memory `key` and then try to forget it.
room.user.say 'alice', '@hubot rem key is value'
room.user.say 'alice', '@hubot forget key'

expect(lastResponse()).to.eql ['hubot', 'I\'ve forgotten key is value.']
expect(room.robot.brain.data.remember['key']).to.eql undefined

# Make sure we respond correctly if no such key exists.
room.user.say 'alice', '@hubot forget key'
expect(lastResponse()).to.eql ['hubot', "I don't remember anything matching `key`... so we're probably all good?"]

it 'should not hallucinate memories', ->
room.user.say 'alice', '@hubot rem key'

expect(lastResponse()).to.eql ['hubot', 'I don\'t remember anything matching `key`']
expect(room.robot.brain.data.remember['key']).to.eql undefined