File size: 27,967 Bytes
3193174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
"""Tests for src/core/schema.py"""

from typing import cast

import pytest
import torch
from pydantic import BaseModel

from core.schema import (
    SCHEMA_VERSION,
    AgentNodeSchema,
    BaseEdgeSchema,
    BaseNodeSchema,
    CostMetrics,
    EdgeType,
    GraphSchema,
    LLMConfig,
    MigrationRegistry,
    MigrationV1ToV2,
    NodeType,
    SchemaValidationResult,
    SchemaValidator,
    SchemaVersion,
    TaskNodeSchema,
    ValidationResult,
    WorkflowEdgeSchema,
    migrate_schema,
)

# ─────────────────────────── SchemaVersion ───────────────────────────────────


class TestSchemaVersion:
    def test_current(self):
        v = SchemaVersion.current()
        assert str(v) == SCHEMA_VERSION

    def test_parse(self):
        v = SchemaVersion.parse("2.0.0")
        assert v.major == 2
        assert v.minor == 0
        assert v.patch == 0

    def test_compatibility_same_major(self):
        v1 = SchemaVersion.parse("2.0.0")
        v2 = SchemaVersion.parse("2.5.1")
        assert v1.is_schema_compatible(v2)

    def test_incompatibility_different_major(self):
        v1 = SchemaVersion.parse("1.0.0")
        v2 = SchemaVersion.parse("2.0.0")
        assert not v1.is_schema_compatible(v2)


# ─────────────────────────── LLMConfig ───────────────────────────────────────


class TestLLMConfig:
    def test_default_init(self):
        cfg = LLMConfig()
        assert cfg.model_name is None
        assert cfg.temperature is None

    def test_configured(self):
        cfg = LLMConfig(model_name="gpt-4", base_url="https://api.openai.com/v1")
        assert cfg.is_configured()

    def test_not_configured(self):
        cfg = LLMConfig()
        assert not cfg.is_configured()

    def test_resolve_api_key_literal(self):
        cfg = LLMConfig(api_key="sk-test-key")
        assert cfg.resolve_api_key() == "sk-test-key"

    def test_resolve_api_key_env_var(self, monkeypatch):
        monkeypatch.setenv("MY_API_KEY", "env-secret")
        cfg = LLMConfig(api_key="$MY_API_KEY")
        assert cfg.resolve_api_key() == "env-secret"

    def test_resolve_api_key_none(self):
        cfg = LLMConfig()
        assert cfg.resolve_api_key() is None

    def test_merge_with_none(self):
        cfg = LLMConfig(model_name="gpt-4", temperature=0.7)
        merged = cfg.merge_with(None)
        assert merged.model_name == "gpt-4"
        assert merged.temperature == 0.7

    def test_merge_with_other_fills_gaps(self):
        cfg = LLMConfig(model_name="gpt-4")
        other = LLMConfig(temperature=0.5, max_tokens=1000)
        merged = cfg.merge_with(other)
        assert merged.model_name == "gpt-4"
        assert merged.temperature == 0.5
        assert merged.max_tokens == 1000

    def test_merge_self_takes_priority(self):
        cfg = LLMConfig(model_name="gpt-4", temperature=0.9)
        other = LLMConfig(model_name="gpt-3.5", temperature=0.1)
        merged = cfg.merge_with(other)
        assert merged.model_name == "gpt-4"
        assert merged.temperature == 0.9

    def test_to_generation_params(self):
        cfg = LLMConfig(temperature=0.7, max_tokens=512, top_p=0.9)
        params = cfg.to_generation_params()
        assert params["temperature"] == 0.7
        assert params["max_tokens"] == 512
        assert params["top_p"] == 0.9

    def test_to_generation_params_empty(self):
        cfg = LLMConfig()
        params = cfg.to_generation_params()
        assert params == {}

    def test_to_generation_params_with_stop(self):
        cfg = LLMConfig(stop_sequences=["END", "STOP"])
        params = cfg.to_generation_params()
        assert params["stop"] == ["END", "STOP"]

    def test_extra_params_merged(self):
        cfg = LLMConfig(extra_params={"stream": True})
        params = cfg.to_generation_params()
        assert params["stream"] is True


# ─────────────────────────── BaseNodeSchema ──────────────────────────────────


