Skip to content

Commit

Permalink
fix mod timeline tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Feb 7, 2025
1 parent adbc776 commit 2cef20a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
17 changes: 10 additions & 7 deletions modules/api/src/main/ModTimeline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import lila.playban.{ TempBan, PlaybanApi }
import lila.shutup.{ PublicLine, ShutupApi }
import lila.core.perm.{ Permission, Granter }
import lila.core.userId.ModId
import lila.core.id.ReportId

case class ModTimeline(
user: User,
Expand Down Expand Up @@ -35,9 +36,9 @@ object ModTimeline:

case class ReportNewAtom(report: Report, atoms: NonEmptyList[Report.Atom]):
def like(r: Report): Boolean = report.room == r.room
case class ReportLineFlag(report: Report, line: PublicLine):
def like(r: Report): Boolean = report.id == r.id
def merge(o: ReportLineFlag) = PublicLine.merge(line, o.line).map(l => copy(line = l))
case class ReportLineFlag(openId: Option[ReportId], line: PublicLine):
def merge(o: ReportLineFlag) = (openId == o.openId).so:
PublicLine.merge(line, o.line).map(l => copy(line = l))
case class PlayBans(list: NonEmptyList[TempBan])
case class AccountCreation(at: Instant)

Expand All @@ -51,8 +52,8 @@ object ModTimeline:
case (head :: rest, n) => mergeOne(head, n).fold(head :: mergeMany(rest, n))(_ :: rest)

private def mergeOne(prev: Event, next: Event): Option[Event] = (prev, next) match
case (p: ReportLineFlag, n: ReportLineFlag) if n.like(p.report) => p.merge(n)
case (p: PlayBans, n: PlayBans) => PlayBans(n.list ::: p.list).some
case (p: ReportLineFlag, n: ReportLineFlag) => p.merge(n)
case (p: PlayBans, n: PlayBans) => PlayBans(n.list ::: p.list).some
case (p: AppealMsg, n: AppealMsg) if p.by.is(n.by) => p.copy(text = s"${n.text}\n\n${p.text}").some
case (p: ReportNewAtom, n: ReportNewAtom) if n.like(p.report) => p.copy(atoms = n.atoms ::: p.atoms).some
case (p: Modlog, n: Modlog) => mergeModlog(p, n)
Expand All @@ -71,7 +72,9 @@ object ModTimeline:
atoms.head.parseFlag match
case None => List(ReportNewAtom(report, atoms))
case Some(flag) =>
flag.quotes.map(text => ReportLineFlag(report, PublicLine(text, flag.source, atoms.head.at)))
flag.quotes.map(text =>
ReportLineFlag(report.open.option(report.id), PublicLine(text, flag.source, atoms.head.at))
)

extension (e: Event)
def key: String = e match
Expand Down Expand Up @@ -132,7 +135,7 @@ object ModTimeline:
case Comm => !l.details.has(lila.playban.PlaybanFeedback.sittingAutoPreset.name)
case _ => true
case r: ReportNewAtom if r.report.is(_.Comm) => angle != Angle.Play
case r: ReportLineFlag => r.report.open || angle == Angle.Comm
case r: ReportLineFlag => r.openId.isDefined || angle == Angle.Comm
case _ => true

final class ModTimelineApi(
Expand Down
12 changes: 6 additions & 6 deletions modules/api/src/main/ui/ModTimelineUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ final class ModTimelineUi(helpers: Helpers)(
frag(pluralize("Playban", e.list.size), ": ", e.list.map(_.mins).toList.mkString(", "), " minutes")

private def renderReportLineFlag(r: ReportLineFlag)(using Translate) = frag(
if r.report.open
then
postForm(action := s"${routes.Report.inquiry(r.report.id.value)}?onlyOpen=1")(
cls := "mod-timeline__report-form mod-timeline__report-form--open"
)(submitButton("Chat flag")(cls := "button button-thin", title := "Open"))
else "Chat flag",
r.openId match
case Some(reportId) =>
postForm(action := s"${routes.Report.inquiry(reportId.value)}?onlyOpen=1")(
cls := "mod-timeline__report-form mod-timeline__report-form--open"
)(submitButton("Chat flag")(cls := "button button-thin", title := "Open"))
case _ => "Chat flag",
publicLineSource(r.line.from),
div(cls := "mod-timeline__texts")(
fragList(
Expand Down
55 changes: 33 additions & 22 deletions modules/api/src/test/ModTimelineTest.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lila.api

import monocle.syntax.all.*

import lila.shutup.PublicLine
import lila.core.shutup.PublicSource as Source
import lila.playban.TempBan
Expand All @@ -11,36 +13,37 @@ class ModTimelineTest extends munit.FunSuite:

val now = nowInstant

def line(text: String, source: String) = PublicLine(text, Source.longNotation.read(source).get, now)
val l1 = line("linguine", "simul/aaa")
val l2 = line("fusilli", "simul/aaa")
val l3 = line("linguine", "relay/bbb")
val l4 = line("bucatini", "study/ccc")
val l5 = line("rigatoni", "study/ccc")
val ban1 = TempBan(now.minusDays(10), 5)
val ban2 = TempBan(now.minusDays(5), 10)
val ban3 = TempBan(now.minusDays(1), 15)
def bans(bs: TempBan*) = PlayBans(NonEmptyList.fromListUnsafe(bs.toList))
given Conversion[TempBan, Event] = bans(_)
def line(text: String, source: String) =
ReportLineFlag(None, PublicLine(text, Source.longNotation.read(source).get, now))
val l1 = line("linguine", "simul/aaa")
val l2 = line("fusilli", "simul/aaa")
val l3 = line("linguine", "relay/bbb")
val l4 = line("bucatini", "study/ccc")
val l5 = line("rigatoni", "study/ccc")
val ban1 = TempBan(now.minusDays(10), 5)
val ban2 = TempBan(now.minusDays(5), 10)
val ban3 = TempBan(now.minusDays(1), 15)
def bans(bs: TempBan*) = PlayBans(NonEmptyList.fromListUnsafe(bs.toList))
given Conversion[TempBan, Event] = bans(_)

test("merge empty"):
assertEquals(aggregateEvents(Nil), Nil)

test("public line merge"):
assertEquals(PublicLine.merge(l1, l1), l1.some)
assertEquals(PublicLine.merge(l1, l2), l1.copy(text = s"linguine${sep}fusilli").some)
assertEquals(PublicLine.merge(l2, l3), none)
assertEquals(l1.merge(l1), l1.some)
assertEquals(l1.merge(l2), l1.focus(_.line.text).replace(s"linguine${sep}fusilli").some)
assertEquals(l2.merge(l3), none)

test("merge same line"):
assertEquals(aggregateEvents(List(l1)), List(l1))
assertEquals(aggregateEvents(List(l1, l1)), List(l1))

test("merge same source lines"):
assertEquals(aggregateEvents(List(l1, l2)), List(l1.copy(text = s"linguine${sep}fusilli")))
assertEquals(aggregateEvents(List(l2, l1)), List(l1.copy(text = s"fusilli${sep}linguine")))
assertEquals(aggregateEvents(List(l1, l2)), List(l1.focus(_.line.text).replace(s"linguine${sep}fusilli")))
assertEquals(aggregateEvents(List(l2, l1)), List(l1.focus(_.line.text).replace(s"fusilli${sep}linguine")))
assertEquals(
aggregateEvents(List(l1, l2, l1)),
List(l1.copy(text = s"linguine${sep}fusilli"))
List(l1.focus(_.line.text).replace(s"linguine${sep}fusilli"))
)

test("merge different source lines"):
Expand All @@ -50,11 +53,15 @@ class ModTimelineTest extends munit.FunSuite:
test("merge consecutive lines"):
assertEquals(
aggregateEvents(List(l4, l1, l1, l2, l3)),
List(l4, l1.copy(text = s"linguine${sep}fusilli"), l3)
List(l4, l1.focus(_.line.text).replace(s"linguine${sep}fusilli"), l3)
)
assertEquals(
aggregateEvents(List(l4, l5, l1, l2, l1, l3)),
List(l4.copy(text = s"bucatini${sep}rigatoni"), l1.copy(text = s"linguine${sep}fusilli"), l3)
List(
l4.focus(_.line.text).replace(s"bucatini${sep}rigatoni"),
l1.focus(_.line.text).replace(s"linguine${sep}fusilli"),
l3
)
)

test("merge mixed lines"):
Expand All @@ -64,7 +71,11 @@ class ModTimelineTest extends munit.FunSuite:
)
assertEquals(
aggregateEvents(List(l1, l4, l2, l3, l5)),
List(l1.copy(text = s"linguine${sep}fusilli"), l4.copy(text = s"bucatini${sep}rigatoni"), l3)
List(
l1.focus(_.line.text).replace(s"linguine${sep}fusilli"),
l4.focus(_.line.text).replace(s"bucatini${sep}rigatoni"),
l3
)
)

test("merge mixed events"):
Expand All @@ -91,9 +102,9 @@ class ModTimelineTest extends munit.FunSuite:
assertEquals(
aggregateEvents(List(l1, ban1, l4, ban2, l2, l3, l5, ban3, ban3)),
List[Event](
l1.copy(text = s"linguine${sep}fusilli"),
l1.focus(_.line.text).replace(s"linguine${sep}fusilli"),
bans(ban3, ban3, ban2, ban1),
l4.copy(text = s"bucatini${sep}rigatoni"),
l4.focus(_.line.text).replace(s"bucatini${sep}rigatoni"),
l3
)
)

0 comments on commit 2cef20a

Please sign in to comment.