# SPDX-FileCopyrightText: 2024 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: LGPL-3.0-only

"""Test file for building the face_edge_connectivity"""
import numpy as np

from psy_ugrid import ugrid


def test_build_face_edge_connectivity():
    faces = [[0, 1, 2], [1, 2, 3]]
    edges = [[0, 1], [1, 2], [2, 0], [2, 3], [3, 1]]
    nodes = [1, 2, 3, 4]
    grid = ugrid.UGrid(
        node_lon=nodes, node_lat=nodes, faces=faces, edges=edges
    )

    ref = [[0, 1, 2], [1, 3, 4]]
    grid.build_face_edge_connectivity()

    assert grid.face_edge_connectivity.tolist() == ref


def test_build_face_edge_connectivity_na():
    faces = np.ma.array(
        [[0, 1, 2, 3], [1, 2, 3, -999]],
        mask=[[False, False, False, False], [False, False, False, True]],
    )
    edges = [[0, 1], [1, 2], [2, 3], [3, 0], [3, 1]]
    nodes = [1, 2, 3, 4]
    grid = ugrid.UGrid(
        node_lon=nodes, node_lat=nodes, faces=faces, edges=edges
    )

    ref = [[0, 1, 2, 3], [1, 2, 4, -999]]
    grid.build_face_edge_connectivity()

    assert grid.face_edge_connectivity.filled(-999).tolist() == ref
