| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestPlacementProtocol(t *testing.T) { |
| 11 | if wire.ProtocolPlacement != 25 || wire.ProtocolCurrent < wire.ProtocolPlacement { |
| 12 | t.Fatal("placement protocol") |
| 13 | } |
| 14 | raw, _ := json.Marshal(wire.MessageCommand{Person: "p", TurnID: "t", PlacementNotes: []wire.NotebookEntry{{ID: "mv1", Source: wire.NoteSystem, Text: "A moved to B"}}}) |
| 15 | f := wire.Frame{ID: "c", Type: wire.FrameMessage, Data: raw} |
| 16 | for _, protocol := range []int{wire.Protocol24, wire.Protocol25} { |
| 17 | got, err := wire.Encode(f, protocol) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | if strings.Contains(string(got.Data), "placement_notes") != (protocol == wire.Protocol25) { |
| 22 | t.Fatal("wrong field at protocol", protocol, string(got.Data)) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestPlacementNotebookMergeBoundedAndIdempotent(t *testing.T) { |
| 28 | notes := []wire.NotebookEntry{} |
| 29 | for i := 0; i < wire.MaxNoteEntries; i++ { |
| 30 | notes = append(notes, wire.NotebookEntry{ID: string(rune('a' + i)), Text: strings.Repeat("x", 300)}) |
| 31 | } |
| 32 | correction := []wire.NotebookEntry{{ID: "mv1", Source: wire.NoteSystem, Text: "Moved to the calendar"}} |
| 33 | notes = wire.MergePlacementNotes(notes, correction) |
| 34 | notes = wire.MergePlacementNotes(notes, correction) |
| 35 | count := 0 |
| 36 | for _, n := range notes { |
| 37 | if n.ID == "mv1" { |
| 38 | count++ |
| 39 | } |
| 40 | } |
| 41 | if len(notes) != wire.MaxNoteEntries+1 || count != 1 { |
| 42 | t.Fatal("lost bound or repeated note", notes) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestPlacementPersonalEntryIDCannotEvictIt(t *testing.T) { |
| 47 | personal := wire.NotebookEntry{ID: "mv-person", Source: wire.NotePerson, Text: "These are my own words"} |
| 48 | entries := wire.MergePlacementNotes([]wire.NotebookEntry{personal}, nil) |
| 49 | if len(entries) != 1 || entries[0] != personal { |
| 50 | t.Fatal("ID prefix evicted a personal entry", entries) |
| 51 | } |
| 52 | } |
| 53 | |