1- import { num , now } from '@/store/sqlite/lib/bigint' ;
1+ import { num , now , likeEscape , assertEmbeddingDim , chunk , safeJson } from '@/store/sqlite/lib/bigint' ;
22
33describe ( 'BigInt helpers' , ( ) => {
44 it ( 'num converts BigInt to number' , ( ) => {
@@ -22,3 +22,74 @@ describe('BigInt helpers', () => {
2222 expect ( ts <= after ) . toBe ( true ) ;
2323 } ) ;
2424} ) ;
25+
26+ describe ( 'likeEscape' , ( ) => {
27+ it ( 'escapes % and _ characters' , ( ) => {
28+ expect ( likeEscape ( '100%' ) ) . toBe ( '100\\%' ) ;
29+ expect ( likeEscape ( 'file_name' ) ) . toBe ( 'file\\_name' ) ;
30+ } ) ;
31+
32+ it ( 'escapes backslashes' , ( ) => {
33+ expect ( likeEscape ( 'path\\to' ) ) . toBe ( 'path\\\\to' ) ;
34+ } ) ;
35+
36+ it ( 'handles combined special characters' , ( ) => {
37+ expect ( likeEscape ( '50%_done\\ok' ) ) . toBe ( '50\\%\\_done\\\\ok' ) ;
38+ } ) ;
39+
40+ it ( 'returns plain text unchanged' , ( ) => {
41+ expect ( likeEscape ( 'hello world' ) ) . toBe ( 'hello world' ) ;
42+ } ) ;
43+ } ) ;
44+
45+ describe ( 'assertEmbeddingDim' , ( ) => {
46+ it ( 'passes for correct dimension' , ( ) => {
47+ expect ( ( ) => assertEmbeddingDim ( [ 1 , 2 , 3 ] , 3 ) ) . not . toThrow ( ) ;
48+ } ) ;
49+
50+ it ( 'throws for wrong dimension' , ( ) => {
51+ expect ( ( ) => assertEmbeddingDim ( [ 1 , 2 ] , 3 ) ) . toThrow ( 'Embedding dimension mismatch: expected 3, got 2' ) ;
52+ } ) ;
53+
54+ it ( 'throws for empty embedding' , ( ) => {
55+ expect ( ( ) => assertEmbeddingDim ( [ ] , 384 ) ) . toThrow ( 'expected 384, got 0' ) ;
56+ } ) ;
57+ } ) ;
58+
59+ describe ( 'chunk' , ( ) => {
60+ it ( 'returns single chunk for small array' , ( ) => {
61+ const result = chunk ( [ 1 , 2 , 3 ] , 10 ) ;
62+ expect ( result ) . toEqual ( [ [ 1 , 2 , 3 ] ] ) ;
63+ } ) ;
64+
65+ it ( 'splits array into multiple chunks' , ( ) => {
66+ const result = chunk ( [ 1 , 2 , 3 , 4 , 5 ] , 2 ) ;
67+ expect ( result ) . toEqual ( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 ] ] ) ;
68+ } ) ;
69+
70+ it ( 'handles empty array' , ( ) => {
71+ const result = chunk ( [ ] , 10 ) ;
72+ expect ( result ) . toEqual ( [ [ ] ] ) ;
73+ } ) ;
74+
75+ it ( 'handles exact chunk size' , ( ) => {
76+ const result = chunk ( [ 1 , 2 , 3 , 4 ] , 2 ) ;
77+ expect ( result ) . toEqual ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) ;
78+ } ) ;
79+ } ) ;
80+
81+ describe ( 'safeJson' , ( ) => {
82+ it ( 'parses valid JSON' , ( ) => {
83+ expect ( safeJson ( '["a","b"]' , [ ] ) ) . toEqual ( [ 'a' , 'b' ] ) ;
84+ expect ( safeJson ( '{"x":1}' , { } ) ) . toEqual ( { x : 1 } ) ;
85+ } ) ;
86+
87+ it ( 'returns fallback for invalid JSON' , ( ) => {
88+ expect ( safeJson ( 'not json' , [ ] ) ) . toEqual ( [ ] ) ;
89+ expect ( safeJson ( '{broken' , 'default' ) ) . toBe ( 'default' ) ;
90+ } ) ;
91+
92+ it ( 'returns fallback for empty string' , ( ) => {
93+ expect ( safeJson ( '' , null ) ) . toBeNull ( ) ;
94+ } ) ;
95+ } ) ;
0 commit comments