tests/test_artifact.py
| 1 | """Tests for artifact data structures.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pqc_enclave_sdk import ( |
| 6 | ArtifactKind, |
| 7 | ArtifactMetadata, |
| 8 | EnclaveArtifact, |
| 9 | EncryptedArtifact, |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | def test_artifact_metadata_to_dict_roundtrip() -> None: |
| 14 | meta = ArtifactMetadata( |
| 15 | artifact_id="urn:pqc-enclave-art:abc", |
| 16 | name="llama-3", |
| 17 | kind=ArtifactKind.MODEL_WEIGHTS, |
| 18 | version="1.0", |
| 19 | app_bundle_id="com.example.llm", |
| 20 | size_bytes=1024, |
| 21 | created_at="2026-01-01T00:00:00+00:00", |
| 22 | device_id="iphone-1", |
| 23 | tags=("prod", "int4"), |
| 24 | ) |
| 25 | d = meta.to_dict() |
| 26 | assert d["kind"] == "model-weights" |
| 27 | assert d["tags"] == ["prod", "int4"] |
| 28 | assert d["name"] == "llama-3" |
| 29 | assert d["size_bytes"] == 1024 |
| 30 | |
| 31 | |
| 32 | def test_encrypted_artifact_from_to_dict_roundtrip() -> None: |
| 33 | meta = ArtifactMetadata( |
| 34 | artifact_id="urn:pqc-enclave-art:xyz", |
| 35 | name="tokenizer", |
| 36 | kind=ArtifactKind.TOKENIZER, |
| 37 | ) |
| 38 | enc = EncryptedArtifact( |
| 39 | metadata=meta, |
| 40 | nonce="00" * 12, |
| 41 | ciphertext="deadbeef", |
| 42 | content_hash="cafebabe", |
| 43 | key_id="urn:pqc-enclave-key:1", |
| 44 | ) |
| 45 | d = enc.to_dict() |
| 46 | rebuilt = EncryptedArtifact.from_dict(d) |
| 47 | assert rebuilt.metadata.name == "tokenizer" |
| 48 | assert rebuilt.metadata.kind == ArtifactKind.TOKENIZER |
| 49 | assert rebuilt.nonce == enc.nonce |
| 50 | assert rebuilt.ciphertext == enc.ciphertext |
| 51 | assert rebuilt.content_hash == enc.content_hash |
| 52 | assert rebuilt.key_id == enc.key_id |
| 53 | |
| 54 | |
| 55 | def test_enclave_artifact_sha3_256_hex_is_deterministic() -> None: |
| 56 | meta = ArtifactMetadata( |
| 57 | artifact_id="id", |
| 58 | name="x", |
| 59 | kind=ArtifactKind.OTHER, |
| 60 | ) |
| 61 | a = EnclaveArtifact(metadata=meta, content=b"hello world") |
| 62 | b = EnclaveArtifact(metadata=meta, content=b"hello world") |
| 63 | assert a.sha3_256_hex() == b.sha3_256_hex() |
| 64 | assert len(a.sha3_256_hex()) == 64 |
| 65 | |
| 66 | |
| 67 | def test_artifact_kind_enum_round_trips() -> None: |
| 68 | for kind in ArtifactKind: |
| 69 | assert ArtifactKind(kind.value) == kind |
| 70 | assert ArtifactKind("model-weights") == ArtifactKind.MODEL_WEIGHTS |
| 71 | assert ArtifactKind.CREDENTIAL.value == "credential" |
| 72 | |