Home / Class/ TestFileOperations Class — langchain Architecture

TestFileOperations Class — langchain Architecture

Architecture documentation for the TestFileOperations class in test_anthropic_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d["TestFileOperations"]
  511a8f91_080f_9750_8245_6c31ee0b22eb["ToolMessage"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|extends| 511a8f91_080f_9750_8245_6c31ee0b22eb
  ed6269fc_1a0e_6b64_f39f_7fdd1e5bcdc1["test_anthropic_tools.py"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|defined in| ed6269fc_1a0e_6b64_f39f_7fdd1e5bcdc1
  968ba05f_3571_e3c2_2dcf_63f80fb85fde["test_view_operation()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 968ba05f_3571_e3c2_2dcf_63f80fb85fde
  504018cf_fdee_726a_8712_5f2a9d334ef2["test_create_operation()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 504018cf_fdee_726a_8712_5f2a9d334ef2
  47d36c15_194f_1061_2468_8b8b12b3e13a["test_path_prefix_enforcement()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 47d36c15_194f_1061_2468_8b8b12b3e13a
  7a04170c_cd7e_b9f1_73d9_3c2510c71fa9["test_memories_prefix_enforcement()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 7a04170c_cd7e_b9f1_73d9_3c2510c71fa9
  b9c2bb66_e5f2_e551_c93c_a3f0c702fbf3["test_str_replace_operation()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| b9c2bb66_e5f2_e551_c93c_a3f0c702fbf3
  13900572_2b4e_505d_2283_a8dae3bcae66["test_insert_operation()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 13900572_2b4e_505d_2283_a8dae3bcae66
  2879b5c1_0e81_d6c2_9f85_617653abebc3["test_delete_operation()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 2879b5c1_0e81_d6c2_9f85_617653abebc3
  03f2d9f0_599a_8474_5392_f1cc5f568beb["test_rename_operation()"]
  6ec57bb9_6c88_435d_d3c5_ccf498cb814d -->|method| 03f2d9f0_599a_8474_5392_f1cc5f568beb

Relationship Graph

Source Code

libs/partners/anthropic/tests/unit_tests/middleware/test_anthropic_tools.py lines 103–283

class TestFileOperations:
    """Test file operation implementations via wrap_tool_call."""

    def test_view_operation(self) -> None:
        """Test view command execution."""
        middleware = StateClaudeTextEditorMiddleware()

        state: AnthropicToolsState = {
            "messages": [],
            "text_editor_files": {
                "/test.txt": {
                    "content": ["line1", "line2", "line3"],
                    "created_at": "2025-01-01T00:00:00",
                    "modified_at": "2025-01-01T00:00:00",
                }
            },
        }

        args = {"command": "view", "path": "/test.txt"}
        result = middleware._handle_view(args, state, "test_id")

        assert isinstance(result, Command)
        assert result.update is not None
        messages = result.update.get("messages", [])
        assert len(messages) == 1
        assert isinstance(messages[0], ToolMessage)
        assert messages[0].content == "1|line1\n2|line2\n3|line3"
        assert messages[0].tool_call_id == "test_id"

    def test_create_operation(self) -> None:
        """Test create command execution."""
        middleware = StateClaudeTextEditorMiddleware()

        state: AnthropicToolsState = {"messages": []}

        args = {"command": "create", "path": "/test.txt", "file_text": "line1\nline2"}
        result = middleware._handle_create(args, state, "test_id")

        assert isinstance(result, Command)
        assert result.update is not None
        files = result.update.get("text_editor_files", {})
        assert "/test.txt" in files
        assert files["/test.txt"]["content"] == ["line1", "line2"]

    def test_path_prefix_enforcement(self) -> None:
        """Test that path prefixes are enforced."""
        middleware = StateClaudeTextEditorMiddleware(
            allowed_path_prefixes=["/workspace"]
        )

        state: AnthropicToolsState = {"messages": []}

        # Should fail with /etc/passwd
        args = {"command": "create", "path": "/etc/passwd", "file_text": "test"}

        with pytest.raises(ValueError, match="Path must start with"):
            middleware._handle_create(args, state, "test_id")

    def test_memories_prefix_enforcement(self) -> None:
        """Test that /memories prefix is enforced for memory middleware."""
        middleware = StateClaudeMemoryMiddleware()

        state: AnthropicToolsState = {"messages": []}

        # Should fail with /other/path
        args = {"command": "create", "path": "/other/path.txt", "file_text": "test"}

        with pytest.raises(ValueError, match="/memories"):
            middleware._handle_create(args, state, "test_id")

    def test_str_replace_operation(self) -> None:
        """Test str_replace command execution."""
        middleware = StateClaudeTextEditorMiddleware()

        state: AnthropicToolsState = {
            "messages": [],
            "text_editor_files": {
                "/test.txt": {
                    "content": ["Hello world", "Goodbye world"],
                    "created_at": "2025-01-01T00:00:00",
                    "modified_at": "2025-01-01T00:00:00",

Extends

Frequently Asked Questions

What is the TestFileOperations class?
TestFileOperations is a class in the langchain codebase, defined in libs/partners/anthropic/tests/unit_tests/middleware/test_anthropic_tools.py.
Where is TestFileOperations defined?
TestFileOperations is defined in libs/partners/anthropic/tests/unit_tests/middleware/test_anthropic_tools.py at line 103.
What does TestFileOperations extend?
TestFileOperations extends ToolMessage.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free