-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { MacroActionType } from '$entities/keys' | ||
|
||
import { paramsToMacro } from '../macros' | ||
|
||
describe('paramsToMacro', () => { | ||
it('should map title from params to Macro object', () => { | ||
const params = { index: 0, title: 'Test Title', repeats: 3, actions: [] } | ||
const result = paramsToMacro(params) | ||
expect(result.title).toBe('Test Title') | ||
}) | ||
|
||
it('should map repeats from params to Macro object', () => { | ||
const params = { index: 0, title: 'Test Title', repeats: 3, actions: [] } | ||
const result = paramsToMacro(params) | ||
expect(result.repeats).toBe(3) | ||
}) | ||
|
||
it('should map actions from params to Macro object', () => { | ||
const actions = [ | ||
{ key: 'A', type: MacroActionType.KeyDown }, | ||
{ key: 'B', type: MacroActionType.KeyUp } | ||
] | ||
const params = { index: 0, title: 'Test Title', repeats: 3, actions } | ||
const result = paramsToMacro(params) | ||
expect(result.actions).toEqual(actions) | ||
}) | ||
}) |
161 changes: 161 additions & 0 deletions
161
ui/src/features/keys/select-macro/lib/__tests__/convert.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { type Macro, MacroActionType } from '$entities/keys' | ||
|
||
import { type MacroStep, MacroStepType } from '../../model' | ||
import { | ||
actionToStepType, | ||
macroToSteps, | ||
stepsToActions, | ||
stepToActionType | ||
} from '../convert' | ||
|
||
describe('actionToStepType', () => { | ||
it('should convert MacroActionType.KeyDown to MacroStepType.KeyDown', () => { | ||
expect(actionToStepType(MacroActionType.KeyDown)).toEqual(MacroStepType.KeyDown) | ||
}) | ||
|
||
it('should convert MacroActionType.KeyUp to MacroStepType.KeyUp', () => { | ||
expect(actionToStepType(MacroActionType.KeyUp)).toEqual(MacroStepType.KeyUp) | ||
}) | ||
|
||
it('should throw an error for an unknown action type', () => { | ||
expect( | ||
() => actionToStepType('UnknownActionType' as MacroActionType) | ||
).toThrowError('Unknown action type') | ||
}) | ||
}) | ||
|
||
describe('stepToActionType', () => { | ||
it('should return MacroActionType.KeyDown when type is MacroStepType.KeyDown', () => { | ||
expect(stepToActionType(MacroStepType.KeyDown)).toEqual(MacroActionType.KeyDown) | ||
}) | ||
|
||
it('should return MacroActionType.KeyUp when type is MacroStepType.KeyUp', () => { | ||
expect(stepToActionType(MacroStepType.KeyUp)).toEqual(MacroActionType.KeyUp) | ||
}) | ||
|
||
it('should throw an error for an unknown step type', () => { | ||
const wrongType = 'UnknownStepType' as MacroStepType.KeyDown | ||
expect(() => stepToActionType(wrongType)).toThrowError('Unknown step type') | ||
}) | ||
}) | ||
|
||
describe('macroToSteps', () => { | ||
it('should return an empty array for an empty macro', () => { | ||
const macro: Macro = { | ||
title: 'Empty', | ||
repeats: 0, | ||
actions: [] | ||
} | ||
expect(macroToSteps(macro)).toEqual([]) | ||
}) | ||
|
||
it('should correctly convert a macro with one action without delay', () => { | ||
const macro: Macro = { | ||
title: 'Single', | ||
repeats: 1, | ||
actions: [ | ||
{ type: MacroActionType.KeyDown, key: 'm' } | ||
] | ||
} | ||
expect(macroToSteps(macro)).toEqual([ | ||
{ id: expect.any(String), type: MacroStepType.KeyDown, keyName: 'm' } | ||
]) | ||
}) | ||
|
||
it('should correctly convert a macro with one action with delay', () => { | ||
const macro: Macro = { | ||
title: 'Delay', | ||
repeats: 1, | ||
actions: [ | ||
{ type: MacroActionType.KeyDown, key: 'm', delay: 100 } | ||
] | ||
} | ||
expect(macroToSteps(macro)).toEqual([ | ||
{ id: expect.any(String), type: MacroStepType.KeyDown, keyName: 'm' }, | ||
{ id: expect.any(String), type: MacroStepType.Wait, delay: 100 } | ||
]) | ||
}) | ||
|
||
it('should correctly convert a macro with multiple actions without delays', () => { | ||
const macro: Macro = { | ||
title: 'Multiple', | ||
repeats: 1, | ||
actions: [ | ||
{ type: MacroActionType.KeyDown, key: 'm' }, | ||
{ type: MacroActionType.KeyUp, key: 'm' } | ||
] | ||
} | ||
expect(macroToSteps(macro)).toEqual([ | ||
{ id: expect.any(String), type: MacroStepType.KeyDown, keyName: 'm' }, | ||
{ id: expect.any(String), type: MacroStepType.KeyUp, keyName: 'm' } | ||
]) | ||
}) | ||
|
||
it('should correctly convert a macro with multiple actions with delays', () => { | ||
const macro: Macro = { | ||
title: 'Multiple', | ||
repeats: 1, | ||
actions: [ | ||
{ type: MacroActionType.KeyDown, key: 'm', delay: 100 }, | ||
{ type: MacroActionType.KeyUp, key: 'm', delay: 200 } | ||
] | ||
} | ||
expect(macroToSteps(macro)).toEqual([ | ||
{ id: expect.any(String), type: MacroStepType.KeyDown, keyName: 'm' }, | ||
{ id: expect.any(String), type: MacroStepType.Wait, delay: 100 }, | ||
{ id: expect.any(String), type: MacroStepType.KeyUp, keyName: 'm' }, | ||
{ id: expect.any(String), type: MacroStepType.Wait, delay: 200 } | ||
]) | ||
}) | ||
}) | ||
|
||
describe('stepsToActions', () => { | ||
it('should return an empty array when input steps array is empty', () => { | ||
const result = stepsToActions([]) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it('should return a single MacroAction with type KeyDown', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' } | ||
] | ||
const result = stepsToActions(steps) | ||
expect(result).toEqual([{ key: 'a', type: MacroActionType.KeyDown, delay: 0 }]) | ||
}) | ||
|
||
it('should return a single MacroAction with type KeyUp', () => { | ||
const steps: MacroStep[] = [{ id: '2', type: MacroStepType.KeyUp, keyName: 'b' }] | ||
const result = stepsToActions(steps) | ||
expect(result).toEqual([{ key: 'b', type: MacroActionType.KeyUp, delay: 0 }]) | ||
}) | ||
|
||
it('should return MacroActions for KeyDown and Wait steps', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' }, | ||
{ id: '2', type: MacroStepType.Wait, delay: 100 } | ||
] | ||
const result = stepsToActions(steps) | ||
expect(result).toEqual([{ key: 'a', type: MacroActionType.KeyDown, delay: 100 }]) | ||
}) | ||
|
||
it('should return MacroActions for KeyUp and Wait steps', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyUp, keyName: 'b' }, | ||
{ id: '2', type: MacroStepType.Wait, delay: 200 } | ||
] | ||
const result = stepsToActions(steps) | ||
expect(result).toEqual([{ key: 'b', type: MacroActionType.KeyUp, delay: 200 }]) | ||
}) | ||
|
||
it('should throw an error for incorrect step order', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'b' }, | ||
{ id: '2', type: MacroStepType.Wait, delay: 200 }, | ||
{ id: '3', type: MacroStepType.Wait, delay: 200 }, | ||
{ id: '4', type: MacroStepType.KeyUp, keyName: 'b' } | ||
] | ||
expect(() => stepsToActions(steps)).toThrow('Incorrect step order') | ||
}) | ||
}) |
51 changes: 51 additions & 0 deletions
51
ui/src/features/keys/select-macro/lib/__tests__/normalize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { type MacroStep, MacroStepType } from '../../model' | ||
import { normalizeDelaySteps } from '../normalize' | ||
|
||
describe('normalizeDelaySteps', () => { | ||
it('should return the same steps when there are no Wait steps', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'x' }, | ||
{ id: '2', type: MacroStepType.KeyUp, keyName: 'x' } | ||
] | ||
const normalizedSteps = normalizeDelaySteps(steps) | ||
expect(normalizedSteps).toEqual(steps) | ||
}) | ||
|
||
it('should combine a single Wait step into one', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.Wait, delay: 100 } | ||
] | ||
const normalizedSteps = normalizeDelaySteps(steps) | ||
expect(normalizedSteps).toEqual(steps) | ||
}) | ||
|
||
it('should combine multiple consecutive Wait steps into one', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.Wait, delay: 100 }, | ||
{ id: '2', type: MacroStepType.Wait, delay: 200 } | ||
] | ||
const normalizedSteps = normalizeDelaySteps(steps) | ||
expect(normalizedSteps).toEqual([ | ||
{ id: '2', type: MacroStepType.Wait, delay: 300 } | ||
]) | ||
}) | ||
|
||
it('should keep Wait steps interspersed with other step types', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'y' }, | ||
{ id: '2', type: MacroStepType.Wait, delay: 1300 }, | ||
{ id: '3', type: MacroStepType.Wait, delay: 12 }, | ||
{ id: '4', type: MacroStepType.KeyUp, keyName: 'y' }, | ||
{ id: '5', type: MacroStepType.Wait, delay: 200 } | ||
] | ||
const normalizedSteps = normalizeDelaySteps(steps) | ||
expect(normalizedSteps).toEqual([ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'y' }, | ||
{ id: '3', type: MacroStepType.Wait, delay: 1312 }, | ||
{ id: '4', type: MacroStepType.KeyUp, keyName: 'y' }, | ||
{ id: '5', type: MacroStepType.Wait, delay: 200 } | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { type MacroStep, MacroStepType } from '../../model' | ||
import { checkMacroStepsOrder, findStepIndexToBottom, findStepIndexToTop } from '../order' | ||
|
||
const exampleSteps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'x' }, | ||
{ id: '2', type: MacroStepType.Wait, delay: 100 }, | ||
{ id: '3', type: MacroStepType.KeyUp, keyName: 'x' }, | ||
{ id: '4', type: MacroStepType.KeyDown, keyName: 'y' }, | ||
{ id: '5', type: MacroStepType.KeyUp, keyName: 'y' }, | ||
{ id: '6', type: MacroStepType.Wait, delay: 200 }, | ||
{ id: '7', type: MacroStepType.KeyDown, keyName: 'z' } | ||
] | ||
|
||
describe('findStepIndexToTop', () => { | ||
it('should return the correct index of the keyName from the top', () => { | ||
expect(findStepIndexToTop(exampleSteps, 'y', 5)).toBe(4) | ||
}) | ||
|
||
it('should return -1 if keyName is not found in the steps array', () => { | ||
expect(findStepIndexToTop(exampleSteps, 'a', 5)).toBe(-1) | ||
}) | ||
|
||
it('should return the correct index if keyName is found at the beginning', () => { | ||
expect(findStepIndexToTop(exampleSteps, 'x', 2)).toBe(0) | ||
}) | ||
}) | ||
|
||
describe('findStepIndexToBottom', () => { | ||
it('should return the correct index of the keyName from the bottom', () => { | ||
expect(findStepIndexToBottom(exampleSteps, 'x', 0)).toBe(2) | ||
}) | ||
|
||
it('should return -1 if keyName is not found in the steps array', () => { | ||
expect(findStepIndexToBottom(exampleSteps, 'y', 4)).toBe(-1) | ||
}) | ||
|
||
it('should return the correct index if keyName is found at the end', () => { | ||
expect(findStepIndexToBottom(exampleSteps, 'z', 3)).toBe(6) | ||
}) | ||
}) | ||
|
||
describe('checkMacroStepsOrder', () => { | ||
it('correctly handles order of KeyDown and KeyUp steps', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' }, | ||
{ id: '2', type: MacroStepType.KeyUp, keyName: 'a' } | ||
] | ||
expect(checkMacroStepsOrder(steps)).toBe(true) | ||
}) | ||
|
||
it('correctly handles Wait steps at the beginning', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.Wait, delay: 100 }, | ||
{ id: '2', type: MacroStepType.KeyDown, keyName: 'a' }, | ||
{ id: '3', type: MacroStepType.KeyUp, keyName: 'a' } | ||
] | ||
expect(checkMacroStepsOrder(steps)).toBe(false) | ||
}) | ||
|
||
it('correctly handles of missing pairs of KeyDown and KeyUp steps', () => { | ||
const steps: MacroStep[] = [ | ||
{ id: '1', type: MacroStepType.KeyDown, keyName: 'a' }, | ||
{ id: '2', type: MacroStepType.KeyUp, keyName: 'a' }, | ||
{ id: '3', type: MacroStepType.KeyDown, keyName: 'b' } | ||
] | ||
expect(checkMacroStepsOrder(steps)).toBe(false) | ||
}) | ||
}) |