Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/v3/compiler/src/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{compile_to_dag, CompileError};
pub enum ClaimResult {
Pass,
Fail(String),
NotYetImplemented,
NotYetImplemented(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -103,7 +103,10 @@ impl<'a> TestRunner<'a> {
"OutputEquals" => self.eval_output_equals(claim, &payload),
"PortHasState" => self.eval_port_has_state(claim, &payload),
"CostBounded" => self.eval_cost_bounded(claim, &payload),
_ => ClaimResult::NotYetImplemented,
"MockBackedInvariant" => self.eval_mock_backed_invariant(claim, &payload),
other => ClaimResult::NotYetImplemented(format!(
"TestPredicate::{other} is not wired in the Rust runner yet"
)),
},
None => ClaimResult::Fail("predicate is not a structural variant".to_string()),
}
Expand Down Expand Up @@ -237,6 +240,51 @@ impl<'a> TestRunner<'a> {
}
}

fn eval_mock_backed_invariant(
&self,
_claim: &TestClaimValue,
payload: &[FieldValue],
) -> ClaimResult {
let [subject, invariant] = payload else {
return ClaimResult::Fail(
"MockBackedInvariant payload should be (subject: DeclarationRef, invariant: DeclarationRef)"
.to_string(),
);
};
let subject = match self.resolve_declaration_ref_edge(subject, "subject") {
Ok(name) => name,
Err(reason) => return ClaimResult::Fail(reason),
};
let invariant = match self.resolve_declaration_ref_edge(invariant, "invariant") {
Ok(name) => name,
Err(reason) => return ClaimResult::Fail(reason),
};
ClaimResult::NotYetImplemented(format!(
"MockBackedInvariant resolved subject `{subject}` and invariant `{invariant}`, but service simulation is not wired in the Rust runner yet"
))
}

fn resolve_declaration_ref_edge(
&self,
value: &FieldValue,
label: &str,
) -> Result<String, String> {
match value {
FieldValue::Reference(id) => Ok(self
.dag
.declaration(*id)
.name
.clone()
.unwrap_or_else(|| format!("Declaration#{}", id.raw()))),
FieldValue::Record(fields) if fields.is_empty() => Err(format!(
"MockBackedInvariant `{label}` must be a DeclarationRef edge, got empty record literal"
)),
other => Err(format!(
"MockBackedInvariant `{label}` must be a DeclarationRef edge, got {other:?}"
)),
}
}

fn diagnostic_matches(&self, actual_dag: &Dag, reference: &FieldValue) -> Result<bool, String> {
let reference = self.diagnostic_reference(reference)?;
Ok(actual_dag
Expand Down
29 changes: 29 additions & 0 deletions src/v3/compiler/tests/fixtures/r1_mock_backed_invariant_gate.dag
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Minimal MockBackedInvariant gate fixture.
// Lives under tests/fixtures/ -- NOT src/v3/std/ -- not auto-staged into bootstrap.
//
// **requires: [] is intentional.** A real MockBackedInvariant claim puts its mock
// transport on TestClaim.requires. The runner's requires gate (run_claim) currently
// fails-closed on non-empty requires -- so this fixture uses requires: [] to reach
// eval_mock_backed_invariant. When service simulation lands, both gates wire together.

module std.r1_mock_backed_invariant_gate

import std.verification { TestClaim, TestSuite }

data mock_subject_ref: Int = 0
data mock_invariant_ref: Int = 0

data mock_backed_invariant_gate: TestClaim = {
name: "testgen_mock_backed_integration_safe",
source: "let _: Int = 0\n",
file_name: "mock_backed_placeholder.v3",
// Struct-variant field names are schema metadata today; the surface parser
// accepts positional payload syntax for authored values.
predicate: MockBackedInvariant(mock_subject_ref, mock_invariant_ref),
requires: []
}

data mock_backed_invariant_suite: TestSuite = {
name: "mock_backed_invariant_suite",
claims: [mock_backed_invariant_gate]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use v3_compiler::dag::{Dag, FieldValue, LiteralBits, ValueBody};
use v3_compiler::CompileError;

const R1_GATES_SOURCE: &str = include_str!("../fixtures/r1_gates.dag");
const R1_MOCK_BACKED_INVARIANT_GATE_SOURCE: &str =
include_str!("../fixtures/r1_mock_backed_invariant_gate.dag");
const ON_DISK_LENS: &str = include_str!("../../../lenses/named_function_count.dag");

fn testclaim_string_field(dag: &Dag, gate_name: &str, label: &str) -> String {
Expand Down Expand Up @@ -64,6 +66,15 @@ fn r1_gates_fixture_compiles_against_bootstrap_context() {
);
}

#[test]
fn r1_mock_backed_invariant_gate_fixture_compiles_against_bootstrap_context() {
assert_compile_clean(
R1_MOCK_BACKED_INVARIANT_GATE_SOURCE,
"src/v3/compiler/tests/fixtures/r1_mock_backed_invariant_gate.dag",
"gate fixture `r1_mock_backed_invariant_gate.dag`",
);
}

#[test]
fn user_authored_lens_testclaim_payload_tracks_on_disk_lens_and_compiles() {
let gate_dag = match compile_to_dag(
Expand Down
42 changes: 41 additions & 1 deletion src/v3/compiler/tests/integration/test_runner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use v3_compiler::test_runner::TestClaimValue;
use v3_compiler::test_runner::{ClaimResult, TestRunner};
use v3_compiler::{compile_to_dag, CompileError};

const MOCK_BACKED_INVARIANT_FIXTURE: &str =
include_str!("../fixtures/r1_mock_backed_invariant_gate.dag");

fn compile_clean(source: &str, file: &str) -> v3_compiler::dag::Dag {
match compile_to_dag(source, file) {
Ok(dag) => dag,
Expand Down Expand Up @@ -299,7 +302,44 @@ data suite: TestSuite = {
let results = TestRunner::new(&dag).run_suite("suite");

assert_eq!(results.len(), 1);
assert_eq!(results[0].result, ClaimResult::NotYetImplemented);
assert!(matches!(
&results[0].result,
ClaimResult::NotYetImplemented(reason)
if reason.contains("MockBackedInvariant")
&& reason.contains("service simulation is not wired")
));
}

#[test]
fn mock_backed_invariant_predicate_accepts_declaration_ref_like_lens_output_equals() {
let source = r#"
data claim: TestClaim = {
name: "c",
source: "let _: Int = 0",
file_name: "f.v3",
predicate: MockBackedInvariant(Int, Int),
requires: []
}
"#;
compile_clean(source, "mock_backed_invariant_harness.v3");
}

#[test]
fn test_runner_dispatches_mock_backed_invariant_claim() {
let dag = compile_clean(
MOCK_BACKED_INVARIANT_FIXTURE,
"src/v3/compiler/tests/fixtures/r1_mock_backed_invariant_gate.dag",
);
let results = TestRunner::new(&dag).run_suite("mock_backed_invariant_suite");

assert_eq!(results.len(), 1);
assert!(matches!(
&results[0].result,
ClaimResult::NotYetImplemented(reason)
if reason.contains("mock_subject_ref")
&& reason.contains("mock_invariant_ref")
&& reason.contains("service simulation is not wired")
));
}

#[test]
Expand Down
Loading