harmos-packaging 0.1.13

Harmos packaging: reusable Cargo builds, inert inspection and guest componentization.
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
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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

// Export Generated Python Packages And Consume Them From The Repository
// Interpreter. Prerequisite: `uv sync --locked --project tools/python --group test`
// provisions the root `.venv` with protobuf, build and ty.

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fs;
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::Stdio;

// Third Party Imports

// Local Imports
use crate::fixtures;
use harmos_domain::config::SchemaSettings;
use harmos_domain::operations::{Codec, Contract, Contracts, Grpc, OperationPolicy, RetryPolicy};
use harmos_domain::schema::{
    Cardinality, Described,
    graph::{Field, Kind, Schema},
};
use harmos_domain::{Aggregate, Identifier, QualifiedId};
use harmos_packaging::{
    proto::Artifacts,
    python::{self, Settings},
};
use harmos_sdk::grpc::{self, DecodeInput};

/* === Constants === */

const POLICY: OperationPolicy = OperationPolicy {
    timeout: std::time::Duration::from_secs(1),
    attempt_timeout: std::time::Duration::from_secs(1),
    retries: RetryPolicy::Never,
};

// The Generated Python Surface Of A Request Carrying Aggregates
const AGGREGATE_SNAPSHOT: &str = include_str!("fixtures/aggregate-bindings.pyi.snapshot");

// A Wide Schema Shared With The Domain's Graph Stories
const CHECKPOINT: &str = include_str!("../../../harmos-domain/tests/fixtures/checkpoint-schema.json");

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

#[harmos_domain::message]
struct Payload {
    text: String,
    optional: Option<u32>,
    values: Vec<i64>,
}

#[harmos_domain::operation(
    id = "read",
    response = Payload,
    policy = POLICY,
    grpc(package = "test.v1", service = "Shapes", method = "Read"),
)]
struct ReadOperation {
    text: String,
    optional: Option<u32>,
    values: Vec<i64>,
}

#[harmos_domain::operation(
    id = "exchange",
    response = Payload,
    input = Payload,
    output = Payload,
    policy = POLICY,
    grpc(package = "test.v1", service = "Shapes", method = "Exchange"),
)]
struct ExchangeOperation {
    text: String,
    optional: Option<u32>,
    values: Vec<i64>,
}

#[harmos_domain::operation(
    id = "upload",
    response = Payload,
    input = Payload,
    policy = POLICY,
    grpc(package = "test.v1", service = "Shapes", method = "Upload"),
)]
struct UploadOperation {
    text: String,
    optional: Option<u32>,
    values: Vec<i64>,
}

#[harmos_domain::operation(
    id = "download",
    response = Payload,
    output = Payload,
    policy = POLICY,
    grpc(package = "test.v1", service = "Shapes", method = "Download"),
)]
struct DownloadOperation {
    text: String,
    optional: Option<u32>,
    values: Vec<i64>,
}

#[harmos_domain::message]
#[derive(PartialEq)]
struct Summary {
    aggregate: Aggregate,
    aggregates: Vec<Aggregate>,
}

// Two Producers Exported From One Catalog, Each In Its Own Directory
struct Exported {
    target: tempfile::TempDir,
    alpha: PathBuf,
    beta: PathBuf,
}

/* === Enums === */

#[harmos_domain::catalog]
enum Routes {
    Read(ReadOperation),
    Exchange(ExchangeOperation),
    Upload(UploadOperation),
    Download(DownloadOperation),
}

/* === Traits === */

/* === Implementations === */

/* === Public Functions === */

/* === Private Functions === */

// Four Streaming Shapes Plus A Wide, A Recursive And A Reserved-Name Schema
fn contracts() -> Vec<Contract> {
    let mut contracts = Contracts::of::<Routes>().unwrap();
    let template = contracts[0].clone();
    let checkpoint: Schema = serde_json::from_str(CHECKPOINT).unwrap();
    let recursive = Schema {
        root: 0,
        definitions: vec![vec![Field {
            name: Cow::Borrowed("next"),
            tag: 1,
            cardinality: Cardinality::Optional,
            kind: Kind::Message { definition: 0 },
        }]],
    };
    contracts.push(contract(&template, "fixture_checkpoint", "Checkpoint", checkpoint));
    contracts.push(contract(&template, "fixture_recursive", "Recursive", recursive));
    contracts.push(contract(&template, "fixture_reserved", "Reserved", reserved()));

    contracts
}

