Skip to content

Commit

Permalink
test: add more ordering edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Apr 6, 2024
1 parent da0901c commit 3e81869
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions ui/src/features/keys/select-macro/lib/__tests__/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ describe('findStepIndexToTop', () => {
it('should return the correct index if keyName is found at the beginning', () => {
expect(findStepIndexToTop(exampleSteps, 'x', 2)).toBe(0)
})

it('should return the correct index of the matching step above the start index', () => {
const steps: MacroStep[] = [
{ id: '1', keyName: 'a', type: MacroStepType.KeyDown },
{ id: '2', keyName: 'b', type: MacroStepType.KeyDown },
{ id: '3', keyName: 'a', type: MacroStepType.KeyUp }
]
expect(findStepIndexToTop(steps, 'a', 2)).toEqual(0)
})

it('should return -1 if no matching step is found above the start index', () => {
const steps: MacroStep[] = [
{ id: '1', keyName: 'a', type: MacroStepType.KeyDown }
]
expect(findStepIndexToTop(steps, 'b', 1)).toEqual(-1)
})
})

describe('findStepIndexToBottom', () => {
Expand All @@ -39,6 +55,22 @@ describe('findStepIndexToBottom', () => {
it('should return the correct index if keyName is found at the end', () => {
expect(findStepIndexToBottom(exampleSteps, 'z', 3)).toBe(6)
})

it('should return the correct index of the matching step below the start index', () => {
const steps: MacroStep[] = [
{ id: '1', keyName: 'a', type: MacroStepType.KeyDown },
{ id: '2', keyName: 'b', type: MacroStepType.KeyDown },
{ id: '3', keyName: 'a', type: MacroStepType.KeyUp }
]
expect(findStepIndexToBottom(steps, 'b', 0)).toEqual(1)
})

it('should return -1 if no matching step is found below the start index', () => {
const steps: MacroStep[] = [
{ id: '1', keyName: 'b', type: MacroStepType.KeyUp }
]
expect(findStepIndexToBottom(steps, 'b', 0)).toEqual(-1)
})
})

describe('checkMacroStepsOrder', () => {
Expand Down Expand Up @@ -67,4 +99,52 @@ describe('checkMacroStepsOrder', () => {
]
expect(checkMacroStepsOrder(steps)).toBe(false)
})

it('handles multiple key presses in sequence', () => {
const steps: MacroStep[] = [
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' },
{ id: '2', type: MacroStepType.KeyDown, keyName: 'b' },
{ id: '3', type: MacroStepType.KeyUp, keyName: 'a' },
{ id: '4', type: MacroStepType.KeyUp, keyName: 'b' }
]
expect(checkMacroStepsOrder(steps)).toBe(true)
})

it('handles nested key presses', () => {
const steps: MacroStep[] = [
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' },
{ id: '2', type: MacroStepType.KeyDown, keyName: 'b' },
{ id: '3', type: MacroStepType.KeyUp, keyName: 'b' },
{ id: '4', type: MacroStepType.KeyUp, keyName: 'a' }
]
expect(checkMacroStepsOrder(steps)).toBe(true)
})

it('handles multiple Wait steps interspersed between key presses', () => {
const steps: MacroStep[] = [
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' },
{ id: '2', type: MacroStepType.Wait, delay: 100 },
{ id: '3', type: MacroStepType.KeyUp, keyName: 'a' },
{ id: '4', type: MacroStepType.Wait, delay: 200 },
{ id: '5', type: MacroStepType.KeyDown, keyName: 'b' },
{ id: '6', type: MacroStepType.KeyUp, keyName: 'b' }
]
expect(checkMacroStepsOrder(steps)).toBe(true)
})

it('detects KeyDown without a corresponding KeyUp', () => {
const steps: MacroStep[] = [
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' },
{ id: '2', type: MacroStepType.KeyDown, keyName: 'b' }
]
expect(checkMacroStepsOrder(steps)).toBe(false)
})

it('detects repeated KeyDown steps for the same key without an intervening KeyUp', () => {
const steps: MacroStep[] = [
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' },
{ id: '2', type: MacroStepType.KeyDown, keyName: 'a' }
]
expect(checkMacroStepsOrder(steps)).toBe(false)
})
})

0 comments on commit 3e81869

Please sign in to comment.