File size: 8,044 Bytes
70d3b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""Turing can BUILD now, not just describe building.

`dee/core/cloning.py` has done Gibson, Golden Gate and restriction cloning
since the plasmid work — with junction detection, overhang checking and primer
design — and until 2026-08-04 no agent tool imported it. So the "Build" step of
this product's own Design→Build→Edit→Learn loop had no agent path: asked to
clone three fragments into pET-28a, Turing could only write prose about how one
would. The capability was in the repo the whole time.

That is the fourth built-but-unreachable capability found in one day (the
vector catalogue, BLAST, pairwise alignment, and this), which is why the
capability audit leads with the category rather than with any one gap.

The tests here are weighted toward the ways an assembly can LIE:

  * a design that does not close must be reported as a failure, not dressed up
    as a product;
  * a junction with no native overlap means primers someone has to buy, and a
    summary that omits that makes a design look free;
  * the engine's own warnings must survive to the model verbatim, not be
    summarised into optimism.
"""
import pytest

from dee.core import agent_tools as t
from dee.core import cloning as cl
from dee.core import orchestrator as orch


# Three fragments with real, detectable overlaps between neighbours.
A = "ATGGCTAGCAAAGGAGAAGAACTTTTCACTGGAGTTGTCCCAATTCTTGTTGAATTAGATG"
B = A[-30:] + "GTGATGTTAATGGGCACAAATTTTCTGTCAGTGGAGAGGGTGAAGGTGATGCAACATACG"
C = B[-30:] + "GAAAACTTACCCTTAAATTTATTTGCACTACTGGAAAACTACCTGTTCCATGGCCAACAC"


def _frags():
    return [{"name": "vector", "sequence": A},
            {"name": "insert1", "sequence": B},
            {"name": "insert2", "sequence": C}]


def _run(**kw):
    args = {"fragments": _frags()}
    args.update(kw)
    return t.execute_tool("simulate_assembly", args, auth_anonymous=True)


# --------------------------------------------------------------------------- #
# it actually assembles
# --------------------------------------------------------------------------- #
def test_the_agent_can_reach_the_cloning_engine_at_all():
    """The whole point. Before this, cloning.py was not imported anywhere in
    the tool layer."""
    assert "simulate_assembly" in t._TOOLS
    src = open("dee/core/agent_tools.py", encoding="utf-8").read()
    assert "from dee.core import cloning" in src


def test_a_gibson_assembly_returns_a_real_product():
    out = _run()
    assert out["ok"] is True
    assert out["_ui"]["sequence"]
    assert out["length"] == len(out["_ui"]["sequence"])
    assert len(out["junctions"]) >= 2


def test_native_overlaps_are_detected_rather_than_designed_over():
    """These fragments genuinely share 30 bp with their neighbours. If the
    engine reports them as 'designed', it is inventing primer work that the
    user does not need to pay for."""
    out = _run()
    kinds = [j["kind"] for j in out["junctions"]]
    assert "native" in kinds, kinds


def test_the_assembled_product_matches_the_engine_directly():
    """The tool must not transform the sequence on its way through — it is a
    wrapper, and a wrapper that alters its payload is a second implementation."""
    direct = cl.gibson([{"name": f["name"], "seq": f["sequence"]} for f in _frags()])
    assert _run()["_ui"]["sequence"] == direct["assembled"]


# --------------------------------------------------------------------------- #
# the ways it can lie
# --------------------------------------------------------------------------- #
def test_a_failed_assembly_is_reported_as_a_failure():
    """Golden Gate on fragments with no compatible overhangs does not close.
    That is a real, useful answer — and the one an agent is most tempted to
    round up into 'assembled successfully'."""
    out = t.execute_tool("simulate_assembly",
                         {"fragments": _frags(), "method": "golden_gate"},
                         auth_anonymous=True)
    assert out["ok"] is False
    assert out["kind"] == "assembly_failed"
    assert "do not describe the product as if it assembled" in out["next"].lower()


def test_primers_that_must_be_ordered_are_returned():
    """A designed junction is a purchase. Returning the product without the
    primers makes the design look cheaper than it is."""
    out = t.execute_tool("simulate_assembly",
                         {"fragments": [{"name": "x", "sequence": "ATGC" * 30},
                                        {"name": "y", "sequence": "GGTT" * 30}]},
                         auth_anonymous=True)
    if out["ok"]:
        assert out["designed_junctions"] >= 1
        assert "primers" in out


def test_engine_warnings_survive_verbatim():
    """Not summarised, not dropped. The warning is the honest part."""
    out = t.execute_tool("simulate_assembly",
                         {"fragments": [{"name": "x", "sequence": "ATGC" * 30},
                                        {"name": "y", "sequence": "GGTT" * 30}]},
                         auth_anonymous=True)
    if out["ok"]:
        direct = cl.gibson([{"name": "x", "seq": "ATGC" * 30},
                            {"name": "y", "seq": "GGTT" * 30}])
        assert out["warnings"] == (direct.get("warnings") or [])


def test_the_summary_leads_with_the_primer_cost():
    """A one-line summary reading only "3,200 bp" hides the thing the user
    has to act on."""
    out = _run()
    line = orch._summarize("simulate_assembly", out)
    assert "junction" in line
    if out.get("designed_junctions"):
        assert "primer" in line


def test_the_result_asks_for_verification():
    out = _run()
    assert "map_plasmid" in out["next"]
    assert "designed" in out["next"]


# --------------------------------------------------------------------------- #
# input handling
# --------------------------------------------------------------------------- #
def test_one_fragment_is_not_an_assembly():
    out = t.execute_tool("simulate_assembly",
                         {"fragments": [{"name": "a", "sequence": A}]},
                         auth_anonymous=True)
    assert out["ok"] is False
    assert "two fragments" in out["error"]


def test_a_fragment_with_no_dna_is_refused_by_name():
    """Naming which fragment is empty is the difference between a fixable
    error and a shrug."""
    out = t.execute_tool("simulate_assembly",
                         {"fragments": [{"name": "vector", "sequence": A},
                                        {"name": "bad insert", "sequence": "???"}]},
                         auth_anonymous=True)
    assert out["ok"] is False
    assert "bad insert" in out["error"]


def test_restriction_cloning_requires_its_enzyme():
    out = t.execute_tool("simulate_assembly",
                         {"fragments": _frags(), "method": "restriction"},
                         auth_anonymous=True)
    assert out["ok"] is False and "enzyme" in out["error"]


def test_an_unknown_method_is_refused_with_the_real_options():
    out = t.execute_tool("simulate_assembly",
                         {"fragments": _frags(), "method": "magic"},
                         auth_anonymous=True)
    assert out["ok"] is False
    for m in ("gibson", "golden_gate", "restriction"):
        assert m in out["error"]


# --------------------------------------------------------------------------- #
# wiring
# --------------------------------------------------------------------------- #
def test_the_bases_do_not_travel_through_the_model():
    out = _run()
    stripped = orch._strip_ui(out)
    assert "_ui" not in stripped
    assert not any(isinstance(v, str) and len(v) > 40 and set(v) <= set("ACGT")
                   for v in stripped.values())


def test_it_paints_the_plasmid_view():
    assert orch._tool_view("simulate_assembly") == "plasmid"
    assert "simulate_assembly" in open("dee/static/cockpit.js", encoding="utf-8").read()


def test_the_prompt_tells_it_to_build_rather_than_describe():
    p = orch.build_system_prompt(False, {}, "")
    assert "BUILD MEANS BUILD" in p
    assert "simulate_assembly" in p