323 lines
12 KiB
Rust
323 lines
12 KiB
Rust
use super::*;
|
|
|
|
fn multi(name: &str, port: &str, first: &str, second: &str, app: &str) -> PathBuf {
|
|
let root = project(name, port, first, app);
|
|
let domain = root.join("module/demo/service");
|
|
fs::create_dir(domain.join("adapter")).unwrap();
|
|
fs::rename(
|
|
domain.join("adapter.dever"),
|
|
domain.join("adapter/first.dever"),
|
|
)
|
|
.unwrap();
|
|
fs::write(domain.join("adapter/second.dever"), second).unwrap();
|
|
fs::create_dir(root.join("config")).unwrap();
|
|
root
|
|
}
|
|
fn selected(binary: &Path, root: &Path, configuration: &str) -> std::process::Output {
|
|
let path = root.join("config/setting.json");
|
|
fs::write(&path, configuration).unwrap();
|
|
Command::new(binary)
|
|
.arg("--config")
|
|
.arg(path)
|
|
.output()
|
|
.unwrap()
|
|
}
|
|
#[test]
|
|
fn multi_adapter_selects_at_startup_and_decodes_only_selected_settings() {
|
|
let root = multi(
|
|
"port-selection",
|
|
"read(value: Text) (out: Text)",
|
|
"setting { prefix: Text }\nport.read(value: Text) (out: Text) { out = prefix() + value }\nprefix() (out: Text) { out = setting.prefix }",
|
|
"setting { count: Int >= 1 and <= 3\nkey: Secret }\nport.read(value: Text) (out: Text) { out = value + value }",
|
|
"flow start() (out: Text)\n| step | kind | do |\n| out | port | port.read(\"x\") |",
|
|
);
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(
|
|
&loaded,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
for (config, expected) in [
|
|
(
|
|
r#"{"adapter":{"demo.service":{"use":"first","setting":{"prefix":"A"}}}}"#,
|
|
"\"Ax\"",
|
|
),
|
|
(
|
|
r#"{"adapter":{"demo.service":{"setting":{"count":2,"key":"private-value"},"use":"second"}}}"#,
|
|
"\"xx\"",
|
|
),
|
|
] {
|
|
let output = selected(&artifact.binary, &root, config);
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), expected);
|
|
}
|
|
for config in [
|
|
r#"{"adapter":{"demo.service":{"setting":{"prefix":"A"}}}}"#,
|
|
r#"{"adapter":{"demo.service":{"use":"unknown"}}}"#,
|
|
r#"{"adapter":{"demo.service":{"use":"first","use":"second","setting":{"prefix":"A"}}}}"#,
|
|
r#"{"adapter":{"demo.service":{"use":"second","setting":{"count":0,"key":"private-value"}}}}"#,
|
|
r#"{"adapter":{"demo.service":{"use":"first","setting":{"prefix":"A","prefix":"B"}}}}"#,
|
|
r#"{"adapter":{"demo.service":{"setting":{"prefix":"A","prefix":"B"},"use":"first"}}}"#,
|
|
r#"{"adapter":{"demo.service":{"use":"first","setting":{"prefix":"A"},"other":1}}}"#,
|
|
] {
|
|
let output = selected(&artifact.binary, &root, config);
|
|
assert!(!output.status.success(), "accepted {config}");
|
|
assert!(!String::from_utf8_lossy(&output.stderr).contains("private-value"));
|
|
}
|
|
let index: serde_json::Value = serde_json::from_str(&success(cli(&[
|
|
"inspect",
|
|
root.to_str().unwrap(),
|
|
"--json",
|
|
])))
|
|
.unwrap();
|
|
let ports = index["symbols"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.filter(|s| s["name"] == "demo.service.port.read")
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(ports.len(), 1);
|
|
assert_eq!(
|
|
index["calls"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.filter(|edge| edge["caller"] == ports[0]["id"] && edge["kind"] == "implementation")
|
|
.count(),
|
|
2
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn multi_adapter_returns_multiple_fields_with_one_call_per_branch() {
|
|
let root = multi(
|
|
"port-selection-outputs",
|
|
"read(value: Text) (first: Text, second: Int)",
|
|
"port.read(value: Text) (first: Text, second: Int) { first = value\nsecond = 1 }",
|
|
"port.read(value: Text) (first: Text, second: Int) { first = value\nsecond = 2 }",
|
|
"flow start() (out: Int)\n| step | kind | do |\n| answer | port | port.read(\"hello\") |\n| out | calc | answer.second |",
|
|
);
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(
|
|
&loaded,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
for (name, expected) in [("first", "1"), ("second", "2")] {
|
|
let output = selected(
|
|
&artifact.binary,
|
|
&root,
|
|
&format!(r#"{{"adapter":{{"demo.service":{{"use":"{name}"}}}}}}"#),
|
|
);
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), expected);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_adapter_must_implement_the_whole_port_and_obey_its_contract() {
|
|
for (name, second, message) in [
|
|
(
|
|
"incomplete",
|
|
"port.read(value: Int) (out: Int) { out = value }",
|
|
"requires an Adapter",
|
|
),
|
|
(
|
|
"signature",
|
|
"port.read(value: Bool) (out: Int) { out = 1 }\nport.write() () {}",
|
|
"signature",
|
|
),
|
|
(
|
|
"failure",
|
|
"port.read(value: Int) (out: Int) { check value > 0 else Invalid\nout = value }\nport.write() () {}",
|
|
"failures",
|
|
),
|
|
(
|
|
"cycle",
|
|
"port.read(value: Int) (out: Int) { out = helper(value) }\nhelper(value: Int) (out: Int) { out = helper(value) }\nport.write() () {}",
|
|
"recursive",
|
|
),
|
|
] {
|
|
let root = multi(
|
|
&format!("port-selection-{name}"),
|
|
"read(value: Int) (out: Int)\nwrite() ()",
|
|
"port.read(value: Int) (out: Int) { out = value }\nport.write() () {}",
|
|
second,
|
|
"flow start() (out: Int)\n| step | kind | do |\n| out | port | port.read(1) |",
|
|
);
|
|
let loaded = Project::load(&root).unwrap();
|
|
let error = program::check(&loaded.declarations, &loaded.modules)
|
|
.err()
|
|
.unwrap();
|
|
assert!(error.message.contains(message), "{name}: {error}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn multi_adapter_keeps_async_failures_and_private_settings_in_child_tasks() {
|
|
let root = multi(
|
|
"port-selection-task",
|
|
"read() (out: Text) fails dever.time.SleepResult",
|
|
"setting { message: Text }\nflow port.read() (out: Text)\n| step | kind | do |\n| work | calc | run(pause()) |\n| out | calc | wait(work) |\nflow pause() (out: Text)\n| step | kind | do |\n| - | flow | dever.time.sleep(0) |\n| out | calc | setting.message |",
|
|
"port.read() (out: Text) { out = \"immediate\" }",
|
|
"flow start() (out: Text)\n| step | kind | do |\n| work | calc | run(port.read()) |\n| out | calc | wait(work) |",
|
|
);
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(
|
|
&loaded,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
for (config, expected) in [
|
|
(
|
|
r#"{"adapter":{"demo.service":{"use":"first","setting":{"message":"async"}}}}"#,
|
|
"\"async\"",
|
|
),
|
|
(
|
|
r#"{"adapter":{"demo.service":{"use":"second"}}}"#,
|
|
"\"immediate\"",
|
|
),
|
|
] {
|
|
let output = selected(&artifact.binary, &root, config);
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), expected);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn multi_adapter_propagates_selected_failure_without_fallback() {
|
|
let root = multi(
|
|
"port-selection-propagate",
|
|
"type Error { error Rejected }\nread() (out: Int) fails Error",
|
|
"port.read() (out: Int) { out = 1 }",
|
|
"port.read() (out: Int) { out = fail(port.Error.Rejected) }",
|
|
"flow start() (out: Int)\n| step | kind | do |\n| out | port | port.read() |",
|
|
);
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(
|
|
&loaded,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
let output = selected(
|
|
&artifact.binary,
|
|
&root,
|
|
r#"{"adapter":{"demo.service":{"use":"second"}}}"#,
|
|
);
|
|
assert!(!output.status.success());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("Rejected"));
|
|
let output = selected(
|
|
&artifact.binary,
|
|
&root,
|
|
r#"{"adapter":{"demo.service":{"use":"first"}}}"#,
|
|
);
|
|
assert_eq!(success(output).trim(), "1");
|
|
}
|
|
|
|
#[test]
|
|
fn multi_adapter_test_fake_replaces_all_production_choices() {
|
|
let root = multi(
|
|
"port-selection-fake",
|
|
"read() (out: Int)",
|
|
"setting { value: Int }\nport.read() (out: Int) { out = setting.value }",
|
|
"setting { label: Text }\nport.read() (out: Int) { out = 2 }",
|
|
"start() (out: Int) { out = port.read() }",
|
|
);
|
|
let tests = root.join("test/demo/service");
|
|
fs::create_dir_all(&tests).unwrap();
|
|
fs::write(
|
|
tests.join("fake.dever"),
|
|
"fake() () { assertEq(app.start(), 42) }\nport.read() (out: Int) { out = 42 }",
|
|
)
|
|
.unwrap();
|
|
success(cli(&["fmt", root.to_str().unwrap()]));
|
|
let result = success(cli(&["test", root.to_str().unwrap()]));
|
|
assert!(result.contains("PASS demo.service.fake"), "{result}");
|
|
assert!(!root.join("config/setting.json").exists());
|
|
|
|
write_source(
|
|
root.join("module/demo/service/app/main.dever"),
|
|
"setting base:Int\nstart() (out:Int) { out=port.read()+base }",
|
|
)
|
|
.unwrap();
|
|
fs::write(
|
|
tests.join("fake.dever"),
|
|
"fake() () { assertEq(app.start(),44) }\nport.read() (out:Int) { out=42 }",
|
|
)
|
|
.unwrap();
|
|
fs::write(root.join("test/setting.json"), r#"{"module":{"demo":{"service":{"base":2}}},"adapter":{"demo.service":{"use":"first","setting":{"value":7}}}}"#).unwrap();
|
|
success(cli(&["fmt", root.to_str().unwrap()]));
|
|
assert!(success(cli(&["test", root.to_str().unwrap()])).contains("PASS demo.service.fake"));
|
|
fs::write(root.join("test/setting.json"), r#"{"module":{"demo":{"service":{"base":2}}},"adapter":{"demo.service":{},"demo.service":{}}}"#).unwrap();
|
|
let output = cli(&["test", root.to_str().unwrap()]);
|
|
assert!(!output.status.success());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("duplicate adapter configuration"));
|
|
}
|
|
|
|
#[test]
|
|
fn multi_adapter_does_not_erase_returned_error_obligations_or_effects() {
|
|
for (name, contract, first, second, app, message) in [
|
|
(
|
|
"port-body",
|
|
"read() (out: Int) { out = 1 }",
|
|
"port.read() (out: Int) { out = 1 }",
|
|
"port.read() (out: Int) { out = 2 }",
|
|
"start() (out: Int) { out = port.read() }",
|
|
"does not allow function bodies",
|
|
),
|
|
(
|
|
"error-value",
|
|
"type Outcome { Good\nerror Rejected }\nread() (out: Outcome)",
|
|
"port.read() (out: port.Outcome) { out = port.Outcome.Good }",
|
|
"port.read() (out: port.Outcome) { out = port.Outcome.Rejected }",
|
|
"start() () { ignored = port.read() }",
|
|
"failure is not handled",
|
|
),
|
|
(
|
|
"effect-union",
|
|
"read() () fails dever.time.SleepResult",
|
|
"port.read() () {}",
|
|
"flow port.read() ()\n| step | kind | do |\n| - | flow | dever.time.sleep(0) |",
|
|
"start() () { port.read() }",
|
|
"effectful",
|
|
),
|
|
] {
|
|
let root = multi(
|
|
&format!("port-selection-{name}"),
|
|
contract,
|
|
first,
|
|
second,
|
|
app,
|
|
);
|
|
let loaded = Project::load(&root).unwrap();
|
|
let error = program::check(&loaded.declarations, &loaded.modules)
|
|
.err()
|
|
.unwrap();
|
|
assert!(error.message.contains(message), "{name}: {error}");
|
|
}
|
|
}
|