@@ -497,3 +497,88 @@ def __init__(self):
497497 instruction_template , invocation_context
498498 )
499499 assert populated_instruction == "Name: Frank, Role: Engineer"
500+
501+
502+ @pytest .mark .asyncio
503+ async def test_inject_session_state_with_invalid_nested_path_returns_original ():
504+ """Test that invalid nested paths (e.g. containing spaces or equals) are ignored."""
505+ instruction_template = "Value: {LabelConfidence.Enum confidence = 441216274;}"
506+ invocation_context = await _create_test_readonly_context ()
507+
508+ populated_instruction = await instructions_utils .inject_session_state (
509+ instruction_template , invocation_context
510+ )
511+ assert (
512+ populated_instruction
513+ == "Value: {LabelConfidence.Enum confidence = 441216274;}"
514+ )
515+
516+
517+ @pytest .mark .asyncio
518+ async def test_inject_session_state_escaped_braces ():
519+ instruction_template = "This is a literal {{placeholder}} and this is {value}."
520+ invocation_context = await _create_test_readonly_context (
521+ state = {"value" : "real_value" }
522+ )
523+ populated_instruction = await instructions_utils .inject_session_state (
524+ instruction_template , invocation_context
525+ )
526+ assert populated_instruction == "This is a literal {placeholder} and this is real_value."
527+
528+
529+ @pytest .mark .asyncio
530+ async def test_inject_session_state_escaped_braces_missing_state ():
531+ instruction_template = "This is a literal {{task_N.result}}."
532+ # task_N is NOT in state
533+ invocation_context = await _create_test_readonly_context (state = {})
534+ populated_instruction = await instructions_utils .inject_session_state (
535+ instruction_template , invocation_context
536+ )
537+ assert populated_instruction == "This is a literal {task_N.result}."
538+
539+
540+ @pytest .mark .asyncio
541+ async def test_inject_session_state_triple_braces_evaluates_and_wraps ():
542+ instruction_template = "This is a wrapped value: {{{value}}}."
543+ invocation_context = await _create_test_readonly_context (
544+ state = {"value" : "real_value" }
545+ )
546+ populated_instruction = await instructions_utils .inject_session_state (
547+ instruction_template , invocation_context
548+ )
549+ assert populated_instruction == "This is a wrapped value: {real_value}."
550+
551+
552+ @pytest .mark .asyncio
553+ async def test_inject_session_state_quadruple_braces ():
554+ instruction_template = "This is literal double braces: {{{{placeholder}}}}."
555+ invocation_context = await _create_test_readonly_context (state = {})
556+ populated_instruction = await instructions_utils .inject_session_state (
557+ instruction_template , invocation_context
558+ )
559+ assert populated_instruction == "This is literal double braces: {{placeholder}}."
560+
561+
562+ @pytest .mark .asyncio
563+ async def test_inject_session_state_asymmetric_braces_left ():
564+ instruction_template = "Asymmetric left: {{value}."
565+ invocation_context = await _create_test_readonly_context (
566+ state = {"value" : "real_value" }
567+ )
568+ populated_instruction = await instructions_utils .inject_session_state (
569+ instruction_template , invocation_context
570+ )
571+ assert populated_instruction == "Asymmetric left: real_value."
572+
573+
574+ @pytest .mark .asyncio
575+ async def test_inject_session_state_asymmetric_braces_right ():
576+ instruction_template = "Asymmetric right: {value}}."
577+ invocation_context = await _create_test_readonly_context (
578+ state = {"value" : "real_value" }
579+ )
580+ populated_instruction = await instructions_utils .inject_session_state (
581+ instruction_template , invocation_context
582+ )
583+ assert populated_instruction == "Asymmetric right: real_value."
584+
0 commit comments