diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b421267..8f54639 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,8 @@ jobs: run: cargo test -p marqdo-cli --test docs_consistency - name: Architecture boundaries run: cargo test -p marqdo-cli --test architecture_boundaries + - name: Whole stdlib product tests + run: cargo test -p marqdo-cli --test stdlib_product - name: Validate public JSON schemas run: | python3 - <<'PY' diff --git a/.woodpecker.yml b/.woodpecker.yml index 4caa45f..4f3ec00 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -18,6 +18,7 @@ steps: - cargo test --workspace --locked - cargo test -p marqdo-cli --test docs_consistency - cargo test -p marqdo-cli --test architecture_boundaries + - cargo test -p marqdo-cli --test stdlib_product build: image: rust:1.100-bookworm diff --git a/crates/cli/tests/stdlib_product.rs b/crates/cli/tests/stdlib_product.rs new file mode 100644 index 0000000..7a0457f --- /dev/null +++ b/crates/cli/tests/stdlib_product.rs @@ -0,0 +1,181 @@ +//! Product-level tests for the whole bundled `marqdo-stdlib` tree. +//! +//! Every public module in `stdlib/README.md` has a `.mq.md` self-test under +//! `stdlib/tests/`, and this lane runs the whole directory through the real +//! user import path (`std:` + the verified bundle), not through a conformance +//! fixture or a Rust shortcut. + +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +fn repo_root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .to_path_buf() +} + +fn stdlib_dir() -> PathBuf { + repo_root().join("stdlib") +} + +struct TestFile { + name: String, + stdin: &'static str, + stdout: &'static str, +} + +fn product_tests() -> Vec { + let dir = stdlib_dir().join("tests"); + let mut files: Vec = std::fs::read_dir(&dir) + .expect("read stdlib tests") + .map(|entry| { + let path = entry.unwrap().path(); + let name = path.file_name().unwrap().to_string_lossy().to_string(); + let (stdin, stdout) = match name.as_str() { + "console.mq.md" => ("hello\n", ""), + "io.mq.md" => ("", "stdlib self-test\n"), + "stream.mq.md" => ("", "stream\n"), + _ => ("", ""), + }; + TestFile { + name, + stdin, + stdout, + } + }) + .collect(); + files.sort_by(|left, right| left.name.cmp(&right.name)); + files +} + +fn run_product_test(file: &TestFile) -> serde_json::Value { + let mut child = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run"]) + .arg(stdlib_dir().join("tests").join(&file.name)) + .args(["--json"]) + .current_dir(stdlib_dir()) + .env("MARQDO_STDLIB_DIR", stdlib_dir()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("spawn marqdo"); + child + .stdin + .as_mut() + .unwrap() + .write_all(file.stdin.as_bytes()) + .unwrap(); + let output = child.wait_with_output().expect("wait marqdo"); + assert_eq!( + output.status.code(), + Some(0), + "{} failed: {}", + file.name, + String::from_utf8_lossy(&output.stderr) + ); + serde_json::from_slice(&output.stdout).expect("json report") +} + +#[test] +fn every_public_stdlib_module_has_a_product_test() { + let names: Vec = product_tests().into_iter().map(|file| file.name).collect(); + assert_eq!( + names, + vec![ + "console.mq.md", + "format.mq.md", + "http.mq.md", + "io.mq.md", + "stream.mq.md", + "table.mq.md", + "text.mq.md", + "utf8.mq.md", + ], + "a public stdlib module is missing a product test" + ); +} + +#[test] +fn the_whole_bundled_library_runs_through_std_imports() { + for file in product_tests() { + let report = run_product_test(&file); + assert_eq!(report["success"], true, "{}", file.name); + assert_eq!( + report["stdout"], file.stdout, + "{} wrote unexpected stdout", + file.name + ); + } +} + +#[test] +fn format_test_pins_value_semantics_and_handle_observation() { + let report = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "format.mq.md") + .expect("format test"), + ); + let items = report["value"]["items"].as_array().expect("list"); + assert_eq!(items[0]["decimal"], "3"); + assert_eq!(items[1]["value"], "[\"文本\", 2, 1.5]"); + assert_eq!(items[2]["value"], "{a: 1}"); + assert_eq!(items[3]["value"], "[\"a\", 2]"); + assert_eq!(items[4]["value"], "None"); +} + +#[test] +fn text_and_utf8_tests_agree_on_the_byte_encoding() { + let text = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "text.mq.md") + .expect("text test"), + ); + let utf8 = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "utf8.mq.md") + .expect("utf8 test"), + ); + assert_eq!(text["value"]["items"][0]["decimal"], "2"); + assert_eq!(text["value"]["items"][1]["decimal"], "195"); + assert_eq!(text["value"]["items"][2]["decimal"], "169"); + assert_eq!(utf8["value"]["items"][0]["decimal"], "2"); + assert_eq!(utf8["value"]["items"][1]["value"], "é"); + assert_eq!(utf8["value"]["items"][2]["value"], false); +} + +#[test] +fn stream_and_console_use_the_resource_endpoints() { + let stream = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "stream.mq.md") + .expect("stream test"), + ); + assert_eq!(stream["value"]["items"][0]["value"], true); + assert_eq!(stream["value"]["items"][1]["decimal"], "7"); + + let console = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "console.mq.md") + .expect("console test"), + ); + assert_eq!(console["value"]["items"][0]["value"], true); + assert_eq!(console["value"]["items"][1]["value"], "hello"); + assert!( + console["trace"] + .as_array() + .unwrap() + .iter() + .any(|event| event["op"] == "resource.read" || event["op"] == "resource.open"), + "console test did not go through a resource endpoint" + ); +} diff --git a/docs/STATUS.md b/docs/STATUS.md index ee16c40..17eb570 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -58,11 +58,13 @@ byte-identical). cargo fmt --all -- --check PASS cargo clippy --workspace --all-targets -- -D warnings PASS cargo test --workspace --locked PASS - 449 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests + 463 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests ``` The documentation and test migration is local work; re-run this block after it lands and update the count from the log rather than copying the number. +`docs_consistency`, `architecture_boundaries` and `stdlib_product` run inside +this workspace suite. ### G-C security lane (disposable Ubuntu 26.04 VM, ordinary user `dev`) diff --git a/docs/architecture/CURRENT.md b/docs/architecture/CURRENT.md index 1352aed..9a91307 100644 --- a/docs/architecture/CURRENT.md +++ b/docs/architecture/CURRENT.md @@ -18,7 +18,7 @@ platform". | Dimension | Current state | Source | |---|---|---| | Implemented | Rust workspace `0.1.0`: Markdown/frontend/IR, tree-walk runtime, `Memory`, capability host, Tool/Analysis/Trace, Plugin ABI, Foreign kernels, bundled stdlib, CLI composition root | `Cargo.toml`, `crates/`, `stdlib/` | -| Tested locally | `cargo test --workspace --locked`: **449 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 | +| Tested locally | `cargo test --workspace --locked`: **463 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 | | Bundled stdlib released | In-repo bundle `marqdo-stdlib@0.1.0`, SHA-256 digest verified before loading; it is **not** an external registry package | `stdlib/stdlib.json`, `stdlib/README.md` | | Multi-platform verified | Linux local gate is the only executed lane here; Windows/macOS native lanes are **UNVERIFIED**, and the GitHub security lane remains a **VM-only** gate | `docs/STATUS.md` §1, §4 | diff --git a/stdlib/README.md b/stdlib/README.md index f1125e9..25a3fda 100644 --- a/stdlib/README.md +++ b/stdlib/README.md @@ -13,7 +13,8 @@ module; a mismatch is a hard error. - `demo.mq.md` — side-by-side comparison with builtin `打印` - `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group/GFM - `net/http.mq.md` — HTTP/1.1 response parsing (headers/chunked) and request builder -- `tests/` — bundle self-tests +- `tests/` — product self-tests for every public module + (`console`, `format`, `http`, `io`, `stream`, `table`, `text`, `utf8`) The digest is a SHA-256 over sorted `*.mq.md` relative paths, a NUL, file bytes, and a NUL. It excludes `stdlib.json` and non-`.mq.md` files. After any diff --git a/stdlib/stdlib.json b/stdlib/stdlib.json index e6fb90c..a4550a5 100644 --- a/stdlib/stdlib.json +++ b/stdlib/stdlib.json @@ -1,5 +1,5 @@ { "name": "marqdo-stdlib", "version": "0.1.0", - "digest": "sha256:bf11fbdd5fedcaf596308630bdd9590600d9ffac6658c9af483215b4b91aa13f" + "digest": "sha256:86edd79cb84057dba847cc66136a6d6a647d44ea889fff537bd8d162707a3285" } diff --git a/stdlib/tests/console.mq.md b/stdlib/tests/console.mq.md new file mode 100644 index 0000000..a8d784d --- /dev/null +++ b/stdlib/tests/console.mq.md @@ -0,0 +1,10 @@ +--- +import: + console: "std:io/console" +--- + +## main + +**行 = console.读取行()** + +*[行.ok, 行.value]* diff --git a/stdlib/tests/format.mq.md b/stdlib/tests/format.mq.md new file mode 100644 index 0000000..aeb00a0 --- /dev/null +++ b/stdlib/tests/format.mq.md @@ -0,0 +1,8 @@ +--- +import: + format: "std:format" +--- + +## main + +*[format.字节长度("Aé"), format.显示(["文本", 2, 1.5]), format.显示({a: 1}), format.列表显示(["a", 2]), format.显示(None)]* diff --git a/stdlib/tests/stream.mq.md b/stdlib/tests/stream.mq.md new file mode 100644 index 0000000..d343477 --- /dev/null +++ b/stdlib/tests/stream.mq.md @@ -0,0 +1,10 @@ +--- +import: + stream: "std:io/stream" +--- + +## main + +**结果 = stream.写文本("stream\n")** + +*[结果.ok, 结果.value.count]* diff --git a/stdlib/tests/text.mq.md b/stdlib/tests/text.mq.md new file mode 100644 index 0000000..a5807f6 --- /dev/null +++ b/stdlib/tests/text.mq.md @@ -0,0 +1,12 @@ +--- +import: + text: "std:text" +--- + +## main + +**内存 = memory.new(1, 1)** + +**长度 = text.utf8编码("é", 内存, 0)** + +*[长度, 内存[0], 内存[1]]* diff --git a/stdlib/tests/utf8.mq.md b/stdlib/tests/utf8.mq.md new file mode 100644 index 0000000..c7eb9dd --- /dev/null +++ b/stdlib/tests/utf8.mq.md @@ -0,0 +1,12 @@ +--- +import: + utf8: "std:text/utf8" +--- + +## main + +**内存 = memory.new(1, 1)** + +**长度 = utf8.编码("é", 内存, 0)** + +*[长度, utf8.解码(内存, 0, 长度).value, utf8.解码(内存, 0, 1).ok]* diff --git a/tools/pack-stdlib.py b/tools/pack-stdlib.py index 7ed078c..5d420c5 100755 --- a/tools/pack-stdlib.py +++ b/tools/pack-stdlib.py @@ -20,7 +20,9 @@ DIST = ROOT / "dist" def tree_digest() -> str: digest = hashlib.sha256() - for path in sorted(STDLIB.rglob("*.mq.md")): + for path in sorted( + STDLIB.rglob("*.mq.md"), key=lambda p: p.relative_to(STDLIB).as_posix() + ): relative = path.relative_to(STDLIB).as_posix() digest.update(relative.encode()) digest.update(b"\0")