44import contextlib
55import json
66import sys
7+ from datetime import datetime , timezone
78from typing import Any , Callable , cast
9+ from unittest .mock import AsyncMock , Mock
810
911import pytest
1012import websockets .server
1113from lazy_object_proxy import Proxy
1214
13- from apify_shared .consts import ApifyEnvVars
15+ from apify_shared .consts import ActorEnvVars , ApifyEnvVars
1416from crawlee .events ._types import Event , EventPersistStateData
1517
1618import apify ._actor
@@ -129,6 +131,7 @@ async def test_actor_handles_migrating_event_correctly(monkeypatch: pytest.Monke
129131 # the Actor automatically emits the PERSIST_STATE event with data `{'isMigrating': True}`
130132 monkeypatch .setenv (ApifyEnvVars .PERSIST_STATE_INTERVAL_MILLIS , '500' )
131133 monkeypatch .setenv (ApifyEnvVars .IS_AT_HOME , '1' )
134+ monkeypatch .setenv (ActorEnvVars .RUN_ID , 'asdf' )
132135
133136 persist_state_events_data = []
134137
@@ -143,6 +146,37 @@ async def handler(websocket: websockets.server.WebSocketServerProtocol) -> None:
143146 port : int = ws_server .sockets [0 ].getsockname ()[1 ] # type: ignore[index]
144147 monkeypatch .setenv (ApifyEnvVars .ACTOR_EVENTS_WS_URL , f'ws://localhost:{ port } ' )
145148
149+ mock_run_client = Mock ()
150+ mock_run_client .run .return_value .get = AsyncMock (
151+ side_effect = lambda : {
152+ 'id' : 'asdf' ,
153+ 'actId' : 'asdf' ,
154+ 'userId' : 'adsf' ,
155+ 'startedAt' : datetime .now (timezone .utc ),
156+ 'status' : 'RUNNING' ,
157+ 'meta' : {'origin' : 'DEVELOPMENT' },
158+ 'stats' : {
159+ 'inputBodyLen' : 99 ,
160+ 'restartCount' : 0 ,
161+ 'resurrectCount' : 0 ,
162+ 'computeUnits' : 1 ,
163+ },
164+ 'options' : {
165+ 'build' : 'asdf' ,
166+ 'timeoutSecs' : 4 ,
167+ 'memoryMbytes' : 1024 ,
168+ 'diskMbytes' : 1024 ,
169+ },
170+ 'buildId' : 'hjkl' ,
171+ 'defaultDatasetId' : 'hjkl' ,
172+ 'defaultKeyValueStoreId' : 'hjkl' ,
173+ 'defaultRequestQueueId' : 'hjkl' ,
174+ 'containerUrl' : 'https://hjkl' ,
175+ }
176+ )
177+
178+ monkeypatch .setattr (Actor ._charging_manager , '_client' , mock_run_client )
179+
146180 async with Actor :
147181 Actor .on (Event .PERSIST_STATE , log_persist_state )
148182 await asyncio .sleep (2 )
0 commit comments