@@ -325,7 +325,8 @@ describe('AiHistoryStorage', () => {
325325 } ) ;
326326
327327 it ( 'should limit the amount of stored images' , async ( ) => {
328- const storage = getStorage ( 2 ) ;
328+ const MAX_STORAGE_SIZE = 2 ;
329+ const storage = getStorage ( MAX_STORAGE_SIZE ) ;
329330
330331 await storage . upsertImage ( {
331332 id : 'image-id1' ,
@@ -402,5 +403,115 @@ describe('AiHistoryStorage', () => {
402403 'this is more than 80 characters because I\'m just going to keep typing words and …' ) ;
403404 } ) ;
404405 } ) ;
406+
407+ describe ( 'addHistoryItem' , ( ) => {
408+ const historyItem1 : AiAssistance . ResponseData = {
409+ type : AiAssistance . ResponseType . USER_QUERY ,
410+ query : 'text' ,
411+ imageInput : {
412+ inlineData : {
413+ data : '1' ,
414+ mimeType : 'image/jpeg' ,
415+ }
416+ } ,
417+ imageId : 'image-id1' ,
418+ } ;
419+ const historyItem2 : AiAssistance . ResponseData = {
420+ type : AiAssistance . ResponseType . USER_QUERY ,
421+ query : 'text' ,
422+ imageInput : {
423+ inlineData : {
424+ data : '2' ,
425+ mimeType : 'image/jpeg' ,
426+ }
427+ } ,
428+ imageId : 'image-id2' ,
429+ } ;
430+
431+ it ( 'should store images and text conversation separately' , async ( ) => {
432+ const storage = getStorage ( ) ;
433+ sinon . stub ( AiAssistance . AiHistoryStorage , 'instance' ) . returns ( storage ) ;
434+ const conversation1 = new AiAssistance . Conversation ( AiAssistance . ConversationType . STYLING , [ ] , 'id1' , false ) ;
435+ await conversation1 . addHistoryItem ( historyItem1 ) ;
436+ const conversation2 = new AiAssistance . Conversation ( AiAssistance . ConversationType . STYLING , [ ] , 'id2' , false ) ;
437+ await conversation2 . addHistoryItem ( historyItem2 ) ;
438+
439+ const imageHistory = storage . getImageHistory ( ) ;
440+ assert . lengthOf ( imageHistory , 2 ) ;
441+ assert . deepEqual ( imageHistory [ 0 ] , {
442+ id : 'image-id1' ,
443+ data : '1' ,
444+ mimeType : 'image/jpeg' ,
445+ } ) ;
446+ assert . deepEqual ( imageHistory [ 1 ] , {
447+ id : 'image-id2' ,
448+ data : '2' ,
449+ mimeType : 'image/jpeg' ,
450+ } ) ;
451+
452+ const historyWithoutImages = storage . getHistory ( ) ;
453+ assert . lengthOf ( historyWithoutImages , 2 ) ;
454+ assert . deepEqual ( historyWithoutImages [ 0 ] , {
455+ id : 'id1' ,
456+ type : AiAssistance . ConversationType . STYLING ,
457+ history : [ {
458+ type : AiAssistance . ResponseType . USER_QUERY ,
459+ query : 'text' ,
460+ imageId : 'image-id1' ,
461+ } ]
462+ } ) ;
463+ assert . deepEqual ( historyWithoutImages [ 1 ] , {
464+ id : 'id2' ,
465+ type : AiAssistance . ConversationType . STYLING ,
466+ history : [ {
467+ type : AiAssistance . ResponseType . USER_QUERY ,
468+ query : 'text' ,
469+ imageInput : undefined ,
470+ imageId : 'image-id2' ,
471+ } ]
472+ } ) ;
473+ } ) ;
474+
475+ it ( 'should have empty image data for image not present in history' , async ( ) => {
476+ const MAX_STORAGE_SIZE = 1 ;
477+ const storage = getStorage ( MAX_STORAGE_SIZE ) ;
478+ sinon . stub ( AiAssistance . AiHistoryStorage , 'instance' ) . returns ( storage ) ;
479+ const conversation1 = new AiAssistance . Conversation ( AiAssistance . ConversationType . STYLING , [ ] , 'id1' , false ) ;
480+ await conversation1 . addHistoryItem ( historyItem1 ) ;
481+ const conversation2 = new AiAssistance . Conversation ( AiAssistance . ConversationType . STYLING , [ ] , 'id2' , false ) ;
482+ await conversation2 . addHistoryItem ( historyItem2 ) ;
483+
484+ const imageHistory = storage . getImageHistory ( ) ;
485+ assert . lengthOf ( imageHistory , 1 ) ;
486+ const historyWithoutImages = storage . getHistory ( ) ;
487+ assert . lengthOf ( historyWithoutImages , 2 ) ;
488+ const conversationFromHistory = historyWithoutImages . map ( item => {
489+ return new AiAssistance . Conversation ( item . type , item . history , item . id , true ) ;
490+ } ) ;
491+ assert . lengthOf ( conversationFromHistory , 2 ) ;
492+ assert . deepEqual ( conversationFromHistory [ 0 ] . history , [ {
493+ type : AiAssistance . ResponseType . USER_QUERY ,
494+ query : 'text' ,
495+ imageInput : {
496+ inlineData : {
497+ data : AiAssistance . NOT_FOUND_IMAGE_DATA ,
498+ mimeType : 'image/jpeg' ,
499+ }
500+ } ,
501+ imageId : 'image-id1' ,
502+ } ] ) ;
503+ assert . deepEqual ( conversationFromHistory [ 1 ] . history , [ {
504+ type : AiAssistance . ResponseType . USER_QUERY ,
505+ query : 'text' ,
506+ imageInput : {
507+ inlineData : {
508+ data : '2' ,
509+ mimeType : 'image/jpeg' ,
510+ }
511+ } ,
512+ imageId : 'image-id2' ,
513+ } ] ) ;
514+ } ) ;
515+ } ) ;
405516 } ) ;
406517} ) ;
0 commit comments