summaryrefslogtreecommitdiff
path: root/main_test.go
blob: 134f22299b491ccd718a4c4284fbafb4cfa6e1cf (plain) (blame)
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
package main

import (
	"encoding/json"
	"strings"
	"testing"
)

func TestReplyToRoundTripsInTheLog(t *testing.T) {
	b, err := json.Marshal(Entry{Op: "note", NoteID: 2, ReplyTo: 7, Text: "child"})
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(string(b), `"reply_to":7`) {
		t.Fatalf("reply edge missing from %s", b)
	}
	var got Entry
	if err := json.Unmarshal(b, &got); err != nil {
		t.Fatal(err)
	}
	if got.ReplyTo != 7 {
		t.Errorf("reply_to = %d, want 7", got.ReplyTo)
	}
}

func TestHasNoteUsesLogWideNumbers(t *testing.T) {
	entries := []Entry{
		{Op: "read"},
		{Op: "note", Session: "a", NoteID: 1},
		{Op: "note", Session: "b", NoteID: 1},
	}
	if !hasNote(entries, 2) {
		t.Error("second log-wide note was not found when its session-local id repeated")
	}
	if hasNote(entries, 3) || hasNote(entries, 0) {
		t.Error("a nonexistent log-wide note was accepted")
	}
}