Exemple #1
0
 def corrected_fens(self, fens: list[list[str]]) -> list[list[str]]:
     return Mappable(fens).replaced_with_disjoint(
         Mappable.indice_to_disjoint([
             (self.last_color_indexed(fens, "w"),
              lambda i: fens[i] + [FEN.black_end()]),
             (self.last_color_indexed(fens, "b"),
              lambda i: fens[i] + [FEN.white_end()]),
         ]))
Exemple #2
0
 def generated(self) -> list[tuple]:
     return [
         (
             [FEN.starting(), FEN.first()],
             status_type(self.rest_prefix),
             ColoredTrace(
                 Trace(self.common_white.fens, self.common_white.sans, self.additional_results[i]),
                 Trace(self.common_black.fens, self.common_black.sans, self.additional_results[i]),
             ),
             producable,
         )
         for status_type, i in [(FakeCheckmateStatus, 0), (FakeStalemateStatus, 1)]
         for producable in [FiniteTraceProducable(3), InfiniteTraceProducable()]
     ]
Exemple #3
0
async def test_trajectory_not_found(async_client: AsyncClient) -> None:
    response = await async_client.post(
        url="/player/trajectory",
        json={
            "fens": [FEN.starting(), FEN.first()],
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
            "step": 3,
        },
    )

    assert response.status_code == status.HTTP_404_NOT_FOUND
Exemple #4
0
async def test_trajectory_player(container: Container) -> None:
    container.api_info.override(providers.Factory(PlayerAPIInfo, name="trajectory", method="post"))

    assert (
        await TrajectoryPlayer.from_usecase(FakeTrajectory({})).intent.dispatch(
            PlayerTrajectoryRequest(
                fens=[FEN.starting(), FEN.first()],
                white=PlayerAIInfo(url="http://test"),
                black=PlayerAIInfo(url="http://test"),
                step=3,
            )
        )
        == TrajectoryResponseToDTO(
            (([[FEN.starting()] * 2] * 2) + ([[FEN.first()] * 2] * 2)), [[SAN.first()]] * 4, [[0, 0]] * 4
        ).convert()
    )
Exemple #5
0
async def test_game_intent(container: Container) -> None:
    container.api_info.override(providers.Factory(PlayerAPIInfo, name="game", method="post"))

    assert (
        await GamePlayer.from_usecase(FakeGame({})).intent.dispatch(
            PlayerGameRequest(white=PlayerAIInfo(url="http://test"), black=PlayerAIInfo(url="http://test"))
        )
        == GameResponseToDTO([FEN.starting()], [], "1-0").convert()
    )
