170 lines
5.6 KiB
Python
170 lines
5.6 KiB
Python
"""Sources digested into a spec at the start of an Execution."""
|
|
|
|
import dataclasses
|
|
import hashlib
|
|
import json
|
|
|
|
import pytest
|
|
import yaml
|
|
|
|
from execution_engine.scope import ESSENTIAL, Catalogue, FieldSpec
|
|
from execution_engine.workflows.workflow import Digested
|
|
from tests.toy.support import make_engine, once_each
|
|
from tests.toy.workflow import TOY
|
|
|
|
CATALOGUE = Catalogue(
|
|
schema_version=1,
|
|
kinds=("picnic",),
|
|
fields={
|
|
spec.path: spec
|
|
for spec in (
|
|
FieldSpec("metadata.customer_case", tier=ESSENTIAL),
|
|
FieldSpec("engagement.kind", type="enum", values=("picnic",)),
|
|
FieldSpec("intent.statement", type="text", tier=ESSENTIAL),
|
|
FieldSpec("guests.count", type="integer"),
|
|
FieldSpec("guests.allergies", type="list"),
|
|
FieldSpec("run.pre_approved", type="boolean"),
|
|
)
|
|
},
|
|
)
|
|
INVITATION = json.dumps(
|
|
{"party": "park", "wants": "A picnic for the team.", "coming": 9}
|
|
)
|
|
|
|
|
|
def digest(sources):
|
|
"""Reads `invitation.json`, a format this Workflow knows; nothing else."""
|
|
if "invitation.json" not in sources:
|
|
return Digested({})
|
|
try:
|
|
invitation = json.loads(sources["invitation.json"])
|
|
except ValueError as exc:
|
|
raise ValueError(f"invitation.json: not valid JSON ({exc})") from exc
|
|
return Digested(
|
|
spec={
|
|
"metadata": {"customer_case": invitation["party"]},
|
|
"engagement": {"kind": "picnic"},
|
|
"intent": {"statement": invitation["wants"]},
|
|
"guests": {"count": invitation["coming"]},
|
|
"run": {"pre_approved": True},
|
|
},
|
|
used=("invitation.json",),
|
|
notes={"guests.count": "invitation.json, `coming`"},
|
|
)
|
|
|
|
|
|
DIGESTING = dataclasses.replace(TOY, digest=digest)
|
|
|
|
|
|
def engine_for(tmp_path, workflow=DIGESTING):
|
|
return make_engine(
|
|
tmp_path, once_each(), workflow=workflow, scope_catalogue=CATALOGUE
|
|
)
|
|
|
|
|
|
def completed(tmp_path) -> dict:
|
|
path = tmp_path / "deliverables" / "cases" / "park" / "intent"
|
|
return yaml.safe_load((path / "001.scope.completed.yaml").read_text())
|
|
|
|
|
|
async def test_an_execution_starts_from_what_its_sources_say(tmp_path):
|
|
engine, _ = engine_for(tmp_path)
|
|
|
|
result = await engine.start("", sources={"invitation.json": INVITATION})
|
|
|
|
assert result.status == "completed"
|
|
assert result.state["customer_case"] == "park"
|
|
assert result.state["workflow_request"] == "A picnic for the team."
|
|
assert result.state["scope"]["context"]["stated"]["guests"] == {"count": 9}
|
|
|
|
|
|
async def test_a_digested_value_says_where_it_came_from(tmp_path):
|
|
engine, _ = engine_for(tmp_path)
|
|
|
|
await engine.start("", sources={"invitation.json": INVITATION})
|
|
|
|
spec = completed(tmp_path)
|
|
assert spec["provenance"]["guests.count"] == "digested"
|
|
assert spec["provenance"]["guests.allergies"] == "unknown"
|
|
assert spec["readiness"]["digested"] == 5
|
|
|
|
|
|
async def test_what_the_caller_writes_is_laid_over_what_was_digested(tmp_path):
|
|
engine, _ = engine_for(tmp_path)
|
|
|
|
result = await engine.start(
|
|
"",
|
|
sources={"invitation.json": INVITATION},
|
|
scope="guests:\n count: 12\n allergies: [nuts]\n",
|
|
)
|
|
|
|
spec = completed(tmp_path)
|
|
assert result.state["scope"]["context"]["stated"]["guests"] == {
|
|
"count": 12,
|
|
"allergies": ["nuts"],
|
|
}
|
|
assert spec["provenance"]["guests.count"] == "supplied"
|
|
assert spec["provenance"]["intent.statement"] == "digested"
|
|
|
|
|
|
async def test_the_sources_are_kept_as_supplied_with_their_hashes(tmp_path):
|
|
engine, _ = engine_for(tmp_path)
|
|
notes = "Bring a blanket."
|
|
|
|
result = await engine.start(
|
|
"", sources={"invitation.json": INVITATION, "notes/host.md": notes}
|
|
)
|
|
|
|
kept = tmp_path / "deliverables" / "cases" / "park" / "intent" / "001.sources"
|
|
assert (kept / "invitation.json").read_text() == INVITATION
|
|
assert (kept / "notes" / "host.md").read_text() == notes
|
|
assert result.state["sources"] == {
|
|
"invitation.json": {
|
|
"sha256": hashlib.sha256(INVITATION.encode()).hexdigest(),
|
|
"digested": True,
|
|
},
|
|
"notes/host.md": {
|
|
"sha256": hashlib.sha256(notes.encode()).hexdigest(),
|
|
"digested": False,
|
|
},
|
|
}
|
|
# What code did not read is a document for a Specialist to read.
|
|
assert result.state["source_documents"] == {"notes/host.md": notes}
|
|
execution = next(
|
|
(tmp_path / "deliverables" / "cases" / "park" / "executions").iterdir()
|
|
)
|
|
record = json.loads((execution / "execution.json").read_text())
|
|
assert record["sources"]["invitation.json"]["digested"] is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("sources", "problem"),
|
|
[
|
|
({"../invitation.json": INVITATION}, "source names must be relative paths"),
|
|
({"invitation.json": "{"}, "invitation.json: not valid JSON"),
|
|
({"invitation.json": 7}, "sources must be files by name"),
|
|
],
|
|
)
|
|
async def test_sources_that_cannot_be_read_refuse_the_start(tmp_path, sources, problem):
|
|
engine, runtime = engine_for(tmp_path)
|
|
|
|
with pytest.raises(ValueError, match=problem):
|
|
await engine.start("A picnic", sources=sources)
|
|
|
|
assert runtime.runs == []
|
|
assert not (tmp_path / "deliverables").exists()
|
|
|
|
|
|
async def test_a_workflow_that_digests_nothing_passes_every_source_on_to_be_read(
|
|
tmp_path,
|
|
):
|
|
engine, _ = engine_for(tmp_path, TOY)
|
|
|
|
result = await engine.start(
|
|
"A picnic", pre_approved=True, sources={"notes.md": "Bring a blanket."}
|
|
)
|
|
|
|
assert result.status == "completed"
|
|
assert result.state["scope"] is None
|
|
assert result.state["source_documents"] == {"notes.md": "Bring a blanket."}
|