class TestBaseNodeSchema:
    def test_basic_creation(self):
        node = BaseNodeSchema(id="node1")
        assert node.id == "node1"
        assert node.type == NodeType.AGENT

    def test_with_embedding(self):
        node = BaseNodeSchema(id="n1", embedding=[0.1, 0.2, 0.3])
        assert node.embedding is not None
        assert len(node.embedding) == 3
        assert node.embedding_dim == 3

    def test_embedding_from_tensor(self):
        t = torch.tensor([1.0, 2.0, 3.0])
        node = BaseNodeSchema(id="n1", embedding=cast("list[float]", t))
        assert node.embedding == [1.0, 2.0, 3.0]

    def test_to_tensor_embedding_none(self):
        node = BaseNodeSchema(id="n1")
        assert node.to_tensor_embedding() is None

    def test_to_tensor_embedding(self):
        node = BaseNodeSchema(id="n1", embedding=[1.0, 2.0])
        t = node.to_tensor_embedding()
        assert isinstance(t, torch.Tensor)
        assert t.shape == (2,)

    def test_get_feature_vector_with_embedding(self):
        node = BaseNodeSchema(id="n1", embedding=[1.0, 2.0, 3.0])
        fv = node.get_feature_vector()
        assert fv.shape == (3,)

    def test_get_feature_vector_with_metadata(self):
        node = BaseNodeSchema(id="n1", embedding=[1.0], metadata={"score": 0.5})
        fv = node.get_feature_vector(feature_names=["score"])
        assert fv.shape == (2,)

    def test_get_feature_vector_empty(self):
        node = BaseNodeSchema(id="n1")
        fv = node.get_feature_vector()
        assert fv.shape == (0,)

    def test_tags(self):
        node = BaseNodeSchema(id="n1", tags={"tag1", "tag2"})
        assert "tag1" in node.tags


# ─────────────────────────── AgentNodeSchema ─────────────────────────────────


class TestAgentNodeSchema:
    def test_basic_creation(self):
        agent = AgentNodeSchema(id="solver", persona="You are a solver")
        assert agent.id == "solver"
        assert agent.type == NodeType.AGENT
        assert agent.trust_score == 1.0

    def test_validate_input_no_schema(self):
        agent = AgentNodeSchema(id="solver")
        result = agent.validate_input({"question": "2+2"})
        assert result.valid is True
        assert "No schema" in result.message

    def test_validate_input_with_pydantic_schema(self):
        class InputSchema(BaseModel):
            question: str

        agent = AgentNodeSchema(id="solver", input_schema=InputSchema)
        result = agent.validate_input({"question": "what is 2+2?"})
        assert result.valid is True
        assert result.validated_data == {"question": "what is 2+2?"}

    def test_validate_input_pydantic_failure(self):
        class InputSchema(BaseModel):
            question: str
            required_field: int

        agent = AgentNodeSchema(id="solver", input_schema=InputSchema)
        result = agent.validate_input({"question": "test"})
        assert result.valid is False
        assert len(result.errors) > 0

    def test_validate_input_json_string(self):
        class InputSchema(BaseModel):
            value: int

        agent = AgentNodeSchema(id="solver", input_schema=InputSchema)
        result = agent.validate_input('{"value": 42}')
        assert result.valid is True

    def test_validate_input_invalid_json(self):
        class InputSchema(BaseModel):
            value: int

        agent = AgentNodeSchema(id="solver", input_schema=InputSchema)
        result = agent.validate_input("not json")
        assert result.valid is False

    def test_validate_input_json_schema_dict(self):
        schema = {
            "type": "object",
            "required": ["name"],
            "properties": {"name": {"type": "string"}, "age": {"type": "integer"}},
        }
        agent = AgentNodeSchema(id="solver", input_schema=schema)
        result = agent.validate_input({"name": "Alice", "age": 30})
        assert result.valid is True

    def test_validate_input_json_schema_missing_required(self):
        schema = {
            "type": "object",
            "required": ["name"],
            "properties": {"name": {"type": "string"}},
        }
        agent = AgentNodeSchema(id="solver", input_schema=schema)
        result = agent.validate_input({"age": 30})
        assert result.valid is False
        assert any("name" in e for e in result.errors)

    def test_validate_input_json_schema_wrong_type(self):
        schema = {
            "type": "object",
            "properties": {"age": {"type": "integer"}},
        }
        agent = AgentNodeSchema(id="solver", input_schema=schema)
        result = agent.validate_input({"age": "not_int"})
        assert result.valid is False

    def test_validate_output_no_schema(self):
        agent = AgentNodeSchema(id="solver")
        result = agent.validate_output({"answer": "42"})
        assert result.valid is True

    def test_has_input_schema_true(self):
        class S(BaseModel):
            x: int

        agent = AgentNodeSchema(id="a", input_schema=S)
        assert agent.has_input_schema() is True

    def test_has_input_schema_false(self):
        agent = AgentNodeSchema(id="a")
        assert agent.has_input_schema() is False

    def test_has_output_schema_true(self):
        class S(BaseModel):
            y: str

        agent = AgentNodeSchema(id="a", output_schema=S)
        assert agent.has_output_schema() is True

    def test_get_llm_config(self):
        agent = AgentNodeSchema(
            id="solver",
            llm_backbone="gpt-4",
            base_url="https://api.openai.com/v1",
            temperature=0.7,
        )
        cfg = agent.get_llm_config()
        assert isinstance(cfg, LLMConfig)
        assert cfg.model_name == "gpt-4"
        assert cfg.temperature == 0.7

    def test_has_llm_config_true(self):
        agent = AgentNodeSchema(id="a", llm_backbone="gpt-4")
        assert agent.has_llm_config() is True

    def test_has_llm_config_false(self):
        agent = AgentNodeSchema(id="a")
        assert agent.has_llm_config() is False

    def test_auto_extracts_input_schema_json(self):
        class InputS(BaseModel):
            q: str

        agent = AgentNodeSchema(id="a", input_schema=InputS)
        assert agent.input_schema_json is not None
        assert "q" in agent.input_schema_json.get("properties", {})


