import json
import sys


# Validate if notebook cells were executed in order

if __name__ == '__main__':
    if len(sys.argv) > 0:
        path = sys.argv[1]
    else:
        raise RuntimeError("no path provided")

    if not path.endswith(".ipynb"):
        raise RuntimeError(f"{path} is not a notebook")

    with open(path, "r") as file:
        notebook = json.load(file)

        prev_execution_count = 0
        for i, cell in enumerate(notebook["cells"]):
            try:
                execution_count = cell["execution_count"]

                if execution_count is None:
                    continue

                if execution_count != prev_execution_count + 1:
                    raise AssertionError(f"cell {i} was executed out of order")

                prev_execution_count = execution_count
            except KeyError:
                pass