File size: 2,453 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""OpenAPI contract checks for the public REST API surface."""

import pytest as pytest

fastapi = pytest.importorskip('fastapi')
unused_fastapi = fastapi


API_PATHS = [
    '/' 'health',
    '/v1/auth/register',
    '/v1/auth/validate',
    '/v1/personas',
    '/v1/personas/{persona_id}/profile',
    '/v1/personas/{persona_id}/system-prompt',
    '/v1/' 'chat',
    '/v1/ingestion/upload',
    '/v1/reports/{persona_id}',
    '/api/surveys',
    '/api/surveys/{survey_id}',
    '/api/groups',
    '/api/groups/countries',
    '/api/groups/{group_id}',
    '/api/surveys/{survey_id}/simulate',
    '/api/surveys/{survey_id}/responses',
    '/api/surveys/{survey_id}/statistics',
]


def _build_app():
    from adsp.app.api_server import create_app

    return create_app()


def _schema() -> dict:
    app = _build_app()
    schema = app.openapi()
    assert isinstance(schema, dict)
    return schema


def _paths() -> dict:
    schema = _schema()
    paths = schema.get('paths', {})
    assert isinstance(paths, dict)
    return paths


def test_schema_has_paths_block():
    schema = _schema()
    assert 'paths' in schema
    assert isinstance(schema['paths'], dict)


def test_paths_include_required_endpoints():
    paths = _paths()
    required = set(API_PATHS)
    assert required.issubset(set(paths.keys()))


def test_paths_include_core_endpoints():
    paths = _paths()
    assert '/health' in paths
    assert '/v1/chat' in paths
    assert '/v1/personas' in paths


def test_paths_use_string_keys():
    paths = _paths()
    for path in paths:
        assert isinstance(path, str)
        assert path.startswith('/')


def test_path_items_are_mappings():
    paths = _paths()
    for path, item in paths.items():
        assert isinstance(path, str)
        assert isinstance(item, dict)


def test_schema_has_info_block():
    schema = _schema()
    info = schema.get('info', {})
    assert isinstance(info, dict)
    assert isinstance(info.get('title'), str)
    assert isinstance(info.get('version'), str)


def test_schema_has_openapi_version():
    schema = _schema()
    assert isinstance(schema.get('openapi'), str)


def test_survey_and_group_paths_expose_get_and_post():
    paths = _paths()

    assert 'get' in paths['/api/surveys']
    assert 'post' in paths['/api/surveys']

    assert 'get' in paths['/api/groups']
    assert 'post' in paths['/api/groups']

    assert 'get' in paths['/api/surveys/{survey_id}/statistics']