@@ -390,6 +390,149 @@ describe("VectorSearchEmbeddingsManager", () => {
390390 } ) ;
391391 } ) ;
392392
393+ describe ( "assertFieldsHaveCorrectEmbeddings" , ( ) => {
394+ it ( "does not throw for invalid documents when validation is disabled" , async ( ) => {
395+ const embeddings = new VectorSearchEmbeddingsManager (
396+ embeddingValidationDisabled ,
397+ connectionManager ,
398+ embeddingConfig
399+ ) ;
400+ await expect (
401+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
402+ { embedding_field : "some text" } ,
403+ { embedding_field : [ 1 , 2 , 3 ] } ,
404+ ] )
405+ ) . resolves . not . toThrow ( ) ;
406+ } ) ;
407+
408+ describe ( "when validation is enabled" , ( ) => {
409+ let embeddings : VectorSearchEmbeddingsManager ;
410+
411+ beforeEach ( ( ) => {
412+ embeddings = new VectorSearchEmbeddingsManager (
413+ embeddingValidationEnabled ,
414+ connectionManager ,
415+ embeddingConfig
416+ ) ;
417+ } ) ;
418+
419+ it ( "does not throw when all documents are valid" , async ( ) => {
420+ await expect (
421+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
422+ { embedding_field : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] } ,
423+ { embedding_field : [ 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ] } ,
424+ { field : "no embeddings here" } ,
425+ ] )
426+ ) . resolves . not . toThrow ( ) ;
427+ } ) ;
428+
429+ it ( "throws error when one document has wrong dimensions" , async ( ) => {
430+ await expect (
431+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
432+ { embedding_field : [ 1 , 2 , 3 ] } ,
433+ ] )
434+ ) . rejects . toThrow ( / F i e l d e m b e d d i n g _ f i e l d i s a n e m b e d d i n g w i t h 8 d i m e n s i o n s / ) ;
435+ } ) ;
436+
437+ it ( "throws error when one document has wrong type" , async ( ) => {
438+ await expect (
439+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
440+ { embedding_field : "some text" } ,
441+ ] )
442+ ) . rejects . toThrow ( / F i e l d e m b e d d i n g _ f i e l d i s a n e m b e d d i n g w i t h 8 d i m e n s i o n s / ) ;
443+ } ) ;
444+
445+ it ( "throws error when one document has non-numeric values" , async ( ) => {
446+ await expect (
447+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
448+ { embedding_field : [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" ] } ,
449+ ] )
450+ ) . rejects . toThrow ( / F i e l d e m b e d d i n g _ f i e l d i s a n e m b e d d i n g w i t h 8 d i m e n s i o n s / ) ;
451+ } ) ;
452+
453+ it ( "throws error with details about dimension mismatch" , async ( ) => {
454+ await expect (
455+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
456+ { embedding_field : [ 1 , 2 , 3 ] } ,
457+ ] )
458+ ) . rejects . toThrow ( / A c t u a l d i m e n s i o n s : 3 / ) ;
459+ } ) ;
460+
461+ it ( "throws error with details about quantization" , async ( ) => {
462+ await expect (
463+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
464+ { embedding_field : [ 1 , 2 , 3 ] } ,
465+ ] )
466+ ) . rejects . toThrow ( / a c t u a l q u a n t i z a t i o n : s c a l a r / ) ;
467+ } ) ;
468+
469+ it ( "throws error with details about error type" , async ( ) => {
470+ await expect (
471+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
472+ { embedding_field : [ 1 , 2 , 3 ] } ,
473+ ] )
474+ ) . rejects . toThrow ( / E r r o r : d i m e n s i o n - m i s m a t c h / ) ;
475+ } ) ;
476+
477+ it ( "throws error when multiple documents have invalid embeddings" , async ( ) => {
478+ try {
479+ await embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
480+ { embedding_field : [ 1 , 2 , 3 ] } ,
481+ { embedding_field : "some text" } ,
482+ ] ) ;
483+ expect . fail ( "Should have thrown an error" ) ;
484+ } catch ( error ) {
485+ expect ( ( error as Error ) . message ) . toContain ( "Field embedding_field" ) ;
486+ expect ( ( error as Error ) . message ) . toContain ( "dimension-mismatch" ) ;
487+ }
488+ } ) ;
489+
490+ it ( "handles documents with multiple invalid fields" , async ( ) => {
491+ try {
492+ await embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
493+ {
494+ embedding_field : [ 1 , 2 , 3 ] ,
495+ embedding_field_binary : "not binary" ,
496+ } ,
497+ ] ) ;
498+ expect . fail ( "Should have thrown an error" ) ;
499+ } catch ( error ) {
500+ expect ( ( error as Error ) . message ) . toContain ( "Field embedding_field" ) ;
501+ expect ( ( error as Error ) . message ) . toContain ( "Field embedding_field_binary" ) ;
502+ }
503+ } ) ;
504+
505+ it ( "handles mix of valid and invalid documents" , async ( ) => {
506+ try {
507+ await embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
508+ { embedding_field : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] } , // valid
509+ { embedding_field : [ 1 , 2 , 3 ] } , // invalid
510+ { valid_field : "no embeddings" } , // valid (no embedding field)
511+ ] ) ;
512+ expect . fail ( "Should have thrown an error" ) ;
513+ } catch ( error ) {
514+ expect ( ( error as Error ) . message ) . toContain ( "Field embedding_field" ) ;
515+ expect ( ( error as Error ) . message ) . toContain ( "dimension-mismatch" ) ;
516+ expect ( ( error as Error ) . message ) . not . toContain ( "Field valid_field" ) ;
517+ }
518+ } ) ;
519+
520+ it ( "handles nested fields with validation errors" , async ( ) => {
521+ await expect (
522+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [
523+ { a : { nasty : { scalar : { field : [ 1 , 2 , 3 ] } } } } ,
524+ ] )
525+ ) . rejects . toThrow ( / F i e l d a \. n a s t y \. s c a l a r \. f i e l d / ) ;
526+ } ) ;
527+
528+ it ( "handles empty document array" , async ( ) => {
529+ await expect (
530+ embeddings . assertFieldsHaveCorrectEmbeddings ( { database, collection } , [ ] )
531+ ) . resolves . not . toThrow ( ) ;
532+ } ) ;
533+ } ) ;
534+ } ) ;
535+
393536 describe ( "generate embeddings" , ( ) => {
394537 const embeddingToGenerate = {
395538 database : "mydb" ,
0 commit comments