Files
marqdo-cli/crates/cli/tests/tcp_round_trip.rs
cflmy 72f57beee0
ci/woodpecker/push/main Pipeline failed
ci/woodpecker/pr/main Pipeline failed
refactor: accept the ecosystem stdlib Bundle and remove the in-repo sources
Phase B of ADR-0070 (issue #11): the CLI no longer owns standard-library
sources. The pin names the accepted ecosystem Bundle (marqdo-stdlib@0.2.0,
root commit + tree/alias digests); tools/ci/fetch_ecosystem_bundle.py
materializes it from the pinned root commit and stdlib gitlinks and verifies
the digests. bundle.py and the test harness require an explicit verified Bundle
for the accepted identity, and runtime discovery has no compile-time source
fallback in any profile.

Delete stdlib/ (modules, self-tests, aliases manifest). Module and domain tests
stay with their component owners; the CLI product lane now verifies the
external Bundle's public surface, std: resolution and official print/打印 alias
routing (tests/product-stdlib). Composition assertions whose owner is the
ecosystem (Table split, print-format equivalence) moved to the root Bundle
acceptance; the generic TCP round-trip stays a CLI test. Spawned-CLI tests name
the verified Bundle explicitly.

CI (Woodpecker + GitHub) materializes the pinned Bundle before product tests,
packaging and performance lanes; packaging and release take --stdlib-dir from
that artifact. Docs, STATUS, ADR-0070 and a Phase B evidence record updated.

Refs cflmy/marqdo#9, cflmy/marqdo-cli#11
2026-11-27 01:04:01 +08:00

129 lines
3.5 KiB
Rust

//! C10/P5: a `.mq.md` program round-trips bytes over the TCP resource endpoint.
#[path = "support/stdlib.rs"]
pub mod stdlib_support;
#[path = "support/observation.rs"]
pub mod observation;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::path::Path;
struct TempDir(std::path::PathBuf);
impl TempDir {
fn new(tag: &str) -> Self {
let path =
std::env::temp_dir().join(format!("marqdo-http-tcp-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).unwrap();
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn a_marqdo_program_round_trips_bytes_over_the_tcp_resource() {
// The CLI owns the TCP resource endpoint; the HTTP domain module now lives in
// the explicit `marqdo-ext-http` package, so this test exercises the generic
// endpoint directly instead of importing a domain module that is not part of
// the default Bundle.
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let server = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
let mut request = [0u8; 512];
let count = stream.read(&mut request).unwrap();
assert!(String::from_utf8_lossy(&request[..count]).starts_with("GET / HTTP/1.1\r\n"));
stream
.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello")
.unwrap();
});
let dir = TempDir::new("roundtrip");
let program = format!(
r#"---
import:
编码: "std:text"
---
## @exec main
`流 = resource.open("tcp", "{address}", ["read", "write"])`
`请求 = "GET / HTTP/1.1\r\nHost: example.test\r\n\r\n"`
`请求内存 = memory.new(1, 1)`
`请求长度 = 编码.utf8编码(请求, 请求内存, 0)`
`写入 = resource.write(流, 请求内存, 0, 请求长度)`
`接收 = memory.new(1, 1)`
`读出 = resource.read(流, 接收, 0, 4096)`
`数量 = 读出.value.count`
`响应文本 = ""`
`下标 = 0`
`while 下标 < 数量`
`响应文本 = 响应文本 + text.from_codepoint(接收[下标])`
`下标 = 下标 + 1`
`end`
`关闭 = resource.close(流)`
`return [写入.ok, 写入.value.count == 请求长度, 读出.ok, 数量 > 0, 响应文本, 关闭.ok]`
"#
);
std::fs::write(dir.path().join("index.mq.md"), program).unwrap();
let output = stdlib_support::cli_command()
.args([
"run",
"index.mq.md",
"--allow-net",
"127.0.0.1",
"--output",
"json",
])
.current_dir(dir.path())
.output()
.expect("run marqdo");
assert_eq!(
output.status.code(),
Some(0),
"stderr={} stdout={}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json");
assert_eq!(report["run"]["status"], "ok");
let items = observation::terminal(&report)["value"]["items"]
.as_array()
.expect("list");
assert_eq!(items[0]["value"], true);
assert_eq!(items[1]["value"], true);
assert_eq!(items[2]["value"], true);
assert_eq!(items[3]["value"], true);
assert_eq!(
items[4]["value"],
"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello"
);
assert_eq!(items[5]["value"], true);
server.join().unwrap();
}