moth

Monarch Of The Hill game server
git clone https://git.woozle.org/neale/moth.git

moth / pkg / transpile
Neale Pickett  ·  2023-09-15

puzzle_test.go

  1package transpile
  2
  3import (
  4	"bytes"
  5	"io"
  6	"strings"
  7	"testing"
  8
  9	"github.com/spf13/afero"
 10)
 11
 12func TestPuzzle(t *testing.T) {
 13	puzzleFs := newTestFs()
 14	catFs := NewRecursiveBasePathFs(puzzleFs, "cat0")
 15
 16	{
 17		pd := NewFsPuzzlePoints(catFs, 1)
 18		p, err := pd.Puzzle()
 19		if err != nil {
 20			t.Error(err)
 21		}
 22
 23		if (len(p.Answers) == 0) || (p.Answers[0] != "YAML answer") {
 24			t.Error("Answers are wrong", p.Answers)
 25		}
 26		if len(p.Answers) != len(p.AnswerHashes) {
 27			t.Error("Answer hashes length does not match answers length")
 28		}
 29		if len(p.AnswerHashes[0]) != 4 {
 30			t.Error("Answer hash is wrong length")
 31		}
 32		if (len(p.Authors) != 3) || (p.Authors[1] != "Buster") {
 33			t.Error("Authors are wrong", p.Authors)
 34		}
 35		if p.Objective != "moo like a cow" {
 36			t.Error("Wrong objective")
 37		}
 38		if p.Success.Acceptable != "say moo" {
 39			t.Error("Wrong Success.Acceptable")
 40		}
 41		if p.Success.Mastery != "say mrr" {
 42			t.Error("Wrong Success.Mastery")
 43		}
 44		if len(p.KSAs) != 2 {
 45			t.Error("KSAs are wrong")
 46		}
 47		if p.KSAs[0] != "a1" {
 48			t.Error("KSAs[0] wrong")
 49		}
 50		if p.Body != "<p>YAML body</p>\n" {
 51			t.Errorf("Body parsed wrong: %#v", p.Body)
 52		}
 53
 54		f, err := pd.Open("moo.txt")
 55		if err != nil {
 56			t.Error(err)
 57		}
 58		defer f.Close()
 59		buf := new(bytes.Buffer)
 60		if _, err := io.Copy(buf, f); err != nil {
 61			t.Error(err)
 62		}
 63		if buf.String() != "Moo." {
 64			t.Error("Attachment wrong: ", buf.String())
 65		}
 66	}
 67
 68	{
 69		p, err := NewFsPuzzlePoints(catFs, 2).Puzzle()
 70		if err != nil {
 71			t.Error(err)
 72		}
 73		if (len(p.Answers) == 0) || (p.Answers[0] != "RFC822 answer") {
 74			t.Error("Answers are wrong", p.Answers)
 75		}
 76		if (len(p.Authors) != 3) || (p.Authors[1] != "Arthur") {
 77			t.Error("Authors are wrong", p.Authors)
 78		}
 79		if p.Body != "<p>RFC822 body</p>\n" {
 80			t.Errorf("Body parsed wrong: %#v", p.Body)
 81		}
 82	}
 83
 84	if _, err := NewFsPuzzlePoints(catFs, 3).Puzzle(); err != nil {
 85		t.Error("Legacy `puzzle.moth` file:", err)
 86	}
 87
 88	if puzzle, err := NewFsPuzzlePoints(catFs, 4).Puzzle(); err != nil {
 89		t.Error("Markdown test file:", err)
 90	} else if !strings.Contains(puzzle.Body, "<table>") {
 91		t.Error("Markdown table extension isn't making tables")
 92	} else if !strings.Contains(puzzle.Body, "<dl>") {
 93		t.Error("Markdown dictionary extension isn't making tables")
 94	}
 95
 96	if _, err := NewFsPuzzlePoints(catFs, 99).Puzzle(); err == nil {
 97		t.Error("Non-existent puzzle", err)
 98	}
 99
100	if _, err := NewFsPuzzlePoints(catFs, 10).Puzzle(); err == nil {
101		t.Error("Broken YAML")
102	}
103	if _, err := NewFsPuzzlePoints(catFs, 20).Puzzle(); err == nil {
104		t.Error("Bad RFC822 header")
105	}
106	if _, err := NewFsPuzzlePoints(catFs, 21).Puzzle(); err == nil {
107		t.Error("Boken RFC822 header")
108	}
109
110	{
111		fs := afero.NewMemMapFs()
112		if err := afero.WriteFile(fs, "1/mkpuzzle", []byte("bleat"), 0755); err != nil {
113			t.Error(err)
114		}
115		p := NewFsPuzzlePoints(fs, 1)
116		if _, ok := p.(FsCommandPuzzle); !ok {
117			t.Error("We didn't get an FsCommandPuzzle")
118		}
119		if _, err := p.Puzzle(); err == nil {
120			t.Error("We didn't get an error trying to run a command from a MemMapFs")
121		}
122	}
123}
124
125func TestFsPuzzle(t *testing.T) {
126	catFs := NewRecursiveBasePathFs(NewRecursiveBasePathFs(afero.NewOsFs(), "testdata"), "static")
127
128	if _, err := NewFsPuzzlePoints(catFs, 1).Puzzle(); err != nil {
129		t.Error(err)
130	}
131
132	if puzzle, err := NewFsPuzzlePoints(catFs, 2).Puzzle(); err != nil {
133		t.Error(err)
134	} else if !strings.Contains(puzzle.Body, "class=\"moo\"") {
135		t.Error("Raw HTML didn't make it through")
136	}
137
138	mkpuzzleDir := NewFsPuzzlePoints(catFs, 3)
139	if _, err := mkpuzzleDir.Puzzle(); err != nil {
140		t.Error(err)
141	}
142
143	if r, err := mkpuzzleDir.Open("moo.txt"); err != nil {
144		t.Error(err)
145	} else {
146		defer r.Close()
147		buf := new(bytes.Buffer)
148		if _, err := io.Copy(buf, r); err != nil {
149			t.Error(err)
150		}
151		if buf.String() != "Moo.\n" {
152			t.Errorf("Wrong body: %#v", buf.String())
153		}
154	}
155
156	if r, err := mkpuzzleDir.Open("error"); err == nil {
157		r.Close()
158		t.Error("Error open didn't return error")
159	}
160
161	if !mkpuzzleDir.Answer("moo") {
162		t.Error("Right answer marked wrong")
163	}
164	if mkpuzzleDir.Answer("wrong") {
165		t.Error("Wrong answer marked correct")
166	}
167	if mkpuzzleDir.Answer("error") {
168		t.Error("Error answer marked correct")
169	}
170}
171
172func TestAttachment(t *testing.T) {
173	buf := bytes.NewBufferString(`
174attachments: 
175  - simple
176  - filename: complex
177    filesystempath: backingfile
178`)
179	p, err := yamlHeaderParser(buf)
180	if err != nil {
181		t.Error(err)
182		return
183	}
184
185	att := p.Attachments
186	if len(att) != 2 {
187		t.Error("Wrong number of attachments", att)
188	}
189	if att[0].Filename != "simple" {
190		t.Error("Attachment 0 wrong")
191	}
192	if att[0].Filename != att[0].FilesystemPath {
193		t.Error("Attachment 0 wrong")
194	}
195	if att[1].Filename != "complex" {
196		t.Error("Attachment 1 wrong")
197	}
198	if att[1].FilesystemPath != "backingfile" {
199		t.Error("Attachment 2 wrong")
200	}
201}