feat(stdlib): add datetime and project-mode clock APIs
This commit is contained in:
1 parent
cffb6790be
commit
ae69391bc0
15 files changed
+527
-11
No files matched your search
@@ -127,8 +127,10 @@ fn every_public_stdlib_module_has_a_product_test() {
|
||||
assert_eq!(
|
||||
names,
|
||||
vec![
|
||||
"clock.mq.md",
|
||||
"console.mq.md",
|
||||
"csv.mq.md",
|
||||
"datetime.mq.md",
|
||||
"demo.mq.md",
|
||||
"format.mq.md",
|
||||
"fs.mq.md",
|
||||
@@ -276,6 +278,38 @@ fn regex_test_matches_anchors_classes_and_search() {
|
||||
assert_eq!(items[11]["value"], "regex.unsupported");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn datetime_test_formats_and_parses_utc_instants() {
|
||||
let report = run_product_test(
|
||||
product_tests()
|
||||
.iter()
|
||||
.find(|file| file.name == "datetime.mq.md")
|
||||
.expect("datetime test"),
|
||||
);
|
||||
let items = report["value"]["items"].as_array().expect("list");
|
||||
assert_eq!(items[0]["value"], "1970-01-01T00:00:00Z");
|
||||
assert_eq!(items[1]["value"], "2023-11-14T22:13:20.123Z");
|
||||
assert_eq!(items[2]["decimal"], "0");
|
||||
assert_eq!(items[3]["decimal"], "1795248000123");
|
||||
assert_eq!(items[4]["value"], false);
|
||||
assert_eq!(items[5]["value"], false);
|
||||
assert_eq!(items[6]["value"], "1969-12-31T23:59:59.999Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clock_test_reports_project_mode_unix_millis() {
|
||||
let report = run_product_test(
|
||||
product_tests()
|
||||
.iter()
|
||||
.find(|file| file.name == "clock.mq.md")
|
||||
.expect("clock test"),
|
||||
);
|
||||
let items = report["value"]["items"].as_array().expect("list");
|
||||
assert_eq!(items[0]["value"], true);
|
||||
assert_eq!(items[1]["value"], "Int");
|
||||
assert_eq!(items[2]["value"], true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_test_pins_value_semantics_and_handle_observation() {
|
||||
let report = run_product_test(
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::net::{SocketAddr, TcpStream};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use marqdo_plugin_abi::{
|
||||
ErrorKind as PluginErrorKind, LoadedPlugin, PluginError, PluginKind, PluginManifest,
|
||||
@@ -1127,6 +1127,41 @@ impl Host for StdHost {
|
||||
"resource operations accept only Text path arguments",
|
||||
));
|
||||
}
|
||||
if operation == "clock.now" {
|
||||
if !args.is_empty() {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::Arity,
|
||||
ctx.span,
|
||||
format!(
|
||||
"resource operation `clock.now` expects 0 arguments, got {}",
|
||||
args.len()
|
||||
),
|
||||
));
|
||||
}
|
||||
let millis = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|error| {
|
||||
RuntimeError::new(
|
||||
ErrorCategory::HostError,
|
||||
ctx.span,
|
||||
format!("system clock is before the Unix epoch: {error}"),
|
||||
)
|
||||
})?
|
||||
.as_millis()
|
||||
.min(i64::MAX as u128) as i64;
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("ok".to_string(), Value::Bool(true));
|
||||
map.insert("value".to_string(), Value::int(millis));
|
||||
self.record_semantic(
|
||||
"resource.request",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("op=clock.now"),
|
||||
);
|
||||
self.record_resource_effect("resource.request", ctx, started, "clock.now");
|
||||
return Ok(Value::Map(map));
|
||||
}
|
||||
let Some(name) = operation.strip_prefix("fs.") else {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
|
||||
@@ -614,3 +614,30 @@ fn fs_requests_reject_traversal_before_touching_the_filesystem() {
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::PermissionDenied);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clock_now_is_project_only_and_returns_unix_millis() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let ctx = context();
|
||||
let mut pure = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path());
|
||||
let error = pure
|
||||
.resource_request("clock.now", Vec::new(), &ctx)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::PermissionDenied);
|
||||
|
||||
let mut project = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
|
||||
let value = project
|
||||
.resource_request("clock.now", Vec::new(), &ctx)
|
||||
.unwrap();
|
||||
assert_eq!(field(&value, "ok"), &Value::Bool(true));
|
||||
let Value::Int(millis) = field(&value, "value") else {
|
||||
panic!("clock.now did not return an Int");
|
||||
};
|
||||
let millis = millis.to_string().parse::<i64>().expect("i64 millis");
|
||||
assert!(millis > 1_600_000_000_000);
|
||||
|
||||
let error = project
|
||||
.resource_request("clock.now", vec![Value::text("x")], &ctx)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::Arity);
|
||||
}
|
||||
Reference in new issue
Block a user