summaryrefslogtreecommitdiff
path: root/src/world/agent.test.js
blob: 5d5828fe4b85fe80eca145cfd62dc8ce0e9d8056 (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
import { agent_decide, agent_apply } from './agent.js';


test("simple agent decisions", () => {
  const lattice = null;
  const agent = {
    id: 3,
    net: { compute: () => [[0, 1], 'state'] },
    state: null,
    x: 0, y: 0,
    flags: {},
  };

  const senses = [];
  const actions = [
    { size: 1, propose: (agent, head) => [{ agent_changes: [{ agent_id: 3, flags: { act1: head[0] } }] }] },
    { size: 1, propose: (agent, head) => [{ agent_changes: [{ agent_id: 3, flags: { act2: head[0] } }] }] },
  ];

  expect(agent_decide(lattice, agent, senses, actions)).toEqual([
    { ...agent, state: 'state' },
    actions.map((a, idx) => a.propose(null, [idx])).flat(),
  ]);
});


test("apply proposals to agent", () => {
  const props = [
    { agent_changes: [{ agent_id: 14, x: 4, y: 3 }] },
    { agent_changes: [{ agent_id: 16, x: 5, y: 3 }] },
    { agent_changes: [{ agent_id: 14, flags: { frozen: true } }] },
  ];

  const agent = { 
    id: 14,
    net: null,
    state: null,
    x: 0, y: 0,
    flags: { frozen: false, emit: 6 }
  };

  expect(agent_apply(agent, props)).toEqual({
    id: 14,
    net: null,
    state: null,
    x: 4, y: 3,
    flags: { frozen: true, emit: 6 }
  });
});