# ─────────────────────────── TaskNodeSchema ──────────────────────────────────


class TestTaskNodeSchema:
    def test_creation(self):
        task = TaskNodeSchema(id="task1", query="Solve the problem")
        assert task.id == "task1"
        assert task.type == NodeType.TASK
        assert task.status == "pending"

    def test_with_deadline(self):
        from datetime import UTC, datetime, timedelta

        deadline = datetime.now(UTC) + timedelta(hours=1)
        task = TaskNodeSchema(id="t1", deadline=deadline)
        assert task.deadline is not None


# ─────────────────────────── BaseEdgeSchema ──────────────────────────────────


class TestBaseEdgeSchema:
    def test_basic_creation(self):
        edge = BaseEdgeSchema(source="a", target="b")
        assert edge.source == "a"
        assert edge.target == "b"
        assert edge.weight == 1.0

    def test_to_attr_tensor_with_attr(self):
        edge = BaseEdgeSchema(source="a", target="b", attr=[1.0, 2.0, 3.0])
        t = edge.to_attr_tensor()
        assert isinstance(t, torch.Tensor)
        assert t.shape == (3,)

    def test_to_attr_tensor_default(self):
        edge = BaseEdgeSchema(source="a", target="b")
        t = edge.to_attr_tensor()
        assert isinstance(t, torch.Tensor)
        assert t.shape[0] > 0

    def test_get_feature_vector(self):
        edge = BaseEdgeSchema(source="a", target="b", weight=0.5, probability=0.8)
        fv = edge.get_feature_vector()
        assert isinstance(fv, torch.Tensor)
        # [weight, probability, trust, reliability] = 4 features
        assert fv.shape[0] >= 4

    def test_attr_dim_auto_set(self):
        edge = BaseEdgeSchema(source="a", target="b", attr=[1.0, 2.0])
        assert edge.attr_dim == 2

    def test_embedding_from_tensor(self):
        t = torch.tensor([0.1, 0.2])
        edge = BaseEdgeSchema(source="a", target="b", embedding=cast("list[float]", t))
        assert edge.embedding == [pytest.approx(0.1), pytest.approx(0.2)]


# ─────────────────────────── WorkflowEdgeSchema ──────────────────────────────


class TestWorkflowEdgeSchema:
    def test_basic(self):
        edge = WorkflowEdgeSchema(source="a", target="b")
        assert edge.type == EdgeType.WORKFLOW
        assert edge.is_conditional is False

    def test_with_condition(self):
        edge = WorkflowEdgeSchema(source="a", target="b", condition="source_success")
        assert edge.is_conditional is True
        assert edge.condition == "source_success"

    def test_priority(self):
        edge = WorkflowEdgeSchema(source="a", target="b", priority=5)
        assert edge.priority == 5


