1- import { beforeEach , describe , expect , it , vi } from 'vitest'
1+ import { describe , expect , it } from 'vitest'
2+ import * as api from '../src/api'
3+ import { mockUsbData } from './setup'
24
35describe ( '浏览器环境 API 测试' , ( ) => {
4- let api : typeof import ( '../src/api' )
5-
6- beforeEach ( async ( ) => {
7- // 清理模块缓存
8- vi . resetModules ( )
9-
10- // 模拟fetchUsbIdsData在DOM环境下的行为
11- vi . doMock ( '../src/core' , ( ) => ( {
12- fetchUsbIdsData : vi . fn ( ) . mockResolvedValue ( {
13- data : {
14- 1234 : {
15- vendor : '1234' ,
16- name : 'Test Vendor' ,
17- devices : {
18- 5678 : {
19- devid : '5678' ,
20- devname : 'Test Device' ,
21- } ,
22- } ,
23- } ,
24- } ,
25- source : 'api' as const ,
26- versionInfo : {
27- fetchTime : new Date ( ) . toISOString ( ) ,
28- contentHash : 'test-hash' ,
29- source : 'https://example.com/usb.ids' ,
30- vendorCount : 1 ,
31- deviceCount : 1 ,
32- version : '1.0.0' ,
33- } ,
34- } ) ,
35- } ) )
36-
37- // 动态导入API模块
38- api = await import ( '../src/api' )
39- } )
40-
416 describe ( '异步函数在浏览器环境下的表现' , ( ) => {
427 it ( 'getVendors应该在浏览器环境下正常工作' , async ( ) => {
438 const vendors = await api . getVendors ( )
44- expect ( vendors ) . toHaveLength ( 1 )
45- expect ( vendors [ 0 ] . vendor ) . toBe ( '1234 ')
46- expect ( vendors [ 0 ] . name ) . toBe ( 'Test Vendor ')
9+ expect ( vendors . length ) . toBeGreaterThan ( 0 )
10+ expect ( vendors [ 0 ] ) . toHaveProperty ( 'vendor ')
11+ expect ( vendors [ 0 ] ) . toHaveProperty ( 'name ')
4712 } )
4813
4914 it ( 'getVendor应该在浏览器环境下正常工作' , async ( ) => {
50- const vendor = await api . getVendor ( '1234' )
15+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
16+ const vendor = await api . getVendor ( firstVendorId )
5117 expect ( vendor ) . not . toBeNull ( )
52- expect ( vendor ! . vendor ) . toBe ( '1234' )
53- expect ( vendor ! . name ) . toBe ( 'Test Vendor' )
18+ expect ( vendor ! . vendor ) . toBe ( firstVendorId )
5419 } )
5520
5621 it ( 'getDevices应该在浏览器环境下正常工作' , async ( ) => {
57- const devices = await api . getDevices ( '1234' )
58- expect ( devices ) . toHaveLength ( 1 )
59- expect ( devices [ 0 ] . devid ) . toBe ( '5678' )
60- expect ( devices [ 0 ] . devname ) . toBe ( 'Test Device' )
22+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
23+ const devices = await api . getDevices ( firstVendorId )
24+ expect ( devices . length ) . toBeGreaterThan ( 0 )
25+ expect ( devices [ 0 ] ) . toHaveProperty ( 'devid' )
26+ expect ( devices [ 0 ] ) . toHaveProperty ( 'devname' )
6127 } )
6228
6329 it ( 'getDevice应该在浏览器环境下正常工作' , async ( ) => {
64- const device = await api . getDevice ( '1234' , '5678' )
30+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
31+ const firstDeviceId = Object . keys ( mockUsbData [ firstVendorId ] . devices ) [ 0 ]
32+ const device = await api . getDevice ( firstVendorId , firstDeviceId )
6533 expect ( device ) . not . toBeNull ( )
66- expect ( device ! . devid ) . toBe ( '5678' )
67- expect ( device ! . devname ) . toBe ( 'Test Device' )
34+ expect ( device ! . devid ) . toBe ( firstDeviceId )
6835 } )
6936
7037 it ( 'searchDevices应该在浏览器环境下正常工作' , async ( ) => {
71- const results = await api . searchDevices ( 'Test' )
72- expect ( results ) . toHaveLength ( 1 )
73- expect ( results [ 0 ] . vendor . name ) . toBe ( 'Test Vendor' )
74- expect ( results [ 0 ] . device . devname ) . toBe ( 'Test Device' )
38+ const firstVendor = Object . values ( mockUsbData ) [ 0 ]
39+ const results = await api . searchDevices ( firstVendor . name )
40+ expect ( results . length ) . toBeGreaterThan ( 0 )
41+ expect ( results [ 0 ] . vendor . name ) . toBe ( firstVendor . name )
7542 } )
7643
7744 it ( 'getUsbData应该在浏览器环境下正常工作' , async ( ) => {
7845 const data = await api . getUsbData ( )
79- expect ( Object . keys ( data ) ) . toHaveLength ( 1 )
80- expect ( data [ '1234' ] ) . toBeDefined ( )
81- expect ( data [ '1234' ] . name ) . toBe ( 'Test Vendor' )
46+ expect ( Object . keys ( data ) . length ) . toBeGreaterThan ( 0 )
47+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
48+ expect ( data [ firstVendorId ] ) . toBeDefined ( )
8249 } )
8350 } )
8451
8552 describe ( '同步函数在浏览器环境下的表现' , ( ) => {
86- const testData = {
87- 1234 : {
88- vendor : '1234' ,
89- name : 'Test Vendor' ,
90- devices : {
91- 5678 : {
92- devid : '5678' ,
93- devname : 'Test Device' ,
94- } ,
95- } ,
96- } ,
97- }
98-
9953 it ( 'getVendorsSync应该在浏览器环境下正常工作' , ( ) => {
100- const vendors = api . getVendorsSync ( undefined , testData )
101- expect ( vendors ) . toHaveLength ( 1 )
102- expect ( vendors [ 0 ] . vendor ) . toBe ( '1234 ')
54+ const vendors = api . getVendorsSync ( undefined , mockUsbData )
55+ expect ( vendors . length ) . toBeGreaterThan ( 0 )
56+ expect ( vendors [ 0 ] ) . toHaveProperty ( 'vendor ')
10357 } )
10458
10559 it ( 'getVendorSync应该在浏览器环境下正常工作' , ( ) => {
106- const vendor = api . getVendorSync ( '1234' , testData )
60+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
61+ const vendor = api . getVendorSync ( firstVendorId , mockUsbData )
10762 expect ( vendor ) . not . toBeNull ( )
108- expect ( vendor ! . vendor ) . toBe ( '1234' )
63+ expect ( vendor ! . vendor ) . toBe ( firstVendorId )
10964 } )
11065
11166 it ( 'getDevicesSync应该在浏览器环境下正常工作' , ( ) => {
112- const devices = api . getDevicesSync ( '1234' , undefined , testData )
113- expect ( devices ) . toHaveLength ( 1 )
114- expect ( devices [ 0 ] . devid ) . toBe ( '5678' )
67+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
68+ const devices = api . getDevicesSync ( firstVendorId , undefined , mockUsbData )
69+ expect ( devices . length ) . toBeGreaterThan ( 0 )
70+ expect ( devices [ 0 ] ) . toHaveProperty ( 'devid' )
11571 } )
11672
11773 it ( 'getDeviceSync应该在浏览器环境下正常工作' , ( ) => {
118- const device = api . getDeviceSync ( '1234' , '5678' , testData )
74+ const firstVendorId = Object . keys ( mockUsbData ) [ 0 ]
75+ const firstDeviceId = Object . keys ( mockUsbData [ firstVendorId ] . devices ) [ 0 ]
76+ const device = api . getDeviceSync ( firstVendorId , firstDeviceId , mockUsbData )
11977 expect ( device ) . not . toBeNull ( )
120- expect ( device ! . devid ) . toBe ( '5678' )
78+ expect ( device ! . devid ) . toBe ( firstDeviceId )
12179 } )
12280
12381 it ( 'searchDevicesSync应该在浏览器环境下正常工作' , ( ) => {
124- const results = api . searchDevicesSync ( 'Test' , testData )
125- expect ( results ) . toHaveLength ( 1 )
126- expect ( results [ 0 ] . vendor . name ) . toBe ( 'Test Vendor' )
82+ const firstVendor = Object . values ( mockUsbData ) [ 0 ]
83+ const results = api . searchDevicesSync ( firstVendor . name , mockUsbData )
84+ expect ( results . length ) . toBeGreaterThan ( 0 )
85+ expect ( results [ 0 ] . vendor . name ) . toBe ( firstVendor . name )
12786 } )
12887 } )
12988
@@ -144,6 +103,8 @@ describe('浏览器环境 API 测试', () => {
144103 } )
145104
146105 it ( '异步函数在网络错误时应该正确处理' , async ( ) => {
106+ const { vi } = await import ( 'vitest' )
107+
147108 // 清理所有模块缓存
148109 vi . resetModules ( )
149110
0 commit comments