Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyrit/output/score/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PrettyScorePrinter(_PrettyPrinterMixin, PrinterBase):
Provides ``_render_score`` for inline use by other printers (e.g.,
conversation and attack-result printers) and ``render_async`` /
``write_async`` for standalone rendering of a list of scores.
Categories are comma-separated, with embedded newlines shown as escaped text.
"""

def __init__(
Expand Down Expand Up @@ -54,7 +55,8 @@ def _render_score(self, score: Score, indent_level: int = 3) -> str:
indent = self._indent * indent_level
scorer_name = resolve_scorer_name(score, none_value="Unknown")
lines.append(self._format_colored(f"{indent}Scorer: {scorer_name}"))
lines.append(self._format_colored(f"{indent}• Category: {score.score_category or 'N/A'}", Fore.LIGHTMAGENTA_EX))
category_str = ", ".join(score.score_category).replace("\n", r"\n") if score.score_category else "N/A"
lines.append(self._format_colored(f"{indent}• Category: {category_str}", Fore.LIGHTMAGENTA_EX))
lines.append(self._format_colored(f"{indent}• Type: {score.score_type}", Fore.CYAN))

if score.is_undetermined:
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/output/score/test_pretty.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import pytest
from colorama import Fore, Style

from pyrit.models import Score, ScoreStatus
from pyrit.output.score.pretty import PrettyScorePrinter

Expand All @@ -22,3 +25,68 @@ async def test_render_async_supports_undetermined_score() -> None:
output = await printer.render_async([score])

assert "Value: undetermined" in output


@pytest.mark.parametrize(
("categories", "expected"),
[
(["refusal"], "refusal"),
(["refusal", "scams"], "refusal, scams"),
(["scams", "refusal", "scams"], "scams, refusal, scams"),
],
)
async def test_render_async_joins_score_categories(*, categories: list[str], expected: str) -> None:
printer = PrettyScorePrinter(enable_colors=False)
score = Score(score_value="true", score_type="true_false", score_category=categories)

output = await printer.render_async([score])

assert f"Category: {expected}" in output


@pytest.mark.parametrize("enable_colors", [False, True])
@pytest.mark.parametrize(
("categories", "expected"),
[
(["refusal\nscams"], r"refusal\nscams"),
(["refusal\r\nscams"], r"refusal\r\nscams"),
(["refusal\rscams"], r"refusal\rscams"),
(["\nrefusal\n\n"], r"\nrefusal\n\n"),
(["refusal\nscams", "other\ncategory"], r"refusal\nscams, other\ncategory"),
([r"refusal\nscams"], r"refusal\nscams"),
],
)
async def test_write_async_keeps_category_line_breaks_on_one_line(
*,
categories: list[str],
expected: str,
enable_colors: bool,
capsys: pytest.CaptureFixture[str],
) -> None:
printer = PrettyScorePrinter(enable_colors=enable_colors)
score = Score(
score_value="true",
score_type="true_false",
score_category=categories,
score_metadata={"note": "unchanged"},
)
original = score.model_dump(mode="json")

await printer.write_async([score])

lines = capsys.readouterr().out.splitlines()
category_line = f" \u2022 Category: {expected}"
if enable_colors:
category_line = f"{Fore.LIGHTMAGENTA_EX}{category_line}{Style.RESET_ALL}"
assert lines[1] == category_line
assert len(lines) == 4
assert score.model_dump(mode="json") == original


async def test_render_async_renders_missing_category_as_na() -> None:
printer = PrettyScorePrinter(enable_colors=False)
score = Score(score_value="true", score_type="true_false")

output = await printer.render_async([score])

assert "Category: N/A" in output
Loading