# ─────────────────────────── GraphSchema ─────────────────────────────────────


class TestGraphSchema:
    def setup_method(self):
        self.schema = GraphSchema(name="TestGraph")

    def test_empty_schema(self):
        assert self.schema.nodes == {}
        assert self.schema.edges == []

    def test_add_node(self):
        node = AgentNodeSchema(id="solver")
        self.schema.add_node(node)
        assert "solver" in self.schema.nodes

    def test_add_edge(self):
        self.schema.add_node(AgentNodeSchema(id="a"))
        self.schema.add_node(AgentNodeSchema(id="b"))
        edge = BaseEdgeSchema(source="a", target="b")
        self.schema.add_edge(edge)
        assert len(self.schema.edges) == 1

    def test_get_node(self):
        node = AgentNodeSchema(id="solver")
        self.schema.add_node(node)
        retrieved = self.schema.get_node("solver")
        assert retrieved is not None
        assert retrieved.id == "solver"

    def test_get_node_missing(self):
        assert self.schema.get_node("missing") is None

    def test_get_edges_by_source(self):
        for i in range(3):
            self.schema.add_node(AgentNodeSchema(id=f"n{i}"))
        self.schema.add_edge(BaseEdgeSchema(source="n0", target="n1"))
        self.schema.add_edge(BaseEdgeSchema(source="n0", target="n2"))
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="n2"))
        edges = self.schema.get_edges(source="n0")
        assert len(edges) == 2

    def test_get_edges_by_target(self):
        for i in range(3):
            self.schema.add_node(AgentNodeSchema(id=f"n{i}"))
        self.schema.add_edge(BaseEdgeSchema(source="n0", target="n2"))
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="n2"))
        edges = self.schema.get_edges(target="n2")
        assert len(edges) == 2

    def test_compute_feature_dims(self):
        node = AgentNodeSchema(id="n1", embedding=[1.0, 2.0, 3.0])
        self.schema.add_node(node)
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="n1"))
        self.schema.compute_feature_dims()
        assert self.schema.node_feature_dim == 3

    def test_to_dict(self):
        self.schema.add_node(AgentNodeSchema(id="solver"))
        d = self.schema.to_dict()
        assert "nodes" in d
        assert "edges" in d
        assert d["name"] == "TestGraph"


# ─────────────────────────── SchemaValidator ─────────────────────────────────


class TestSchemaValidator:
    def setup_method(self):
        self.schema = GraphSchema()
        self.validator = SchemaValidator(
            check_cycles=True,
            check_duplicates=True,
            check_orphans=True,
        )

    def test_empty_schema_valid(self):
        result = self.validator.validate(self.schema)
        assert result.valid is True

    def test_valid_dag(self):
        for i in range(3):
            self.schema.add_node(AgentNodeSchema(id=f"n{i}"))
        self.schema.add_edge(BaseEdgeSchema(source="n0", target="n1"))
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="n2"))
        result = self.validator.validate(self.schema)
        assert result.valid is True

    def test_invalid_edge_source(self):
        self.schema.add_node(AgentNodeSchema(id="n1"))
        self.schema.add_edge(BaseEdgeSchema(source="missing", target="n1"))
        result = self.validator.validate(self.schema)
        assert result.valid is False
        assert any("missing" in e for e in result.errors)

    def test_invalid_edge_target(self):
        self.schema.add_node(AgentNodeSchema(id="n1"))
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="missing"))
        result = self.validator.validate(self.schema)
        assert result.valid is False

    def test_self_loop_warning(self):
        self.schema.add_node(AgentNodeSchema(id="n1"))
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="n1"))
        result = self.validator.validate(self.schema)
        assert any("self-loop" in w for w in result.warnings)

    def test_cycle_detection(self):
        for i in range(3):
            self.schema.add_node(AgentNodeSchema(id=f"n{i}"))
        self.schema.add_edge(BaseEdgeSchema(source="n0", target="n1"))
        self.schema.add_edge(BaseEdgeSchema(source="n1", target="n2"))
        self.schema.add_edge(BaseEdgeSchema(source="n2", target="n0"))
        result = self.validator.validate(self.schema)
        # Cycle detected β†’ should produce a warning
        assert any("cycle" in w.lower() for w in result.warnings)

    def test_connectivity_check(self):
        validator = SchemaValidator(check_connectivity=True, check_orphans=True)
        for i in range(3):
            self.schema.add_node(AgentNodeSchema(id=f"n{i}"))
        # Only connect n0 β†’ n1, n2 is isolated
        self.schema.add_edge(BaseEdgeSchema(source="n0", target="n1"))
        result = validator.validate(self.schema)
        # n2 should be detected as orphan
        assert any("n2" in w for w in result.warnings)