fn export() -> Exported {
    let contracts = contracts();
    let artifacts = Artifacts::new(&contracts).unwrap();
    let target = tempfile::tempdir().unwrap();
    let alpha = target.path().join("alpha");
    let beta = target.path().join("beta");
    python::generate(
        python::Catalog {
            contracts: &artifacts.operation_contracts().unwrap(),
            settings: &settings("bindings_alpha"),
            protoc: &protoc(),
        },
        &alpha,
    )
    .unwrap();
    python::generate(
        python::Catalog {
            contracts: &artifacts.operation_contracts().unwrap(),
            settings: &settings("bindings_beta"),
            protoc: &protoc(),
        },
        &beta,
    )
    .unwrap();

    Exported { target, alpha, beta }
}

fn reserved() -> Schema {
    Schema {
        root: 0,
        definitions: vec![
            vec![Field {
                name: Cow::Borrowed("Clear"),
                tag: 7,
                cardinality: Cardinality::Variant,
                kind: Kind::Message { definition: 1 },
            }],
            vec![],
        ],
    }
}

fn settings(module: &str) -> Settings {
    Settings {
        distribution: module.replace('_', "-"),
        module: module.into(),
        version: "1.2.3-rc.4".parse().unwrap(),
        description: "Generated Boundary Fixture".into(),
        license: "Apache-2.0".into(),
        operations: Vec::new(),
    }
}

// Wrap A Direction Schema In The Request And Response Root Of A New Contract
fn contract(template: &Contract, id: &str, method: &'static str, mut schema: Schema) -> Contract {
    let mut operation = template.operation().clone();
    operation.id = Identifier::new(id).unwrap();
    let direction = schema.root;
    let root = ["request", "response"]
        .into_iter()
        .zip(1..)
        .map(|(name, tag)| Field {
            name: Cow::Borrowed(name),
            tag,
            cardinality: Cardinality::Required,
            kind: Kind::Message { definition: direction },
        })
        .collect();
    schema.root = schema.definitions.len();
    schema.definitions.push(root);

    {
        let fingerprint = harmos_domain::schema::identity::contract(id, &schema, "harmos.postcard.v1").unwrap();
        operation.fingerprint = fingerprint;
        Contract::new(operation, fingerprint, Codec::Postcard, schema)
    }
    .unwrap()
    .with_grpc(Some(Grpc::new("test.v1", "Shapes", method)))
    .unwrap()
}

fn protoc() -> PathBuf {
    super::fixtures::protoc()
}

fn files(root: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
    let mut directories = vec![root.to_owned()];
    let mut result = BTreeMap::new();
    while let Some(directory) = directories.pop() {
        for entry in fs::read_dir(directory).unwrap() {
            let entry = entry.unwrap();
            if entry.file_type().unwrap().is_dir() {
                directories.push(entry.path());
                continue;
            }
            let relative = entry.path().strip_prefix(root).unwrap().to_owned();
            result.insert(relative, fs::read(entry.path()).unwrap());
        }
    }

    result
}

// Run One Consumer Scene; `pure` Selects The Pure-Python Protobuf Runtime
fn scene(name: &str, arguments: &[&Path], pure: bool, input: &[u8]) -> Vec<u8> {
    let repository = fixtures::workspace();
    let interpreter = repository.join(".venv").join(if cfg!(windows) { "Scripts/python.exe" } else { "bin/python" });
    assert!(interpreter.is_file(), "{} is missing; run `uv sync --locked --project tools/python --group test`", interpreter.display());
    let mut command = fixtures::command(interpreter);
    command
        .arg(fixtures::manifest().join("tests/acceptance/bindings.py"))
        .arg(name)
        .args(arguments)
        .env_remove("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    if pure {
        command.env("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python");
    }

    let mut child = command.spawn().unwrap();
    child.stdin.take().unwrap().write_all(input).unwrap();
    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "scene {name}:\n{}\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    output.stdout
}

fn both(name: &str, exported: &Exported, input: &[u8]) -> Vec<Vec<u8>> {
    let arguments = [exported.target.path(), &exported.alpha, &exported.beta];

    [false, true].into_iter().map(|pure| scene(name, &arguments, pure, input)).collect()
}

/* === Tests === */

