| @@ -21,6 +21,7 @@ module Agents | ||
| 21 | 21 | * `this.memory()` | 
| 22 | 22 | * `this.memory(key)` | 
| 23 | 23 | * `this.memory(keyToSet, valueToSet)` | 
| 24 | + * `this.deleteKey(key)` (deletes a key from memory and returns the value) | |
| 24 | 25 | * `this.credential(name)` | 
| 25 | 26 | * `this.credential(name, valueToSet)` | 
| 26 | 27 | * `this.options()` | 
| @@ -120,6 +121,7 @@ module Agents | ||
| 120 | 121 | memory.to_json | 
| 121 | 122 | end | 
| 122 | 123 | end | 
| 124 | +      context["deleteKey"] = lambda { |a, x| memory.delete(x).to_json } | |
| 123 | 125 |        context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) } | 
| 124 | 126 |        context["unescapeHtml"] = lambda { |a, x| CGI.unescapeHTML(x) } | 
| 125 | 127 |        context['getCredential'] = lambda { |a, k| credential(k); } | 
| @@ -198,6 +200,10 @@ module Agents | ||
| 198 | 200 | doError(message); | 
| 199 | 201 | } | 
| 200 | 202 |  | 
| 203 | +        Agent.deleteKey = function(key) { | |
| 204 | + return JSON.parse(deleteKey(key)); | |
| 205 | + } | |
| 206 | + | |
| 201 | 207 |          Agent.escapeHtml = function(html) { | 
| 202 | 208 | return escapeHtml(html); | 
| 203 | 209 | } | 
| @@ -188,6 +188,41 @@ describe Agents::JavaScriptAgent do | ||
| 188 | 188 | end | 
| 189 | 189 | end | 
| 190 | 190 |  | 
| 191 | + describe "deleteKey" do | |
| 192 | + it "deletes a memory key" do | |
| 193 | +        @agent.memory = { foo: "baz" } | |
| 194 | +        @agent.options['code'] = 'Agent.check = function() { | |
| 195 | +          this.deleteKey("foo"); | |
| 196 | + };' | |
| 197 | + @agent.save! | |
| 198 | + @agent.check | |
| 199 | + expect(@agent.memory['foo']).to be_nil | |
| 200 | +        expect { @agent.reload.memory }.not_to raise_error | |
| 201 | + end | |
| 202 | + | |
| 203 | + it "returns the string value of the deleted key" do | |
| 204 | +        @agent.memory = { foo: "baz" } | |
| 205 | +        @agent.options['code'] = 'Agent.check = function() { | |
| 206 | +          this.createEvent({ message: this.deleteKey("foo")}); | |
| 207 | + };' | |
| 208 | + @agent.save! | |
| 209 | + @agent.check | |
| 210 | + created_event = @agent.events.last | |
| 211 | +        expect(created_event.payload).to eq('message' => "baz") | |
| 212 | + end | |
| 213 | + | |
| 214 | + it "returns the hash value of the deleted key" do | |
| 215 | +        @agent.memory = { foo: { baz: 'test' }  } | |
| 216 | +        @agent.options['code'] = 'Agent.check = function() { | |
| 217 | +          this.createEvent({ message: this.deleteKey("foo")}); | |
| 218 | + };' | |
| 219 | + @agent.save! | |
| 220 | + @agent.check | |
| 221 | + created_event = @agent.events.last | |
| 222 | +        expect(created_event.payload).to eq('message' => { 'baz' => 'test' }) | |
| 223 | + end | |
| 224 | + end | |
| 225 | + | |
| 191 | 226 | describe "creating events" do | 
| 192 | 227 | it "creates events with this.createEvent in the JavaScript environment" do | 
| 193 | 228 |          @agent.options['code'] = 'Agent.check = function() { this.createEvent({ message: "This is an event!", stuff: { foo: 5 } }); };' |