summaryrefslogtreecommitdiff
path: root/src/world/sense.test.js
blob: 27ee2b5d66fb539c02cd303e7ba4a5c52c038ca8 (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
import { sense_read } from './sense.js';


test("basic sense works", () => {
  const flag_sense = {
    size: 1,
    read: (world, agent) => {
      const {x, y} = agent;
      return [ world.lattice[y-1][x].type === 'flag' ? 1.0 : 0.0 ]
    },
  };

  const lattice = [[ { type: 'flag' } ]];
  const agent = { x: 0, y: 1 };

  expect(sense_read({lattice}, agent, flag_sense)).toEqual([1.0]);
});


test("senses throw if the size is incorrect", () => {
  const flag_sense = {
    size: 2,
    read: (world, agent) => {
      const {x, y} = agent;
      return [ world.lattice[y-1][x].type === 'flag' ? 1.0 : 0.0 ]
    },
  }

  const lattice = [[ { type: 'flag' } ]];
  const agent = { x: 0, y: 1 };

  expect(() => sense_read({lattice}, agent, flag_sense)).toThrow();
});