@@ -3,58 +3,85 @@ import { expect } from 'chai';
33import 'mocha' ;
44import { SerialPort } from 'serialport' ;
55import { upload } from '../src/index' ;
6- import { waitForData , config , getHex } from './util' ;
6+ import { waitForData , config , getHex , espIdentify , ESPIdentifyResult } from './util' ;
77import { waitForOpen } from '../src/util/serial-helpers' ;
88import { ProgramFile } from '../src/index.d' ;
99
10+ const numEsps = Object . values ( config . devices ) . filter ( ( d ) => d . espChip ) . length ;
11+ const listPromise = espIdentify ( numEsps ) ;
12+
1013Object . keys ( config . devices ) . forEach ( ( deviceRef ) => {
1114 const device = config . devices [ deviceRef ] ;
1215 let key = '' ;
1316 let hex : Buffer | undefined ;
1417 let files : ProgramFile [ ] | undefined ;
1518 let serial : SerialPort ;
19+ let flashMode : string | undefined ;
20+ let flashFreq : string | undefined ;
21+ let portList : ESPIdentifyResult [ ] = [ ] ;
1622
1723 describe ( `upload to ${ device . name } ` , function ( ) {
1824 this . timeout ( 120 * 1000 ) ;
1925
2026 before ( async ( ) => {
21- const res = await getHex ( device . code , device . fqbn ) ;
27+ const res = await getHex ( device . code , device . fqbn . trim ( ) ) ;
2228 key = res . key ;
2329 hex = res . hex ;
2430 files = res . files ;
31+ flashMode = res . flashMode ;
32+ flashFreq = res . flashFreq ;
2533 console . log ( 'compiled hex' ) ;
2634 } ) ;
2735
2836 beforeEach ( async ( ) => {
2937 if ( serial ?. isOpen ) await ( new Promise ( resolve => serial . close ( resolve ) ) ) ;
3038
31- // dynamically find the device path by using VID & PID
32- const list = await SerialPort . list ( ) ;
33- const port = list . find ( p => device . vendorIds . includes ( p . vendorId ) && device . productIds . includes ( p . productId ) ) ;
39+ // dynamically find the device path by using VID & PID or esp props
40+ portList = await listPromise ;
41+ const port = portList . find ( p => {
42+ if ( device . vendorIds && device . productIds ) {
43+ return device . vendorIds . includes ( p . vendorId || '' ) && device . productIds . includes ( p . productId || '' ) ;
44+ }
45+ if ( device . espChip ) {
46+ return p . esp ?. chip === device . espChip ;
47+ }
48+ if ( device . mac ) {
49+ return p . esp ?. mac === device . mac ;
50+ }
51+ return false ;
52+ } ) ;
3453 if ( ! port ) throw new Error ( `could not locate ${ device . name } ` ) ;
3554
3655 // connect to the device
3756 serial = new SerialPort ( { path : port . path , baudRate : device . speed } ) ;
3857 await waitForOpen ( serial ) ;
39- console . log ( `connected to ${ device . name } ` ) ;
58+ console . log ( `connected to ${ device . name } on ${ port . path } ` ) ;
4059 } ) ;
4160
4261 this . afterEach ( async ( ) => {
4362 // make sure connection is closed when we're done
4463 if ( serial ?. isOpen ) await ( new Promise ( resolve => serial . close ( resolve ) ) ) ;
4564 } ) ;
4665
47- it ( `should upload to ${ device . name } ` , async ( ) => {
66+ it ( `should upload to ${ device . name } ` , async function ( ) {
67+ this . retries ( config . retries || 1 ) ;
4868 await upload ( serial , {
4969 hex,
5070 files,
71+ flashMode,
72+ flashFreq,
5173 speed : device . speed ,
74+ uploadSpeed : device . uploadSpeed ,
5275 tool : device . tool ,
5376 cpu : device . cpu ,
5477 verbose : config . verbose ,
5578 } ) ;
79+
5680 console . log ( `uploaded to ${ device . name } , validating...` ) ;
57- expect ( await waitForData ( serial , key , 3000 ) ) . to . be . true ;
81+ if ( device . code === 'ping' ) {
82+ await serial . write ( 'ping\n' ) ;
83+ }
84+ expect ( await waitForData ( serial , key , 5000 ) ) . to . be . true ;
5885 } ) ;
5986 } ) ;
6087} ) ;
0 commit comments