File size: 4,390 Bytes
0646b18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Simple tests ensuring CLI interface can be called, with underlying functionality mocked.
"""

import datetime
import re
from cuga.cli import app
from typer.testing import CliRunner

runner = CliRunner()


def test_health_check(monkeypatch):
    from cuga.backend.memory import Memory

    monkeypatch.setattr(Memory, "health_check", lambda self: False)

    result = runner.invoke(app, ["memory", "namespace", "create", "foobar", "--user-id", "baz"])
    assert result.exit_code == 1
    assert result.output == "Memory service is not running.\n"


def test_create_namespace(monkeypatch):
    from cuga.backend.memory import Memory, Namespace

    def create_namespace(self, namespace_id, user_id, agent_id, app_id):
        return Namespace(id="foobar", created_at=datetime.datetime.now(datetime.UTC), user_id="baz")

    monkeypatch.setattr(Memory, "health_check", lambda self: True)
    monkeypatch.setattr(Memory, "create_namespace", create_namespace)

    result = runner.invoke(app, ["memory", "namespace", "create", "foobar", "--user-id", "baz"])
    assert result.exit_code == 0
    assert result.output == "Created namespace `foobar`\n"


def test_create_namespace_already_exists(monkeypatch):
    from cuga.backend.memory import Memory, APIRequestException

    def create_namespace(self, namespace_id, user_id, agent_id, app_id):
        raise APIRequestException('409')

    monkeypatch.setattr(Memory, "health_check", lambda self: True)
    monkeypatch.setattr(Memory, "create_namespace", create_namespace)

    result = runner.invoke(app, ["memory", "namespace", "create", "foobar", "--user-id", "baz"])
    assert result.exit_code == 1
    assert result.output == "Namespace `foobar` already exists.\n"


def test_get_namespace_details(monkeypatch):
    from cuga.backend.memory import Memory, Namespace

    created_at = datetime.datetime.now(datetime.UTC)

    def get_namespace_details(self, namespace_id):
        return Namespace(id="foobar", created_at=created_at, user_id="baz")

    monkeypatch.setattr(Memory, "health_check", lambda self: True)
    monkeypatch.setattr(Memory, "get_namespace_details", get_namespace_details)

    result = runner.invoke(app, ["memory", "namespace", "details", "foobar"])
    assert result.exit_code == 0
    header = re.compile(r"┃ ID\s+┃ Created At\s+┃ User ID\s+┃ Agent ID\s+┃ Application ID\s+┃ Entities\s+┃")
    assert header.search(result.output) is not None
    assert "foobar" in result.output
    assert "baz" in result.output
    assert created_at.isoformat()[:10] in result.output


def test_get_namespace_details_not_found(monkeypatch):
    from cuga.backend.memory import Memory, NamespaceNotFoundException

    def get_namespace_details(self, namespace_id):
        raise NamespaceNotFoundException()

    monkeypatch.setattr(Memory, "health_check", lambda self: True)
    monkeypatch.setattr(Memory, "get_namespace_details", get_namespace_details)

    result = runner.invoke(app, ["memory", "namespace", "details", "foobar"])
    assert result.exit_code == 1
    assert result.output == "Namespace `foobar` not found.\n"


def test_search_namespaces(monkeypatch):
    from cuga.backend.memory import Memory, Namespace

    created_at = datetime.datetime.now(datetime.UTC)

    def search_namespaces(self, user_id, agent_id, app_id, limit):
        return [Namespace(id="foobar", created_at=created_at, user_id="baz")]

    monkeypatch.setattr(Memory, "health_check", lambda self: True)
    monkeypatch.setattr(Memory, "search_namespaces", search_namespaces)

    result = runner.invoke(app, ["memory", "namespace", "search"])
    assert result.exit_code == 0
    header = re.compile(r"┃ ID\s+┃ Created At\s+┃ User ID\s+┃ Agent ID\s+┃ Application ID\s+┃")
    assert header.search(result.output) is not None
    assert "foobar" in result.output
    assert "baz" in result.output
    assert created_at.isoformat()[:10] in result.output


def test_delete_namespace(monkeypatch):
    from cuga.backend.memory import Memory

    def delete_namespace(self, namespace_id):
        pass

    monkeypatch.setattr(Memory, "health_check", lambda self: True)
    monkeypatch.setattr(Memory, "delete_namespace", delete_namespace)

    result = runner.invoke(app, ["memory", "namespace", "delete", "foobar"])
    assert result.exit_code == 0
    assert result.output == "Deleted namespace `foobar`\n"