refactor: reorganize tests/tools, share python protocol helpers
Some checks failed
CI / test (push) Has been cancelled

This commit is contained in:
2026-08-17 23:22:44 +08:00
parent 5f5477d658
commit 2a23168728
9 changed files with 266 additions and 174 deletions

View File

@@ -0,0 +1,43 @@
local buffer = ""
return {
feed = function(bytes)
buffer = buffer .. bytes
local frames = {}
while true do
local i = buffer:find("\n", 1, true)
if not i then
break
end
local frame = buffer:sub(1, i - 1)
if frame:sub(-1) == "\r" then
frame = frame:sub(1, -2)
end
frames[#frames + 1] = frame
buffer = buffer:sub(i + 1)
end
return frames
end,
flush = function()
if #buffer == 0 then
return nil
end
local frame = buffer
buffer = ""
return frame
end,
reset = function()
buffer = ""
end,
pending_len = function()
return #buffer
end,
}

View File

@@ -0,0 +1,12 @@
return {
decode = function(frame)
if #frame == 0 then
return nil
end
return {
kind = "text",
data = frame,
}
end,
}

View File

@@ -0,0 +1,66 @@
use std::fs;
use std::path::PathBuf;
use mlua::{Function, Lua, Table};
fn fixture_path(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.join(name)
}
fn load_fixture(name: &str) -> (Lua, Table) {
let lua = Lua::new();
let src = fs::read_to_string(fixture_path(name)).unwrap();
let table: Table = lua.load(&src).set_name(name).eval().unwrap();
(lua, table)
}
#[test]
fn lua_line_framer_fixture_splits_crlf_and_lf() {
let (lua, table) = load_fixture("lua_line_framer.lua");
let feed: Function = table.get("feed").unwrap();
let input = lua.create_string(b"hello\r\nworld\n").unwrap();
let frames: Vec<String> = feed.call(input).unwrap();
assert_eq!(frames, vec!["hello", "world"]);
}
#[test]
fn lua_line_framer_fixture_handles_split_chunks_and_flush() {
let (lua, table) = load_fixture("lua_line_framer.lua");
let feed: Function = table.get("feed").unwrap();
let flush: Function = table.get("flush").unwrap();
let pending_len: Function = table.get("pending_len").unwrap();
let reset: Function = table.get("reset").unwrap();
let frames: Vec<String> = feed.call(lua.create_string(b"par").unwrap()).unwrap();
assert!(frames.is_empty());
assert_eq!(pending_len.call::<usize>(()).unwrap(), 3);
let frames: Vec<String> = feed.call(lua.create_string(b"tial\n").unwrap()).unwrap();
assert_eq!(frames, vec!["partial"]);
let flushed: Option<String> = flush.call(()).unwrap();
assert_eq!(flushed.as_deref(), None);
reset.call::<()>(()).unwrap();
assert_eq!(pending_len.call::<usize>(()).unwrap(), 0);
}
#[test]
fn lua_text_decoder_fixture_decodes_text_and_rejects_empty() {
let (lua, table) = load_fixture("lua_text_decoder.lua");
let decode: Function = table.get("decode").unwrap();
let decoded: Option<Table> = decode.call(lua.create_string(b"abc").unwrap()).unwrap();
let decoded = decoded.expect("non-empty frame should decode");
let kind: String = decoded.get("kind").unwrap();
let data: String = decoded.get("data").unwrap();
assert_eq!(kind, "text");
assert_eq!(data, "abc");
let empty: Option<Table> = decode.call(lua.create_string(b"").unwrap()).unwrap();
assert!(empty.is_none(), "empty frame should be rejected");
}