reifydb-codec 0.9.0

Unified encoding and decoding for all ReifyDB values, frames, rows, keys, and boundaries
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use reifydb_codec::row::shape::{RowFamily, RowShape};
use reifydb_value::value::{uuid::Uuid4, value_type::ValueType};

#[test]
fn test_set_get_uuid4() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid = Uuid4::generate();
	shape.set::<Uuid4>(&mut row, 0, uuid.clone());
	assert_eq!(shape.get::<Uuid4>(&row, 0), uuid);
}

#[test]
fn test_try_get_uuid4() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	assert_eq!(shape.try_get::<Uuid4>(&row, 0), None);

	let uuid = Uuid4::generate();
	shape.set::<Uuid4>(&mut row, 0, uuid.clone());
	assert_eq!(shape.try_get::<Uuid4>(&row, 0), Some(uuid));
}

#[test]
fn test_multiple_generations() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);

	let mut uuids = Vec::new();
	for _ in 0..10 {
		let mut row = shape.allocate_pod();
		let uuid = Uuid4::generate();
		shape.set::<Uuid4>(&mut row, 0, uuid.clone());
		let retrieved = shape.get::<Uuid4>(&row, 0);
		assert_eq!(retrieved, uuid);
		uuids.push(uuid);
	}

	for i in 0..uuids.len() {
		for j in (i + 1)..uuids.len() {
			assert_ne!(uuids[i], uuids[j], "UUIDs should be unique");
		}
	}
}

#[test]
fn test_version_check() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid = Uuid4::generate();
	shape.set::<Uuid4>(&mut row, 0, uuid.clone());
	let retrieved = shape.get::<Uuid4>(&row, 0);

	// The version nibble must survive the row slot.
	assert_eq!(retrieved.get_version_num(), 4);
}

#[test]
fn test_mixed_with_other_types() {
	let shape = RowShape::testing(
		RowFamily::Pod,
		&[ValueType::Uuid4, ValueType::Boolean, ValueType::Uuid4, ValueType::Int4],
	);
	let mut row = shape.allocate_pod();

	let uuid1 = Uuid4::generate();
	let uuid2 = Uuid4::generate();

	shape.set::<Uuid4>(&mut row, 0, uuid1.clone());
	shape.set::<bool>(&mut row, 1, true);
	shape.set::<Uuid4>(&mut row, 2, uuid2.clone());
	shape.set::<i32>(&mut row, 3, 42i32);

	assert_eq!(shape.get::<Uuid4>(&row, 0), uuid1);
	assert_eq!(shape.get::<bool>(&row, 1), true);
	assert_eq!(shape.get::<Uuid4>(&row, 2), uuid2);
	assert_eq!(shape.get::<i32>(&row, 3), 42);
}

#[test]
fn test_undefined_handling() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4, ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid = Uuid4::generate();
	shape.set::<Uuid4>(&mut row, 0, uuid.clone());

	assert_eq!(shape.try_get::<Uuid4>(&row, 0), Some(uuid));
	assert_eq!(shape.try_get::<Uuid4>(&row, 1), None);

	shape.set_none(&mut row, 0);
	assert_eq!(shape.try_get::<Uuid4>(&row, 0), None);
}

#[test]
fn test_persistence() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid = Uuid4::generate();
	let uuid_string = uuid.to_string();

	shape.set::<Uuid4>(&mut row, 0, uuid.clone());
	let retrieved = shape.get::<Uuid4>(&row, 0);

	assert_eq!(retrieved, uuid);
	assert_eq!(retrieved.to_string(), uuid_string);
	assert_eq!(retrieved.as_bytes(), uuid.as_bytes());
}

#[test]
fn test_clone_consistency() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let original_uuid = Uuid4::generate();
	shape.set::<Uuid4>(&mut row, 0, original_uuid.clone());

	let retrieved_uuid = shape.get::<Uuid4>(&row, 0);
	assert_eq!(retrieved_uuid, original_uuid);

	assert_eq!(retrieved_uuid.as_bytes(), original_uuid.as_bytes());
}

#[test]
fn test_multiple_fields() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4, ValueType::Uuid4, ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid1 = Uuid4::generate();
	let uuid2 = Uuid4::generate();
	let uuid3 = Uuid4::generate();

	shape.set::<Uuid4>(&mut row, 0, uuid1.clone());
	shape.set::<Uuid4>(&mut row, 1, uuid2.clone());
	shape.set::<Uuid4>(&mut row, 2, uuid3.clone());

	assert_eq!(shape.get::<Uuid4>(&row, 0), uuid1);
	assert_eq!(shape.get::<Uuid4>(&row, 1), uuid2);
	assert_eq!(shape.get::<Uuid4>(&row, 2), uuid3);

	assert_ne!(uuid1, uuid2);
	assert_ne!(uuid1, uuid3);
	assert_ne!(uuid2, uuid3);
}

#[test]
fn test_format_consistency() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid = Uuid4::generate();
	let original_string = uuid.to_string();

	shape.set::<Uuid4>(&mut row, 0, uuid.clone());
	let retrieved = shape.get::<Uuid4>(&row, 0);
	let retrieved_string = retrieved.to_string();

	assert_eq!(original_string, retrieved_string);

	// 36 chars with 4 hyphens is the 8-4-4-4-12 UUID rendering.
	assert_eq!(original_string.len(), 36);
	assert_eq!(original_string.matches('-').count(), 4);
}

#[test]
fn test_byte_level_storage() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Uuid4]);
	let mut row = shape.allocate_pod();

	let uuid = Uuid4::generate();
	let original_bytes = *uuid.as_bytes();

	shape.set::<Uuid4>(&mut row, 0, uuid.clone());
	let retrieved = shape.get::<Uuid4>(&row, 0);
	let retrieved_bytes = *retrieved.as_bytes();

	assert_eq!(original_bytes, retrieved_bytes);

	assert_eq!(original_bytes.len(), 16);
	assert_eq!(retrieved_bytes.len(), 16);
}

#[test]
fn test_try_get_uuid4_wrong_type() {
	let shape = RowShape::testing(RowFamily::Pod, &[ValueType::Boolean]);
	let mut row = shape.allocate_pod();

	shape.set::<bool>(&mut row, 0, true);

	assert_eq!(shape.try_get::<Uuid4>(&row, 0), None);
}