merry-tools 0.1.4

Workspace file, search, patch, web fetch, and document tools for the Merry agent runtime.
Documentation
use super::*;

#[test]
fn config_rejects_missing_workspace_root() {
    let temp = TempWorkspace::new("missing-root");

    let err = WorkspaceTools::new(WorkspaceToolsConfig::new(temp.path().join("absent")))
        .expect_err("a missing workspace root should be rejected");
    assert!(matches!(err, WorkspaceToolConfigError::RootNotFound { .. }));
}

#[test]
fn config_skips_missing_and_duplicate_resource_roots() {
    let temp = TempWorkspace::new("resource-roots");
    let config = WorkspaceToolsConfig::new(temp.path().to_path_buf())
        .with_readonly_resource_roots(vec![temp.path().join("absent"), temp.path().to_path_buf()]);

    let tools = WorkspaceTools::new(config).expect("resource roots are optional");

    assert!(
        tools.state.readonly_resource_roots.is_empty(),
        "a missing or workspace-identical resource root must be skipped: {:?}",
        tools.state.readonly_resource_roots
    );
}

#[test]
fn config_rejects_non_directory_root() {
    let temp = TempWorkspace::new("non-directory-root");
    temp.write_text("file.txt", "content\n");

    let err = WorkspaceTools::new(WorkspaceToolsConfig::new(temp.path().join("file.txt")))
        .expect_err("file root should be rejected");
    assert!(matches!(
        err,
        WorkspaceToolConfigError::RootNotDirectory { .. }
    ));
}

#[test]
fn config_rejects_zero_read_limit() {
    let temp = TempWorkspace::new("zero-limit");
    let config =
        WorkspaceToolsConfig::new(temp.path().to_path_buf()).with_limits(WorkspaceToolLimits {
            max_read_bytes: 0,
            ..WorkspaceToolLimits::default()
        });

    let err = WorkspaceTools::new(config).expect_err("zero limit should be rejected");
    assert!(matches!(
        err,
        WorkspaceToolConfigError::InvalidLimit {
            name: "max_read_bytes"
        }
    ));
}

#[test]
fn config_rejects_each_zero_limit() {
    let temp = TempWorkspace::new("zero-all-limits");

    for invalid_name in [
        "max_read_bytes",
        "max_read_lines",
        "max_write_bytes",
        "max_patch_bytes",
    ] {
        let mut limits = WorkspaceToolLimits::default();
        match invalid_name {
            "max_read_bytes" => limits.max_read_bytes = 0,
            "max_read_lines" => limits.max_read_lines = 0,
            "max_write_bytes" => limits.max_write_bytes = 0,
            "max_patch_bytes" => limits.max_patch_bytes = 0,
            other => panic!("unexpected limit name {other}"),
        }

        let config = WorkspaceToolsConfig::new(temp.path().to_path_buf()).with_limits(limits);
        let err = WorkspaceTools::new(config).expect_err("zero limit should be rejected");
        assert!(matches!(
            err,
            WorkspaceToolConfigError::InvalidLimit { name } if name == invalid_name
        ));
    }
}

#[test]
fn config_rejects_invalid_patch_scope_paths() {
    let temp = TempWorkspace::new("invalid-patch-scope");

    for config in [
        WorkspaceToolsConfig::new(temp.path().to_path_buf())
            .with_patch_write_scope(Some(vec![PathBuf::from("../outside")])),
        WorkspaceToolsConfig::new(temp.path().to_path_buf())
            .with_forbidden_paths(vec![PathBuf::from("bad\npath")]),
    ] {
        let err = WorkspaceTools::new(config).expect_err("invalid scope should be rejected");
        assert!(matches!(
            err,
            WorkspaceToolConfigError::InvalidScopePath { .. }
        ));
    }
}

#[test]
fn into_registered_tools_exposes_read_file_only() {
    let temp = TempWorkspace::new("registered-tools");
    let tools = tools_for(temp.path()).into_registered_tools();
    let names: Vec<_> = tools
        .iter()
        .map(|tool| tool.spec().name().as_str())
        .collect();

    assert_eq!(names, [READ_FILE_TOOL]);
    assert!(
        tools
            .iter()
            .all(|tool| tool.concurrency() == ToolConcurrency::ParallelSafe)
    );
}

#[test]
fn patch_tool_registration_is_opt_in_and_workspace_write() {
    let temp = TempWorkspace::new("registered-patch-tools");

    let read_only_tools = tools_for(temp.path()).into_registered_tools();
    assert!(
        read_only_tools
            .iter()
            .all(|tool| tool.spec().name().as_str() != APPLY_PATCH_TOOL)
    );

    let tools = tools_for(temp.path()).into_registered_tools_with_patch();
    let patch = tools
        .iter()
        .find(|tool| tool.spec().name().as_str() == APPLY_PATCH_TOOL)
        .expect("patch tool should be registered only by opt-in method");
    assert_eq!(patch.action_kind(), ToolActionKind::WorkspaceWrite);
    assert_eq!(patch.concurrency(), ToolConcurrency::Exclusive);
    assert!(patch.proposals_enabled());
    assert_eq!(tools.len(), 2);
    assert!(
        tools
            .iter()
            .filter(|tool| tool.spec().name().as_str() != APPLY_PATCH_TOOL)
            .all(|tool| {
                tool.action_kind() == ToolActionKind::ReadOnly && !tool.proposals_enabled()
            })
    );
}

#[test]
fn hidden_path_components_are_ordinary_spelling() {
    let temp = TempWorkspace::new("allow-hidden");
    temp.write_text(".secret", "ok\n");
    let tools = tools_for(temp.path());

    let outcome = read_outcome(&tools, ".secret");
    assert_eq!(
        outcome.status(),
        ToolCallResultStatus::Succeeded,
        "a leading dot is ordinary spelling, not a path policy the tool enforces"
    );
    assert_eq!(json_content(&outcome)["content"], "ok\n");
}

#[test]
fn non_utf8_component_is_rejected_when_constructible() {
    let path = PathBuf::from(OsStr::new("plain"));
    let text = path.to_str().expect("plain path is utf8");
    let validated = validate_workspace_path_argument(text, Path::new("/workspace"))
        .expect("plain path validates");
    assert_eq!(validated.display, "plain");
}