package wire_test import ( "amadan.net/oficina/equipo/internal/wire" "encoding/json" "strings" "testing" ) func TestPlacementProtocol(t *testing.T) { if wire.ProtocolPlacement != 25 || wire.ProtocolCurrent < wire.ProtocolPlacement { t.Fatal("placement protocol") } raw, _ := json.Marshal(wire.MessageCommand{Person: "p", TurnID: "t", PlacementNotes: []wire.NotebookEntry{{ID: "mv1", Source: wire.NoteSystem, Text: "A moved to B"}}}) f := wire.Frame{ID: "c", Type: wire.FrameMessage, Data: raw} for _, protocol := range []int{wire.Protocol24, wire.Protocol25} { got, err := wire.Encode(f, protocol) if err != nil { t.Fatal(err) } if strings.Contains(string(got.Data), "placement_notes") != (protocol == wire.Protocol25) { t.Fatal("wrong field at protocol", protocol, string(got.Data)) } } } func TestPlacementNotebookMergeBoundedAndIdempotent(t *testing.T) { notes := []wire.NotebookEntry{} for i := 0; i < wire.MaxNoteEntries; i++ { notes = append(notes, wire.NotebookEntry{ID: string(rune('a' + i)), Text: strings.Repeat("x", 300)}) } correction := []wire.NotebookEntry{{ID: "mv1", Source: wire.NoteSystem, Text: "Moved to the calendar"}} notes = wire.MergePlacementNotes(notes, correction) notes = wire.MergePlacementNotes(notes, correction) count := 0 for _, n := range notes { if n.ID == "mv1" { count++ } } if len(notes) != wire.MaxNoteEntries+1 || count != 1 { t.Fatal("lost bound or repeated note", notes) } } func TestPlacementPersonalEntryIDCannotEvictIt(t *testing.T) { personal := wire.NotebookEntry{ID: "mv-person", Source: wire.NotePerson, Text: "These are my own words"} entries := wire.MergePlacementNotes([]wire.NotebookEntry{personal}, nil) if len(entries) != 1 || entries[0] != personal { t.Fatal("ID prefix evicted a personal entry", entries) } }