summaryrefslogtreecommitdiff
path: root/src/simulation/actions.test.js
blob: c5f35be3d20269d26a7323244acdbc63c86ae231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
'use strict';

import { actions } from './actions.js';


const [
  move_forward, move_backward, 
  turn_left, turn_right,
  place, trigger,
  pretend_frozen, unfreeze,
  ...rest
] = actions;


test("move forward", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(move_forward.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] },
  ]);
  expect(move_forward.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] },
  ]);
  expect(move_forward.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] },
  ]);
  expect(move_forward.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] },
  ]);

  expect(move_forward.propose(null, n, [0])).toEqual([]);
  expect(move_forward.propose(null, s, [-1])).toEqual([]);
  expect(move_forward.propose(null, e, [0])).toEqual([]);
  expect(move_forward.propose(null, w, [-1])).toEqual([]);
});


test("move backward", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(move_backward.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] },
  ]);
  expect(move_backward.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] },
  ]);
  expect(move_backward.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] },
  ]);
  expect(move_backward.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] },
  ]);

  expect(move_backward.propose(null, n, [0])).toEqual([]);
  expect(move_backward.propose(null, s, [-1])).toEqual([]);
  expect(move_backward.propose(null, e, [0])).toEqual([]);
  expect(move_backward.propose(null, w, [-1])).toEqual([]);
});


test("turn_left", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(turn_left.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] },
  ]);
  expect(turn_left.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] },
  ]);
  expect(turn_left.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] },
  ]);
  expect(turn_left.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] },
  ]);

  expect(turn_left.propose(null, n, [0])).toEqual([]);
  expect(turn_left.propose(null, s, [-1])).toEqual([]);
  expect(turn_left.propose(null, e, [0])).toEqual([]);
  expect(turn_left.propose(null, w, [-1])).toEqual([]);
});


test("turn_right", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(turn_right.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] },
  ]);
  expect(turn_right.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] },
  ]);
  expect(turn_right.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] },
  ]);
  expect(turn_right.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] },
  ]);

  expect(turn_right.propose(null, n, [0])).toEqual([]);
  expect(turn_right.propose(null, s, [-1])).toEqual([]);
  expect(turn_right.propose(null, e, [0])).toEqual([]);
  expect(turn_right.propose(null, w, [-1])).toEqual([]);
});


test("place", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(place.propose(null, n, [1])).toEqual([
    { lattice_changes: [
      { 
        x: 0, y: -1, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);
  expect(place.propose(null, s, [1])).toEqual([
    { lattice_changes: [
      { 
        x: 0, y: 1, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);
  expect(place.propose(null, e, [1])).toEqual([
    { lattice_changes: [
      { 
        x: 1, y: 0, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);
  expect(place.propose(null, w, [1])).toEqual([
    { lattice_changes: [
      { 
        x: -1, y: 0, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);

  expect(place.propose(null, n, [0])).toEqual([]);
  expect(place.propose(null, s, [-1])).toEqual([]);
  expect(place.propose(null, e, [0])).toEqual([]);
  expect(place.propose(null, w, [-1])).toEqual([]);
});


test("trigger", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(trigger.propose(null, n, [1])).toEqual([
    { lattice_changes: [{
      x: 0, y: -1, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);
  expect(trigger.propose(null, s, [1])).toEqual([
    { lattice_changes: [{
      x: 0, y: 1, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);
  expect(trigger.propose(null, e, [1])).toEqual([
    { lattice_changes: [{
      x: 1, y: 0, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);
  expect(trigger.propose(null, w, [1])).toEqual([
    { lattice_changes: [{
      x: -1, y: 0, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);

  expect(trigger.propose(null, n, [0])).toEqual([]);
  expect(trigger.propose(null, s, [-1])).toEqual([]);
  expect(trigger.propose(null, e, [0])).toEqual([]);
  expect(trigger.propose(null, w, [-1])).toEqual([]);
 });


test("pretend frozen", () => {
  const agent = { id: 2, x: 0, y: 0, flags: { orientation: 'n' } };

  expect(pretend_frozen.propose(null, agent, [1])).toEqual([{
    agent_changes: [{
      agent_id: 2,
      flags: { pretend_frozen: true },
    }]
  }]);

  expect(pretend_frozen.propose(null, agent, [0])).toEqual([{
    agent_changes: [{
      agent_id: 2,
      flags: { pretend_frozen: false },
    }]
  }]);
});


test("unfreeze", () => {
  const agent1 = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const agent2 = { id: 1, x: 0,  y: -1 };
  const agent3 = { id: 10, x: -1, y: 0 };
  const agents = [ agent1, agent2, agent3 ];

  expect(unfreeze.propose({agents}, agent1, [1])).toEqual([{
    agent_changes: [{
      agent_id: 1,
      flags: { frozen: false, emit: [ 0, 1, 0, 0, 0, 0, 0, 0 ] },
    }]
  }]);

  agent1.flags.orientation = 'w';

  expect(unfreeze.propose({agents}, agent1, [1])).toEqual([{
    agent_changes: [{
      agent_id: 10,
      flags: { frozen: false, emit: [ 0, 1, 0, 0, 0, 0, 0, 0 ] },
    }]
  }]);

  agent1.flags.orientation = 's';
  expect(unfreeze.propose({agents}, agent1, [1])).toEqual([]);

  expect(unfreeze.propose({agents}, agent1, [0])).toEqual([]);
});