Files
endorphin-tessera-gui/vendor/nexus/tests/history.rs
T

118 lines
4.8 KiB
Rust

//! Console 历史与累计预算的无 UI 契约回归;需在获授权构建环境编译执行。
use endor_core::Value;
use endorphin_nexus_runtime::{
MAX_SOURCE_BYTES, VmSession, budget::MAX_AST_NODES, console::ConsoleHistory,
program::PreparedProgram,
};
use frontend::EndorReader;
/// 创建真实环境但不安装 UI;历史本身没有管理对象或回放职责。
fn session() -> VmSession {
let mut runtime = VmSession::new().unwrap();
runtime.create_globals().unwrap();
runtime
}
#[test]
/// 解析/编译失败不接受输入;运行错误发生在接受之后,历史和前段写入都保留。
fn only_accepted_inputs_enter_history_and_execution_errors_remain_recorded() {
let mut runtime = session();
let mut history = ConsoleHistory::default();
assert!(
history
.prepare(&runtime, 7, "value = 1\nunknownName")
.is_err()
);
assert_eq!(history.inputs(), 0);
assert_eq!(history.ast_nodes(), 0);
assert_eq!(history.source(), "");
let input = history.prepare(&runtime, 7, "value = 9\n1 / 0").unwrap();
assert_eq!(
format!("{}", input.source().origin()),
"console session 7 input 1"
);
let cost = input.ast_nodes();
let accepted = history.accept(input).unwrap();
assert_eq!(accepted.number(), 1);
assert_eq!(accepted.line_offset(), 1);
assert!(accepted.execute(&mut runtime).is_err());
assert_eq!(history.inputs(), 1);
assert_eq!(history.ast_nodes(), cost);
assert_eq!(history.source(), "// Console input 1\nvalue = 9\n1 / 0\n");
assert_eq!(
runtime
.vm()
.dict_get(runtime.globals().unwrap(), &Value::Text("value".into()))
.unwrap(),
Some(Value::Number(9_i64.into()))
);
runtime.close().unwrap();
}
#[test]
/// 输入次数与 AST 总量分别限制,失败准备不增加计数或追加分隔注释。
fn input_count_and_cumulative_ast_budgets_are_shared_by_headless_hosts() {
let mut runtime = session();
let mut history = ConsoleHistory::default();
// 简单表达式不触发 AST/字节上限,因此第 257 次明确检验次数门槛。
for number in 1..=256 {
let input = history.prepare(&runtime, 1, "1").unwrap();
let accepted = history.accept(input).unwrap();
assert_eq!(accepted.number(), number);
}
let before = history.source().to_owned();
assert!(history.prepare(&runtime, 1, "1").is_err());
assert_eq!(history.inputs(), 256);
assert_eq!(history.source(), before);
// 每个输入有多个浅层语句,避开单输入深度限制,实际检验跨输入累计结构量。
let mut history = ConsoleHistory::default();
let text = "value = 1\n".repeat(50);
let cost = history.prepare(&runtime, 2, &text).unwrap().ast_nodes();
let allowed = MAX_AST_NODES / cost;
assert!(allowed < 256);
for _ in 0..allowed {
let input = history.prepare(&runtime, 2, &text).unwrap();
history.accept(input).unwrap();
}
let before = history.source().to_owned();
let error = history.prepare(&runtime, 2, &text).err().unwrap();
assert!(error.to_string().contains("10000 AST nodes"));
assert_eq!(history.inputs(), allowed);
assert_eq!(history.ast_nodes(), cost * allowed);
assert_eq!(history.source(), before);
runtime.close().unwrap();
}
#[test]
/// 完整源码、Unicode 与 CRLF/CR 原文保持;输入偏移按共享字节行首规则计算,累计字节预算含程序文本。
fn program_history_preserves_source_and_enforces_the_combined_byte_budget() {
let mut runtime = session();
let raw = "base = 1\r\nlabel = \"中\"\r";
let program =
PreparedProgram::compile(EndorReader::from_text("program", raw), Default::default())
.unwrap();
let mut history = ConsoleHistory::from_program(&program);
assert_eq!(history.inputs(), 0);
assert_eq!(history.ast_nodes(), program.ast_nodes());
assert_eq!(history.source(), raw);
let input = history.prepare(&runtime, 8, "\"新\"\r\n").unwrap();
let accepted = history.accept(input).unwrap();
assert_eq!(accepted.line_offset(), 3);
assert_eq!(
history.source(),
format!("{raw}\n// Console input 1\n\"新\"\r\n")
);
assert_eq!(accepted.execute(&mut runtime).unwrap(), "\"新\"");
// 近上限的有效程序只包含长注释和一个表达式;新输入要计算既有文本和固定分隔余量。
let raw = format!("//{}\n1", "x".repeat(MAX_SOURCE_BYTES - 20));
let program = PreparedProgram::compile(
EndorReader::from_text("large program", raw),
Default::default(),
)
.unwrap();
let history = ConsoleHistory::from_program(&program);
assert!(history.prepare(&runtime, 9, "1").is_err());
assert_eq!(history.inputs(), 0);
runtime.close().unwrap();
}