2020-08-17 17:43:57 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-17 20:33:23 -06:00
|
|
|
"archive/zip"
|
2022-05-10 13:20:54 -06:00
|
|
|
"bytes"
|
2020-08-17 20:33:23 -06:00
|
|
|
"fmt"
|
2021-10-13 18:25:27 -06:00
|
|
|
"io/ioutil"
|
2020-08-17 17:43:57 -06:00
|
|
|
"testing"
|
2022-05-10 13:20:54 -06:00
|
|
|
"testing/fstest"
|
|
|
|
"time"
|
2020-08-17 17:43:57 -06:00
|
|
|
)
|
|
|
|
|
2021-10-14 19:17:38 -06:00
|
|
|
type testFileContents struct {
|
2020-08-17 20:33:23 -06:00
|
|
|
Name, Body string
|
2021-10-14 19:17:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var testFiles = []testFileContents{
|
2020-08-28 14:15:19 -06:00
|
|
|
{"puzzles.txt", "1\n3\n2\n"},
|
2020-08-21 17:02:38 -06:00
|
|
|
{"answers.txt", "1 answer123\n1 answer456\n2 wat\n"},
|
2020-10-13 16:41:50 -06:00
|
|
|
{"1/puzzle.json", `{"name": "moo"}`},
|
|
|
|
{"2/puzzle.json", `{}`},
|
|
|
|
{"2/moo.txt", `moo`},
|
|
|
|
{"3/puzzle.json", `{}`},
|
|
|
|
{"3/moo.txt", `moo`},
|
2020-08-17 20:33:23 -06:00
|
|
|
}
|
|
|
|
|
2022-05-10 13:20:54 -06:00
|
|
|
type TestMothballs struct {
|
|
|
|
*Mothballs
|
|
|
|
fsys fstest.MapFS
|
|
|
|
now time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestMothballs() TestMothballs {
|
|
|
|
fsys := make(fstest.MapFS)
|
|
|
|
m := TestMothballs{
|
|
|
|
fsys: fsys,
|
|
|
|
Mothballs: NewMothballs(fsys),
|
|
|
|
now: time.Now(),
|
|
|
|
}
|
|
|
|
m.createMothball("pategory")
|
|
|
|
m.refresh()
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMothballs) createMothballWithFiles(cat string, contents []testFileContents) {
|
|
|
|
f := new(bytes.Buffer)
|
2020-08-17 20:33:23 -06:00
|
|
|
|
|
|
|
w := zip.NewWriter(f)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
for _, file := range testFiles {
|
|
|
|
of, _ := w.Create(file.Name)
|
|
|
|
of.Write([]byte(file.Body))
|
|
|
|
}
|
2021-10-14 19:17:38 -06:00
|
|
|
for _, file := range contents {
|
|
|
|
of, _ := w.Create(file.Name)
|
|
|
|
of.Write([]byte(file.Body))
|
|
|
|
}
|
2022-05-10 13:20:54 -06:00
|
|
|
filename := fmt.Sprintf("%.mb", cat)
|
|
|
|
m.now = m.now.Add(time.Millisecond)
|
|
|
|
m.fsys[filename] = &fstest.MapFile{
|
|
|
|
Data: f.Bytes(),
|
|
|
|
Mode: 0x644,
|
|
|
|
ModTime: m.now,
|
|
|
|
}
|
2021-10-13 18:25:27 -06:00
|
|
|
}
|
|
|
|
|
2022-05-10 13:20:54 -06:00
|
|
|
func (m *TestMothballs) createMothball(cat string) {
|
2021-10-14 19:17:38 -06:00
|
|
|
m.createMothballWithFiles(
|
|
|
|
cat,
|
|
|
|
[]testFileContents{
|
|
|
|
{"1/moo.txt", "moo"},
|
|
|
|
},
|
|
|
|
)
|
2020-08-17 20:33:23 -06:00
|
|
|
}
|
|
|
|
|
2022-05-10 13:20:54 -06:00
|
|
|
func TestMothballStuff(t *testing.T) {
|
2020-08-19 18:01:21 -06:00
|
|
|
m := NewTestMothballs()
|
|
|
|
if _, ok := m.categories["pategory"]; !ok {
|
2020-08-17 20:33:23 -06:00
|
|
|
t.Error("Didn't create a new category")
|
|
|
|
}
|
|
|
|
|
|
|
|
inv := m.Inventory()
|
|
|
|
if len(inv) != 1 {
|
|
|
|
t.Error("Wrong inventory size:", inv)
|
|
|
|
}
|
|
|
|
for _, cat := range inv {
|
2020-08-28 14:15:19 -06:00
|
|
|
switch cat.Name {
|
|
|
|
case "pategory":
|
|
|
|
if len(cat.Puzzles) != 3 {
|
|
|
|
t.Error("Puzzles list wrong length")
|
|
|
|
}
|
|
|
|
if cat.Puzzles[1] != 2 {
|
|
|
|
t.Error("Puzzles list not sorted")
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 20:33:23 -06:00
|
|
|
for _, points := range cat.Puzzles {
|
|
|
|
f, _, err := m.Open(cat.Name, points, "puzzle.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(cat.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 17:02:38 -06:00
|
|
|
if f, _, err := m.Open("nealegory", 1, "puzzle.json"); err == nil {
|
|
|
|
f.Close()
|
|
|
|
t.Error("You can't open a puzzle in a nealegory, that doesn't even rhyme!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, _, err := m.Open("pategory", 1, "bozo"); err == nil {
|
|
|
|
f.Close()
|
|
|
|
t.Error("This file shouldn't exist")
|
|
|
|
}
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
if ok, _ := m.CheckAnswer("pategory", 1, "answer"); ok {
|
2020-08-19 18:01:21 -06:00
|
|
|
t.Error("Wrong answer marked right")
|
|
|
|
}
|
2020-09-08 17:49:02 -06:00
|
|
|
if _, err := m.CheckAnswer("pategory", 1, "answer123"); err != nil {
|
2020-08-19 18:01:21 -06:00
|
|
|
t.Error("Right answer marked wrong", err)
|
|
|
|
}
|
2020-09-08 17:49:02 -06:00
|
|
|
if _, err := m.CheckAnswer("pategory", 1, "answer456"); err != nil {
|
2020-08-19 18:01:21 -06:00
|
|
|
t.Error("Right answer marked wrong", err)
|
|
|
|
}
|
2020-09-08 17:49:02 -06:00
|
|
|
if ok, err := m.CheckAnswer("nealegory", 1, "moo"); ok {
|
2020-08-21 17:02:38 -06:00
|
|
|
t.Error("Checking answer in non-existent category should fail")
|
2021-10-13 18:25:27 -06:00
|
|
|
} else if err.Error() != "no such category: nealegory" {
|
2020-08-21 17:02:38 -06:00
|
|
|
t.Error("Wrong error message")
|
|
|
|
}
|
2020-08-19 18:01:21 -06:00
|
|
|
|
2021-10-13 18:25:27 -06:00
|
|
|
goofyText := "bozonics"
|
|
|
|
//time.Sleep(1 * time.Second) // I don't love this, but we need the mtime to increase, and it's only accurate to 1s
|
2021-10-14 19:17:38 -06:00
|
|
|
m.createMothballWithFiles(
|
|
|
|
"pategory",
|
|
|
|
[]testFileContents{
|
|
|
|
{"1/moo.txt", goofyText},
|
|
|
|
},
|
|
|
|
)
|
2021-10-13 18:25:27 -06:00
|
|
|
m.refresh()
|
|
|
|
if f, _, err := m.Open("pategory", 1, "moo.txt"); err != nil {
|
|
|
|
t.Error("pategory/1/moo.txt", err)
|
|
|
|
} else if contents, err := ioutil.ReadAll(f); err != nil {
|
|
|
|
t.Error("read all pategory/1/moo.txt", err)
|
|
|
|
} else if string(contents) != goofyText {
|
|
|
|
t.Error("read all replacement pategory/1/moo.txt contents wrong, got", string(contents))
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:33:23 -06:00
|
|
|
m.createMothball("test2")
|
2022-05-10 13:20:54 -06:00
|
|
|
delete(m.fsys, "pategory.mb")
|
2020-08-18 17:04:23 -06:00
|
|
|
m.refresh()
|
2020-08-17 20:33:23 -06:00
|
|
|
inv = m.Inventory()
|
|
|
|
if len(inv) != 1 {
|
|
|
|
t.Error("Deleted mothball is still around", inv)
|
|
|
|
}
|
2020-08-19 18:01:21 -06:00
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
}
|