Merge pull request #1551 from cantino/jsagent_fix_memory

Fix `this.memory(key, falsy)` not working in JavaScriptAgent

Akinori MUSHA 8 jaren geleden
bovenliggende
commit
371bfa34b7
2 gewijzigde bestanden met toevoegingen van 28 en 7 verwijderingen
  1. 4 7
      app/models/agents/java_script_agent.rb
  2. 24 0
      spec/models/agents/java_script_agent_spec.rb

+ 4 - 7
app/models/agents/java_script_agent.rb

@@ -114,12 +114,9 @@ module Agents
114 114
       context["getOptions"] = lambda { |a, x| interpolated.to_json }
115 115
       context["doLog"] = lambda { |a, x| log x }
116 116
       context["doError"] = lambda { |a, x| error x }
117
-      context["getMemory"] = lambda do |a, x, y|
118
-        if x && y
119
-          memory[x] = clean_nans(y)
120
-        else
121
-          memory.to_json
122
-        end
117
+      context["getMemory"] = lambda { |a| memory.to_json }
118
+      context["setMemory"] = lambda do |a, x, y|
119
+        memory[x] = clean_nans(y)
123 120
       end
124 121
       context["deleteKey"] = lambda { |a, x| memory.delete(x).to_json }
125 122
       context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
@@ -168,7 +165,7 @@ module Agents
168 165
 
169 166
         Agent.memory = function(key, value) {
170 167
           if (typeof(key) !== "undefined" && typeof(value) !== "undefined") {
171
-            getMemory(key, value);
168
+            setMemory(key, value);
172 169
           } else if (typeof(key) !== "undefined") {
173 170
             return JSON.parse(getMemory())[key];
174 171
           } else {

+ 24 - 0
spec/models/agents/java_script_agent_spec.rb

@@ -186,6 +186,30 @@ describe Agents::JavaScriptAgent do
186 186
         @agent.save!
187 187
         expect { @agent.reload.memory }.not_to raise_error
188 188
       end
189
+
190
+      it "it stores null" do
191
+        @agent.options['code'] = 'Agent.check = function() {
192
+          this.memory("foo", "test");
193
+          this.memory("foo", null);
194
+          };'
195
+        @agent.save!
196
+        @agent.check
197
+        expect(@agent.memory['foo']).to eq(nil)
198
+        @agent.save!
199
+        expect { @agent.reload.memory }.not_to raise_error
200
+      end
201
+
202
+      it "it stores false" do
203
+        @agent.options['code'] = 'Agent.check = function() {
204
+          this.memory("foo", "test");
205
+          this.memory("foo", false);
206
+          };'
207
+        @agent.save!
208
+        @agent.check
209
+        expect(@agent.memory['foo']).to eq(false)
210
+        @agent.save!
211
+        expect { @agent.reload.memory }.not_to raise_error
212
+      end
189 213
     end
190 214
 
191 215
     describe "deleteKey" do