@@ -44,6 +44,17 @@ function makeModel(capture: Captured) {
4444 } ) ;
4545}
4646
47+ /** Poll until the mock model captures the system message (bounded), instead of a fixed sleep. */
48+ async function waitForSystemCaptured ( capture : Captured , timeoutMs = 1000 , intervalMs = 5 ) {
49+ const startedAt = Date . now ( ) ;
50+ while ( ! capture . system ) {
51+ if ( Date . now ( ) - startedAt > timeoutMs ) {
52+ throw new Error ( "Timed out waiting for system message capture" ) ;
53+ }
54+ await new Promise ( ( r ) => setTimeout ( r , intervalMs ) ) ;
55+ }
56+ }
57+
4758const SYSTEM = "You are a helpful assistant for tests." ;
4859
4960describe ( "chat prompt caching — system providerOptions" , ( ) => {
@@ -63,7 +74,7 @@ describe("chat prompt caching — system providerOptions", () => {
6374 const harness = mockChatAgent ( agent , { chatId : "pc-default" } ) ;
6475 try {
6576 await harness . sendMessage ( userMessage ( "hi" ) ) ;
66- await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
77+ await waitForSystemCaptured ( cap ) ;
6778 expect ( cap . system ?. content ) . toContain ( "helpful assistant" ) ;
6879 expect ( cap . system ?. providerOptions ) . toBeUndefined ( ) ;
6980 } finally {
@@ -92,7 +103,7 @@ describe("chat prompt caching — system providerOptions", () => {
92103 const harness = mockChatAgent ( agent , { chatId : "pc-sugar" } ) ;
93104 try {
94105 await harness . sendMessage ( userMessage ( "hi" ) ) ;
95- await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
106+ await waitForSystemCaptured ( cap ) ;
96107 expect ( cap . system ?. content ) . toContain ( "helpful assistant" ) ;
97108 expect ( cap . system ?. providerOptions ?. anthropic ?. cacheControl ) . toEqual ( { type : "ephemeral" } ) ;
98109 } finally {
@@ -123,7 +134,7 @@ describe("chat prompt caching — system providerOptions", () => {
123134 const harness = mockChatAgent ( agent , { chatId : "pc-explicit" } ) ;
124135 try {
125136 await harness . sendMessage ( userMessage ( "hi" ) ) ;
126- await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
137+ await waitForSystemCaptured ( cap ) ;
127138 expect ( cap . system ?. providerOptions ?. anthropic ?. cacheControl ) . toEqual ( {
128139 type : "ephemeral" ,
129140 ttl : "1h" ,
@@ -151,7 +162,7 @@ describe("chat prompt caching — system providerOptions", () => {
151162 const harness = mockChatAgent ( agent , { chatId : "pc-prompt-set" } ) ;
152163 try {
153164 await harness . sendMessage ( userMessage ( "hi" ) ) ;
154- await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
165+ await waitForSystemCaptured ( cap ) ;
155166 expect ( cap . system ?. providerOptions ?. anthropic ?. cacheControl ) . toEqual ( { type : "ephemeral" } ) ;
156167 } finally {
157168 await harness . close ( ) ;
@@ -183,7 +194,7 @@ describe("chat prompt caching — system providerOptions", () => {
183194 const harness = mockChatAgent ( agent , { chatId : "pc-precedence" } ) ;
184195 try {
185196 await harness . sendMessage ( userMessage ( "hi" ) ) ;
186- await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
197+ await waitForSystemCaptured ( cap ) ;
187198 // The call-site option wins (ttl: "1h"), not the prompt-set default.
188199 expect ( cap . system ?. providerOptions ?. anthropic ?. cacheControl ) . toEqual ( {
189200 type : "ephemeral" ,
0 commit comments