refactor(storage-server): add SectorId, FileName, PackedPath newtypes, eliminate raw primitives and DRY violations

This commit is contained in:
2026-10-18 03:41:14 +02:00
parent 7a4600b3a2
commit 2dc503e07e
8 changed files with 162 additions and 111 deletions

View File

@@ -4,7 +4,8 @@ use server_framework::{Decode, Encode};
#[test]
fn open_roundtrip() {
let req = StorageRequest::Open { path_word0: 0x61626364, path_word1: 0 };
let path = PackedPath::from_bytes(b"test.txt");
let req = StorageRequest::Open { path };
assert_eq!(StorageRequest::decode(req.encode()), Some(req));
}
@@ -62,3 +63,11 @@ fn all_response_variants() {
fn invalid_request_returns_none() {
assert!(StorageRequest::decode([99, 0, 0, 0]).is_none());
}
#[test]
fn packed_path_roundtrip() {
let path = PackedPath::from_bytes(b"hello");
let buf = path.to_bytes();
assert_eq!(&buf[..5], b"hello");
assert_eq!(buf[5], 0);
}

View File

@@ -1,5 +1,4 @@
use crate::adapters::mock_fs::MockFileSystem;
use crate::filesystem::FileHandle;
use crate::protocol::*;
use crate::server::StorageServer;
use server_framework::Server;
@@ -8,42 +7,33 @@ fn test_server() -> StorageServer<MockFileSystem> {
StorageServer::new(MockFileSystem::new())
}
fn path_words(s: &[u8]) -> (usize, usize) {
let mut w0 = [0u8; 8];
let mut w1 = [0u8; 8];
for (i, &b) in s.iter().enumerate() {
if i < 8 { w0[i] = b; }
else if i < 16 { w1[i - 8] = b; }
}
(usize::from_le_bytes(w0), usize::from_le_bytes(w1))
fn path(s: &[u8]) -> PackedPath {
PackedPath::from_bytes(s)
}
#[test]
fn create_and_open() {
let mut server = test_server();
let (w0, w1) = path_words(b"test.txt");
let resp = server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 });
let resp = server.handle(StorageRequest::Create { path: path(b"test.txt") });
assert!(matches!(resp, StorageResponse::Handle(_)));
let resp = server.handle(StorageRequest::Open { path_word0: w0, path_word1: w1 });
let resp = server.handle(StorageRequest::Open { path: path(b"test.txt") });
assert!(matches!(resp, StorageResponse::Handle(_)));
}
#[test]
fn open_nonexistent_returns_not_found() {
let mut server = test_server();
let (w0, w1) = path_words(b"nope.txt");
let resp = server.handle(StorageRequest::Open { path_word0: w0, path_word1: w1 });
let resp = server.handle(StorageRequest::Open { path: path(b"nope.txt") });
assert_eq!(resp, StorageResponse::NotFound);
}
#[test]
fn write_then_read() {
let mut server = test_server();
let (w0, w1) = path_words(b"data");
let handle = match server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 }) {
let handle = match server.handle(StorageRequest::Create { path: path(b"data") }) {
StorageResponse::Handle(h) => h,
_ => panic!("expected handle"),
};
@@ -58,35 +48,30 @@ fn write_then_read() {
#[test]
fn delete_file() {
let mut server = test_server();
let (w0, w1) = path_words(b"del.txt");
server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 });
let resp = server.handle(StorageRequest::Delete { path_word0: w0, path_word1: w1 });
server.handle(StorageRequest::Create { path: path(b"del.txt") });
let resp = server.handle(StorageRequest::Delete { path: path(b"del.txt") });
assert_eq!(resp, StorageResponse::Ok);
let resp = server.handle(StorageRequest::Open { path_word0: w0, path_word1: w1 });
let resp = server.handle(StorageRequest::Open { path: path(b"del.txt") });
assert_eq!(resp, StorageResponse::NotFound);
}
#[test]
fn list_dir() {
let mut server = test_server();
let (w0a, w1a) = path_words(b"a.txt");
let (w0b, w1b) = path_words(b"b.txt");
server.handle(StorageRequest::Create { path_word0: w0a, path_word1: w1a });
server.handle(StorageRequest::Create { path_word0: w0b, path_word1: w1b });
server.handle(StorageRequest::Create { path: path(b"a.txt") });
server.handle(StorageRequest::Create { path: path(b"b.txt") });
let (w0r, w1r) = path_words(b"/");
let resp = server.handle(StorageRequest::ListDir { path_word0: w0r, path_word1: w1r });
let resp = server.handle(StorageRequest::ListDir { path: path(b"/") });
assert_eq!(resp, StorageResponse::DirEntries { count: 2 });
}
#[test]
fn close_succeeds() {
let mut server = test_server();
let (w0, w1) = path_words(b"c.txt");
let handle = match server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 }) {
let handle = match server.handle(StorageRequest::Create { path: path(b"c.txt") }) {
StorageResponse::Handle(h) => h,
_ => panic!("expected handle"),
};