-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtilityAi.spec.js
254 lines (182 loc) · 6.77 KB
/
UtilityAi.spec.js
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
249
250
251
252
253
254
const { expect } = require("chai")
const UtilityAi = require("./UtilityAi")
describe("UtilityAi", () => {
it("is a function", () => {
expect(UtilityAi).to.be.a("function")
expect(new UtilityAi).to.be.an("object")
})
describe("#addAction", () => {
let utility_ai
beforeEach(() => {
utility_ai = new UtilityAi
})
it("is a function", () => {
expect(utility_ai.addAction).to.be.a("function")
})
it("requires a description and callback", () => {
expect(() => utility_ai.addAction()).to.throw(/missing description/i)
expect(() => utility_ai.addAction("test")).to.throw(/missing callback/i)
expect(() => utility_ai.addAction("test", () => {})).to.not.throw()
})
it("registers actions", () => {
expect(utility_ai._actions).to.be.an("array").that.has.lengthOf(0)
utility_ai.addAction("test", () => {})
expect(utility_ai._actions).to.be.an("array").that.has.lengthOf(1)
const [action] = utility_ai._actions
expect(action).to.be.an("object")
expect(action).to.have.a.property("description", "test")
utility_ai.addAction("test2", () => {})
expect(utility_ai._actions).to.be.an("array").that.has.lengthOf(2)
const [,action2] = utility_ai._actions
expect(action2).to.be.an("object")
expect(action2).to.have.a.property("description", "test2")
})
it("calls callback with action param", () => {
let cb_called = false
utility_ai.addAction("test", action => {
expect(action).to.be.an("object").with.property("score")
cb_called = true
})
utility_ai.evaluate()
expect(cb_called).to.be.true
})
describe("Action", () => {
let action
beforeEach(() => {
utility_ai.addAction("test", a => action = a)
})
describe("#score", () => {
it("is a function", () => {
expect(action.score).to.be.a("function")
})
it("requires a description, score and callback", () => {
expect(() => action.score()).to.throw(/missing description/i)
expect(() => action.score("test")).to.throw(/missing callback/i)
expect(() => action.score("test", () => {})).to.not.throw()
})
it("evaluates as 0 if no number is returned", () => {
action.score("test", () => {})
expect(action.evaluate()).to.be.eq(0)
action.score("test", () => { return "10" })
expect(action.evaluate()).to.be.eq(0)
action.score("test", () => { return null })
expect(action.evaluate()).to.be.eq(0)
action.score("test", () => { return 10 })
expect(action.evaluate()).to.be.eq(10)
action.score("test", () => { return 10.10 })
expect(action.evaluate()).to.be.eq(20.10)
action.score("test", () => { return true })
expect(action.evaluate()).to.be.eq(20.10)
action.score("test", () => { return false })
expect(action.evaluate()).to.be.eq(20.10)
action.score("test", () => { return "10.10" })
expect(action.evaluate()).to.be.eq(20.10)
})
}) // # score
describe("#condition", () => {
it("is a function", () => {
expect(action.condition).to.be.a("function")
})
it("expects a callback as param", () => {
expect(() => action.condition()).to.throw(/missing callback/i)
expect(() => action.condition(() => {})).to.not.throw()
})
it("does not execute scorers if condition fails", () => {
action.score("test", () => 20)
expect(action.evaluate()).to.be.eq(20)
action.condition(data => {
return data.is_true === true
})
expect(action.evaluate({ is_true: true })).to.be.eq(20)
expect(action.evaluate({ is_true: false })).to.be.eq(-Infinity)
})
it("evaluates missing return value as false", () => {
action.score("test", () => 20)
expect(action.evaluate()).to.be.eq(20)
action.condition(data => {
if (data.is_true) {
return true
}
})
expect(action.evaluate({ is_true: true })).to.be.eq(20)
expect(action.evaluate({ is_true: false })).to.be.eq(-Infinity)
})
}) // #condition
describe("#evaluate", () => {
it("evaluates score", () => {
let action
utility_ai.addAction("attack", _action => {
action = _action
_action.score("can attack", (entity) => {
return entity.can_attack && 20
})
_action.score("can kill enemy", (entity) => {
return entity.can_kill_enemy && 40
})
_action.score("has low health", (entity) => {
return entity.has_low_health && -20
})
})
const entity = {
can_attack: true,
can_kill_enemy: true,
has_low_health: false
}
const result = action.evaluate(entity)
expect(result).to.be.a("number")
expect(result).to.be.eq(60)
entity.has_low_health = true
expect(action.evaluate(entity)).to.be.eq(40)
entity.can_kill_enemy = false
expect(action.evaluate(entity)).to.be.eq(0)
})
}) // # evaluate
}) // # Action
}) // #addAction
describe("#evaluate", () => {
let utility_ai
beforeEach(() => {
utility_ai = new UtilityAi
})
it("is a function", () => {
expect(utility_ai.evaluate).to.be.a("function")
})
it("evaluates actions", () => {
const data = {
entity: {
can_attack: true,
can_heal: true,
can_kill_enemy: true,
is_full_hp: true,
has_low_health: false
}
}
utility_ai.addAction("attack", action => {
action.score("can attack", ({ entity }) => {
return entity.can_attack && 20
})
action.score("can kill enemy", ({ entity }) => {
return entity.can_kill_enemy && 40
})
action.score("has low health", ({ entity }) => {
return entity.has_low_health && -20
})
})
utility_ai.addAction("heal", action => {
action.score("can heal", ({ entity }) => {
return entity.can_heal && 20
})
action.score("is full hp", ({ entity }) => {
return entity.is_full_hp && -60
})
action.score("has low health", ({ entity }) => {
return entity.has_low_health && 40
})
})
const result = utility_ai.evaluate(data)
expect(result).to.be.an("object")
expect(result).to.have.property("action", "attack")
expect(result).to.have.property("score", 60)
})
}) // #evaluate
}) // #UtilityAi