@@ -53,21 +53,41 @@ def test_compilation_tracer_generates_valid_json(tmp_path: Path) -> None:
5353 with trace_file .open (encoding = "utf-8" ) as f :
5454 trace_data = json .load (f )
5555
56+ # Validate Top-Level Metadata
5657 assert "circuit_name" in trace_data , "Tracer JSON is missing the circuit name."
5758 assert "mdp_policy" in trace_data , "Tracer JSON is missing the mdp policy."
5859 assert "device" in trace_data , "Tracer JSON is missing the device information."
5960 assert "schema_version" in trace_data , "Tracer JSON is missing the schema version."
6061 assert "timestamp" in trace_data , "Tracer JSON is missing the timestamp."
6162 assert "steps" in trace_data , "Tracer JSON is missing the steps array."
63+ assert "total_duration" in trace_data , "Tracer JSON is missing the total_duration."
6264
63- assert len (trace_data ["steps" ]) > 1 , "Tracer should record subsequent compilation steps beyond the Baseline."
64- assert trace_data ["steps" ][0 ]["action_name" ] == "Baseline" , "First step must be Baseline."
6565 assert trace_data ["schema_version" ] == "1.0.0"
66+ assert trace_data ["total_duration" ] >= 0.0 , "Total duration must be non-negative."
67+
68+ # Validate Step Array Length
69+ assert len (trace_data ["steps" ]) > 1 , "Tracer should record subsequent compilation steps beyond the Baseline."
6670
71+ # Validate Baseline (First Step)
72+ first_step = trace_data ["steps" ][0 ]
73+ assert first_step ["action_name" ] == "Baseline" , "First step must be Baseline."
74+ assert first_step ["action_type" ] == "INITIAL" , "First step action_type must be INITIAL."
75+ assert first_step ["action_duration" ] == pytest .approx (0.0 ), "Baseline step duration should be 0.0."
76+
77+ # Validate Terminal Step (Last Step)
6778 last_step_data = trace_data ["steps" ][- 1 ]
6879 assert last_step_data .get ("is_terminal" ) is True , "The final compilation step must be marked as terminal."
80+ assert "action_type" in last_step_data , "Action type is missing from the trace step."
81+ assert "action_duration" in last_step_data , "Action duration is missing from the trace step."
82+ assert last_step_data ["action_duration" ] >= 0.0 , "Action duration must be non-negative."
83+
84+ # Validate that total duration mathematically matches the sum of step durations
85+ calculated_total = sum (step .get ("action_duration" , 0.0 ) for step in trace_data ["steps" ])
86+ assert trace_data ["total_duration" ] == pytest .approx (calculated_total ), (
87+ "total_duration does not equal the sum of step durations."
88+ )
6989
70- # Verify Figures of Merit
90+ # Verify Figures of Merit on the final step
7191 fom_data = last_step_data .get ("figures_of_merit" )
7292 assert fom_data is not None , "Figures of merit dictionary is missing from the trace step."
7393
@@ -86,15 +106,16 @@ def test_compilation_tracer_generates_valid_json(tmp_path: Path) -> None:
86106 assert "value" in hd_metric , "Hellinger distance is missing its float value."
87107 assert "kind" in hd_metric , "Hellinger distance is missing its kind string."
88108
109+ # Semantic Validation via Dataclasses
89110 try :
90111 # Initialize from JSON (throws if the structures don't match)
91112 DeviceMetadata (** trace_data ["device" ])
92113
93114 # Semantically validate both the first and the last steps
94- CompilationStep (** trace_data [ "steps" ][ 0 ] )
115+ CompilationStep (** first_step )
95116 CompilationStep (** last_step_data )
96117
97118 except TypeError as e :
98119 pytest .fail (
99- f"Semantic Validation Failed! The generated JSON does not match your Python dataclasses. Error: { e } "
120+ f"Semantic Validation Failed! The generated JSON does not match the corresponding Python dataclasses. Error: { e } "
100121 )
0 commit comments