|
| 1 | +"""Unit tests for page-navigation prefix handling. |
| 2 | +
|
| 3 | +`extract_page_prefix_from_last_turn` reads the `[pg:...]` tag off the previous |
| 4 | +user turn so job_chat can detect navigation between turns. It pairs with |
| 5 | +`add_page_prefix`, which writes that tag when building history. These lock in the |
| 6 | +round-trip and the guard against non-string history content (e.g. a stale |
| 7 | +planner-format turn whose content is a content-block list). |
| 8 | +""" |
| 9 | + |
| 10 | +from job_chat.job_chat import extract_page_prefix_from_last_turn |
| 11 | +from util import add_page_prefix |
| 12 | + |
| 13 | +PAGE = {"type": "job_code", "name": "Transform", "adaptor": "http@6.5.4"} |
| 14 | + |
| 15 | + |
| 16 | +def _turns(user_content): |
| 17 | + return [{"role": "user", "content": user_content}, {"role": "assistant", "content": "ok"}] |
| 18 | + |
| 19 | + |
| 20 | +def test_extracts_prefix_from_previous_user_turn(): |
| 21 | + history = _turns("[pg:job_code/Transform/http@6.5.4] tweak this") |
| 22 | + assert extract_page_prefix_from_last_turn(history) == "[pg:job_code/Transform/http@6.5.4]" |
| 23 | + |
| 24 | + |
| 25 | +def test_returns_none_when_fewer_than_two_turns(): |
| 26 | + assert extract_page_prefix_from_last_turn([]) is None |
| 27 | + assert extract_page_prefix_from_last_turn([{"role": "user", "content": "[pg:x] hi"}]) is None |
| 28 | + |
| 29 | + |
| 30 | +def test_returns_none_when_no_prefix(): |
| 31 | + assert extract_page_prefix_from_last_turn(_turns("just a message")) is None |
| 32 | + |
| 33 | + |
| 34 | +def test_returns_none_when_content_not_string(): |
| 35 | + history = _turns([{"type": "tool_result", "content": "x"}]) |
| 36 | + assert extract_page_prefix_from_last_turn(history) is None |
| 37 | + |
| 38 | + |
| 39 | +def test_roundtrip_with_add_page_prefix(): |
| 40 | + history = _turns(add_page_prefix("tweak this", PAGE)) |
| 41 | + expected = add_page_prefix("", PAGE).strip() |
| 42 | + assert extract_page_prefix_from_last_turn(history) == expected |
0 commit comments