File size: 5,256 Bytes
6155b26
 
b05b6f5
 
 
6155b26
 
 
 
 
 
 
754345f
 
 
6155b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b05b6f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import json

import pytest
from pydantic import ValidationError

from agent import config as config_module


def _write_json(path, data):
    path.write_text(json.dumps(data), encoding="utf-8")


def test_load_config_does_not_apply_slack_user_defaults_by_default(
    tmp_path, monkeypatch
):
    config_path = tmp_path / "config.json"
    _write_json(
        config_path,
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": False,
                "destinations": {},
            },
        },
    )
    monkeypatch.setenv("SLACK_BOT_TOKEN", "xoxb-test")
    monkeypatch.setenv("SLACK_CHANNEL_ID", "C123")

    config = config_module.load_config(str(config_path))

    assert not config.messaging.enabled
    assert config.messaging.destinations == {}


def test_load_config_applies_slack_user_defaults_from_env(tmp_path, monkeypatch):
    config_path = tmp_path / "config.json"
    _write_json(config_path, {"model_name": "moonshotai/Kimi-K2.6"})
    monkeypatch.delenv("ML_INTERN_CLI_CONFIG", raising=False)
    monkeypatch.setattr(
        config_module,
        "DEFAULT_USER_CONFIG_PATH",
        tmp_path / "missing-user-config.json",
    )
    monkeypatch.setenv("SLACK_BOT_TOKEN", "xoxb-test")
    monkeypatch.setenv("SLACK_CHANNEL_ID", "C123")

    config = config_module.load_config(str(config_path), include_user_defaults=True)

    assert config.messaging.enabled
    assert config.messaging.auto_event_types == [
        "approval_required",
        "error",
        "turn_complete",
    ]
    destination = config.messaging.destinations["slack.default"]
    assert destination.token == "xoxb-test"
    assert destination.channel == "C123"
    assert destination.allow_agent_tool
    assert destination.allow_auto_events


def test_load_config_merges_user_config_before_env_substitution(tmp_path, monkeypatch):
    config_path = tmp_path / "config.json"
    user_config_path = tmp_path / "user-config.json"
    _write_json(config_path, {"model_name": "moonshotai/Kimi-K2.6"})
    _write_json(
        user_config_path,
        {
            "messaging": {
                "enabled": True,
                "auto_event_types": ["approval_required"],
                "destinations": {
                    "slack.team": {
                        "provider": "slack",
                        "token": "${USER_SLACK_TOKEN}",
                        "channel": "C999",
                        "allow_agent_tool": False,
                        "allow_auto_events": True,
                    },
                },
            },
        },
    )
    monkeypatch.setenv("ML_INTERN_CLI_CONFIG", str(user_config_path))
    monkeypatch.setenv("ML_INTERN_SLACK_NOTIFICATIONS", "0")
    monkeypatch.setenv("USER_SLACK_TOKEN", "xoxb-user")

    config = config_module.load_config(str(config_path), include_user_defaults=True)

    assert config.messaging.enabled
    assert config.messaging.auto_event_types == ["approval_required"]
    assert set(config.messaging.destinations) == {"slack.team"}
    destination = config.messaging.destinations["slack.team"]
    assert destination.token == "xoxb-user"
    assert destination.channel == "C999"
    assert not destination.allow_agent_tool
    assert destination.allow_auto_events


def test_slack_user_defaults_can_be_disabled(tmp_path, monkeypatch):
    config_path = tmp_path / "config.json"
    _write_json(
        config_path,
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": False,
                "destinations": {},
            },
        },
    )
    monkeypatch.delenv("ML_INTERN_CLI_CONFIG", raising=False)
    monkeypatch.setattr(
        config_module,
        "DEFAULT_USER_CONFIG_PATH",
        tmp_path / "missing-user-config.json",
    )
    monkeypatch.setenv("ML_INTERN_SLACK_NOTIFICATIONS", "false")
    monkeypatch.setenv("SLACK_BOT_TOKEN", "xoxb-test")
    monkeypatch.setenv("SLACK_CHANNEL_ID", "C123")

    config = config_module.load_config(str(config_path), include_user_defaults=True)

    assert not config.messaging.enabled
    assert config.messaging.destinations == {}


def test_tool_runtime_defaults_to_local(tmp_path):
    config_path = tmp_path / "config.json"
    _write_json(config_path, {"model_name": "moonshotai/Kimi-K2.6"})

    config = config_module.load_config(str(config_path))

    assert config.tool_runtime == "local"


def test_user_config_can_set_sandbox_tool_runtime(tmp_path, monkeypatch):
    config_path = tmp_path / "config.json"
    user_config_path = tmp_path / "user-config.json"
    _write_json(config_path, {"model_name": "moonshotai/Kimi-K2.6"})
    _write_json(user_config_path, {"tool_runtime": "sandbox"})
    monkeypatch.setenv("ML_INTERN_CLI_CONFIG", str(user_config_path))

    config = config_module.load_config(str(config_path), include_user_defaults=True)

    assert config.tool_runtime == "sandbox"


def test_invalid_tool_runtime_is_rejected(tmp_path):
    config_path = tmp_path / "config.json"
    _write_json(
        config_path,
        {"model_name": "moonshotai/Kimi-K2.6", "tool_runtime": "hybrid"},
    )

    with pytest.raises(ValidationError):
        config_module.load_config(str(config_path))