# ─────────────────────────── ValidationResult ────────────────────────────────


class TestValidationResult:
    def test_initial_valid(self):
        vr = ValidationResult()
        assert vr.valid is True

    def test_add_error(self):
        vr = ValidationResult()
        vr.add_error("Something went wrong")
        assert vr.valid is False
        assert "Something went wrong" in vr.errors

    def test_add_warning(self):
        vr = ValidationResult()
        vr.add_warning("Be careful")
        assert vr.valid is True  # warnings don't affect validity
        assert "Be careful" in vr.warnings


# ─────────────────────────── SchemaValidationResult ──────────────────────────


class TestSchemaValidationResult:
    def test_valid_result(self):
        svr = SchemaValidationResult(valid=True)
        assert svr.valid is True
        svr.raise_if_invalid()  # should not raise

    def test_invalid_result_raises(self):
        svr = SchemaValidationResult(valid=False, errors=["field missing"])
        with pytest.raises(ValueError, match="Schema validation failed"):
            svr.raise_if_invalid()


# ─────────────────────────── Migration ───────────────────────────────────────


class TestMigrationV1ToV2:
    def test_migrate_agents_to_nodes(self):
        migration = MigrationV1ToV2()
        data = {
            "agents": [
                {"agent_id": "solver", "persona": "You are a solver", "description": "desc"},
            ],
            "edges": [
                {"source": "solver", "target": "reviewer", "weight": 1.0},
            ],
        }
        result = migration.migrate(data)
        assert "nodes" in result
        assert "solver" in result["nodes"]
        assert result["edges"][0]["probability"] == 1.0

    def test_migrate_empty_agents(self):
        migration = MigrationV1ToV2()
        data = {"agents": [], "edges": []}
        result = migration.migrate(data)
        assert result["nodes"] == {}

    def test_can_migrate_correct_version(self):
        migration = MigrationV1ToV2()
        assert migration.can_migrate("1.0.0") is True
        assert migration.can_migrate("2.0.0") is False

    def test_migrate_schema_already_current(self):
        data = {"schema_version": SCHEMA_VERSION, "nodes": {}}
        result = migrate_schema(data)
        assert result["schema_version"] == SCHEMA_VERSION


class TestMigrationRegistry:
    def test_migrate_to_latest(self):
        data = {
            "schema_version": "1.0.0",
            "agents": [{"agent_id": "a1", "persona": "p"}],
            "edges": [],
        }
        result = migrate_schema(data)
        assert result["schema_version"] == SCHEMA_VERSION

    def test_get_migration_path(self):
        registry = MigrationRegistry()
        migration = MigrationV1ToV2()
        registry.register(migration)
        path = registry.get_migration_path("1.0.0", "2.0.0")
        assert len(path) == 1

    def test_get_migration_path_unknown(self):
        registry = MigrationRegistry()
        path = registry.get_migration_path("99.0.0", "100.0.0")
        assert path == []


# ─────────────────────────── CostMetrics ─────────────────────────────────────


class TestCostMetrics:
    def test_default(self):
        cm = CostMetrics()
        assert cm.trust == 1.0
        assert cm.reliability == 1.0

    def test_custom_values(self):
        cm = CostMetrics(trust=0.8, reliability=0.9, latency_ms=100.0)
        assert cm.trust == 0.8
        assert cm.latency_ms == 100.0

    def test_extra_fields_allowed(self):
        cm = CostMetrics(custom={"my_metric": 0.5})
        assert cm.custom["my_metric"] == 0.5


# ─────────────────────────── SchemaValidator extra paths ─────────────────────


