use assert_cmd::Command; use assert_fs::TempDir; const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane"); /// The tool as this test's binary carries it, so the print path is pinned to the file in /// the repo rather than to a description of it. const TOOL: &str = include_str!("../assets/TOOL.ts"); /// `XDG_CONFIG_HOME` is redirected, and `HOME` with it, so the suite never writes into /// the config of whoever runs it. Both, because the fallback reads `HOME` and a machine /// that has neither set is not the case under test here. fn run(config: &TempDir, args: &[&str]) -> std::process::Output { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.env("XDG_CONFIG_HOME", config.path()) .env("HOME", config.path()) .args(args); cmd.assert().get_output().clone() } fn installed(config: &TempDir) -> std::path::PathBuf { config.path().join("opencode/tool/worklane.ts") } fn stdout(output: &std::process::Output) -> String { String::from_utf8(output.stdout.clone()).unwrap() } /// Nothing about the tool needs a board, and it is wanted before there is one. /// /// The file name is asserted, not just the directory: opencode names each tool as the /// file then the export, so `worklane.ts` is what makes them `worklane_add` and /// `worklane_mv`. #[test] fn the_tool_installs_outside_a_worklane_directory() { let config = TempDir::new().unwrap(); let output = run(&config, &["install-tool"]); assert!(output.status.success()); assert_eq!(std::fs::read_to_string(installed(&config)).unwrap(), TOOL); assert!( stdout(&output).contains("Installed the worklane tool"), "{}", stdout(&output) ); } /// What the message has to carry, and it is not the skills' "next session": opencode /// reads its config once at startup and never reloads it, so a running one keeps the /// tools it began with. The second half is there because the file is executable and /// looks like it needs a toolchain, which it does not. #[test] fn the_tool_says_to_restart_opencode_and_that_nothing_else_is_needed() { let config = TempDir::new().unwrap(); let out = stdout(&run(&config, &["install-tool"])); assert!(out.contains("Restart it"), "{out}"); assert!(out.contains("never reloaded"), "{out}"); assert!(out.contains("Nothing else to install"), "{out}"); assert!(out.contains("Bun"), "{out}"); } /// Installing twice is a no-op rather than a rewrite, exactly as `install-skill` is. #[test] fn a_second_install_reports_the_copy_unchanged() { let config = TempDir::new().unwrap(); run(&config, &["install-tool"]); let out = stdout(&run(&config, &["install-tool"])); assert!(out.contains("Already up to date"), "{out}"); assert!(!out.contains("Installed the"), "{out}"); } /// An edited copy is somebody's work, and the refusal names the invocation that shows /// what it would be replaced with. `--print` there and not a skill name: there is one /// tool file, so nothing has to say which. #[test] fn a_modified_copy_is_refused_until_force() { let config = TempDir::new().unwrap(); run(&config, &["install-tool"]); std::fs::write(installed(&config), "// hand written\n").unwrap(); let refused = stdout(&run(&config, &["install-tool"])); assert!(refused.contains("Differs from the version in this binary"), "{refused}"); assert!(refused.contains("worklane install-tool --print"), "{refused}"); assert_eq!(std::fs::read_to_string(installed(&config)).unwrap(), "// hand written\n"); let forced = stdout(&run(&config, &["install-tool", "--force"])); assert!(forced.contains("Installed the worklane tool"), "{forced}"); assert_eq!(std::fs::read_to_string(installed(&config)).unwrap(), TOOL); } /// `--print` is for diffing against an installed copy, so it writes nothing. #[test] fn print_writes_the_tool_to_stdout_and_installs_nothing() { let config = TempDir::new().unwrap(); let output = run(&config, &["install-tool", "--print"]); assert_eq!(stdout(&output), TOOL); assert!(!installed(&config).exists()); } /// With no `XDG_CONFIG_HOME` the tool lands under `HOME`, which is the same answer /// opencode gives: its config root is `$XDG_CONFIG_HOME/opencode` when that is set and /// `~/.config/opencode` when it is not. #[test] fn the_tool_falls_back_to_the_home_directory_without_xdg_config_home() { let home = TempDir::new().unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); let output = cmd .env_remove("XDG_CONFIG_HOME") .env("HOME", home.path()) .arg("install-tool") .assert() .success() .get_output() .clone(); let path = home.path().join(".config/opencode/tool/worklane.ts"); assert_eq!(std::fs::read_to_string(&path).unwrap(), TOOL); assert!( stdout(&output).contains(&path.display().to_string()), "{}", stdout(&output) ); } /// The two commands are independent, which is the whole point of them being two: a /// person on Claude Code takes the skills and never the tool, and one who wants the /// tools is not made to take a second copy of the documents to get them. #[test] fn each_install_command_leaves_the_other_target_alone() { let config = TempDir::new().unwrap(); run(&config, &["install-tool"]); assert!(installed(&config).exists()); assert!(!config.path().join(".claude/skills").exists()); let skills_only = TempDir::new().unwrap(); run(&skills_only, &["install-skill"]); assert!( skills_only .path() .join(".claude/skills/worklane/SKILL.md") .exists() ); assert!(!installed(&skills_only).exists()); }