@@ -1242,7 +1242,73 @@ public async Task UpdateArtifactAsync_ShouldThrowOperationCanceledException_When
12421242 await cts . CancelAsync ( ) ;
12431243
12441244 // Act & Assert
1245- await Assert . ThrowsAsync < OperationCanceledException > ( ( ) =>
1246- taskManager . UpdateArtifactAsync ( "test-id" , artifact , cancellationToken : cts . Token ) ) ;
1247- }
1245+ await Assert . ThrowsAsync < OperationCanceledException > ( ( ) =>
1246+ taskManager . UpdateArtifactAsync ( "test-id" , artifact , cancellationToken : cts . Token ) ) ;
1247+ }
1248+
1249+ [ Fact ]
1250+ public async Task UpdateArtifactAsync_ShouldSetAppendFalse_WhenAppendTrueButNoExistingArtifacts ( )
1251+ {
1252+ // Arrange
1253+ var taskManager = new TaskManager ( ) ;
1254+ var task = await taskManager . CreateTaskAsync ( ) ;
1255+
1256+ // Send initial message to ensure event stream is registered
1257+ var sendParams = new MessageSendParams
1258+ {
1259+ Message = new Message
1260+ {
1261+ TaskId = task . Id ,
1262+ Parts = [ new TextPart { Text = "init" } ]
1263+ }
1264+ } ;
1265+ await taskManager . SendMessageAsync ( sendParams ) ;
1266+
1267+ var events = new List < TaskArtifactUpdateEvent > ( ) ;
1268+ var tcs = new TaskCompletionSource ( ) ;
1269+
1270+ // Capture events using the stream
1271+ var eventProcessor = Task . Run ( async ( ) =>
1272+ {
1273+ await foreach ( var evt in taskManager . SendMessageStreamAsync ( sendParams ) )
1274+ {
1275+ if ( ! tcs . Task . IsCompleted )
1276+ {
1277+ tcs . SetResult ( ) ;
1278+ }
1279+
1280+ if ( evt is TaskArtifactUpdateEvent artifactEvent )
1281+ {
1282+ events . Add ( artifactEvent ) ;
1283+ break ; // Just capture the first artifact event
1284+ }
1285+ }
1286+ } ) ;
1287+
1288+ await tcs . Task ; // Wait for event processor to start
1289+
1290+ // Act - Call with append=true when no artifacts exist
1291+ var artifact = new Artifact
1292+ {
1293+ ArtifactId = "test-artifact" ,
1294+ Name = "Test Artifact" ,
1295+ Parts = [ new TextPart { Text = "First content" } ]
1296+ } ;
1297+ await taskManager . UpdateArtifactAsync ( task . Id , artifact , append : true ) ;
1298+
1299+ await eventProcessor ;
1300+
1301+ // Assert
1302+ Assert . Single ( events ) ;
1303+ Assert . False ( events [ 0 ] . Append ) ; // Should be false because we created a new artifact, not appended
1304+ Assert . Equal ( "test-artifact" , events [ 0 ] . Artifact . ArtifactId ) ;
1305+ Assert . Single ( events [ 0 ] . Artifact . Parts ) ;
1306+
1307+ // Verify that the artifact was actually created
1308+ var retrievedTask = await taskManager . GetTaskAsync ( new TaskQueryParams { Id = task . Id } ) ;
1309+ Assert . NotNull ( retrievedTask ) ;
1310+ Assert . NotNull ( retrievedTask . Artifacts ) ;
1311+ Assert . Single ( retrievedTask . Artifacts ) ;
1312+ Assert . Equal ( "test-artifact" , retrievedTask . Artifacts [ 0 ] . ArtifactId ) ;
1313+ }
12481314}
0 commit comments