@@ -329,6 +329,7 @@ def test_complete_file_inventory_sh(self, tmp_path):
329329 ".github/agents/speckit.plan.agent.md" ,
330330 ".github/agents/speckit.specify.agent.md" ,
331331 ".github/agents/speckit.tasks.agent.md" ,
332+ ".github/hooks/speckit.json" ,
332333 ".github/prompts/speckit.analyze.prompt.md" ,
333334 ".github/prompts/speckit.checklist.prompt.md" ,
334335 ".github/prompts/speckit.clarify.prompt.md" ,
@@ -339,6 +340,7 @@ def test_complete_file_inventory_sh(self, tmp_path):
339340 ".github/prompts/speckit.specify.prompt.md" ,
340341 ".github/prompts/speckit.tasks.prompt.md" ,
341342 ".vscode/settings.json" ,
343+ ".specify/events.py" ,
342344 ".specify/integration.json" ,
343345 ".specify/init-options.json" ,
344346 ".specify/integrations/copilot.manifest.json" ,
@@ -393,6 +395,7 @@ def test_complete_file_inventory_ps(self, tmp_path):
393395 ".github/agents/speckit.plan.agent.md" ,
394396 ".github/agents/speckit.specify.agent.md" ,
395397 ".github/agents/speckit.tasks.agent.md" ,
398+ ".github/hooks/speckit.json" ,
396399 ".github/prompts/speckit.analyze.prompt.md" ,
397400 ".github/prompts/speckit.checklist.prompt.md" ,
398401 ".github/prompts/speckit.clarify.prompt.md" ,
@@ -403,6 +406,7 @@ def test_complete_file_inventory_ps(self, tmp_path):
403406 ".github/prompts/speckit.specify.prompt.md" ,
404407 ".github/prompts/speckit.tasks.prompt.md" ,
405408 ".vscode/settings.json" ,
409+ ".specify/events.py" ,
406410 ".specify/integration.json" ,
407411 ".specify/init-options.json" ,
408412 ".specify/integrations/copilot.manifest.json" ,
@@ -913,6 +917,8 @@ def test_complete_file_inventory_skills_sh(self, tmp_path):
913917 assert result .exit_code == 0 , f"init failed: { result .output } "
914918 actual = sorted (p .relative_to (project ).as_posix () for p in project .rglob ("*" ) if p .is_file () and ".git" not in p .parts )
915919 expected = sorted ([
920+ ".github/hooks/speckit.json" ,
921+ ".specify/events.py" ,
916922 # Skill files (core commands)
917923 * [f".github/skills/speckit-{ cmd } /SKILL.md" for cmd in self ._SKILL_COMMANDS ],
918924 # Integration metadata
@@ -1674,3 +1680,119 @@ def run(args, **kwargs):
16741680 tmp_path / ".specify" / "workflows" / "runs" / state .run_id / "log.jsonl"
16751681 ).read_text (encoding = "utf-8" )
16761682 assert _SECRET_OUTPUT not in log
1683+
1684+
1685+ @pytest .fixture
1686+ def context_delivery_install (tmp_path ):
1687+ from specify_cli .events import install_integration_events , resolve_events
1688+
1689+ copilot = get_integration ("copilot" )
1690+ state = tmp_path / ".specify/integration.json"
1691+ state .parent .mkdir ()
1692+ state .write_text (json .dumps ({
1693+ "default_integration" : "copilot" , "installed_integrations" : ["copilot" ]
1694+ }), encoding = "utf-8" )
1695+ manifest = IntegrationManifest ("copilot" , tmp_path , version = "test" )
1696+ install_integration_events (copilot , tmp_path , manifest , resolve_events ("copilot" , copilot .config , tmp_path , None ))
1697+ return copilot
1698+
1699+
1700+ def test_copilot_context_delivery_install (tmp_path , context_delivery_install ):
1701+ from specify_cli .events import ADDITIONAL_CONTEXT_HANDLER , context_delivery_error
1702+
1703+ copilot = context_delivery_install
1704+ native = json .loads ((tmp_path / copilot .events_config_file ).read_text (encoding = "utf-8" ))
1705+ entry = native ["hooks" ]["sessionStart" ][0 ]
1706+ assert entry ["__speckit_event__" ] is True
1707+ assert all (ADDITIONAL_CONTEXT_HANDLER in entry [field ] for field in ("bash" , "powershell" ))
1708+ assert copilot .installed_event_handlers (tmp_path ) == {"session_start" : [ADDITIONAL_CONTEXT_HANDLER ]}
1709+ assert context_delivery_error (copilot , tmp_path ) is None
1710+
1711+
1712+ @pytest .mark .parametrize ("damage" , [
1713+ "malformed" , "jsonc" , "wrong_native_event" , "wrong_command" , "wrong_canonical_event" ,
1714+ "wrong_envelope" , "unmarked" , "marker_not_true" , "stale_dispatcher" , "modified_dispatcher" ,
1715+ ])
1716+ def test_copilot_context_delivery_rejects_damaged_install (tmp_path , context_delivery_install , damage ):
1717+ from specify_cli .events import ADDITIONAL_CONTEXT_HANDLER , context_delivery_error
1718+
1719+ copilot = context_delivery_install
1720+ path = tmp_path / copilot .events_config_file
1721+ native = json .loads (path .read_text (encoding = "utf-8" ))
1722+ entry = native ["hooks" ]["sessionStart" ][0 ]
1723+ if damage == "wrong_native_event" :
1724+ native ["hooks" ]["sessionEnd" ] = native ["hooks" ].pop ("sessionStart" )
1725+ elif damage in ("wrong_command" , "wrong_canonical_event" , "wrong_envelope" ):
1726+ original , replacement = {
1727+ "wrong_command" : (ADDITIONAL_CONTEXT_HANDLER , "other-handler" ),
1728+ "wrong_canonical_event" : ("session_start" , "session_end" ),
1729+ "wrong_envelope" : ("additionalContext" , "plain" ),
1730+ }[damage ]
1731+ for field in ("bash" , "powershell" ):
1732+ entry [field ] = entry [field ].replace (original , replacement )
1733+ elif damage == "unmarked" :
1734+ entry .pop ("__speckit_event__" )
1735+ elif damage == "marker_not_true" :
1736+ entry ["__speckit_event__" ] = 1
1737+ elif damage in ("stale_dispatcher" , "modified_dispatcher" ):
1738+ dispatcher = tmp_path / ".specify/events.py"
1739+ dispatcher .write_text ("# stale\n " if damage == "stale_dispatcher" else
1740+ dispatcher .read_text (encoding = "utf-8" ) + "# modified\n " , encoding = "utf-8" )
1741+ content = {"malformed" : "{" , "jsonc" : '{// comment\n "hooks": {}}' }.get (damage , json .dumps (native ))
1742+ path .write_text (content , encoding = "utf-8" )
1743+ error = context_delivery_error (copilot , tmp_path )
1744+ assert "missing or outdated" in error
1745+ assert "specify integration upgrade copilot" in error
1746+
1747+
1748+ @pytest .mark .parametrize ("target,selected,other" , [("posix" , "bash" , "powershell" ), ("nt" , "powershell" , "bash" )])
1749+ def test_copilot_context_delivery_selects_current_platform_command (
1750+ tmp_path , context_delivery_install , monkeypatch , target , selected , other
1751+ ):
1752+ from specify_cli .events import ADDITIONAL_CONTEXT_HANDLER
1753+
1754+ copilot = context_delivery_install
1755+ path = tmp_path / copilot .events_config_file
1756+ native = json .loads (path .read_text (encoding = "utf-8" ))
1757+ entry = native ["hooks" ]["sessionStart" ][0 ]
1758+ valid = entry [other ]
1759+ entry [other ] = "broken"
1760+ path .write_text (json .dumps (native ), encoding = "utf-8" )
1761+ with monkeypatch .context () as patcher :
1762+ patcher .setattr ("specify_cli.events.os.name" , target )
1763+ assert copilot .installed_event_handlers (tmp_path ) == {"session_start" : [ADDITIONAL_CONTEXT_HANDLER ]}
1764+ entry [other ], entry [selected ] = valid , "broken"
1765+ path .write_text (json .dumps (native ), encoding = "utf-8" )
1766+ assert copilot .installed_event_handlers (tmp_path ) == {}
1767+
1768+
1769+ def test_copilot_context_delivery_reader_maps_owned_handlers (tmp_path , context_delivery_install ):
1770+ from specify_cli .events import ADDITIONAL_CONTEXT_HANDLER , _dispatcher_command
1771+
1772+ copilot = context_delivery_install
1773+ path = tmp_path / copilot .events_config_file
1774+ native = json .loads (path .read_text (encoding = "utf-8" ))
1775+ start = native ["hooks" ]["sessionStart" ][0 ]
1776+ command = _dispatcher_command (copilot , tmp_path , "end-handler" , "session_end" , target_os = "posix" )
1777+ ps_command = _dispatcher_command (copilot , tmp_path , "end-handler" , "session_end" , target_os = "windows" )
1778+ end = {"__speckit_event__" : True , "bash" : command , "powershell" : ps_command }
1779+ native ["hooks" ]["sessionStart" ].extend ([None , {}, {** start , "__speckit_event__" : False }, start ])
1780+ native ["hooks" ]["sessionEnd" ] = [end ]
1781+ native ["hooks" ]["unknown" ] = [start ]
1782+ path .write_text (json .dumps (native ), encoding = "utf-8" )
1783+ assert copilot .installed_event_handlers (tmp_path ) == {
1784+ "session_start" : [ADDITIONAL_CONTEXT_HANDLER , ADDITIONAL_CONTEXT_HANDLER ],
1785+ "session_end" : ["end-handler" ],
1786+ }
1787+
1788+
1789+ @pytest .mark .parametrize ("content" , ['[]' , '{"hooks": []}' , '{"hooks": {"sessionStart": {}}}' ])
1790+ def test_copilot_context_delivery_reader_rejects_non_object_or_invalid_hooks (
1791+ tmp_path , context_delivery_install , content
1792+ ):
1793+ from specify_cli .events import context_delivery_error
1794+
1795+ copilot = context_delivery_install
1796+ (tmp_path / copilot .events_config_file ).write_text (content , encoding = "utf-8" )
1797+ assert copilot .installed_event_handlers (tmp_path ) == {}
1798+ assert "missing or outdated" in context_delivery_error (copilot , tmp_path )
0 commit comments