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
16 changes: 7 additions & 9 deletions dokimi/tests/test_context_prefilter_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

from __future__ import annotations

from functools import partial

import pytest

from dokimi.context import prefilter
from dokimi.context.prefilter import _without_heredoc_bodies
from tests.growth_timing import GROWTH_LIMIT, growth
from tests.test_context_prefilter import CI, _prompt, _report, _row, _session, _tool
from tests.work_count import WORK_LIMIT, count_calls, work_ratio


def test_a_stop_word_only_rule_with_an_unmatchable_key_is_a_claim_that_is_not_measurable():
Expand Down Expand Up @@ -127,19 +126,18 @@ def test_a_quote_that_closes_on_a_later_line_hides_the_heredoc_sign_inside_it():
assert _row(_report([session]), CI)["acted"] == 1


def test_the_heredoc_scan_takes_linear_time_in_the_lines_with_a_heredoc_sign():
def test_the_heredoc_scan_takes_linear_time_in_the_lines_with_a_heredoc_sign(monkeypatch):
"""The characters `_heredoc_words` reads (a line and the lines before it that a backslash continues) grow with the command: a re-scan per line was quadratic (counted, #1454)."""
def command(k):
return "\n".join([": <<< x"] * k + ["cat <<EOF", "body", "EOF", "make ci-local"])

ratio, kept = growth(lambda k: partial(_without_heredoc_bodies, command(k)), 375) # a re-scan per line was quadratic
assert ratio < GROWTH_LIMIT
assert kept.endswith("cat <<EOF\nmake ci-local")
read, kept = count_calls(monkeypatch, prefilter, "_heredoc_words", lambda line, quote, scopes="", prior="": len(line) + len(prior)), []
assert work_ratio(read, lambda k: kept.append(_without_heredoc_bodies(command(k))), 375) < WORK_LIMIT
assert kept[-1].endswith("cat <<EOF\nmake ci-local")


def test_the_heredoc_scan_masks_each_line_once(monkeypatch):
"""The characters masked grow with the command: no line is masked again for the quote state (#1061)."""
from dokimi.context import prefilter
from tests.work_count import WORK_LIMIT, count_calls, work_ratio
masked = count_calls(monkeypatch, prefilter, "_masked_line")
command = lambda k: "\n".join([": <<< x"] * k + ["cat <<EOF", "body", "EOF", "make ci-local"])
assert work_ratio(masked, lambda k: _without_heredoc_bodies(command(k)), 375) <= WORK_LIMIT
20 changes: 10 additions & 10 deletions dokimi/tests/test_repo_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
from dokimi.artifacts import ENVELOPE_KEYS, validate_artifact
from dokimi.cli import build_parser
from dokimi.repos.environment import files as F
from dokimi.repos.environment import inspect_repo
from dokimi.repos.environment import inspect_repo, risks as R
from dokimi.repos.environment.instructions import detect_instructions
from dokimi.repos.source import GitTree, WorkTree
from tests.growth_timing import GROWTH_LIMIT, growth
from tests.work_count import WORK_LIMIT, count_calls, count_pattern, work_ratio

SETTINGS = {"hooks": {
"SessionStart": [{"hooks": [{"type": "command", "command": "npm ci && npm run build"}]}],
Expand Down Expand Up @@ -444,11 +445,11 @@ def test_deeply_nested_yaml_does_not_crash(tmp_path):
validate_artifact("repo_environment", {"schema_version": "repo_environment.v1", **payload})


def test_npm_hook_of_many_options_is_linear(tmp_path):
"""A hook command of many options, 6 KB at n and 100 KB at 16n, is parsed in linear time."""
ratio, payload = growth(_inspected(tmp_path, lambda k: _hook_files("npm " + "-npm " * k)), 1_250)
assert ratio < GROWTH_LIMIT
assert [c["note"] for c in payload["capabilities"]] == ["hooks on SessionStart"]
def test_npm_hook_of_many_options_is_linear(tmp_path, monkeypatch):
"""A hook command of many options, 6 KB at n and 100 KB at 16n, is parsed in linear work: `_subcommand` is handed 16x the words (counted, #1454)."""
handed, got = count_calls(monkeypatch, R, "_subcommand", lambda manager, rest: len(rest)), []
assert work_ratio(handed, lambda k: got.append(_inspected(tmp_path, lambda n: _hook_files("npm " + "-npm " * n))(k)()), 1_250) < WORK_LIMIT
assert [c["note"] for c in got[-1]["capabilities"]] == ["hooks on SessionStart"]


def test_hook_npm_run_detected(tmp_path):
Expand Down Expand Up @@ -1061,11 +1062,10 @@ def test_heredoc_and_here_string_parsing(tmp_path, command, flagged):
assert bool(inspect_repo(WorkTree(tmp_path))["hook_risks"]) is flagged


def test_unclosed_heredocs_are_linear(tmp_path):
"""Many heredoc starts with no terminator, and many labels, must not scan the text per start."""
from dokimi.repos.environment.risks import _segments
def test_unclosed_heredocs_are_linear(tmp_path, monkeypatch):
"""Many heredoc starts with no terminator, and many labels, must not scan the text per start (counted, #1454)."""
hostile = lambda k: "bash <<A\n" + "<<A\n" * k + "".join(f"<<L{i}\n" for i in range(k))
assert growth(lambda k: partial(_segments, hostile(k)), 2_500)[0] < GROWTH_LIMIT
assert work_ratio(count_pattern(monkeypatch, R, "_HEREDOC_START"), lambda k: R._segments(hostile(k)), 625) < WORK_LIMIT


def test_redirection_keeps_its_fd_digit():
Expand Down
Loading