11"""Simple test showing the lazy client fix works."""
22
33import os
4- from unittest .mock import MagicMock , patch
54import sys
5+ from unittest .mock import MagicMock , patch
66
77
88def test_use_does_not_create_client_immediately ():
99 """Test that replicate.use() does not create a client until the model is called."""
10- sys .path .insert (0 , ' src' )
11-
10+ sys .path .insert (0 , " src" )
11+
1212 # Clear any existing token to simulate the original error condition
1313 with patch .dict (os .environ , {}, clear = True ):
1414 with patch .dict (sys .modules , {"cog" : None }):
1515 try :
1616 import replicate
17+
1718 # This should work now - no client is created yet
1819 model = replicate .use ("test/model" )
19-
20+
2021 # Verify we got a Function object back
2122 from replicate .lib ._predictions_use import Function
23+
2224 assert isinstance (model , Function )
2325 print ("✓ replicate.use() works without immediate client creation" )
24-
26+
2527 # Verify the client is stored as a callable (factory function)
2628 assert callable (model ._client )
2729 print ("✓ Client is stored as factory function" )
28-
30+
2931 except Exception as e :
3032 print (f"✗ Test failed: { e } " )
3133 raise
3234
3335
3436def test_client_created_when_model_called ():
3537 """Test that the client is created when the model is called."""
36- sys .path .insert (0 , ' src' )
37-
38+ sys .path .insert (0 , " src" )
39+
3840 # Mock the client creation to track when it happens
3941 created_clients = []
40-
41- def track_client_creation (* args , * *kwargs ):
42+
43+ def track_client_creation (** kwargs ):
4244 client = MagicMock ()
43- client .bearer_token = kwargs .get (' bearer_token' , ' no-token' )
45+ client .bearer_token = kwargs .get (" bearer_token" , " no-token" )
4446 created_clients .append (client )
4547 return client
46-
48+
4749 # Mock cog to provide a token
4850 mock_scope = MagicMock ()
4951 mock_scope .context .items .return_value = [("REPLICATE_API_TOKEN" , "cog-token" )]
5052 mock_cog = MagicMock ()
5153 mock_cog .current_scope .return_value = mock_scope
52-
54+
5355 with patch .dict (os .environ , {}, clear = True ):
5456 with patch .dict (sys .modules , {"cog" : mock_cog }):
55- with patch (' replicate._module_client._ModuleClient' , side_effect = track_client_creation ):
57+ with patch (" replicate._module_client._ModuleClient" , side_effect = track_client_creation ):
5658 import replicate
57-
59+
5860 # Create model function - should not create client yet
5961 model = replicate .use ("test/model" )
6062 assert len (created_clients ) == 0
6163 print ("✓ No client created when use() is called" )
62-
64+
6365 # Try to call the model - this should create a client
6466 try :
6567 model (prompt = "test" )
6668 except Exception :
6769 # Expected to fail due to mocking, but client should be created
6870 pass
69-
71+
7072 # Verify client was created with the cog token
7173 assert len (created_clients ) == 1
7274 assert created_clients [0 ].bearer_token == "cog-token"
@@ -76,4 +78,4 @@ def track_client_creation(*args, **kwargs):
7678if __name__ == "__main__" :
7779 test_use_does_not_create_client_immediately ()
7880 test_client_created_when_model_called ()
79- print ("\n ✓ All tests passed! The lazy client fix works correctly." )
81+ print ("\n ✓ All tests passed! The lazy client fix works correctly." )
0 commit comments