|
| 1 | +"""Tests for tools/skill_provenance.py — write-origin ContextVar.""" |
| 2 | + |
| 3 | +import contextvars |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +def test_default_origin_is_foreground(): |
| 9 | + from tools.skill_provenance import get_current_write_origin |
| 10 | + # In a fresh ContextVar context, default kicks in. |
| 11 | + ctx = contextvars.copy_context() |
| 12 | + origin = ctx.run(get_current_write_origin) |
| 13 | + assert origin == "foreground" |
| 14 | + |
| 15 | + |
| 16 | +def test_set_and_get_origin(): |
| 17 | + from tools.skill_provenance import ( |
| 18 | + set_current_write_origin, |
| 19 | + reset_current_write_origin, |
| 20 | + get_current_write_origin, |
| 21 | + ) |
| 22 | + token = set_current_write_origin("background_review") |
| 23 | + try: |
| 24 | + assert get_current_write_origin() == "background_review" |
| 25 | + finally: |
| 26 | + reset_current_write_origin(token) |
| 27 | + |
| 28 | + |
| 29 | +def test_reset_restores_prior_origin(): |
| 30 | + from tools.skill_provenance import ( |
| 31 | + set_current_write_origin, |
| 32 | + reset_current_write_origin, |
| 33 | + get_current_write_origin, |
| 34 | + ) |
| 35 | + outer = set_current_write_origin("assistant_tool") |
| 36 | + try: |
| 37 | + inner = set_current_write_origin("background_review") |
| 38 | + try: |
| 39 | + assert get_current_write_origin() == "background_review" |
| 40 | + finally: |
| 41 | + reset_current_write_origin(inner) |
| 42 | + assert get_current_write_origin() == "assistant_tool" |
| 43 | + finally: |
| 44 | + reset_current_write_origin(outer) |
| 45 | + |
| 46 | + |
| 47 | +def test_is_background_review_truthy_only_for_review(): |
| 48 | + from tools.skill_provenance import ( |
| 49 | + set_current_write_origin, |
| 50 | + reset_current_write_origin, |
| 51 | + is_background_review, |
| 52 | + BACKGROUND_REVIEW, |
| 53 | + ) |
| 54 | + for origin, expected in ( |
| 55 | + ("foreground", False), |
| 56 | + ("assistant_tool", False), |
| 57 | + ("random_other_value", False), |
| 58 | + (BACKGROUND_REVIEW, True), |
| 59 | + ): |
| 60 | + token = set_current_write_origin(origin) |
| 61 | + try: |
| 62 | + assert is_background_review() is expected, ( |
| 63 | + f"is_background_review() wrong for origin={origin!r}" |
| 64 | + ) |
| 65 | + finally: |
| 66 | + reset_current_write_origin(token) |
| 67 | + |
| 68 | + |
| 69 | +def test_empty_origin_falls_back_to_foreground(): |
| 70 | + from tools.skill_provenance import ( |
| 71 | + set_current_write_origin, |
| 72 | + reset_current_write_origin, |
| 73 | + get_current_write_origin, |
| 74 | + ) |
| 75 | + token = set_current_write_origin("") |
| 76 | + try: |
| 77 | + # Empty is coerced to "foreground" at the set() boundary. |
| 78 | + assert get_current_write_origin() == "foreground" |
| 79 | + finally: |
| 80 | + reset_current_write_origin(token) |
| 81 | + |
| 82 | + |
| 83 | +def test_context_isolation_between_copies(): |
| 84 | + """ContextVar scoping: modifications in one copy do not leak out.""" |
| 85 | + from tools.skill_provenance import ( |
| 86 | + set_current_write_origin, |
| 87 | + get_current_write_origin, |
| 88 | + BACKGROUND_REVIEW, |
| 89 | + ) |
| 90 | + |
| 91 | + # Start at the module default. |
| 92 | + original = get_current_write_origin() |
| 93 | + |
| 94 | + def _run_in_copy(): |
| 95 | + set_current_write_origin(BACKGROUND_REVIEW) |
| 96 | + return get_current_write_origin() |
| 97 | + |
| 98 | + ctx = contextvars.copy_context() |
| 99 | + inside = ctx.run(_run_in_copy) |
| 100 | + assert inside == BACKGROUND_REVIEW |
| 101 | + # Parent context unaffected. |
| 102 | + assert get_current_write_origin() == original |
0 commit comments