@@ -457,6 +457,38 @@ final class GenerativeModelVertexAITests: XCTestCase {
457
457
XCTAssertEqual ( response. text, textPart. text)
458
458
}
459
459
460
+ func testGenerateContent_success_codeExecution( ) async throws {
461
+ MockURLProtocol . requestHandler = try GenerativeModelTestUtil . httpRequestHandler (
462
+ forResource: " unary-success-code-execution " ,
463
+ withExtension: " json " ,
464
+ subdirectory: vertexSubdirectory
465
+ )
466
+
467
+ let response = try await model. generateContent ( testPrompt)
468
+
469
+ XCTAssertEqual ( response. candidates. count, 1 )
470
+ let candidate = try XCTUnwrap ( response. candidates. first)
471
+ let parts = candidate. content. parts
472
+ XCTAssertEqual ( candidate. finishReason, . stop)
473
+ XCTAssertEqual ( parts. count, 4 )
474
+ let textPart1 = try XCTUnwrap ( parts [ 0 ] as? TextPart )
475
+ XCTAssertFalse ( textPart1. isThought)
476
+ XCTAssertTrue ( textPart1. text. hasPrefix ( " To find the sum of the first 5 prime numbers " ) )
477
+ let executableCodePart = try XCTUnwrap ( parts [ 1 ] as? ExecutableCodePart )
478
+ XCTAssertFalse ( executableCodePart. isThought)
479
+ XCTAssertEqual ( executableCodePart. language, . python)
480
+ XCTAssertTrue ( executableCodePart. code. starts ( with: " prime_numbers = [2, 3, 5, 7, 11] " ) )
481
+ let codeExecutionResultPart = try XCTUnwrap ( parts [ 2 ] as? CodeExecutionResultPart )
482
+ XCTAssertFalse ( codeExecutionResultPart. isThought)
483
+ XCTAssertEqual ( codeExecutionResultPart. outcome, . ok)
484
+ XCTAssertEqual ( codeExecutionResultPart. output, " The sum of the first 5 prime numbers is: 28 \n " )
485
+ let textPart2 = try XCTUnwrap ( parts [ 3 ] as? TextPart )
486
+ XCTAssertFalse ( textPart2. isThought)
487
+ XCTAssertEqual (
488
+ textPart2. text, " The sum of the first 5 prime numbers (2, 3, 5, 7, and 11) is 28. "
489
+ )
490
+ }
491
+
460
492
func testGenerateContent_success_image_invalidSafetyRatingsIgnored( ) async throws {
461
493
MockURLProtocol . requestHandler = try GenerativeModelTestUtil . httpRequestHandler (
462
494
forResource: " unary-success-image-invalid-safety-ratings " ,
@@ -1405,6 +1437,39 @@ final class GenerativeModelVertexAITests: XCTestCase {
1405
1437
XCTAssertTrue ( text. hasPrefix ( " The sky is blue due to a phenomenon " ) )
1406
1438
}
1407
1439
1440
+ func testGenerateContentStream_success_codeExecution( ) async throws {
1441
+ MockURLProtocol . requestHandler = try GenerativeModelTestUtil . httpRequestHandler (
1442
+ forResource: " streaming-success-code-execution " ,
1443
+ withExtension: " txt " ,
1444
+ subdirectory: vertexSubdirectory
1445
+ )
1446
+
1447
+ var parts = [ any Part ] ( )
1448
+ let stream = try model. generateContentStream ( testPrompt)
1449
+ for try await response in stream {
1450
+ if let responseParts = response. candidates. first? . content. parts {
1451
+ parts. append ( contentsOf: responseParts)
1452
+ }
1453
+ }
1454
+
1455
+ let thoughtParts = parts. filter { $0. isThought }
1456
+ XCTAssertEqual ( thoughtParts. count, 0 )
1457
+ let textParts = parts. filter { $0 is TextPart }
1458
+ XCTAssertGreaterThan ( textParts. count, 0 )
1459
+ let executableCodeParts = parts. compactMap { $0 as? ExecutableCodePart }
1460
+ XCTAssertEqual ( executableCodeParts. count, 1 )
1461
+ let executableCodePart = try XCTUnwrap ( executableCodeParts. first)
1462
+ XCTAssertFalse ( executableCodePart. isThought)
1463
+ XCTAssertEqual ( executableCodePart. language, . python)
1464
+ XCTAssertTrue ( executableCodePart. code. starts ( with: " prime_numbers = [2, 3, 5, 7, 11] " ) )
1465
+ let codeExecutionResultParts = parts. compactMap { $0 as? CodeExecutionResultPart }
1466
+ XCTAssertEqual ( codeExecutionResultParts. count, 1 )
1467
+ let codeExecutionResultPart = try XCTUnwrap ( codeExecutionResultParts. first)
1468
+ XCTAssertFalse ( codeExecutionResultPart. isThought)
1469
+ XCTAssertEqual ( codeExecutionResultPart. outcome, . ok)
1470
+ XCTAssertEqual ( codeExecutionResultPart. output, " The sum of the first 5 prime numbers is: 28 \n " )
1471
+ }
1472
+
1408
1473
func testGenerateContentStream_successWithInvalidSafetyRatingsIgnored( ) async throws {
1409
1474
MockURLProtocol . requestHandler = try GenerativeModelTestUtil . httpRequestHandler (
1410
1475
forResource: " streaming-success-image-invalid-safety-ratings " ,
0 commit comments