"""Characterization tests for the Knowledge Base API.

Written before extracting Knowledge Base into a plugin (pinning-flask-behavior):
the endpoints must behave identically whether KB is a core blueprint or a plugin
blueprint, since both register at /api/knowledgebase.
"""


def test_create_list_get_article(client, db, auth_headers):
    """Create an article, see it in the list, fetch it by id."""
    created = client.post('/api/knowledgebase',
                          json={'shortdescription': 'How to reset a printer',
                                'linkurl': 'https://kb.example/printer-reset',
                                'keywords': 'printer reset'},
                          headers=auth_headers)
    assert created.status_code == 201, created.get_json()
    linkid = created.get_json()['data']['linkid']

    listing = client.get('/api/knowledgebase', headers=auth_headers)
    assert listing.status_code == 200
    ids = [a['linkid'] for a in listing.get_json()['data']]
    assert linkid in ids

    fetched = client.get(f'/api/knowledgebase/{linkid}', headers=auth_headers)
    assert fetched.status_code == 200
    assert fetched.get_json()['data']['shortdescription'] == 'How to reset a printer'


def test_create_requires_shortdescription(client, db, auth_headers):
    """shortdescription is required."""
    resp = client.post('/api/knowledgebase',
                       json={'linkurl': 'https://kb.example/x'},
                       headers=auth_headers)
    assert resp.status_code == 400
