uor-foundation 0.4.13

UOR Foundation — typed Rust traits for the complete ontology. Import and implement.
Documentation
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
//! Behavioral contract for every builder's `validate_const` rejection path.
//!
//! Target §3: each of 9 builders validates against a specific
//! `conformance:*Shape`. Missing any required field must produce a
//! `ShapeViolation` whose `property_iri` exactly matches the ontology's
//! property IRI for that field, and whose `kind` is
//! `ViolationKind::Missing`.
//!
//! A regression where a builder accepts an empty or missing-field input
//! would let invalid declarations through the const-evidence pattern
//! downstream. This test pins the exact property_iri strings so any drift
//! is caught.

use uor_foundation::enforcement::{
    CompileUnitBuilder, ConstrainedTypeInput, DispatchDeclarationBuilder, EffectDeclarationBuilder,
    InteractionDeclarationBuilder, LeaseDeclarationBuilder, ParallelDeclarationBuilder,
    PredicateDeclarationBuilder, ShapeViolation, StreamDeclarationBuilder, Term,
    WittLevelDeclarationBuilder,
};
use uor_foundation::enums::ViolationKind;
use uor_foundation::{VerificationDomain, WittLevel};

static SENTINEL_TERMS: &[Term] = &[uor_foundation::pipeline::literal_u64(1, WittLevel::W8)];
static SENTINEL_DOMAINS: &[VerificationDomain] = &[VerificationDomain::Enumerative];

fn assert_missing(err: ShapeViolation, expected_property_iri: &str) {
    assert_eq!(
        err.kind,
        ViolationKind::Missing,
        "expected ViolationKind::Missing for property {expected_property_iri}, got {:?}",
        err.kind
    );
    assert_eq!(
        err.property_iri, expected_property_iri,
        "expected property_iri `{expected_property_iri}`, got `{}`",
        err.property_iri
    );
}

// ─── CompileUnitBuilder (5 required fields) ──────────────────────────────

#[test]
fn compile_unit_rejects_missing_root_term() {
    // Use the pipeline's const validator (the only const path for CompileUnit).
    let builder = CompileUnitBuilder::new()
        .witt_level_ceiling(WittLevel::W8)
        .thermodynamic_budget(100)
        .target_domains(SENTINEL_DOMAINS)
        .result_type::<ConstrainedTypeInput>();
    let err = uor_foundation::pipeline::validate_compile_unit_const(&builder)
        .expect_err("missing root_term must be rejected");
    assert_missing(err, "https://uor.foundation/reduction/rootTerm");
}

#[test]
fn compile_unit_rejects_missing_witt_level() {
    let builder = CompileUnitBuilder::new()
        .root_term(SENTINEL_TERMS)
        .thermodynamic_budget(100)
        .target_domains(SENTINEL_DOMAINS)
        .result_type::<ConstrainedTypeInput>();
    let err = uor_foundation::pipeline::validate_compile_unit_const(&builder)
        .expect_err("missing witt_level_ceiling must be rejected");
    assert_missing(err, "https://uor.foundation/reduction/unitWittLevel");
}

#[test]
fn compile_unit_rejects_missing_thermodynamic_budget() {
    let builder = CompileUnitBuilder::new()
        .root_term(SENTINEL_TERMS)
        .witt_level_ceiling(WittLevel::W8)
        .target_domains(SENTINEL_DOMAINS)
        .result_type::<ConstrainedTypeInput>();
    let err = uor_foundation::pipeline::validate_compile_unit_const(&builder)
        .expect_err("missing thermodynamic_budget must be rejected");
    assert_missing(err, "https://uor.foundation/reduction/thermodynamicBudget");
}

#[test]
fn compile_unit_rejects_missing_target_domains() {
    let builder = CompileUnitBuilder::new()
        .root_term(SENTINEL_TERMS)
        .witt_level_ceiling(WittLevel::W8)
        .thermodynamic_budget(100)
        .result_type::<ConstrainedTypeInput>();
    let err = uor_foundation::pipeline::validate_compile_unit_const(&builder)
        .expect_err("missing target_domains must be rejected");
    assert_missing(err, "https://uor.foundation/reduction/targetDomains");
}

#[test]
fn compile_unit_rejects_missing_result_type() {
    let builder = CompileUnitBuilder::new()
        .root_term(SENTINEL_TERMS)
        .witt_level_ceiling(WittLevel::W8)
        .thermodynamic_budget(100)
        .target_domains(SENTINEL_DOMAINS);
    let err = uor_foundation::pipeline::validate_compile_unit_const(&builder)
        .expect_err("missing result_type must be rejected");
    assert_missing(err, "https://uor.foundation/reduction/resultType");
}

// ─── EffectDeclarationBuilder (4 required fields) ────────────────────────

#[test]
fn effect_rejects_missing_name() {
    let builder = EffectDeclarationBuilder::new()
        .target_sites(&[0u32])
        .budget_delta(-1)
        .commutes(true);
    let err = builder
        .validate_const()
        .expect_err("missing name must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/name");
}

