@@ -20,7 +20,7 @@ describe("utils", () => {
20
20
beforeEach ( ( ) => {
21
21
// 重置所有 mock
22
22
vi . clearAllMocks ( ) ;
23
-
23
+
24
24
// 设置 console mock 来避免测试输出污染
25
25
vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
26
26
vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
@@ -30,7 +30,7 @@ describe("utils", () => {
30
30
// 清理 DOM
31
31
document . head . innerHTML = "" ;
32
32
document . documentElement . innerHTML = "<head></head><body></body>" ;
33
-
33
+
34
34
vi . restoreAllMocks ( ) ;
35
35
} ) ;
36
36
@@ -221,9 +221,9 @@ describe("utils", () => {
221
221
it ( "应该编译并执行简单代码" , ( ) => {
222
222
const code = "return arguments[0].value + arguments[1];" ;
223
223
const func : ScriptFunc = compileScript ( code ) ;
224
-
224
+
225
225
const result = func ( { value : 10 } , "test-script" ) ;
226
-
226
+
227
227
expect ( result ) . toBe ( "10test-script" ) ;
228
228
} ) ;
229
229
@@ -237,10 +237,10 @@ describe("utils", () => {
237
237
return scriptName;
238
238
` ;
239
239
const func : ScriptFunc = compileScript ( code ) ;
240
-
240
+
241
241
const result1 = func ( { value : 5 , multiply : 3 } , "test" ) ;
242
242
const result2 = func ( { value : 5 } , "fallback" ) ;
243
-
243
+
244
244
expect ( result1 ) . toBe ( 15 ) ;
245
245
expect ( result2 ) . toBe ( "fallback" ) ;
246
246
} ) ;
@@ -252,16 +252,16 @@ describe("utils", () => {
252
252
});
253
253
` ;
254
254
const func : ScriptFunc = compileScript ( code ) ;
255
-
255
+
256
256
const result = await func ( { value : 5 } , "async-test" ) ;
257
-
257
+
258
258
expect ( result ) . toBe ( 10 ) ;
259
259
} ) ;
260
260
261
261
it ( "应该正确处理错误" , ( ) => {
262
262
const code = "throw new Error('Test error');" ;
263
263
const func : ScriptFunc = compileScript ( code ) ;
264
-
264
+
265
265
expect ( ( ) => func ( { } , "error-test" ) ) . toThrow ( "Test error" ) ;
266
266
} ) ;
267
267
} ) ;
@@ -288,18 +288,18 @@ describe("utils", () => {
288
288
it ( "应该生成基本的注入脚本代码" , ( ) => {
289
289
const script = createMockScript ( ) ;
290
290
const scriptCode = "console.log('injected');" ;
291
-
291
+
292
292
const result = compileInjectScript ( script , scriptCode ) ;
293
-
293
+
294
294
expect ( result ) . toBe ( `window['inject-test-flag'] = function(){console.log('injected');}` ) ;
295
295
} ) ;
296
296
297
297
it ( "应该包含自动删除挂载函数的代码" , ( ) => {
298
298
const script = createMockScript ( ) ;
299
299
const scriptCode = "console.log('with auto delete');" ;
300
-
300
+
301
301
const result = compileInjectScript ( script , scriptCode , true ) ;
302
-
302
+
303
303
expect ( result ) . toContain ( `try{delete window['inject-test-flag']}catch(e){}` ) ;
304
304
expect ( result ) . toContain ( "console.log('with auto delete');" ) ;
305
305
expect ( result ) . toBe (
@@ -310,9 +310,9 @@ describe("utils", () => {
310
310
it ( "默认情况下不应该包含自动删除代码" , ( ) => {
311
311
const script = createMockScript ( ) ;
312
312
const scriptCode = "console.log('without auto delete');" ;
313
-
313
+
314
314
const result = compileInjectScript ( script , scriptCode ) ;
315
-
315
+
316
316
expect ( result ) . not . toContain ( "try{delete window" ) ;
317
317
expect ( result ) . toBe ( `window['inject-test-flag'] = function(){console.log('without auto delete');}` ) ;
318
318
} ) ;
@@ -324,9 +324,9 @@ describe("utils", () => {
324
324
function test() { return x + 1; }
325
325
console.log(test());
326
326
` ;
327
-
327
+
328
328
const result = compileInjectScript ( script , scriptCode , true ) ;
329
-
329
+
330
330
expect ( result ) . toContain ( "window['complex-flag']" ) ;
331
331
expect ( result ) . toContain ( "var x = 1;" ) ;
332
332
expect ( result ) . toContain ( "function test()" ) ;
@@ -336,19 +336,19 @@ describe("utils", () => {
336
336
it ( "应该正确转义脚本标志名称" , ( ) => {
337
337
const script = createMockScript ( { flag : "flag-with-special-chars_123" } ) ;
338
338
const scriptCode = "console.log('test');" ;
339
-
339
+
340
340
const result = compileInjectScript ( script , scriptCode ) ;
341
-
341
+
342
342
expect ( result ) . toContain ( `window['flag-with-special-chars_123']` ) ;
343
343
} ) ;
344
344
} ) ;
345
345
346
346
describe ( "addStyle" , ( ) => {
347
347
it ( "应该创建并添加 style 元素到 head" , ( ) => {
348
348
const css = "body { background: red; }" ;
349
-
349
+
350
350
const styleElement = addStyle ( css ) ;
351
-
351
+
352
352
expect ( styleElement ) . toBeInstanceOf ( HTMLStyleElement ) ;
353
353
expect ( styleElement . textContent ) . toBe ( css ) ;
354
354
expect ( document . head . contains ( styleElement ) ) . toBe ( true ) ;
@@ -358,23 +358,23 @@ describe("utils", () => {
358
358
// 移除 head 元素
359
359
const head = document . head ;
360
360
head . remove ( ) ;
361
-
361
+
362
362
const css = ".test { color: blue; }" ;
363
363
const styleElement = addStyle ( css ) ;
364
-
364
+
365
365
expect ( styleElement ) . toBeInstanceOf ( HTMLStyleElement ) ;
366
366
expect ( styleElement . textContent ) . toBe ( css ) ;
367
367
expect ( document . documentElement . contains ( styleElement ) ) . toBe ( true ) ;
368
-
368
+
369
369
// 恢复 head 元素以便其他测试
370
370
document . documentElement . appendChild ( head ) ;
371
371
} ) ;
372
372
373
373
it ( "应该处理空的 CSS 字符串" , ( ) => {
374
374
const css = "" ;
375
-
375
+
376
376
const styleElement = addStyle ( css ) ;
377
-
377
+
378
378
expect ( styleElement . textContent ) . toBe ( "" ) ;
379
379
expect ( document . head . contains ( styleElement ) ) . toBe ( true ) ;
380
380
} ) ;
@@ -398,20 +398,20 @@ describe("utils", () => {
398
398
transition: transform 0.3s ease;
399
399
}
400
400
` ;
401
-
401
+
402
402
const styleElement = addStyle ( css ) ;
403
-
403
+
404
404
expect ( styleElement . textContent ) . toBe ( css ) ;
405
405
expect ( document . head . contains ( styleElement ) ) . toBe ( true ) ;
406
406
} ) ;
407
407
408
408
it ( "应该允许添加多个样式" , ( ) => {
409
409
const css1 = ".class1 { color: red; }" ;
410
410
const css2 = ".class2 { color: blue; }" ;
411
-
411
+
412
412
const style1 = addStyle ( css1 ) ;
413
413
const style2 = addStyle ( css2 ) ;
414
-
414
+
415
415
expect ( document . head . contains ( style1 ) ) . toBe ( true ) ;
416
416
expect ( document . head . contains ( style2 ) ) . toBe ( true ) ;
417
417
expect ( style1 . textContent ) . toBe ( css1 ) ;
@@ -421,10 +421,10 @@ describe("utils", () => {
421
421
422
422
it ( "应该返回添加的 style 元素" , ( ) => {
423
423
const css = ".return-test { font-size: 14px; }" ;
424
-
424
+
425
425
const returnedElement = addStyle ( css ) ;
426
426
const queriedElement = document . querySelector ( "style" ) ;
427
-
427
+
428
428
expect ( returnedElement ) . toBe ( queriedElement ) ;
429
429
expect ( returnedElement ?. textContent ) . toBe ( css ) ;
430
430
} ) ;
0 commit comments