from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, PartitionKey

import events.enroll as app


def test_enroll(
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    order_id = 'cpYSbBcie2NDbZhDKCxCih'
    event = {
        'detail': {
            'new_image': {
                'id': order_id,
                'sk': 'generated_items',
            }
        }
    }
    assert app.lambda_handler(event, lambda_context)  # type: ignore

    # Parent knows the child
    r = dynamodb_persistence_layer.collection.query(
        KeyPair(order_id, 'LINKED_ENTITIES#CHILD')
    )
    *_, enrollment_id = r['items'][0]['sk'].split('#')

    # Child knows the parent
    enrollment = dynamodb_persistence_layer.collection.get_item(
        KeyPair(enrollment_id, f'LINKED_ENTITIES#PARENT#ORDER#{order_id}'),
    )
    assert enrollment

    r = dynamodb_persistence_layer.collection.query(PartitionKey(enrollment['id']))
    assert not any(x['sk'] == 'METADATA#DEDUPLICATION_WINDOW' for x in r['items'])