Exemple #6
0
async def test_trajectory_unprocessable_entity(
        async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(
        side_effect=[Response(status.HTTP_422_UNPROCESSABLE_ENTITY, json={})])

    response = await async_client.post(
        url="/player/trajectory",
        json={
            "fens": [FEN.starting(), FEN.first()],
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
            "step": 3,
        },
    )

    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
Exemple #7
0
 async def request_to_responsable(self, request: TrajectoryRequestModel) -> TrajectoryResponsableModel:
     return TrajectoryResponseModel._make(
         ColoredTrace(
             Trace(
                 [[FEN.starting(), FEN.starting()], [FEN.starting(), FEN.starting()]],
                 [[SAN.first()], [SAN.first()]],
                 [[0, 0], [0, 0]],
             ),
             Trace(
                 [[FEN.first(), FEN.first()], [FEN.first(), FEN.first()]],
                 [[SAN.first()], [SAN.first()]],
                 [[0, 0], [0, 0]],
             ),
         ).concatenated()
     )
Exemple #8
0
async def test_game(async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [2],
                "legal_moves": [[]],
            },
        )
    ])

    response = await async_client.post(
        url="/player/game",
        json={
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json() == {
        "links": {
            rel: {
                "href": url.href
            }
            for rel, url in HALBase.from_routes_with_requested(
                {
                    "trajectory": "/player/trajectory",
                    "game": "/player/game",
                    "measurement": "/player/measurement",
                },
                "game",
                "post",
            ).links.items()
        },
        "fens": [FEN.starting()],
        "sans": [],
        "result": "1-0",
    }
Exemple #9
0
 async def request_to_responsable(
         self, request: GameRequestModel) -> GameResponsableModel:
     try:
         return ResultTrace._make(await ProducableTrace(
             Status(request.env),
             Movement(request.env, request.ai_white),
             Movement(request.env, request.ai_black),
             InfiniteTraceProducable(),
         ).produced([FEN.starting()])).to_response()
     except RequestError as ex:
         return RequestErrorResponseModel(
             f"An error occurred while requesting {ex.request.url!r}: {ex.args[0]!r}",
             "request.RequestError",
         )
     except HTTPStatusError as ex:
         return HTTPStatusErrorResponseModel(
             f"Error response {ex.response.status_code} " +
             f"while requesting {ex.request.url!r}: {ex.response.json()!r}",
             "request.HTTPStatusError",
         )
Exemple #10
0
async def test_measurement(async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [2, 3, 1],
                "legal_moves": [
                    [],
                    [],
                    [
                        "e4e5",
                        "e4e6",
                        "e4e7",
                        "f4e5",
                        "f4g5",
                        "f4h6",
                        "g4e5",
                        "g4f6",
                        "g4h6",
                        "h4g5",
                        "h5h6",
                    ],
                ],
            },
        ),
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [2],
                "legal_moves": [[]],
            },
        ),
    ])

    respx.post("http://fake-env/model/next-fen").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_fens": [FEN.first()]},
        )
    ])

    respx.post("http://fake-ai/ai/next-san").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_sans": [SAN.first()]},
        )
    ])

    response = await async_client.post(
        url="/player/measurement",
        json={
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
            "playtime": 3,
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json() == {
        "links": {
            rel: {
                "href": url.href
            }
            for rel, url in HALBase.from_routes_with_requested(
                {
                    "trajectory": "/player/trajectory",
                    "game": "/player/game",
                    "measurement": "/player/measurement",
                },
                "measurement",
                "post",
            ).links.items()
        },
        "white": {
            "score": 1.5,
            "win": 1,
            "draw": 1,
            "lose": 1
        },
        "black": {
            "score": 1.5,
            "win": 1,
            "draw": 1,
            "lose": 1
        },
    }
Exemple #11
0
async def test_trajectory(async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [1],
                "legal_moves": [[
                    "e4e5",
                    "e4e6",
                    "e4e7",
                    "f4e5",
                    "f4g5",
                    "f4h6",
                    "g4e5",
                    "g4f6",
                    "g4h6",
                    "h4g5",
                    "h5h6",
                ]],
            },
        )
    ] * 8)

    respx.post("http://fake-env/model/next-fen").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_fens": [FEN.first()]},
        ),
        Response(
            status.HTTP_200_OK,
            json={"next_fens": [FEN.starting()]},
        ),
    ] * 3)

    respx.post("http://fake-ai/ai/next-san").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_sans": [SAN.first()]},
        ),
    ] * 6)

    response = await async_client.post(
        url="/player/trajectory",
        json={
            "fens": [FEN.starting(), FEN.first()],
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
            "step": 3,
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json() == {
        "links": {
            rel: {
                "href": url.href
            }
            for rel, url in HALBase.from_routes_with_requested(
                {
                    "trajectory": "/player/trajectory",
                    "game": "/player/game",
                    "measurement": "/player/measurement",
                },
                "trajectory",
                "post",
            ).links.items()
        },
        "fens": (([[FEN.starting()] * 2] * 2) + ([[FEN.first()] * 2] * 2)),
        "sans": [[SAN.first()]] * 4,
        "results": [[0, 0]] * 4,
    }
Exemple #12
0
def test_moved() -> None:
    assert MovableTrace.from_FENs([FEN.starting(), FEN.starting(), FEN.first(), FEN.first()]).moved(
        OneStepTrace(Trace([[FEN.first()]], [[SAN.first()]], [[0], [0.5]]), [0, 1], [0]),
        OneStepTrace(Trace([[FEN.starting()]], [[SAN.first()]], [[0], [0.5]]), [2, 3], [2]),
    ) == MovableTrace(
        [[FEN.starting(), FEN.first()], [FEN.starting()], [FEN.first(), FEN.starting()], [FEN.first()]],
        [[SAN.first()], [], [SAN.first()], []],
        [[0], [0.5], [0], [0.5]],
    )
Exemple #13
0
    InfiniteTraceProducable,
    ITraceProducable,
    MovableTrace,
    OneStepTrace,
    ProducableTrace,
    SplitableTrace,
    Trace,
)


@pytest.mark.parametrize(
    "trace, corrected",
    [
        (
            CorrectableTrace(
                [[FEN.starting()], [FEN.first()], [FEN.starting()], [FEN.first()]],
                [[], [], [], []],
                [[1], [1], [0.5], [0.5]],
            ),
            CorrectableTrace(
                [[FEN.starting()], [FEN.first()], [FEN.starting()], [FEN.first()]],
                [[], [], [], []],
                [[1], [1], [0.5], [0.5]],
            ),
        ),
        (
            CorrectableTrace(
                [
                    [FEN.starting(), FEN.first()],
                    [FEN.first(), FEN.starting()],
                    [FEN.starting(), FEN.first()],
Exemple #14
0
 async def request_to_responsable(
         self, request: GameRequestModel) -> GameResponsableModel:
     return GameResponseModel([FEN.starting()], [], "1-0")