92 lines
2.9 KiB
Go
92 lines
2.9 KiB
Go
package challenge
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSample(t *testing.T) {
|
|
out, err := Run("../../config.json", "../../events")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
want := strings.TrimLeft(`
|
|
[14:00:00] Player [1] registered
|
|
[14:00:00] Player [2] registered
|
|
[14:10:00] Player [2] entered the dungeon
|
|
[14:10:00] Player [3] is disqualified
|
|
[14:11:00] Player [2] makes imposible move [5]
|
|
[14:14:00] Player [2] killed the monster
|
|
[14:27:00] Player [2] recieved [60] of damage
|
|
[14:29:00] Player [2] recieved [50] of damage
|
|
[14:29:00] Player [2] is dead
|
|
[14:40:00] Player [1] entered the dungeon
|
|
[14:41:00] Player [1] killed the monster
|
|
[14:44:00] Player [1] recieved [50] of damage
|
|
[14:45:00] Player [1] killed the monster
|
|
[14:48:00] Player [1] went to the next floor
|
|
[14:48:00] Player [1] entered the boss's floor
|
|
[14:49:00] Player [1] recieved [25] of damage
|
|
[14:49:02] Player [1] has restored [80] of health
|
|
[14:50:00] Player [1] recieved [65] of damage
|
|
[14:59:00] Player [1] killed the boss
|
|
[15:04:00] Player [1] left the dungeon
|
|
Final report:
|
|
[SUCCESS] 1 [00:24:00, 00:05:00, 00:11:00] HP:35
|
|
[FAIL] 2 [00:19:00, 00:00:00, 00:00:00] HP:0
|
|
[DISQUAL] 3 [00:00:00, 00:00:00, 00:00:00] HP:100
|
|
`, "\n")
|
|
|
|
if out != want {
|
|
t.Fatalf("unexpected output\nwant:\n%s\ngot:\n%s", want, out)
|
|
}
|
|
}
|
|
|
|
func TestHealthIsCappedAndTerminalEventsIgnored(t *testing.T) {
|
|
e, err := NewEngine(Config{Floors: 1, OpenAt: "10:00:00", Duration: 1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Apply(Event{At: mustClock("10:00:00"), Player: 7, ID: 1})
|
|
e.Apply(Event{At: mustClock("10:00:01"), Player: 7, ID: 2})
|
|
e.Apply(Event{At: mustClock("10:00:02"), Player: 7, ID: 11, Extra: "70"})
|
|
e.Apply(Event{At: mustClock("10:00:03"), Player: 7, ID: 10, Extra: "90"})
|
|
e.Apply(Event{At: mustClock("10:00:04"), Player: 7, ID: 11, Extra: "100"})
|
|
e.Apply(Event{At: mustClock("10:00:05"), Player: 7, ID: 10, Extra: "10"})
|
|
|
|
p := e.players[7]
|
|
if p.Health != 0 || !p.Dead || !p.Terminal {
|
|
t.Fatalf("unexpected player state: hp=%d dead=%v terminal=%v", p.Health, p.Dead, p.Terminal)
|
|
}
|
|
if got := len(e.logs); got != 6 {
|
|
t.Fatalf("terminal event was not ignored, logs=%d", got)
|
|
}
|
|
}
|
|
|
|
func TestCloseExpiresActiveAttempt(t *testing.T) {
|
|
e, err := NewEngine(Config{Floors: 2, Monsters: 1, OpenAt: "10:00:00", Duration: 1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Apply(Event{At: mustClock("10:00:00"), Player: 1, ID: 1})
|
|
e.Apply(Event{At: mustClock("10:10:00"), Player: 1, ID: 2})
|
|
e.Apply(Event{At: mustClock("11:30:00"), Player: 1, ID: 3})
|
|
|
|
p := e.players[1]
|
|
if !p.Terminal || p.EndedAt != mustClock("11:00:00") {
|
|
t.Fatalf("expected close at 11:00:00, got terminal=%v ended=%s", p.Terminal, formatDuration(p.EndedAt))
|
|
}
|
|
if strings.Contains(e.Output(), "killed the monster") {
|
|
t.Fatal("event after close was processed")
|
|
}
|
|
}
|
|
|
|
func mustClock(value string) time.Duration {
|
|
d, err := parseClock(value)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return d
|
|
}
|