Break the alknet-typedef architecture specs into atomic, dependency-ordered implementation tasks covering crate init, error types, schema layer, data access, both layout modes (aligned static + packed sequential), TUnion dispatch, jsonschema custom keyword validators, TypedefEngine integration, comprehensive tests, and a final review checkpoint. Validated: 12 tasks, 0 cycles, 7 parallel generations.
9.2 KiB
id, name, status, depends_on, scope, risk, impact, level
| id | name | status | depends_on | scope | risk | impact | level | |
|---|---|---|---|---|---|---|---|---|
| typedef/tests | Write comprehensive tests for alknet-typedef: unit tests, integration tests, and POC round-trip tests | pending |
|
moderate | low | component | implementation |
Description
Write comprehensive tests for the alknet-typedef crate. Tests should cover all
17 TypeDef:* kinds, both layout modes, endianness, TUnion dispatch, validation,
and error paths. Include the POC-verified round-trip tests that validate
byte-identical output against known-good serialization.
Test categories
1. Schema layer tests (tests/schema_tests.rs or #[cfg(test)] mod tests in schema.rs)
get_typedef_kind()correctly identifies all 17 kindstype_size()returns correct sizes for all fixed-size typestype_size()returnsNonefor variable-length typesnatural_alignment()returns correct alignmentEndian::from_schema()defaults to Little, parses "big" correctlyparse_encoding()handlestrue,{ "encoding": "length-prefixed" },{ "encoding": "offset-indirect" }parse_align()returnsNonewhen absent, correct value when presentparse_max_length()returnsNonewhen absent, correct value when presentparse_discriminator()handles byte-offset and field-name discriminatorsnormalize_refs()rewrites bare-name refs, leaves full paths unchanged, handles nested objects
2. Data access tests (tests/data_access_tests.rs)
- Fixed-size read/write round-trip for all types:
i8,i16,i32,u8,u16,u32,u64,f32,f64,bool,enum - Endianness: little-endian and big-endian produce correct byte order
read_bool:0x00→ false,0x01→ true, other values → errorread_string: correct length-prefixed read, UTF-8 validation, buffer-too-short errorread_bytes: correct length-prefixed read, buffer-too-short errorwrite_string/write_bytes: correct length prefix + data written, returns bytes writtenread_string_indirect/read_bytes_indirect: correct offset-indirect read- Buffer bounds checking: all functions return
TypedefError::Accessfor too-short buffers - Field path in error messages is correct
3. OffsetMap tests (aligned static mode) (tests/offset_map_tests.rs)
- Simple struct:
{ u8, u32, f32 }→ correct offsets with natural alignment (0, 4, 8) - Nested struct:
{ header: { version: u32, magic: u32 }, payload: bytes }→ correct dotted paths TArrayof fixed-size elements: correct stride and element offsetsTArraywith fixed count (minItems == maxItems): correct total sizeTArraywith variable count: length-prefixed encoding- Variable-length types (inline length-prefixing): 4-byte length prefix at known offset
- Variable-length types (
maxLength): reserved bytes at fixed offset - Variable-length types (offset-indirect): 8-byte
{offset, length}struct TUnionwith byte-offset discriminator: discriminator at offset, variant at offset + disc_sizeTUnionwith field-name discriminator: discriminator is a regular field- Struct-level
"align"annotation: rounds up total size - Field-level
"align"annotation: overrides struct default OffsetMap::get()returns correctByteRangefor dotted pathsOffsetMap::total_size()is correctOffsetMap::iter()returns all fields
4. LayoutBuilder tests (packed sequential mode, write-side) (tests/layout_builder_tests.rs)
- Simple struct:
{ u8, u32, string }→ packed offsets (0, 1, 5) with no padding - Variable-length fields shift subsequent fields based on actual sizes
- Nested struct: correct dotted paths with packed offsets
TArrayof fixed-size elements: correct stride (element size, no padding)TUnionwith byte-offset discriminator: correct discriminator and variant positions- Missing variable-length field size →
TypedefError::Offset
5. SequentialReader tests (packed sequential mode, read-side) (tests/sequential_reader_tests.rs)
- Read fields sequentially: correct values and position advancement
- Variable-length fields: reads length prefix, skips data, advances correctly
read_next()returnsNonewhen all fields readread_field()walks through preceding fieldsreset()resets position- Nested struct: returns
FieldValue::Struct { start, end } TUnion: returnsFieldValue::Union { ... }TArray: returnsFieldValue::Array { count, element_start, element_stride }- Endianness respected for multi-byte reads
6. TUnion dispatch tests (tests/tunion_tests.rs)
- Byte-offset discriminator: reads u8 at offset 0, returns correct key and variant offset
- Byte-offset discriminator with u16 and u32 types
- Field-name discriminator: reads string field, returns correct key
- Field-name discriminator with u8 and enum field types
resolve_variant(): resolves$refpointers, returns error for unknown keysdiscriminator_size(): correct for each discriminator type
7. Validation tests (tests/validation_tests.rs)
build_validator()returns a working validator- Each
TypeDef:*kind's validator rejects invalid values Float32Validator: rejects NaN, InfinityInt8Validator: rejects 128, -129Uint8Validator: rejects -1, 256StringValidator: rejects non-string, respectsmaxLengthTimestampValidator: rejects non-RFC 3339 stringsStructValidator: validates nested fieldsUnionValidator: validates discriminator membershipArrayValidator: validates element types and length bounds
8. TypedefEngine integration tests (tests/engine_tests.rs)
compile()in aligned mode produces a working enginecompile()in packed mode produces a working enginecompile()normalizes$refvaluescompile()returns error for invalid schemasendian()andmode()accessorsoffset_map(),layout_builder(),sequential_reader()accessorsvalidate_json()andis_valid_json()work correctlyread_field()andwrite_field()in aligned mode
9. POC round-trip tests (tests/poc_roundtrip_tests.rs)
Replicate the key POC tests from /workspace/alknet-typedef-poc/:
- Fixed-size round-trip: Write u8, u16, u32, u64, f32, f64, bool, enum to a buffer at computed offsets; read back; verify values match.
- String round-trip: Write a length-prefixed string; read back; verify.
- Nested struct round-trip: Write a struct with nested fields; read back; verify.
- Endianness round-trip: Write in big-endian; read back in big-endian; verify.
- SFTP packet round-trip (if SFTP schemas are available): Write an SFTP Read/Write/Status packet; verify byte-identical output against known-good serialization.
10. Error path tests
- Buffer too short for every read function
- Invalid UTF-8 in
read_string - Invalid boolean byte value
- Missing required schema keywords
- Unknown
TypeDef:*kind - Malformed discriminator annotation
- Unknown discriminator value
- Missing variable-length field size in
LayoutBuilder::build()
Test organization
Tests can be organized as:
- Unit tests in
#[cfg(test)] mod testsblocks within each source file (for module-internal functions). - Integration tests in
tests/directory (for public API tests that exercise multiple modules).
What this does NOT include
- Tests for arrays of variable-length-element structs (deferred, OQ-069)
- WASM-specific tests (deferred, OQ-070)
- Performance benchmarks
Acceptance Criteria
- Schema layer tests: all
schema.rspublic functions tested - Data access tests: all read/write functions tested with round-trip, endianness, and error paths
- OffsetMap tests: aligned static layout with alignment, nesting, arrays, unions, variable-length types
- LayoutBuilder tests: packed sequential layout with variable-length shifting
- SequentialReader tests: sequential field reading with position tracking
- TUnion tests: both discriminator kinds, variant resolution
- Validation tests: all 17 custom keyword validators tested
- Engine tests: compile, accessors, convenience methods
- POC round-trip tests: fixed-size, string, nested struct, endianness
- Error path tests: buffer-too-short, invalid UTF-8, malformed schemas
- All tests pass:
cargo test -p alknet-typedef - No
unwrap()in test code that would mask failures (use?orassert!(matches!(...))) cargo clippy -p alknet-typedef --all-targetssucceeds with no warningscargo build --workspacestill succeedscargo test --workspacestill succeeds (old tests untouched)
References
- docs/research/alknet-typedef/findings.md — POC results (26 tests passing)
- /workspace/alknet-typedef-poc/src/lib.rs — POC test reference
- docs/architecture/crates/typedef/README.md — design principles
- All preceding task files in tasks/typedef/
Notes
This is the testing task — it validates the entire crate. The POC had 26 tests passing; this task should cover at least that many scenarios plus additional error-path tests. Tests should be organized by module/concern. The SFTP round-trip test is a stretch goal — it requires SFTP packet schemas which may not be in the repo yet. If SFTP schemas aren't available, test with hand-crafted schemas that exercise the same patterns (byte-offset TUnion, big-endian, mixed fixed/variable fields).
Summary
To be filled on completion