Repository: emcie-co/parlant
File: server/tests/test_loading_configuration.py (Python)

Code before:
    config_file = new_file()
    with open(config_file, "w") as f:
        f.write(
            json.dumps(
                {
                    "agents": [{"name": "Default Agent"}],
                    "guidelines": {"Default Agent": []},
                    "tools": {},
                }
            )
        )
    assert ConfigurationFileValidator().validate(config_file) is True


async def test_valid_config(
    valid_config: JSONSerializable,
    new_file: Callable[[], Path],
) -> None:
    config_file = new_file()
    with open(config_file, "w") as f:
        f.write(json.dumps(valid_config))

    assert ConfigurationFileValidator().validate(config_file) is True


async def test_invalid_tool(
    valid_config: JSONSerializable,
    new_file: Callable[[], Path],
) -> None:
    config_file = new_file()
    tool_file = new_file()

    invalid_config: JSONSerializable = copy.deepcopy(valid_config)
    invalid_config["tools"]["multiply"]["module_path"] = "invalid.path.to.multiply"  # type: ignore

    with open(config_file, "w") as f:
        f.write(json.dumps(invalid_config))

    assert ConfigurationFileValidator().validate(config_file) is False

    with open(tool_file, "w") as f:
        f.write("""def not_multiply(): return""")

    assert ConfigurationFileValidator().validate(config_file) is False


async def test_guideline_missing_mandatory_keys(
    valid_config: JSONSerializable,
    new_file: Callable[[], Path],
) -> None:
    config_file = new_file()

Review comment (bug):
"test_that_a_config_with_an_invalid_tool_fails_validation"

Or, better yet, explain the type of invalidity you're checking for; i.e. "test_that_a_config_with_a_tool_whose_module_cannot_be_found_fails_validation"

Code after:
                        "guidelines": {"Default Agent": []},
                        "tools": {},
                    }
                )
            )
        assert ConfigurationFileValidator().validate(config_file) is True


async def test_that_a_valid_config_passes_validation(
    valid_config: JSONSerializable,
) -> None:
    async with new_file_path() as config_file:
        with open(config_file, "w") as f:
            f.write(json.dumps(valid_config))

        assert ConfigurationFileValidator().validate(config_file) is True


async def test_that_a_config_with_a_tool_whose_module_cannot_be_found_fails_validation(
    valid_config: JSONSerializable,
) -> None:
    async with new_file_path() as config_file, new_file_path() as tool_file:
        invalid_config: JSONSerializable = copy.deepcopy(valid_config)
        invalid_config["tools"]["multiply"][  # type: ignore
            "module_path"
        ] = "invalid.path.to.multiply"

        with open(config_file, "w") as f:
            f.write(json.dumps(invalid_config))

        assert ConfigurationFileValidator().validate(config_file) is False

        with open(tool_file, "w") as f:
            f.write("""def not_multiply(): return""")

        assert ConfigurationFileValidator().validate(config_file) is False


async def test_that_a_config_with_missing_mandatory_keys_in_guideline_fails_validation(
    valid_config: JSONSerializable,
) -> None:
    async with new_file_path() as config_file:
        invalid_config = copy.deepcopy(valid_config)
        del invalid_config["guidelines"]["Default Agent"][0]["when"]  # type: ignore

        with open(config_file, "w") as f:
            f.write(json.dumps(invalid_config))

        assert ConfigurationFileValidator().validate(config_file) is False

        invalid_config = copy.deepcopy(valid_config)