#[test]
fn export_refuses_invalid() {
    let contracts = Contracts::of::<Routes>().unwrap();
    let artifacts = Artifacts::new(&contracts).unwrap();
    let target = tempfile::tempdir().unwrap();
    let output = target.path().join("package");
    let refused = |settings: &Settings| {
        python::generate(
            python::Catalog {
                contracts: &artifacts.operation_contracts().unwrap(),
                settings,
                protoc: &protoc(),
            },
            &output,
        )
    };

    // Selections, Module Names And Versions Python Cannot Carry
    let mut settings = settings("bindings_alpha");
    settings.operations = vec!["missing.operation".into()];
    assert!(matches!(refused(&settings), Err(python::Error::Selection(_))));
    settings.operations = vec![contracts[0].operation().id.to_string(); 2];
    assert!(matches!(refused(&settings), Err(python::Error::Collision(_))));
    settings.operations.clear();
    settings.module = "class".into();
    assert!(matches!(refused(&settings), Err(python::Error::Settings(_))));
    settings.module = "bindings_alpha".into();
    settings.version = "1.0.0-preview.1".parse().unwrap();
    assert!(matches!(refused(&settings), Err(python::Error::Settings(_))));
    settings.version = "1.0.0".parse().unwrap();

    // Contracts Without gRPC Routes Or Whose Escaped Names Collide
    let unsupported = contracts[0].clone().with_grpc(None).unwrap();
    let unsupported = Artifacts::new([&unsupported]).unwrap();
    assert!(matches!(
        python::generate(
            python::Catalog {
                contracts: &unsupported.operation_contracts().unwrap(),
                settings: &settings,
                protoc: &protoc()
            },
            &output
        ),
        Err(python::Error::Unsupported(_))
    ));
    let mut collision = reserved();
    collision.definitions[0].push(Field {
        name: Cow::Borrowed("Clear_"),
        tag: 8,
        cardinality: Cardinality::Variant,
        kind: Kind::Message { definition: 1 },
    });
    let collision = contract(&contracts[0], "fixture_collision", "Collision", collision);
    assert!(Artifacts::new([&collision]).is_err());
    assert!(!output.exists());

    // An Existing Directory Is Never Overwritten
    fs::create_dir(&output).unwrap();
    fs::write(output.join("owned.txt"), "preserved").unwrap();
    assert!(matches!(refused(&settings), Err(python::Error::Exists(_))));
    assert_eq!(fs::read_to_string(output.join("owned.txt")).unwrap(), "preserved");
}

#[test]
fn export_is_deterministic() {
    let artifacts = Artifacts::new(&contracts()).unwrap();
    let target = tempfile::tempdir().unwrap();
    let first = target.path().join("first");
    let repeat = target.path().join("repeat");
    python::generate(
        python::Catalog {
            contracts: &artifacts.operation_contracts().unwrap(),
            settings: &settings("bindings_alpha"),
            protoc: &protoc(),
        },
        &first,
    )
    .unwrap();
    python::generate(
        python::Catalog {
            contracts: &artifacts.operation_contracts().unwrap(),
            settings: &settings("bindings_alpha"),
            protoc: &protoc(),
        },
        &repeat,
    )
    .unwrap();

    assert_eq!(files(&first), files(&repeat));
}

#[test]
fn python_reads_rust() {
    // Rust Encodes, Python Decodes And Re-Encodes, Rust Decodes The Same Value
    let exported = export();
    let value = Payload {
        text: "line\n∞".into(),
        optional: Some(u32::MAX),
        values: vec![i64::MIN, -1, 0, i64::MAX],
    };
    let encoded = {
        let mut bytes = Vec::new();
        grpc::encode(&value, &mut bytes).unwrap();
        bytes
    };

    for echoed in both("wire", &exported, &encoded) {
        let decoded = grpc::decode::<Payload>(DecodeInput::new(&echoed[..])).unwrap();
        assert_eq!(decoded.text, value.text);
        assert_eq!(decoded.optional, value.optional);
        assert_eq!(decoded.values, value.values);
    }
}

#[test]
fn descriptors_mirror_contracts() {
    let exported = export();

    both("descriptors", &exported, &[]);
}

#[test]
fn producers_stay_isolated() {
    let exported = export();
    let arguments = [exported.target.path(), &exported.alpha, &exported.beta];

    scene("isolation", &arguments, false, &[]);
}

#[test]
fn stubs_type_check() {
    let exported = export();
    let arguments = [exported.target.path(), &exported.alpha, &exported.beta];

    scene("typing", &arguments, false, &[]);
}

#[test]
fn packages_install_offline() {
    let exported = export();
    let arguments = [exported.target.path(), &exported.alpha, &exported.beta];

    scene("install", &arguments, false, &[]);
}

#[test]
fn dynamic_projects_contracts() {
    let exported = export();

    both("dynamic", &exported, &[]);
}