#[test]
fn effect_rejects_missing_target_sites() {
    let builder = EffectDeclarationBuilder::new()
        .name("e")
        .budget_delta(-1)
        .commutes(true);
    let err = builder
        .validate_const()
        .expect_err("missing target_sites must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/target_sites");
}

#[test]
fn effect_rejects_missing_budget_delta() {
    let builder = EffectDeclarationBuilder::new()
        .name("e")
        .target_sites(&[0u32])
        .commutes(true);
    let err = builder
        .validate_const()
        .expect_err("missing budget_delta must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/budget_delta");
}

#[test]
fn effect_rejects_missing_commutes() {
    let builder = EffectDeclarationBuilder::new()
        .name("e")
        .target_sites(&[0u32])
        .budget_delta(-1);
    let err = builder
        .validate_const()
        .expect_err("missing commutes must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/commutes");
}

// ─── DispatchDeclarationBuilder (3 required fields) ──────────────────────

#[test]
fn dispatch_rejects_missing_predicate() {
    let builder = DispatchDeclarationBuilder::new()
        .target_resolver("https://uor.foundation/resolver/TwoSatDecider")
        .priority(0);
    let err = builder
        .validate_const()
        .expect_err("missing predicate must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/predicate");
}

#[test]
fn dispatch_rejects_missing_target_resolver() {
    let builder = DispatchDeclarationBuilder::new()
        .predicate(SENTINEL_TERMS)
        .priority(0);
    let err = builder
        .validate_const()
        .expect_err("missing target_resolver must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/target_resolver");
}

#[test]
fn dispatch_rejects_missing_priority() {
    let builder = DispatchDeclarationBuilder::new()
        .predicate(SENTINEL_TERMS)
        .target_resolver("https://uor.foundation/resolver/TwoSatDecider");
    let err = builder
        .validate_const()
        .expect_err("missing priority must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/priority");
}

// ─── PredicateDeclarationBuilder (3 required fields) ─────────────────────

#[test]
fn predicate_rejects_missing_input_type() {
    let builder = PredicateDeclarationBuilder::new()
        .evaluator(SENTINEL_TERMS)
        .termination_witness("https://uor.foundation/proof/Primitive");
    let err = builder
        .validate_const()
        .expect_err("missing input_type must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/input_type");
}

#[test]
fn predicate_rejects_missing_evaluator() {
    let builder = PredicateDeclarationBuilder::new()
        .input_type("https://uor.foundation/type/ConstrainedType")
        .termination_witness("https://uor.foundation/proof/Primitive");
    let err = builder
        .validate_const()
        .expect_err("missing evaluator must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/evaluator");
}

#[test]
fn predicate_rejects_missing_termination_witness() {
    let builder = PredicateDeclarationBuilder::new()
        .input_type("https://uor.foundation/type/ConstrainedType")
        .evaluator(SENTINEL_TERMS);
    let err = builder
        .validate_const()
        .expect_err("missing termination_witness must be rejected");
    assert_missing(
        err,
        "https://uor.foundation/conformance/termination_witness",
    );
}

// ─── ParallelDeclarationBuilder (2 required fields) ──────────────────────

#[test]
fn parallel_rejects_missing_site_partition() {
    let builder =
        ParallelDeclarationBuilder::new().disjointness_witness("https://uor.foundation/proof/X");
    let err = builder
        .validate_const()
        .expect_err("missing site_partition must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/site_partition");
}

#[test]
fn parallel_rejects_missing_disjointness_witness() {
    let builder = ParallelDeclarationBuilder::new().site_partition(&[0u32, 1u32]);
    let err = builder
        .validate_const()
        .expect_err("missing disjointness_witness must be rejected");
    assert_missing(
        err,
        "https://uor.foundation/conformance/disjointness_witness",
    );
}

// ─── StreamDeclarationBuilder (3 required fields) ────────────────────────

#[test]
fn stream_rejects_missing_seed() {
    let builder = StreamDeclarationBuilder::new()
        .step(SENTINEL_TERMS)
        .productivity_witness("https://uor.foundation/proof/W");
    let err = builder
        .validate_const()
        .expect_err("missing seed must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/seed");
}

#[test]
fn stream_rejects_missing_step() {
    let builder = StreamDeclarationBuilder::new()
        .seed(SENTINEL_TERMS)
        .productivity_witness("https://uor.foundation/proof/W");
    let err = builder
        .validate_const()
        .expect_err("missing step must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/step");
}

#[test]
fn stream_rejects_missing_productivity_witness() {
    let builder = StreamDeclarationBuilder::new()
        .seed(SENTINEL_TERMS)
        .step(SENTINEL_TERMS);
    let err = builder
        .validate_const()
        .expect_err("missing productivity_witness must be rejected");
    assert_missing(
        err,
        "https://uor.foundation/conformance/productivity_witness",
    );
}

