|
|
@@ -1,10 +1,6 @@ |
|
|
|
package caddyhugo |
|
|
|
|
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"path" |
|
|
|
"sync" |
|
|
|
"testing" |
|
|
@@ -13,135 +9,65 @@ import ( |
|
|
|
"git.stephensearles.com/stephen/acedoc" |
|
|
|
) |
|
|
|
|
|
|
|
type World struct { |
|
|
|
CH *CaddyHugo |
|
|
|
BlogFolder string |
|
|
|
} |
|
|
|
|
|
|
|
func (w *World) Clean() { |
|
|
|
if w.BlogFolder != "" { |
|
|
|
os.RemoveAll(w.BlogFolder) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func NewWorld(t *testing.T) *World { |
|
|
|
dir, err := ioutil.TempDir("", "caddy-hugo2-test-") |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("error initializing test environment: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
w := &World{BlogFolder: dir} |
|
|
|
|
|
|
|
cmd := exec.Command("hugo", "new", "site", dir) |
|
|
|
cmd.Dir = dir |
|
|
|
out, err := cmd.CombinedOutput() |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("error initializing test site: %v\n\n%v", err, string(out)) |
|
|
|
} |
|
|
|
|
|
|
|
w.CH = &CaddyHugo{} |
|
|
|
w.CH.Setup(dir) |
|
|
|
|
|
|
|
return w |
|
|
|
} |
|
|
|
|
|
|
|
func TestEdits(t *testing.T) { |
|
|
|
w := NewWorld(t) |
|
|
|
defer w.Clean() |
|
|
|
|
|
|
|
const title = "sometitle" |
|
|
|
var contentPath = path.Join("content", title+".md") |
|
|
|
|
|
|
|
w.CH.NewContent(title, "") |
|
|
|
var ( |
|
|
|
mtx sync.Mutex |
|
|
|
|
|
|
|
send := []acedoc.Delta{ |
|
|
|
acedoc.Insert(0, 0, "hello"), |
|
|
|
acedoc.Insert(0, 5, " world"), |
|
|
|
acedoc.Insert(0, 11, " world"), |
|
|
|
} |
|
|
|
var mtx sync.Mutex |
|
|
|
received := []acedoc.Delta{} |
|
|
|
contentPath = path.Join("content", title+".md") |
|
|
|
|
|
|
|
// we will send these to the doc |
|
|
|
send = []acedoc.Delta{ |
|
|
|
acedoc.Insert(0, 0, "hello"), |
|
|
|
acedoc.Insert(0, 5, " world"), |
|
|
|
acedoc.Insert(0, 11, " world"), |
|
|
|
} |
|
|
|
|
|
|
|
// we will use this to track what we get out of the doc |
|
|
|
received = []acedoc.Delta{} |
|
|
|
) |
|
|
|
|
|
|
|
// prepare a new post |
|
|
|
w.CH.NewContent(title, "") |
|
|
|
|
|
|
|
// start an edit session |
|
|
|
doc, err := w.CH.editSession(contentPath) |
|
|
|
if err != nil { |
|
|
|
t.Fatal("error creating document client:", err) |
|
|
|
} |
|
|
|
|
|
|
|
doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error { |
|
|
|
// receive some deltas... |
|
|
|
// register an *extra* client that just adds to the received delta slice we're tracking |
|
|
|
c := doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error { |
|
|
|
mtx.Lock() |
|
|
|
defer mtx.Unlock() |
|
|
|
received = append(received, ds...) |
|
|
|
return nil |
|
|
|
})) |
|
|
|
|
|
|
|
// make sure we have an edit session |
|
|
|
_, ok := w.CH.hasEditSession(contentPath) |
|
|
|
if !ok { |
|
|
|
t.Fatal("expected there to be an established client") |
|
|
|
} |
|
|
|
|
|
|
|
doc.doc.Apply(send...) |
|
|
|
// push the deltas |
|
|
|
c.PushDeltas(send...) |
|
|
|
|
|
|
|
// wait... |
|
|
|
<-time.After(5 * time.Second) |
|
|
|
|
|
|
|
// be sure we got the correct number of deltas back |
|
|
|
mtx.Lock() |
|
|
|
defer mtx.Unlock() |
|
|
|
if len(received) != len(send) { |
|
|
|
t.Errorf("expected %d deltas, received %d; expected: %v, received: %v", len(send), len(received), send, received) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
type WebsocketTester struct { |
|
|
|
receivedPointer int |
|
|
|
received [][]byte |
|
|
|
wroteMessages []Message |
|
|
|
wroteDeltas []acedoc.Delta |
|
|
|
mtx sync.Mutex |
|
|
|
} |
|
|
|
|
|
|
|
// ReadJSON reads the next pending message from the "client" into v |
|
|
|
func (ws *WebsocketTester) ReadJSON(v interface{}) error { |
|
|
|
ws.mtx.Lock() |
|
|
|
defer ws.mtx.Unlock() |
|
|
|
|
|
|
|
if len(ws.received) <= ws.receivedPointer { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
err := json.Unmarshal(ws.received[ws.receivedPointer], v) |
|
|
|
ws.receivedPointer++ |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// WriteJSON "sends" a message, v, to the "client" |
|
|
|
func (ws *WebsocketTester) WriteJSON(v interface{}) error { |
|
|
|
ws.mtx.Lock() |
|
|
|
defer ws.mtx.Unlock() |
|
|
|
|
|
|
|
m, ok := v.(Message) |
|
|
|
if !ok { |
|
|
|
panic("wrong type written to WebsocketTester") |
|
|
|
} |
|
|
|
|
|
|
|
ws.wroteMessages = append(ws.wroteMessages, m) |
|
|
|
ws.wroteDeltas = append(ws.wroteDeltas, m.Deltas...) |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// ReceiveJSON queues a message to be sent to the client |
|
|
|
func (ws *WebsocketTester) ReceiveJSON(v interface{}) error { |
|
|
|
ws.mtx.Lock() |
|
|
|
defer ws.mtx.Unlock() |
|
|
|
|
|
|
|
out, err := json.Marshal(v) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
ws.received = append(ws.received, out) |
|
|
|
return nil |
|
|
|
t.Log(w.CH.Contents()) |
|
|
|
} |
|
|
|
|
|
|
|
func TestDeltasSingle(t *testing.T) { |
|
|
|