| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestChatFeed(t *testing.T) { |
| 11 | t.Run("order", func(t *testing.T) { |
| 12 | p := Page{Messages: []MessageView{{Message: Message{ID: "first", Sequence: 1}}, {Message: Message{ID: "reply", Sequence: 3}}}, Cards: []CardView{{Card: Card{ID: "plan", Sequence: 1}}, {Card: Card{ID: "pending"}}}} |
| 13 | p.BuildFeed() |
| 14 | if len(p.Feed) != 4 || p.Feed[0].Message == nil || p.Feed[1].Card == nil || p.Feed[1].Card.ID != "plan" || p.Feed[2].Message == nil || p.Feed[3].Card.ID != "pending" { |
| 15 | t.Fatalf("cards are detached from messages: %+v", p.Feed) |
| 16 | } |
| 17 | }) |
| 18 | t.Run("identity", func(t *testing.T) { |
| 19 | p := Page{Messages: []MessageView{{Message: Message{AuthorKind: "person", AuthorID: "me"}}, {Message: Message{AuthorKind: "person", AuthorID: "teammate"}}, {Message: Message{AuthorKind: "specialist", AuthorID: "custom"}}}} |
| 20 | err := p.PrepareChat("me", []library.Specialist{{Key: "custom", Name: "Accessibility", Icon: "pencil"}}, nil, func(s string) string { return s }) |
| 21 | if err != nil || !p.Messages[0].Own || p.Messages[1].Own || p.Messages[2].AuthorName != "Accessibility" || p.Messages[2].AuthorIcon != "pencil" { |
| 22 | t.Fatalf("identity lost: %+v, %v", p.Messages, err) |
| 23 | } |
| 24 | }) |
| 25 | t.Run("thread questions", func(t *testing.T) { |
| 26 | p := Page{ThreadID: "task"} |
| 27 | err := p.PrepareChat("me", nil, func(id string) (planning.TaskViewData, error) { |
| 28 | if id != "task" { |
| 29 | t.Fatal(id) |
| 30 | } |
| 31 | return planning.TaskViewData{Questions: []planning.TaskQuestion{{ID: "pending", State: "pending", CanAnswer: false}, {ID: "closed", State: "answered"}}}, nil |
| 32 | }, nil) |
| 33 | if err != nil || len(p.Feed) != 2 || p.Feed[0].Card.Work == nil || p.Feed[1].Card.NativeQuestion.ID != "pending" || p.Feed[1].Card.NativeQuestion.CanAnswer { |
| 34 | t.Fatalf("thread cards or authority changed: %+v, %v", p.Feed, err) |
| 35 | } |
| 36 | }) |
| 37 | t.Run("lookup fails closed", func(t *testing.T) { |
| 38 | p := Page{ThreadID: "removed"} |
| 39 | denied := errors.New("denied") |
| 40 | err := p.PrepareChat("me", nil, func(string) (planning.TaskViewData, error) { return planning.TaskViewData{}, denied }, nil) |
| 41 | if !errors.Is(err, denied) { |
| 42 | t.Fatal("task lookup failure hidden", err) |
| 43 | } |
| 44 | }) |
| 45 | } |
| 46 | |