"""The owner list distinguishes saved manual labour from generated field labour."""
from test_fieldwork import app, client, captured, entry, review, timer, write


def test_time_list_retains_exact_origin_across_field_correction(app):
    owner = client(app)
    job, people, clock, timer_id = captured(owner, app)
    manual = entry(owner, job, people[0], duration_minutes=30)
    path = '/api/jobs/' + job['id'] + '/time'
    costs = owner.get('/api/jobs/' + job['id'] + '/costs').json()
    rows = owner.get(path).json()
    assert len(rows) == 2
    assert next(r for r in rows if r['id'] == manual['id'])['source_kind'] == 'manual'
    generated = next(r for r in rows if r['id'] != manual['id'])
    assert generated['source_kind'] == 'field'
    assert generated['duration_minutes'] == 2
    assert owner.get('/api/jobs/' + job['id'] + '/costs').json() == costs
    assert client(app, 1).get(path).status_code == 404
    assert write(owner, '/api/time/' + generated['id'] + '/archive',
                 {'version': generated['version']}).status_code == 409

    # Review archives the previous generated entry, preserving its origin.
    assert review(owner, job, timer_id, timer(owner, job, timer_id)['raw_intervals']).status_code == 200
    active = owner.get(path).json()
    archived = owner.get(path + '?archived=true').json()
    assert [r['id'] for r in active] == [manual['id']]
    assert active[0]['source_kind'] == 'manual'
    assert len(archived) == 1 and archived[0]['id'] == generated['id']
    assert archived[0]['source_kind'] == 'field'
