[W9-4] Add code-snippet viewer for revision findings (#532)

Backend: GET /git/file reads a file at a branch tip (read_file_at_branch)
and slices it to a line window — explicit start/end, a line+context center,
or the whole file capped at 2000 lines. _compute_file_range is the pure
helper (unit-tested).

Frontend: useGitFile hook + CodeSnippet (styled <pre>, line numbers, active-
line highlight — matches git-diff-viewer, no shiki). Wired into FindingCard
so each file:line finding shows the surrounding source. Fail-open: a missing
file renders a muted hint, never breaks the card.

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
This commit is contained in:
Renzo F
2026-07-15 04:34:12 +02:00
committed by GitHub
co-authored by Renn F
parent 1054538d2f
commit f07e2420a8
10 changed files with 427 additions and 2 deletions
+54
View File
@@ -0,0 +1,54 @@
"""Unit tests for the /git/file range computation (roboco.api.routes.git).
Pure logic — no DB, no git. Covers the line/context windowing, explicit
range, whole-file cap, and truncation flag.
"""
from __future__ import annotations
from roboco.api.routes.git import _FILE_MAX_LINES, _compute_file_range
class TestComputeFileRange:
def test_line_centers_context_window(self) -> None:
s, e_, trunc = _compute_file_range(
total=100, line=50, context=10, start=None, end=None
)
assert (s, e_, trunc) == (40, 60, True)
def test_line_window_clamps_to_file_start(self) -> None:
s, e_, trunc = _compute_file_range(
total=100, line=3, context=10, start=None, end=None
)
assert (s, e_, trunc) == (1, 13, True)
def test_line_window_clamps_to_file_end(self) -> None:
s, e_, trunc = _compute_file_range(
total=100, line=98, context=10, start=None, end=None
)
assert (s, e_, trunc) == (88, 100, False)
def test_explicit_start_end_override_line(self) -> None:
s, e_, trunc = _compute_file_range(
total=100, line=50, context=10, start=5, end=8
)
assert (s, e_, trunc) == (5, 8, True)
def test_whole_file_when_no_range_args(self) -> None:
s, e_, trunc = _compute_file_range(
total=50, line=None, context=10, start=None, end=None
)
assert (s, e_, trunc) == (1, 50, False)
def test_whole_file_capped_when_huge(self) -> None:
total = _FILE_MAX_LINES + 500
s, e_, trunc = _compute_file_range(
total=total, line=None, context=10, start=None, end=None
)
assert (s, e_, trunc) == (1, _FILE_MAX_LINES, True)
def test_empty_file(self) -> None:
s, e_, trunc = _compute_file_range(
total=0, line=None, context=10, start=None, end=None
)
assert (s, e_, trunc) == (1, 1, False)