spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / plugins / security-guidance / tests / test_prompt_cache.py
51 lines · 2.1 KBRaw
1"""The code-review request leads with a static, cacheable rubric block."""
2from test_review_model import api # noqa: F401 fixture
3
4import llm
5
6
7def _review(api, files, **kw):
8 api.posts.clear()
9 llm.analyze_code_security(files, **kw)
10 assert len(api.posts) == 1
11 return api.posts[0]
12
13
14def test_first_content_block_is_the_rubric_with_cache_control(api):
15 post = _review(api, [("app.py", "print('hi')\n")])
16 content = post["messages"][0]["content"]
17 assert isinstance(content, list) and len(content) == 2
18 assert content[0] == {
19 "type": "text", "text": llm._CODE_REVIEW_RUBRIC,
20 "cache_control": {"type": "ephemeral"},
21 }
22 assert "cache_control" not in content[1]
23 assert post["system"] == llm.CLAUDE_CODE_SYSTEM_PROMPT
24
25
26def test_rubric_has_no_unfilled_placeholders():
27 rubric = llm._CODE_REVIEW_RUBRIC
28 for name in ("language", "content_desc", "diff_instruction", "prev_section",
29 "file_type", "files_text"):
30 assert "{" + name + "}" not in rubric
31 # str.format escapes are gone: literal braces reach the model as written.
32 assert "/{tenant_id}/" in rubric and "{{tenant_id}}" not in rubric
33
34
35def test_rubric_block_is_identical_across_different_inputs(api):
36 a = _review(api, [("main.go", "package main\n")])
37 b = _review(api, [("views.py", "+x = 1\n")], is_diff=True,
38 previous_findings=[{"filePath": "views.py", "category": "sqli",
39 "vulnerableCode": "cursor.execute(q)"}])
40 ca, cb = a["messages"][0]["content"], b["messages"][0]["content"]
41 assert ca[0]["text"] == cb[0]["text"]
42 assert ca[1]["text"] != cb[1]["text"]
43 assert "Go code" in ca[1]["text"] and "main.go" in ca[1]["text"]
44 assert "Python diff" in cb[1]["text"] and "unified diff" in cb[1]["text"]
45 assert "PREVIOUS FINDINGS" in cb[1]["text"] and "=== DIFF: views.py ===" in cb[1]["text"]
46
47
48def test_plain_prompt_without_prefix_stays_a_string(api):
49 api.posts.clear()
50 llm._call_claude("review this", {"type": "object"})
51 assert api.posts[0]["messages"][0]["content"] == "review this"