class TestSchemaValidatorExtraPaths:
    """Test SchemaValidator edge cases for better coverage."""

    def setup_method(self):
        self.validator = SchemaValidator(
            check_cycles=True,
            check_duplicates=True,
            check_orphans=True,
            check_connectivity=True,
        )

    def test_node_id_mismatch(self):
        """Node.id != key in nodes dict."""
        schema = GraphSchema()
        node = AgentNodeSchema(id="agent1")
        # Manually insert with wrong key
        schema.nodes["wrong_key"] = node
        result = self.validator.validate(schema)
        assert any("mismatch" in e for e in result.errors)

    def test_embedding_length_mismatch_warning(self):
        """Embedding length != embedding_dim."""
        schema = GraphSchema()
        node = AgentNodeSchema(id="agent1", embedding=[0.1, 0.2, 0.3], embedding_dim=5)
        # Override embedding_dim to create mismatch
        node = node.model_copy(update={"embedding_dim": 5})
        schema.add_node(node)
        result = self.validator.validate(schema)
        # Should warn about length mismatch
        assert any("embedding" in w for w in result.warnings)

    def test_duplicate_edge_warning(self):
        """Adding duplicate edge should produce warning."""
        schema = GraphSchema()
        schema.add_node(AgentNodeSchema(id="n1"))
        schema.add_node(AgentNodeSchema(id="n2"))
        schema.add_edge(BaseEdgeSchema(source="n1", target="n2"))
        schema.add_edge(BaseEdgeSchema(source="n1", target="n2"))  # duplicate
        result = self.validator.validate(schema)
        assert any("duplicate" in w.lower() for w in result.warnings)

    def test_connectivity_single_node(self):
        """Graph with single node should not error on connectivity check."""
        schema = GraphSchema()
        schema.add_node(AgentNodeSchema(id="n1"))
        result = self.validator.validate(schema)
        assert result.valid is True


# ─────────────────────────── BaseEdgeSchema extra paths ──────────────────────


class TestBaseEdgeSchemaExtraPaths:
    """Test BaseEdgeSchema edge cases."""

    def test_get_feature_vector_with_embedding(self):
        """BaseEdgeSchema.get_feature_vector with embedding."""
        edge = BaseEdgeSchema(source="a", target="b", embedding=[0.1, 0.2, 0.3])
        fv = edge.get_feature_vector()
        # Should include weight, probability, trust, reliability + embedding
        assert fv.shape[0] >= 4 + 3

    def test_get_feature_vector_with_feature_names(self):
        """Test feature_names including cost attribute."""
        edge = BaseEdgeSchema(source="a", target="b")
        fv = edge.get_feature_vector(feature_names=["trust", "reliability"])
        assert fv.shape[0] == 4 + 2  # base + 2 named features

    def test_get_feature_vector_with_metadata_name(self):
        """Test feature_names including metadata field."""
        edge = BaseEdgeSchema(source="a", target="b", metadata={"custom_feat": 0.7})
        fv = edge.get_feature_vector(feature_names=["custom_feat"])
        assert fv.shape[0] == 5  # 4 base + 1 named

    def test_normalize_embedding_list_input(self):
        """Test that embedding normalizer handles list values."""
        edge = BaseEdgeSchema(source="a", target="b", embedding=[1.0, 2.0, 3.0])
        assert edge.embedding == [1.0, 2.0, 3.0]

    def test_output_schema_as_dict(self):
        """Test that output_schema as dict sets output_schema_json."""
        from core.schema import AgentNodeSchema
        schema_dict = {"type": "object", "properties": {"result": {"type": "string"}}}
        node = AgentNodeSchema(id="n1", output_schema=schema_dict)
        assert node.output_schema_json == schema_dict

    def test_validate_data_unknown_schema_type(self):
        """Test _validate_data when schema is not a pydantic model or dict."""
        from core.schema import AgentNodeSchema
        node = AgentNodeSchema(id="n1")
        # Call _validate_data with a non-standard schema type (string)
        bad_schema = cast("type[BaseModel] | dict | None", "not_a_dict_or_model")
        result = node._validate_data({"key": "value"}, bad_schema, "input")
        assert result.valid is False
        assert "Unknown schema type" in result.errors

    def test_check_type_unknown_returns_true(self):
        """Test _check_type with unknown type returns True."""
        from core.schema import AgentNodeSchema
        node = AgentNodeSchema(id="n1")
        result = node._check_type("any_value", "unknown_type_xyz")
        assert result is True