#[test]
fn identity_parity() {
    // identity::dotted Defines Operation Segments; QualifiedId Requires An Owner.
    // Rust Has No Length Ceiling; Python Retains Its Existing 256-Byte Bound.
    let cases = [
        ("a".to_owned(), true),
        ("read_2".to_owned(), true),
        ("a0.b_2".to_owned(), true),
        ("dokime.appearance.get_appearance".to_owned(), true),
        ("host.storage.read_file".to_owned(), true),
        ("a".repeat(255), true),
        ("a".repeat(256), true),
        ("a".repeat(257), true),
        (format!("host.{}", "a".repeat(251)), true),
        (format!("host.{}", "a".repeat(252)), true),
        ("".to_owned(), false),
        (".read".to_owned(), false),
        ("host.".to_owned(), false),
        ("host..read".to_owned(), false),
        ("Host.read".to_owned(), false),
        ("host.Read".to_owned(), false),
        ("2host.read".to_owned(), false),
        ("host.2read".to_owned(), false),
        ("host.read.2file".to_owned(), false),
        ("_host.read".to_owned(), false),
        ("host._read".to_owned(), false),
        ("host-name.read".to_owned(), false),
        ("host.read-file".to_owned(), false),
        ("host/read".to_owned(), false),
        ("host read".to_owned(), false),
        ("host.é".to_owned(), false),
        ("höst.read".to_owned(), false),
        ("host.read\n".to_owned(), false),
        ("host.read\0".to_owned(), false),
    ];
    let template = Contracts::of::<Routes>().unwrap()[0].operation().clone();
    for (value, valid) in &cases {
        let parsed = Identifier::new(value.clone()).is_ok_and(|id| {
            let mut operation = template.clone();
            operation.id = id;
            operation.validate().is_ok()
        });
        assert_eq!(parsed, *valid, "operation {value:?}");
        assert_eq!(QualifiedId::parse(value).is_ok(), *valid && value.contains('.'), "qualified {value:?}");
    }

    let exported = export();
    both("identities", &exported, &serde_json::to_vec(&cases).unwrap());
}

#[test]
fn owner_parity() {
    let cases: Vec<(String, bool)> =
        serde_json::from_str(include_str!("../../../harmos-domain/tests/fixtures/owner-identities.json")).unwrap();
    for (owner, valid) in &cases {
        assert_eq!(harmos_domain::identity::segment(owner), *valid, "{owner:?}");
    }
    let exported = export();
    both("owners", &exported, &serde_json::to_vec(&cases).unwrap());
}

#[test]
fn dynamic_refuses_graphs() {
    let exported = export();
    let arguments = [exported.target.path(), &exported.alpha, &exported.beta];

    scene("refusals", &arguments, false, &[]);
}

#[test]
fn aggregate_members_typed() {
    // The Generated Surface Of A Request Carrying Aggregates Is Stable
    let template = Contracts::of::<Routes>().unwrap()[0].clone();
    let schema = Schema::from_declaration(Summary::DEFINITION, &SchemaSettings::default()).unwrap();
    let summary = contract(&template, "fixture_aggregate", "Aggregate", schema);
    let artifacts = Artifacts::new([&summary]).unwrap();
    let target = tempfile::tempdir().unwrap();
    let package = target.path().join("package");
    python::generate(
        python::Catalog {
            contracts: &artifacts.operation_contracts().unwrap(),
            settings: &settings("bindings_aggregate"),
            protoc: &protoc(),
        },
        &package,
    )
    .unwrap();
    let module = package.join("src/bindings_aggregate");
    let messages = module.join("_wire/test/v1/ShapesMessages/Aggregate/messages_pb2.pyi");
    let generated = format!("{}\n{}", fs::read_to_string(module.join("__init__.pyi")).unwrap(), fs::read_to_string(messages).unwrap());
    assert_eq!(generated, AGGREGATE_SNAPSHOT, "{generated}");

    // Python Selects The Percentile Member And Its Rank As Typed Fields
    let value = Summary {
        aggregate: Aggregate::Percentile(95),
        aggregates: vec![Aggregate::Count, Aggregate::Percentile(99)],
    };
    let source = package.join("src");
    let output = scene("aggregate", &[&source], false, &{
        let mut bytes = Vec::new();
        grpc::encode(&value, &mut bytes).unwrap();
        bytes
    });
    assert!(grpc::decode::<Summary>(DecodeInput::new(&output[..])).unwrap() == value);
}