Skip to content

Commit aa0c73b

Browse files
committed
fix: resolve linter issues
- Remove unused imports in test files - Fix import organization in Zemu.ts - Fix formatting (newlines at end of files) - Apply Biome linter rules All tests passing after linter fixes.
1 parent 47fb24f commit aa0c73b

5 files changed

Lines changed: 85 additions & 86 deletions

File tree

src/Zemu.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { PNG, type PNGWithMetadata } from 'pngjs'
2727
import rndstr from 'randomstring'
2828
import { ClickNavigation, scheduleToNavElement, TouchNavigation } from './actions'
2929
import { getTouchElement } from './buttons'
30-
import { isCriticalTransportError, TransportError, getAPDUStatusMessage } from './errors'
3130
import {
3231
BASE_NAME,
3332
DEFAULT_EMU_IMG,
@@ -50,6 +49,7 @@ import {
5049
} from './constants'
5150
import { ContainerPool, type IPoolConfig, type IPooledContainer } from './containerPool'
5251
import EmuContainer from './emulator'
52+
import { getAPDUStatusMessage, isCriticalTransportError, TransportError } from './errors'
5353
import GRPCRouter from './grpc'
5454
import {
5555
ActionKind,
@@ -439,39 +439,39 @@ export default class Zemu {
439439

440440
getTransport(): Transport {
441441
if (this.transport == null) throw new Error('Transport is not loaded.')
442-
442+
443443
// Create a wrapper to intercept transport errors
444444
const self = this
445445
const originalTransport = this.transport
446-
446+
447447
// Return a proxy that intercepts send() calls
448448
return new Proxy(originalTransport, {
449449
get(target, prop, receiver) {
450450
if (prop === 'send') {
451-
return async function(cla: number, ins: number, p1: number, p2: number, data?: Buffer, statusList?: number[]) {
451+
return async function (cla: number, ins: number, p1: number, p2: number, data?: Buffer, statusList?: number[]) {
452452
try {
453453
self.lastTransportError = null // Clear previous error
454454
const result = await target.send(cla, ins, p1, p2, data, statusList)
455455
return result
456456
} catch (error) {
457457
// Store the error for later checks
458458
self.lastTransportError = error as Error
459-
459+
460460
// Log critical errors
461461
if (isCriticalTransportError(error)) {
462462
const statusCode = (error as any).statusCode
463463
self.log(`Critical transport error detected: ${getAPDUStatusMessage(statusCode)}`)
464464
}
465-
465+
466466
// Re-throw the error
467467
throw error
468468
}
469469
}
470470
}
471-
471+
472472
// For exchange and other methods, apply similar wrapping
473473
if (prop === 'exchange') {
474-
return async function(apdu: Buffer) {
474+
return async function (apdu: Buffer) {
475475
try {
476476
self.lastTransportError = null
477477
const result = await target.exchange(apdu)
@@ -486,10 +486,10 @@ export default class Zemu {
486486
}
487487
}
488488
}
489-
489+
490490
// For all other properties/methods, return as-is
491491
return Reflect.get(target, prop, receiver)
492-
}
492+
},
493493
}) as Transport
494494
}
495495

@@ -567,7 +567,7 @@ export default class Zemu {
567567
this.lastTransportError
568568
)
569569
}
570-
570+
571571
const currentTime = new Date()
572572
const elapsed = currentTime.getTime() - start.getTime()
573573
if (elapsed > timeout) {
@@ -599,7 +599,7 @@ export default class Zemu {
599599
this.lastTransportError
600600
)
601601
}
602-
602+
603603
const currentTime = new Date()
604604
const elapsed = currentTime.getTime() - start.getTime()
605605
if (elapsed > timeout) {
@@ -956,7 +956,7 @@ export default class Zemu {
956956
if (this.lastTransportError && isCriticalTransportError(this.lastTransportError)) {
957957
throw this.lastTransportError
958958
}
959-
959+
960960
// eslint-disable-next-line @typescript-eslint/unbound-method
961961
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay })
962962
const eventsUrl = `${this.transportProtocol}://${this.host}:${this.speculosApiPort}/events`
@@ -1003,7 +1003,7 @@ export default class Zemu {
10031003
this.lastTransportError
10041004
)
10051005
}
1006-
1006+
10071007
const currentTime = new Date()
10081008
const elapsed = currentTime.getTime() - start.getTime()
10091009
if (elapsed > timeout) {

src/errors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ export class TransportError extends Error {
5959
*/
6060
export function isCriticalTransportError(error: unknown): boolean {
6161
if (!error || typeof error !== 'object') return false
62-
62+
6363
const statusCode = (error as any).statusCode
6464
if (typeof statusCode !== 'number') return false
65-
65+
6666
return CRITICAL_TRANSPORT_ERRORS.includes(statusCode as any)
6767
}
6868

@@ -96,4 +96,4 @@ export function getAPDUStatusMessage(statusCode: number): string {
9696
default:
9797
return `Unknown status code: 0x${statusCode.toString(16)}`
9898
}
99-
}
99+
}

tests/error-handling-fixed.test.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { describe, expect, test } from 'vitest'
21
import { resolve } from 'node:path'
2+
import { describe, expect, test } from 'vitest'
33
import Zemu, { DEFAULT_START_OPTIONS, IStartOptions } from '../src'
4-
import { TransportError, APDU_STATUS_CODES } from '../src/errors'
4+
import { TransportError } from '../src/errors'
55

66
const DEMO_APP_PATH_S = resolve('bin/app_s.elf')
77

@@ -20,31 +20,31 @@ describe('Error Handling - Fixed', () => {
2020
test('Should fail fast on error 0x6984 instead of timing out', async () => {
2121
// Disable pooling globally for this test
2222
process.env.ZEMU_CONTAINER_POOLING = 'false'
23-
23+
2424
const sim = new Zemu(DEMO_APP_PATH_S)
25-
25+
2626
try {
2727
await sim.start(ZEMU_OPTIONS_S)
2828
const transport = sim.getTransport()
29-
29+
3030
console.log('=== Testing invalid APDU command with new error handling ===')
31-
31+
3232
// Send an invalid APDU command that should trigger error 0x6984
3333
const startTime = Date.now()
34-
34+
3535
try {
3636
// Invalid CLA (0xFF) should cause error 0x6984
37-
const result = await transport.send(0xFF, 0x00, 0x00, 0x00)
37+
const result = await transport.send(0xff, 0x00, 0x00, 0x00)
3838
console.log('Unexpected success:', result)
3939
expect.fail('Expected transport.send to throw an error')
4040
} catch (error: any) {
4141
const elapsedTime = Date.now() - startTime
4242
console.log(`Error occurred after ${elapsedTime}ms`)
4343
console.log('Error statusCode:', error.statusCode)
44-
44+
4545
// The error should happen quickly (< 1 second), not after a timeout
4646
expect(elapsedTime).toBeLessThan(1000)
47-
47+
4848
// Check if we got the expected error code
4949
// Note: The actual error code might vary based on the app
5050
expect(error.statusCode).toBeDefined()
@@ -56,47 +56,47 @@ describe('Error Handling - Fixed', () => {
5656

5757
test('Should propagate transport errors in waitUntilScreenIs', async () => {
5858
process.env.ZEMU_CONTAINER_POOLING = 'false'
59-
59+
6060
const sim = new Zemu(DEMO_APP_PATH_S)
61-
61+
6262
try {
6363
await sim.start(ZEMU_OPTIONS_S)
6464
const mainMenuSnapshot = sim.getMainMenuSnapshot()
65-
65+
6666
console.log('=== Testing waitUntilScreenIs with transport error ===')
67-
67+
6868
// First send an invalid command to trigger error
6969
const transport = sim.getTransport()
7070
try {
71-
await transport.send(0xFF, 0x00, 0x00, 0x00)
71+
await transport.send(0xff, 0x00, 0x00, 0x00)
7272
} catch (error: any) {
7373
console.log('Triggered error:', error.statusCode)
7474
}
75-
75+
7676
// Now try to wait for screen - this should fail fast if error is critical
7777
const startTime = Date.now()
78-
78+
7979
try {
8080
// Create a snapshot that won't match to force waiting
8181
const modifiedData = Buffer.from(mainMenuSnapshot.data)
8282
modifiedData[0] = (modifiedData[0] + 1) % 256
8383
const modifiedSnapshot = {
8484
...mainMenuSnapshot,
85-
data: modifiedData
85+
data: modifiedData,
8686
}
87-
87+
8888
await sim.waitUntilScreenIs(modifiedSnapshot, 5000)
89-
89+
9090
// If we get here, check how long it took
9191
const elapsedTime = Date.now() - startTime
9292
console.log(`waitUntilScreenIs completed after ${elapsedTime}ms`)
93-
93+
9494
// If error was critical, it should have failed fast
9595
// Otherwise it might wait for timeout or succeed
9696
} catch (error: any) {
9797
const elapsedTime = Date.now() - startTime
9898
console.log(`Error after ${elapsedTime}ms:`, error.message)
99-
99+
100100
// Check if it's a transport error (fast fail) or timeout
101101
if (error instanceof TransportError) {
102102
expect(elapsedTime).toBeLessThan(1000)
@@ -114,36 +114,36 @@ describe('Error Handling - Fixed', () => {
114114

115115
test('Should handle getEvents with transport error', async () => {
116116
process.env.ZEMU_CONTAINER_POOLING = 'false'
117-
117+
118118
const sim = new Zemu(DEMO_APP_PATH_S)
119-
119+
120120
try {
121121
await sim.start(ZEMU_OPTIONS_S)
122-
122+
123123
console.log('=== Testing getEvents with transport error ===')
124-
124+
125125
// First trigger a critical transport error
126126
const transport = sim.getTransport()
127127
let errorCode: number | undefined
128-
128+
129129
try {
130-
await transport.send(0xFF, 0x00, 0x00, 0x00)
130+
await transport.send(0xff, 0x00, 0x00, 0x00)
131131
} catch (error: any) {
132132
errorCode = error.statusCode
133133
console.log('Triggered error code:', errorCode)
134134
}
135-
135+
136136
// Now try getEvents - should throw if error is critical
137137
try {
138138
const events = await sim.getEvents()
139139
console.log('getEvents succeeded, got', events.length, 'events')
140-
140+
141141
// This is OK - getEvents might succeed if error wasn't critical
142142
// or if the device recovered
143143
expect(Array.isArray(events)).toBe(true)
144144
} catch (error: any) {
145145
console.log('getEvents threw error:', error.message)
146-
146+
147147
// If it throws, should be a transport error with statusCode
148148
expect(error.statusCode).toBeDefined()
149149
expect(error.message).toContain('CLA_NOT_SUPPORTED')
@@ -152,4 +152,4 @@ describe('Error Handling - Fixed', () => {
152152
await sim.close()
153153
}
154154
}, 30000)
155-
})
155+
})

tests/error-handling-simple.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, expect, test } from 'vitest'
21
import { resolve } from 'node:path'
2+
import { describe, expect, test } from 'vitest'
33
import Zemu, { DEFAULT_START_OPTIONS, IStartOptions } from '../src'
44

55
const DEMO_APP_PATH_S = resolve('bin/app_s.elf')
@@ -18,19 +18,19 @@ const ZEMU_OPTIONS_S: IStartOptions = {
1818
describe('Error Handling - Simple', () => {
1919
test.skip('First understand current timeout behavior', async () => {
2020
const sim = new Zemu(DEMO_APP_PATH_S)
21-
21+
2222
try {
2323
await sim.start(ZEMU_OPTIONS_S)
2424
const transport = sim.getTransport()
25-
25+
2626
console.log('=== Testing invalid APDU command ===')
27-
27+
2828
// Send an invalid APDU command that should trigger error 0x6984
2929
const startTime = Date.now()
30-
30+
3131
try {
3232
// Invalid CLA (0xFF) should cause error
33-
const result = await transport.send(0xFF, 0x00, 0x00, 0x00)
33+
const result = await transport.send(0xff, 0x00, 0x00, 0x00)
3434
console.log('Result:', result)
3535
} catch (error: any) {
3636
const elapsedTime = Date.now() - startTime
@@ -48,23 +48,23 @@ describe('Error Handling - Simple', () => {
4848
test('Test waitUntilScreenIs with timeout', async () => {
4949
// Disable pooling globally for this test
5050
process.env.ZEMU_CONTAINER_POOLING = 'false'
51-
51+
5252
const sim = new Zemu(DEMO_APP_PATH_S)
53-
53+
5454
try {
5555
await sim.start(ZEMU_OPTIONS_S)
5656
console.log('=== Testing waitUntilScreenIs behavior ===')
57-
57+
5858
// Get a snapshot that will never match to force timeout
5959
const snapshot = await sim.snapshot()
6060
// Create a new Buffer with modified data so it won't match
6161
const modifiedData = Buffer.from(snapshot.data)
6262
modifiedData[0] = (modifiedData[0] + 1) % 256 // Change first byte
6363
const modifiedSnapshot = {
6464
...snapshot,
65-
data: modifiedData
65+
data: modifiedData,
6666
}
67-
67+
6868
const startTime = Date.now()
6969
try {
7070
// This should timeout after 2 seconds
@@ -73,7 +73,7 @@ describe('Error Handling - Simple', () => {
7373
const elapsedTime = Date.now() - startTime
7474
console.log(`Timeout occurred after ${elapsedTime}ms`)
7575
console.log('Error message:', error.message)
76-
76+
7777
// Verify it actually waited for the timeout
7878
expect(elapsedTime).toBeGreaterThan(1900)
7979
expect(elapsedTime).toBeLessThan(2500)
@@ -83,4 +83,4 @@ describe('Error Handling - Simple', () => {
8383
await sim.close()
8484
}
8585
}, 30000)
86-
})
86+
})

0 commit comments

Comments
 (0)