|
| 1 | +"""Unit tests for writing code into an empty/new job (issue #539). |
| 2 | +
|
| 3 | +Before the fix, the assistant silently dropped generated code when the job body |
| 4 | +was empty: `generate()` only applied edits `if job_code:`, and |
| 5 | +`parse_and_apply_edits()` short-circuited on `not original_code`. The user saw a |
| 6 | +preamble ("I'll add a fn()...") with no code. |
| 7 | +
|
| 8 | +These exercise the pure edit-application methods. We build the instance with |
| 9 | +`__new__` to skip `__init__` (which constructs an Anthropic client, blocked in |
| 10 | +unit tests); the `rewrite` path never touches the LLM client. |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | + |
| 15 | +from job_chat.job_chat import AnthropicClient |
| 16 | + |
| 17 | + |
| 18 | +def _client(): |
| 19 | + return AnthropicClient.__new__(AnthropicClient) |
| 20 | + |
| 21 | + |
| 22 | +def test_rewrite_into_empty_job_returns_full_code(): |
| 23 | + suggested, diff = _client().apply_code_edits( |
| 24 | + content="write a simple http post", |
| 25 | + text_answer="I'll add it", |
| 26 | + original_code="", |
| 27 | + code_edits=[{"action": "rewrite", "new_code": "post('https://x.test', state.data);"}], |
| 28 | + ) |
| 29 | + |
| 30 | + assert suggested == "post('https://x.test', state.data);" |
| 31 | + assert diff["patches_applied"] == 1 |
| 32 | + |
| 33 | + |
| 34 | +def test_replace_action_into_empty_job_writes_code(): |
| 35 | + # The model may send a "replace" against an empty job. With nothing to find, |
| 36 | + # the new code is still the result, deterministically and without an LLM |
| 37 | + # error-correction round-trip. |
| 38 | + suggested, diff = _client().apply_code_edits( |
| 39 | + content="write a simple http post", |
| 40 | + text_answer="I'll add it", |
| 41 | + original_code="", |
| 42 | + code_edits=[ |
| 43 | + {"action": "replace", "old_code": "// anything", "new_code": "post('https://x.test');"} |
| 44 | + ], |
| 45 | + ) |
| 46 | + |
| 47 | + assert suggested == "post('https://x.test');" |
| 48 | + assert diff["patches_applied"] == 1 |
| 49 | + |
| 50 | + |
| 51 | +def test_whitespace_only_job_treated_as_empty(): |
| 52 | + suggested, diff = _client().apply_code_edits( |
| 53 | + content="write a simple http post", |
| 54 | + text_answer="I'll add it", |
| 55 | + original_code=" \n\n ", |
| 56 | + code_edits=[{"action": "rewrite", "new_code": "post('https://x.test');"}], |
| 57 | + ) |
| 58 | + |
| 59 | + assert suggested == "post('https://x.test');" |
| 60 | + assert diff["patches_applied"] == 1 |
| 61 | + |
| 62 | + |
| 63 | +def test_parse_and_apply_no_longer_drops_code_on_empty_original(): |
| 64 | + response = json.dumps( |
| 65 | + { |
| 66 | + "text_answer": "I'll write it", |
| 67 | + "code_edits": [{"action": "rewrite", "new_code": "get('https://x.test');"}], |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + text, suggested, diff = _client().parse_and_apply_edits( |
| 72 | + response, content="x", original_code="" |
| 73 | + ) |
| 74 | + |
| 75 | + assert suggested == "get('https://x.test');" |
| 76 | + assert diff["patches_applied"] == 1 |
| 77 | + |
| 78 | + |
| 79 | +def test_no_code_edits_still_returns_none(): |
| 80 | + response = json.dumps({"text_answer": "just explaining", "code_edits": []}) |
| 81 | + |
| 82 | + text, suggested, diff = _client().parse_and_apply_edits( |
| 83 | + response, content="x", original_code="" |
| 84 | + ) |
| 85 | + |
| 86 | + assert suggested is None |
| 87 | + assert diff is None |
0 commit comments