mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
copilot: Edit service wiring + create_edit_session + merged registry
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
606cff1964
commit
ef255c4dde
6 files changed
+181
-1
No files matched your search
@@ -78,6 +78,22 @@ def test_create_build_scenario_calls_create_build_session(monkeypatch):
|
||||
svc.create_fix_session.assert_not_called()
|
||||
|
||||
|
||||
def test_create_edit_scenario_calls_create_edit_session(monkeypatch):
|
||||
svc = MagicMock()
|
||||
svc.create_edit_session.return_value = _session_view()
|
||||
monkeypatch.setattr(mod, "build_service", lambda: svc)
|
||||
actor = _actor()
|
||||
|
||||
body, status = mod._create({"scenario": "edit", "app_id": "a1"}, actor)
|
||||
|
||||
assert status == 201
|
||||
svc.create_edit_session.assert_called_once()
|
||||
called = svc.create_edit_session.call_args
|
||||
assert called.kwargs["app_id"] == "a1"
|
||||
svc.create_fix_session.assert_not_called()
|
||||
svc.create_build_session.assert_not_called()
|
||||
|
||||
|
||||
def test_create_rejects_non_dict_body():
|
||||
actor = _actor()
|
||||
svc = MagicMock()
|
||||
|
||||
@@ -249,3 +249,28 @@ def test_advance_session_resolves_build_handler_via_merged_registry(
|
||||
stored, _fc = repo.get_session(s.id)
|
||||
assert stored.current_state == PcState.BUILD_GOAL_ANALYSIS # build handler resolved, not a 500
|
||||
assert (s.id, "tok-b1") in released
|
||||
|
||||
|
||||
def _seed_edit_session_at_capability(repo: SqlCopilotRepository) -> Session:
|
||||
s = Session(
|
||||
app_id=APP_ID,
|
||||
tenant_id=TENANT_ID,
|
||||
owner_account_id=ACCOUNT_ID,
|
||||
entry_mode=EntryMode.EDIT,
|
||||
current_state=PcState.EDIT_CAPABILITY_CHECK,
|
||||
)
|
||||
repo.create_session(s, CopilotContext(), [ConversationItem(kind="user", seq=0)])
|
||||
return s
|
||||
|
||||
|
||||
def test_advance_session_resolves_edit_handler_via_merged_registry(
|
||||
repo: SqlCopilotRepository, wired
|
||||
) -> None:
|
||||
_events, released = wired
|
||||
s = _seed_edit_session_at_capability(repo)
|
||||
|
||||
mod.advance_session(s.id, _act("send_edit_goal", 1, text="Tighten risk"), _ACTOR_DICT, "tok-e1")
|
||||
|
||||
stored, _fc = repo.get_session(s.id)
|
||||
assert stored.current_state == PcState.EDIT_IMPACT_ANALYSIS # edit handler resolved, not a 500
|
||||
assert (s.id, "tok-e1") in released
|
||||
@@ -515,3 +515,82 @@ def test_build_waiting_state_actions_resolve_to_handled_kinds() -> None:
|
||||
resolve_action_kind(a.id) for a in actions if a.id not in service_module._CLIENT_ONLY_ACTIONS
|
||||
}
|
||||
assert resolved <= kinds, f"{state}: resolved {resolved} not handled by its handler ({kinds})"
|
||||
|
||||
|
||||
def _seed_edit_at(repo: SqlCopilotRepository, state: PcState) -> Session:
|
||||
s = Session(
|
||||
app_id=APP_ID,
|
||||
tenant_id=TENANT_ID,
|
||||
owner_account_id=ACCOUNT_ID,
|
||||
entry_mode=EntryMode.EDIT,
|
||||
current_state=state,
|
||||
)
|
||||
repo.create_session(s, CopilotContext(goal_text="Tighten risk"), [ConversationItem(kind="user", seq=0)])
|
||||
return s
|
||||
|
||||
|
||||
def test_create_edit_session_rests_at_capability_check_without_dispatch(
|
||||
service: WorkflowCopilotService, enqueued: list[tuple]
|
||||
) -> None:
|
||||
view = service.create_edit_session(APP_ID, _actor())
|
||||
assert view.state == "edit.capability_check"
|
||||
assert view.entry_mode == EntryMode.EDIT
|
||||
assert view.version == 1
|
||||
assert view.run_status == "waiting_input"
|
||||
assert view.canvas_read_only is False
|
||||
assert view.conversation == [] # mock: show history + composer, nothing read yet
|
||||
assert enqueued == [] # NO advance dispatched (unlike Build)
|
||||
assert [a.id for a in view.actions] == ["send_edit_goal"]
|
||||
|
||||
|
||||
def test_edit_apply_changes_actions_and_run_status(service: WorkflowCopilotService, repo: SqlCopilotRepository) -> None:
|
||||
s = _seed_edit_at(repo, PcState.EDIT_APPLY_CHANGES)
|
||||
view = service.get_session_view(s.id, _actor())
|
||||
assert view.run_status == "waiting_input"
|
||||
assert view.phase == "modify"
|
||||
assert [(a.id, a.kind) for a in view.actions] == [
|
||||
("run_affected_tests", ActionKind.PRIMARY),
|
||||
("revert", ActionKind.DESTRUCTIVE),
|
||||
]
|
||||
|
||||
|
||||
def test_edit_review_actions(service: WorkflowCopilotService, repo: SqlCopilotRepository) -> None:
|
||||
s = _seed_edit_at(repo, PcState.EDIT_REVIEW)
|
||||
view = service.get_session_view(s.id, _actor())
|
||||
assert [a.id for a in view.actions] == [
|
||||
"publish_workflow",
|
||||
"keep_draft",
|
||||
"continue_adjusting",
|
||||
"view_changes",
|
||||
"revert",
|
||||
]
|
||||
|
||||
|
||||
def test_edit_publish_is_terminal_with_no_actions(service: WorkflowCopilotService, repo: SqlCopilotRepository) -> None:
|
||||
s = _seed_edit_at(repo, PcState.EDIT_PUBLISH)
|
||||
view = service.get_session_view(s.id, _actor())
|
||||
assert view.run_status == "complete"
|
||||
assert view.actions == []
|
||||
|
||||
|
||||
def test_edit_waiting_state_actions_resolve_to_handled_kinds() -> None:
|
||||
"""Every non-client-only Edit action id must resolve (via
|
||||
resolve_action_kind) to a kind its handler in handlers_edit.py branches on.
|
||||
approve_plan->approve_repair, revert->undo, continue_adjusting/
|
||||
retry_after_revert->re_fix reuse the existing global map; the rest pass
|
||||
through (send_edit_goal, submit_edit_rules, run_affected_tests, keep_draft,
|
||||
publish_workflow)."""
|
||||
handled: dict[PcState, set[str]] = {
|
||||
PcState.EDIT_CAPABILITY_CHECK: {"send_edit_goal"},
|
||||
PcState.EDIT_IMPACT_ANALYSIS: {"submit_edit_rules"},
|
||||
PcState.EDIT_PLAN_APPROVAL: {"approve_repair"},
|
||||
PcState.EDIT_APPLY_CHANGES: {"run_affected_tests", "undo"},
|
||||
PcState.EDIT_REVIEW: {"publish_workflow", "keep_draft", "re_fix", "undo"},
|
||||
PcState.EDIT_REVERTED: {"re_fix"},
|
||||
}
|
||||
for state, kinds in handled.items():
|
||||
actions = service_module._ACTIONS_FOR[state]
|
||||
resolved = {
|
||||
resolve_action_kind(a.id) for a in actions if a.id not in service_module._CLIENT_ONLY_ACTIONS
|
||||
}
|
||||
assert resolved <= kinds, f"{state}: resolved {resolved} not handled by its handler ({kinds})"
|
||||
Reference in new issue
Block a user