diff options
| author | Claude <noreply@anthropic.com> | 2026-11-06 00:54:48 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-06 00:54:48 +0000 |
| commit | 7aba3deae3ff17d032c37e07d2929d1426b34278 (patch) | |
| tree | 9c482f9acda1599bd2f9bed95a6a46b75e634e80 /plugins/security-guidance/tests/test_prompt_cache.py | |
| parent | 5472f5f56e0070feeeb464318b316714fe3e9105 (diff) | |
| parent | b8331dd69ab3e132bbf8cd6e89b5d8c1747e21b1 (diff) | |
Merge remote-tracking branch 'origin/main' into add-plugin/xcodeadd-plugin/xcode
Diffstat (limited to 'plugins/security-guidance/tests/test_prompt_cache.py')
| -rw-r--r-- | plugins/security-guidance/tests/test_prompt_cache.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/plugins/security-guidance/tests/test_prompt_cache.py b/plugins/security-guidance/tests/test_prompt_cache.py new file mode 100644 index 00000000..04d60ead --- /dev/null +++ b/plugins/security-guidance/tests/test_prompt_cache.py @@ -0,0 +1,51 @@ +"""The code-review request leads with a static, cacheable rubric block.""" +from test_review_model import api # noqa: F401 fixture + +import llm + + +def _review(api, files, **kw): + api.posts.clear() + llm.analyze_code_security(files, **kw) + assert len(api.posts) == 1 + return api.posts[0] + + +def test_first_content_block_is_the_rubric_with_cache_control(api): + post = _review(api, [("app.py", "print('hi')\n")]) + content = post["messages"][0]["content"] + assert isinstance(content, list) and len(content) == 2 + assert content[0] == { + "type": "text", "text": llm._CODE_REVIEW_RUBRIC, + "cache_control": {"type": "ephemeral"}, + } + assert "cache_control" not in content[1] + assert post["system"] == llm.CLAUDE_CODE_SYSTEM_PROMPT + + +def test_rubric_has_no_unfilled_placeholders(): + rubric = llm._CODE_REVIEW_RUBRIC + for name in ("language", "content_desc", "diff_instruction", "prev_section", + "file_type", "files_text"): + assert "{" + name + "}" not in rubric + # str.format escapes are gone: literal braces reach the model as written. + assert "/{tenant_id}/" in rubric and "{{tenant_id}}" not in rubric + + +def test_rubric_block_is_identical_across_different_inputs(api): + a = _review(api, [("main.go", "package main\n")]) + b = _review(api, [("views.py", "+x = 1\n")], is_diff=True, + previous_findings=[{"filePath": "views.py", "category": "sqli", + "vulnerableCode": "cursor.execute(q)"}]) + ca, cb = a["messages"][0]["content"], b["messages"][0]["content"] + assert ca[0]["text"] == cb[0]["text"] + assert ca[1]["text"] != cb[1]["text"] + assert "Go code" in ca[1]["text"] and "main.go" in ca[1]["text"] + assert "Python diff" in cb[1]["text"] and "unified diff" in cb[1]["text"] + assert "PREVIOUS FINDINGS" in cb[1]["text"] and "=== DIFF: views.py ===" in cb[1]["text"] + + +def test_plain_prompt_without_prefix_stays_a_string(api): + api.posts.clear() + llm._call_claude("review this", {"type": "object"}) + assert api.posts[0]["messages"][0]["content"] == "review this" |
