use assert_cmd::Command;
use tempfile::tempdir;
#[test]
fn add_sse_appends_handler() {
let d = tempdir().unwrap();
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
std::fs::write(
d.path().join("src/controllers/home.controller.ts"),
"import { Route, Get } from '@green-tea/core';\n\n\
@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n",
)
.unwrap();
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "sse"])
.assert()
.success();
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
assert!(out.contains("@Sse("));
for symbol in ["Sse", "sse", "header"] {
assert!(
out.contains(&format!("{symbol},")) || out.contains(&format!("{symbol} }}")),
"{symbol} not imported:\n{out}"
);
}
assert!(out.contains("last-event-id"));
assert!(out.contains("id: String(next)"));
}
#[test]
fn add_errors_when_no_controller() {
let d = tempdir().unwrap();
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "sse"])
.assert()
.failure();
}
#[test]
fn add_sse_twice_is_idempotent() {
let d = tempdir().unwrap();
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
std::fs::write(d.path().join("src/controllers/home.controller.ts"),
"import { Route, Get } from '@green-tea/core';\n\n@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n").unwrap();
let run = || {
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "sse"])
.assert()
.success()
};
run();
run();
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
assert_eq!(out.matches("@Sse(").count(), 1);
assert_eq!(out.matches("from '@green-tea/core'").count(), 1);
}
#[test]
fn add_errors_when_multiple_controllers_and_writes_nothing() {
let d = tempdir().unwrap();
let cdir = d.path().join("src/controllers");
std::fs::create_dir_all(&cdir).unwrap();
let original = "export class X {}\n";
for n in ["a", "b"] {
std::fs::write(cdir.join(format!("{n}.controller.ts")), original).unwrap();
}
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "sse"])
.assert()
.failure();
assert_eq!(
std::fs::read_to_string(cdir.join("a.controller.ts")).unwrap(),
original
);
assert_eq!(
std::fs::read_to_string(cdir.join("b.controller.ts")).unwrap(),
original
);
}
fn seed(dir: &std::path::Path, name: &str) {
std::fs::create_dir_all(dir.join("src/controllers")).unwrap();
std::fs::write(
dir.join(format!("src/controllers/{name}.controller.ts")),
format!(
"import {{ Route, Get }} from '@green-tea/core';\n\n@Route('/')\nexport class {}Controller {{\n @Get('/')\n home() {{}}\n}}\n",
name.to_uppercase()
),
)
.unwrap();
}
#[test]
fn add_ws_wires_the_duplex_handler_and_names_its_peer_dependency() {
let d = tempdir().unwrap();
seed(d.path(), "home");
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "ws"])
.assert()
.success()
.stdout(predicates::str::contains("npm i ws"));
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
assert!(out.contains("@Ws('/echo')"));
assert!(out.contains("@inbound()"));
assert!(out.contains("channel<string>()"));
}
#[test]
fn add_upload_wires_multipart_and_names_busboy() {
let d = tempdir().unwrap();
seed(d.path(), "home");
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "upload"])
.assert()
.success()
.stdout(predicates::str::contains("npm i busboy"));
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
assert!(out.contains("@Post('/upload')"));
assert!(out.contains("MultipartBody"));
}
#[test]
fn add_targets_the_named_controller_when_several_exist() {
let d = tempdir().unwrap();
seed(d.path(), "home");
seed(d.path(), "users");
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "sse"])
.assert()
.failure()
.stderr(predicates::str::contains("--controller"));
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args([
"add",
"sse",
"--controller",
"src/controllers/users.controller.ts",
])
.assert()
.success();
let users =
std::fs::read_to_string(d.path().join("src/controllers/users.controller.ts")).unwrap();
assert!(users.contains("@Sse("));
let home =
std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
assert!(
!home.contains("@Sse("),
"the other controller was edited too"
);
}
#[test]
fn add_rejects_a_controller_path_that_does_not_exist() {
let d = tempdir().unwrap();
seed(d.path(), "home");
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args([
"add",
"sse",
"--controller",
"src/controllers/nope.controller.ts",
])
.assert()
.failure()
.stderr(predicates::str::contains("no such controller"));
}
#[test]
fn add_cookie_reads_with_the_decorator_and_writes_through_a_transformer() {
let d = tempdir().unwrap();
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
std::fs::write(
d.path().join("src/controllers/home.controller.ts"),
"import { Route, Get } from '@green-tea/core';\n\n\
@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n",
)
.unwrap();
for _ in 0..2 {
Command::cargo_bin("matcha")
.unwrap()
.current_dir(d.path())
.args(["add", "cookie"])
.assert()
.success();
}
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
assert!(out.contains("@cookie('theme')"), "{out}");
assert!(out.contains("'set-cookie': setCookie("), "{out}");
for symbol in ["Post", "Transformer", "body", "cookie", "setCookie"] {
assert!(
out.contains(&format!("{symbol},")) || out.contains(&format!("{symbol} }}")),
"{symbol} not imported:\n{out}"
);
}
assert_eq!(
out.matches("@Post('/prefs')").count(),
1,
"not idempotent:\n{out}"
);
}