use std::path::{Path, PathBuf};
use std::sync::Arc;
use unitype_codegen::{Backend, GenOptions, haskell, python, typescript};
use unitype_core::check::{CheckedWorkspace, check_workspace};
use unitype_core::value::Value;
use unitype_core::{BigO, ImplProfile, QualifiedName, Workspace};
use unitype_runtime::environment::Reach;
use unitype_runtime::implementation::Implementation;
use unitype_runtime::{Dispatcher, Environment, Policy, Registry};
const WS: &str = r#"{"universes":[{"name":"demo","types":[
{"k":"struct","name":"Tally","fields":[
{"name":"total","ty":"i64"},{"name":"seen","ty":"[str]"}]}],
"functions":[
{"name":"tally","params":[{"name":"words","ty":"[str]"}],"ret":"Tally"}]}]}"#;
fn workspace() -> CheckedWorkspace {
check_workspace(Workspace::from_json(WS).expect("reads")).expect("checks")
}
fn have_python() -> bool {
std::process::Command::new("python3")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|s| s.success())
}
fn generated_module(name: &str) -> PathBuf {
let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name);
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("makes a directory");
for file in python::Python
.generate(&workspace(), &GenOptions::default())
.expect("generates")
{
let at = dir.join(file.path.file_name().expect("a file name"));
std::fs::write(at, file.contents).expect("writes");
}
std::fs::write(
dir.join("proof.py"),
r#"from unitype_harness import unitype_main
from demo import Tally
import dataclasses
def tally(words):
counted = Tally(total=len(words), seen=sorted(set(words)))
return dataclasses.asdict(counted)
if __name__ == "__main__":
unitype_main({"tally": tally})
"#,
)
.expect("writes the proof");
dir
}
fn have_node() -> bool {
let Ok(output) = std::process::Command::new("node").arg("--version").output() else {
return false;
};
String::from_utf8_lossy(&output.stdout)
.trim()
.trim_start_matches('v')
.split('.')
.next()
.and_then(|major| major.parse::<u32>().ok())
.is_some_and(|major| major >= 24)
}
fn generated_node_module(name: &str) -> PathBuf {
let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name);
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("makes a directory");
for file in typescript::TypeScript
.generate(&workspace(), &GenOptions::default())
.expect("generates")
{
let at = dir.join(file.path.file_name().expect("a file name"));
std::fs::write(at, file.contents).expect("writes");
}
std::fs::write(dir.join("package.json"), "{\"type\": \"module\"}\n").expect("writes");
std::fs::write(
dir.join("proof.ts"),
r#"import { unitypeMain } from "./unitype_harness.ts";
import type { Tally } from "./demo.ts";
function tally(words: string[]): Tally {
return { total: words.length, seen: [...new Set(words)].sort() };
}
unitypeMain({ tally });
"#,
)
.expect("writes the proof");
dir
}
fn dispatcher(dir: &Path, persistent: bool) -> Dispatcher {
dispatcher_for(
Environment::subprocess("py", "python", "python3", &["proof.py"]),
dir,
persistent,
)
}
fn node_dispatcher(dir: &Path, persistent: bool) -> Dispatcher {
dispatcher_for(
Environment::subprocess("node", "typescript", "node", &["proof.ts"]),
dir,
persistent,
)
}
fn dispatcher_for(mut environment: Environment, dir: &Path, persistent: bool) -> Dispatcher {
let reached = environment.id.clone();
if let Reach::Subprocess {
cwd,
persistent: keep,
timeout_ms,
..
} = &mut environment.reach
{
*cwd = Some(dir.to_path_buf());
*keep = persistent;
*timeout_ms = 30_000;
}
let mut registry = Registry::new();
registry.add_environment(environment).expect("registers");
registry
.register(Implementation::new(
"tally.py",
"demo::tally".parse::<QualifiedName>().unwrap(),
reached,
"tally",
ImplProfile::new(BigO::ON, BigO::ON),
))
.expect("registers");
Dispatcher::new(Arc::new(workspace()), Arc::new(registry))
}
fn words(items: &[&str]) -> Value {
Value::List(items.iter().map(|w| Value::Str((*w).into())).collect())
}
#[tokio::test]
async fn a_generated_harness_is_reachable_as_a_proof() {
if !have_python() {
eprintln!("no python3 on this machine; skipping");
return;
}
let dir = generated_module("generated_oneshot");
let answer = dispatcher(&dir, false)
.call(
&"demo::tally".parse().unwrap(),
vec![words(&["a", "b", "a"])],
&Policy::Default,
)
.await
.expect("answers");
assert_eq!(answer.value.get("total"), Some(&Value::Int(3)));
assert_eq!(
answer.value.get("seen"),
Some(&words(&["a", "b"])),
"{:?}",
answer.value
);
}
#[tokio::test]
async fn the_same_generated_harness_serves_a_worker_without_changing() {
if !have_python() {
return;
}
let dir = generated_module("generated_worker");
let dispatcher = dispatcher(&dir, true);
for (input, total) in [
(&["a"][..], 1),
(&["a", "b"][..], 2),
(&["a", "b", "c"][..], 3),
] {
let answer = dispatcher
.call(
&"demo::tally".parse().unwrap(),
vec![words(input)],
&Policy::Default,
)
.await
.expect("answers");
assert_eq!(answer.value.get("total"), Some(&Value::Int(total)));
}
assert_eq!(
dispatcher.workers().running().await,
1,
"three calls, one interpreter"
);
}
#[tokio::test]
async fn a_generated_proof_is_held_to_the_signature_like_any_other() {
if !have_python() {
return;
}
let dir = generated_module("generated_wrong");
std::fs::write(
dir.join("proof.py"),
r#"from unitype_harness import unitype_main
if __name__ == "__main__":
unitype_main({"tally": lambda words: {"total": "not a number"}})
"#,
)
.expect("writes");
let failed = dispatcher(&dir, false)
.call(
&"demo::tally".parse().unwrap(),
vec![words(&["a"])],
&Policy::Default,
)
.await;
assert!(
failed
.as_ref()
.is_err_and(|e| e.to_string().contains("not a")),
"{failed:?}"
);
}
#[tokio::test]
async fn the_generated_node_harness_is_reachable_as_a_proof_too() {
if !have_node() {
eprintln!("no node that reads TypeScript on this machine; skipping");
return;
}
let dir = generated_node_module("generated_node_oneshot");
let answer = node_dispatcher(&dir, false)
.call(
&"demo::tally".parse().unwrap(),
vec![words(&["a", "b", "a"])],
&Policy::Default,
)
.await
.expect("answers");
assert_eq!(answer.value.get("total"), Some(&Value::Int(3)));
assert_eq!(
answer.value.get("seen"),
Some(&words(&["a", "b"])),
"{:?}",
answer.value
);
}
#[tokio::test]
async fn the_generated_node_harness_serves_a_worker_without_changing() {
if !have_node() {
return;
}
let dir = generated_node_module("generated_node_worker");
let dispatcher = node_dispatcher(&dir, true);
for (input, total) in [(&["a"][..], 1), (&["a", "b"][..], 2)] {
let answer = dispatcher
.call(
&"demo::tally".parse().unwrap(),
vec![words(input)],
&Policy::Default,
)
.await
.expect("answers");
assert_eq!(answer.value.get("total"), Some(&Value::Int(total)));
}
assert_eq!(
dispatcher.workers().running().await,
1,
"two calls, one interpreter"
);
}
fn have_ghc() -> bool {
std::process::Command::new("ghc")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|s| s.success())
}
fn compile_haskell(dir: &Path) {
let built = std::process::Command::new("ghc")
.args(["-v0", "-O0", "-outputdir", "obj", "-o", "proof", "Proof.hs"])
.current_dir(dir)
.output()
.expect("runs ghc");
assert!(
built.status.success(),
"the proof did not build:\n{}",
String::from_utf8_lossy(&built.stderr)
);
}
fn generated_haskell_module(name: &str) -> PathBuf {
let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name);
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("makes a directory");
for file in haskell::Haskell
.generate(&workspace(), &GenOptions::default())
.expect("generates")
{
let at = dir.join(file.path.file_name().expect("a file name"));
std::fs::write(at, file.contents).expect("writes");
}
std::fs::write(
dir.join("Proof.hs"),
r#"{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import qualified Data.Set as Set
import Data.Text (Text)
import Demo (Tally (..))
import UnitypeHarness (proves, unitypeMain)
tally :: [Text] -> Tally
tally words =
Tally {tallyTotal = fromIntegral (length words), tallySeen = Set.toList (Set.fromList words)}
main :: IO ()
main = unitypeMain [("tally", proves tally)]
"#,
)
.expect("writes the proof");
compile_haskell(&dir);
dir
}
fn haskell_dispatcher(dir: &Path, persistent: bool) -> Dispatcher {
let program = dir.join("proof");
dispatcher_for(
Environment::subprocess("hs", "haskell", program.to_str().expect("a path"), &[]),
dir,
persistent,
)
}
#[tokio::test]
async fn the_generated_haskell_harness_is_reachable_as_a_proof_too() {
if !have_ghc() {
eprintln!("no ghc on this machine; skipping");
return;
}
let dir = generated_haskell_module("generated_haskell_oneshot");
let answer = haskell_dispatcher(&dir, false)
.call(
&"demo::tally".parse().unwrap(),
vec![words(&["a", "b", "a"])],
&Policy::Default,
)
.await
.expect("answers");
assert_eq!(answer.value.get("total"), Some(&Value::Int(3)));
assert_eq!(
answer.value.get("seen"),
Some(&words(&["a", "b"])),
"{:?}",
answer.value
);
}
#[tokio::test]
async fn the_generated_haskell_harness_serves_a_worker_without_changing() {
if !have_ghc() {
return;
}
let dir = generated_haskell_module("generated_haskell_worker");
let dispatcher = haskell_dispatcher(&dir, true);
for (input, total) in [(&["a"][..], 1), (&["a", "b"][..], 2), (&["é", "é"][..], 2)] {
let answer = dispatcher
.call(
&"demo::tally".parse().unwrap(),
vec![words(input)],
&Policy::Default,
)
.await
.expect("answers");
assert_eq!(answer.value.get("total"), Some(&Value::Int(total)));
}
assert_eq!(
dispatcher.workers().running().await,
1,
"three calls, one process"
);
}
#[tokio::test]
async fn a_haskell_proof_that_throws_is_reported_as_failing_not_as_a_broken_environment() {
if !have_ghc() {
return;
}
let dir = generated_haskell_module("generated_haskell_throws");
std::fs::write(
dir.join("Proof.hs"),
r#"module Main (main) where
import Data.Text (Text)
import Demo (Tally (..))
import UnitypeHarness (proves, unitypeMain)
tally :: [Text] -> Tally
tally _ = error "the tally went wrong"
main :: IO ()
main = unitypeMain [("tally", proves tally)]
"#,
)
.expect("writes");
compile_haskell(&dir);
let failed = haskell_dispatcher(&dir, false)
.call(
&"demo::tally".parse().unwrap(),
vec![words(&["a"])],
&Policy::Default,
)
.await;
assert!(
failed
.as_ref()
.is_err_and(|e| e.to_string().contains("the tally went wrong")),
"{failed:?}"
);
}
#[tokio::test]
async fn a_generated_haskell_proof_is_held_to_the_signature_like_any_other() {
if !have_ghc() {
return;
}
let dir = generated_haskell_module("generated_haskell_wrong");
std::fs::write(
dir.join("Proof.hs"),
r#"{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import qualified Data.Map.Strict as Map
import Data.Text (Text)
import UnitypeHarness (proves, unitypeMain)
import UnitypeSupport (Json (..))
wrong :: [Text] -> Json
wrong _ = JObject (Map.fromList [("total", JString "not a number")])
main :: IO ()
main = unitypeMain [("tally", proves wrong)]
"#,
)
.expect("writes");
compile_haskell(&dir);
let failed = haskell_dispatcher(&dir, false)
.call(
&"demo::tally".parse().unwrap(),
vec![words(&["a"])],
&Policy::Default,
)
.await;
assert!(
failed
.as_ref()
.is_err_and(|e| e.to_string().contains("not a")),
"{failed:?}"
);
}