package api
import (
"errors"
"strings"
"testing"
"craftdesign.group/quarry/internal/ops"
)
// A room's lines come back RENDERED, like a channel's. team.chat
// returned the raw store message, so the page fell back to escaped
// text and an @mention in a team room was characters where the same
// words in a repository's channel were a link.
func TestTheRoomRendersItsLines(t *testing.T) {
r, _ := mentionRig(t)
if _, err := r.Invoke(asUser("owen"), "team.chat.post",
teamChatPostIn{Name: "grove", Text: "standup in ten @mia"}); err != nil {
t.Fatal(err)
}
out, err := r.Invoke(asUser("mia"), "team.chat", teamChatIn{Name: "grove", Limit: 10})
if err != nil {
t.Fatal(err)
}
got := out.(teamChatOut)
if len(got.Messages) == 0 {
t.Fatal("FAIL: the room is empty")
}
html := string(got.Messages[0].HTML)
if html == "" {
t.Fatalf("FAIL: the line came back unrendered: %+v", got.Messages[0])
}
if !strings.Contains(html, "mia") || strings.Contains(html, "<script") {
t.Errorf("FAIL: rendered as %q", html)
}
}
// The live renderer refuses a repository the room has nothing to do
// with: resolving a reader's text against it would be answering about
// a place the line was never said in.
func TestTheRoomRendererRefusesARepositoryOutsideTheTeam(t *testing.T) {
r, _ := mentionRig(t)
_, err := r.Invoke(asUser("mia"), "team.chat.render",
teamRenderIn{Name: "grove", Repo: "orchard", Text: "hi"})
if !errors.Is(err, ops.ErrNotFound) {
t.Errorf("FAIL: rendered against a repository not under the team: %v", err)
}
// And somebody outside the room may not use it at all.
if _, err := r.Invoke(asUser("outsider"), "team.chat.render",
teamRenderIn{Name: "grove", Text: "hi"}); err == nil {
t.Error("FAIL: an outsider used the room's renderer")
}
}