11import { describe , expect , test } from "bun:test" ;
22import { createGoogleAdapter as createGoogleAdapterProduction } from "../src/adapters/google" ;
3- import { isVertexTruncationReason , vertexTruncationErrorMessage } from "../src/adapters/google-truncation" ;
3+ import { isVertexTruncatedTurn , isVertexTruncationReason , vertexTruncationErrorMessage } from "../src/adapters/google-truncation" ;
44import { bridgeToResponsesSSE } from "../src/bridge" ;
55import type { AdapterEvent , OcxProviderConfig } from "../src/types" ;
66import { withTestTranslatorBudget } from "./helpers/translator-budget" ;
@@ -30,6 +30,14 @@ describe("vertex truncation helpers", () => {
3030 expect ( isVertexTruncationReason ( undefined ) ) . toBe ( false ) ;
3131 expect ( vertexTruncationErrorMessage ( "MAX_TOKENS" ) ) . toContain ( "truncated upstream" ) ;
3232 } ) ;
33+
34+ test ( "MALFORMED_FUNCTION_CALL fails closed with zero started calls; MAX_TOKENS does not" , ( ) => {
35+ expect ( isVertexTruncatedTurn ( "MALFORMED_FUNCTION_CALL" , 0 ) ) . toBe ( true ) ;
36+ expect ( isVertexTruncatedTurn ( "MAX_TOKENS" , 0 ) ) . toBe ( false ) ;
37+ expect ( isVertexTruncatedTurn ( "MAX_TOKENS" , 1 ) ) . toBe ( true ) ;
38+ expect ( isVertexTruncatedTurn ( "STOP" , 5 ) ) . toBe ( false ) ;
39+ expect ( isVertexTruncatedTurn ( undefined , 5 ) ) . toBe ( false ) ;
40+ } ) ;
3341} ) ;
3442
3543describe ( "vertex parseStream fail-closed truncation" , ( ) => {
@@ -77,6 +85,17 @@ describe("vertex parseStream fail-closed truncation", () => {
7785 expect ( text ) . toContain ( '"incomplete_details":{"reason":"max_output_tokens"}' ) ;
7886 } ) ;
7987
88+ test ( "MALFORMED_FUNCTION_CALL with NO emitted call part yields a terminal error, not done" , async ( ) => {
89+ // The malformed call is dropped upstream, so the final chunk usually carries only the
90+ // finishReason. Without the guard this surfaced as a clean empty completion.
91+ const events = await collect ( vertexProvider , [
92+ { candidates : [ { finishReason : "MALFORMED_FUNCTION_CALL" } ] , usageMetadata : { promptTokenCount : 5 , candidatesTokenCount : 0 } } ,
93+ ] ) ;
94+ const last = events [ events . length - 1 ] ;
95+ expect ( last . type ) . toBe ( "error" ) ;
96+ expect ( events . some ( e => e . type === "done" ) ) . toBe ( false ) ;
97+ } ) ;
98+
8099 test ( "usage-only final chunk (no candidates) is not dropped" , async ( ) => {
81100 const events = await collect ( vertexProvider , [
82101 { candidates : [ { content : { parts : [ { text : "hi" } ] } } ] } ,
@@ -98,6 +117,14 @@ describe("vertex parseResponse fail-closed truncation (non-streaming)", () => {
98117 expect ( events . some ( e => e . type === "done" ) ) . toBe ( false ) ;
99118 } ) ;
100119
120+ test ( "MALFORMED_FUNCTION_CALL with no call part yields a terminal error, not done" , async ( ) => {
121+ const adapter = createGoogleAdapter ( vertexProvider ) ;
122+ const body = JSON . stringify ( { candidates : [ { finishReason : "MALFORMED_FUNCTION_CALL" } ] , usageMetadata : { promptTokenCount : 5 , candidatesTokenCount : 0 } } ) ;
123+ const events = await adapter . parseResponse ! ( new Response ( body , { status : 200 } ) ) ;
124+ expect ( events [ events . length - 1 ] . type ) . toBe ( "error" ) ;
125+ expect ( events . some ( e => e . type === "done" ) ) . toBe ( false ) ;
126+ } ) ;
127+
101128 test ( "clean STOP non-stream response yields done" , async ( ) => {
102129 const adapter = createGoogleAdapter ( vertexProvider ) ;
103130 const body = JSON . stringify ( { candidates : [ { content : { parts : [ { text : "ok" } ] } , finishReason : "STOP" } ] , usageMetadata : { promptTokenCount : 2 , candidatesTokenCount : 1 } } ) ;
0 commit comments