// ─── LeaseDeclarationBuilder (2 required fields) ─────────────────────────

#[test]
fn lease_rejects_missing_linear_site() {
    let builder = LeaseDeclarationBuilder::new().scope("scope-name");
    let err = builder
        .validate_const()
        .expect_err("missing linear_site must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/linear_site");
}

#[test]
fn lease_rejects_missing_scope() {
    let builder = LeaseDeclarationBuilder::new().linear_site(0u32);
    let err = builder
        .validate_const()
        .expect_err("missing scope must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/scope");
}

// ─── WittLevelDeclarationBuilder (2 required fields) ────────────────────

#[test]
fn witt_level_rejects_missing_bit_width() {
    let builder = WittLevelDeclarationBuilder::new().predecessor(WittLevel::W8);
    let err = builder
        .validate_const()
        .expect_err("missing bit_width must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/declaredBitWidth");
}

#[test]
fn witt_level_rejects_missing_predecessor() {
    let builder = WittLevelDeclarationBuilder::new().bit_width(8);
    let err = builder
        .validate_const()
        .expect_err("missing predecessor must be rejected");
    assert_missing(err, "https://uor.foundation/conformance/predecessorLevel");
}

// ─── InteractionDeclarationBuilder (3 required fields) ───────────────────

#[test]
fn interaction_rejects_missing_peer_protocol() {
    let builder = InteractionDeclarationBuilder::new()
        .convergence_predicate(0x2u128)
        .commutator_state_class(0x3u128);
    let err = builder
        .validate_const()
        .expect_err("missing peer_protocol must be rejected");
    assert_missing(err, "https://uor.foundation/interaction/peerProtocol");
}

#[test]
fn interaction_rejects_missing_convergence_predicate() {
    let builder = InteractionDeclarationBuilder::new()
        .peer_protocol(0x1u128)
        .commutator_state_class(0x3u128);
    let err = builder
        .validate_const()
        .expect_err("missing convergence_predicate must be rejected");
    assert_missing(
        err,
        "https://uor.foundation/interaction/convergencePredicate",
    );
}

#[test]
fn interaction_rejects_missing_commutator_state_class() {
    let builder = InteractionDeclarationBuilder::new()
        .peer_protocol(0x1u128)
        .convergence_predicate(0x2u128);
    let err = builder
        .validate_const()
        .expect_err("missing commutator_state_class must be rejected");
    assert_missing(
        err,
        "https://uor.foundation/interaction/commutatorStateClass",
    );
}

// ─── Fully-specified builders must accept ────────────────────────────────

#[test]
fn fully_specified_builders_all_validate() {
    // Smoke test: with every required field set, each builder validates.
    let _ = uor_foundation::pipeline::validate_compile_unit_const(
        &CompileUnitBuilder::new()
            .root_term(SENTINEL_TERMS)
            .witt_level_ceiling(WittLevel::W8)
            .thermodynamic_budget(100)
            .target_domains(SENTINEL_DOMAINS)
            .result_type::<ConstrainedTypeInput>(),
    )
    .expect("fully-specified CompileUnit must validate");

    let _ = EffectDeclarationBuilder::new()
        .name("e")
        .target_sites(&[0u32])
        .budget_delta(0)
        .commutes(false)
        .validate_const()
        .expect("fully-specified Effect must validate");

    let _ = DispatchDeclarationBuilder::new()
        .predicate(SENTINEL_TERMS)
        .target_resolver("https://uor.foundation/resolver/TwoSatDecider")
        .priority(0)
        .validate_const()
        .expect("fully-specified Dispatch must validate");

    let _ = PredicateDeclarationBuilder::new()
        .input_type("https://uor.foundation/type/ConstrainedType")
        .evaluator(SENTINEL_TERMS)
        .termination_witness("https://uor.foundation/proof/Primitive")
        .validate_const()
        .expect("fully-specified Predicate must validate");

    let _ = ParallelDeclarationBuilder::new()
        .site_partition(&[0u32])
        .disjointness_witness("https://uor.foundation/proof/D")
        .validate_const()
        .expect("fully-specified Parallel must validate");

    let _ = StreamDeclarationBuilder::new()
        .seed(SENTINEL_TERMS)
        .step(SENTINEL_TERMS)
        .productivity_witness("https://uor.foundation/proof/W")
        .validate_const()
        .expect("fully-specified Stream must validate");

    let _ = LeaseDeclarationBuilder::new()
        .linear_site(0u32)
        .scope("s")
        .validate_const()
        .expect("fully-specified Lease must validate");

    let _ = WittLevelDeclarationBuilder::new()
        .bit_width(8)
        .predecessor(WittLevel::W8)
        .validate_const()
        .expect("fully-specified WittLevel must validate");

    let _ = InteractionDeclarationBuilder::new()
        .peer_protocol(1)
        .convergence_predicate(2)
        .commutator_state_class(3)
        .validate_const()
        .expect("fully-specified Interaction must validate");
}