fix: move draft and template context handling out of create_workflow_run (#560)

* fix: move draft and template context handling out of create_workflow_run

create_workflow_run now only creates
the run with the definition_id and initial_context provided by the caller. Test call paths explicitly resolve draft definitions
and merge template context variables before creating the run, while production/runtime paths bind the published definition
without adding template defaults.

* fix: review comments

* chore: format and minor cleanups

---------

Co-authored-by: Abhishek Kumar <abhishek@a6k.me>
This commit is contained in:
Sabiha Khan 2026-07-21 13:24:51 +05:30 committed by GitHub
parent 94dce42209
commit 90c5831984
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 657 additions and 72 deletions

View file

@ -1,5 +1,6 @@
from datetime import UTC, datetime
from types import SimpleNamespace
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
@ -82,3 +83,51 @@ def test_create_workflow_rejects_duplicate_api_triggers_before_db_write():
assert detail["errors"][0]["kind"] == "workflow"
assert "at most one API Trigger" in detail["errors"][0]["message"]
assert mock_db.mock_calls == []
def test_create_workflow_run_uses_draft_and_template_context():
app = _make_test_app()
client = TestClient(app)
workflow = SimpleNamespace(
id=33,
released_definition=SimpleNamespace(
id=77,
template_context_variables={"name": "published"},
),
current_definition=None,
template_context_variables={"name": "workflow"},
)
draft = SimpleNamespace(
id=88,
template_context_variables={"name": "draft", "draft_only": "kept"},
)
run = SimpleNamespace(
id=501,
workflow_id=workflow.id,
name="WR-test",
mode="smallwebrtc",
created_at=datetime.now(UTC),
definition_id=draft.id,
initial_context={"name": "draft", "draft_only": "kept"},
gathered_context={},
)
with patch("api.routes.workflow.db_client") as mock_db:
mock_db.get_workflow = AsyncMock(return_value=workflow)
mock_db.get_draft_version = AsyncMock(return_value=draft)
mock_db.create_workflow_run = AsyncMock(return_value=run)
response = client.post(
f"/workflow/{workflow.id}/runs",
json={"name": "WR-test", "mode": "smallwebrtc"},
)
assert response.status_code == 200
mock_db.get_draft_version.assert_awaited_once_with(workflow.id)
create_kwargs = mock_db.create_workflow_run.await_args.kwargs
assert create_kwargs["definition_id"] == draft.id
assert create_kwargs["initial_context"] == {
"name": "draft",
"draft_only": "kept",
}