@@ -14,6 +14,38 @@ const mockVTableSheet = {
1414 getActiveSheet : ( ) : any => null
1515} as unknown as VTableSheet ;
1616
17+ // 测试用的基本标准化函数
18+ function normalizeTestData ( data : unknown [ ] [ ] ) : unknown [ ] [ ] {
19+ if ( ! Array . isArray ( data ) || data . length === 0 ) {
20+ return [ [ '' ] ] ;
21+ }
22+
23+ const maxCols = Math . max ( ...data . map ( row => ( Array . isArray ( row ) ? row . length : 0 ) ) ) ;
24+
25+ return data . map ( row => {
26+ if ( ! Array . isArray ( row ) ) {
27+ return Array ( maxCols ) . fill ( '' ) ;
28+ }
29+
30+ const normalizedRow = row . map ( cell => {
31+ if ( typeof cell === 'string' ) {
32+ if ( cell . startsWith ( '=' ) ) {
33+ return cell ; // 保持公式不变
34+ }
35+ const num = Number ( cell ) ;
36+ return ! isNaN ( num ) && cell . trim ( ) !== '' ? num : cell ;
37+ }
38+ return cell ?? '' ;
39+ } ) ;
40+
41+ while ( normalizedRow . length < maxCols ) {
42+ normalizedRow . push ( '' ) ;
43+ }
44+
45+ return normalizedRow ;
46+ } ) ;
47+ }
48+
1749describe ( 'Active Sheet Race Condition Fix' , ( ) => {
1850 let formulaManager : FormulaManager ;
1951
@@ -26,24 +58,31 @@ describe('Active Sheet Race Condition Fix', () => {
2658 } ) ;
2759
2860 test ( 'should only set first sheet as active, not subsequent sheets' , ( ) => {
29- // Add first sheet
30- formulaManager . addSheet ( 'Sheet1' , [ [ 'Data' ] , [ '100' ] ] ) ;
61+ // Add first sheet with normalized data
62+ const sheet1Data = normalizeTestData ( [ [ 'Data' ] , [ '100' ] ] ) ;
63+ formulaManager . addSheet ( 'Sheet1' , sheet1Data ) ;
3164 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet1' ) ;
3265
3366 // Add second sheet - should NOT become active
34- formulaManager . addSheet ( 'Sheet2' , [ [ 'Data' ] , [ '200' ] ] ) ;
67+ const sheet2Data = normalizeTestData ( [ [ 'Data' ] , [ '200' ] ] ) ;
68+ formulaManager . addSheet ( 'Sheet2' , sheet2Data ) ;
3569 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet1' ) ; // Should still be Sheet1
3670
3771 // Add third sheet - should NOT become active
38- formulaManager . addSheet ( 'Sheet3' , [ [ 'Data' ] , [ '300' ] ] ) ;
72+ const sheet3Data = normalizeTestData ( [ [ 'Data' ] , [ '300' ] ] ) ;
73+ formulaManager . addSheet ( 'Sheet3' , sheet3Data ) ;
3974 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet1' ) ; // Should still be Sheet1
4075 } ) ;
4176
4277 test ( 'should handle manual active sheet switching correctly' , ( ) => {
43- // Add multiple sheets
44- formulaManager . addSheet ( 'Sheet1' , [ [ 'Data' ] , [ '100' ] ] ) ;
45- formulaManager . addSheet ( 'Sheet2' , [ [ 'Data' ] , [ '200' ] ] ) ;
46- formulaManager . addSheet ( 'Sheet3' , [ [ 'Data' ] , [ '300' ] ] ) ;
78+ // Add multiple sheets with normalized data
79+ const sheet1Data = normalizeTestData ( [ [ 'Data' ] , [ '100' ] ] ) ;
80+ const sheet2Data = normalizeTestData ( [ [ 'Data' ] , [ '200' ] ] ) ;
81+ const sheet3Data = normalizeTestData ( [ [ 'Data' ] , [ '300' ] ] ) ;
82+
83+ formulaManager . addSheet ( 'Sheet1' , sheet1Data ) ;
84+ formulaManager . addSheet ( 'Sheet2' , sheet2Data ) ;
85+ formulaManager . addSheet ( 'Sheet3' , sheet3Data ) ;
4786
4887 // Initially Sheet1 should be active
4988 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet1' ) ;
@@ -53,7 +92,8 @@ describe('Active Sheet Race Condition Fix', () => {
5392 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet2' ) ;
5493
5594 // Add another sheet - should NOT change active sheet
56- formulaManager . addSheet ( 'Sheet4' , [ [ 'Data' ] , [ '400' ] ] ) ;
95+ const sheet4Data = normalizeTestData ( [ [ 'Data' ] , [ '400' ] ] ) ;
96+ formulaManager . addSheet ( 'Sheet4' , sheet4Data ) ;
5797 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet2' ) ; // Should still be Sheet2
5898
5999 // Switch to Sheet3
@@ -62,24 +102,27 @@ describe('Active Sheet Race Condition Fix', () => {
62102 } ) ;
63103
64104 test ( 'should handle formulas correctly with proper active sheet context' , ( ) => {
65- // Add multiple sheets with different data
66- formulaManager . addSheet ( 'Sheet1' , [
105+ // Add multiple sheets with normalized data
106+ const sheet1Data = normalizeTestData ( [
67107 [ 'A' , 'B' ] ,
68108 [ '10' , '' ] ,
69109 [ '' , '' ]
70110 ] ) ;
111+ formulaManager . addSheet ( 'Sheet1' , sheet1Data ) ;
71112
72- formulaManager . addSheet ( 'Sheet2' , [
113+ const sheet2Data = normalizeTestData ( [
73114 [ 'A' , 'B' ] ,
74115 [ '20' , '' ] ,
75116 [ '' , '' ]
76117 ] ) ;
118+ formulaManager . addSheet ( 'Sheet2' , sheet2Data ) ;
77119
78- formulaManager . addSheet ( 'Sheet3' , [
120+ const sheet3Data = normalizeTestData ( [
79121 [ 'A' , 'B' ] ,
80122 [ '30' , '' ] ,
81123 [ '' , '' ]
82124 ] ) ;
125+ formulaManager . addSheet ( 'Sheet3' , sheet3Data ) ;
83126
84127 // Initially Sheet1 is active
85128 expect ( formulaManager . getActiveSheet ( ) ) . toBe ( 'Sheet1' ) ;
0 commit comments