Skip to content

Commit

Permalink
harden migration 37 (#1551)
Browse files Browse the repository at this point in the history
goosewobbler authored May 10, 2023
1 parent b20ba9e commit 3af513c
Showing 4 changed files with 144 additions and 76 deletions.
48 changes: 42 additions & 6 deletions main/store/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import log from 'electron-log'
import { v5 as uuidv5 } from 'uuid'
import { z } from 'zod'

import { accountNS, isDefaultAccountName } from '../../../resources/domain/account'
import { isWindows } from '../../../resources/platform'

const migrations = {
4: (initial) => {
@@ -930,12 +933,45 @@ const migrations = {
return initial
},
37: (initial) => {
const isWindows = process.platform === 'win32'
const { shortcuts } = initial.main || {}
const altGrIndex = shortcuts.summon.modifierKeys.indexOf('AltGr')
if (altGrIndex > -1) {
const altGrReplacement = isWindows ? ['Alt', 'Control'] : ['Alt']
initial.main.shortcuts.summon.modifierKeys.splice(altGrIndex, 1, ...altGrReplacement)
const replaceAltGr = () => (isWindows() ? ['Alt', 'Control'] : ['Alt'])
const updateModifierKey = (key) => (key === 'AltGr' ? replaceAltGr(key) : key)

const defaultShortcuts = {
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
}

const shortcutsSchema = z
.object({
summon: z.object({
modifierKeys: z.array(z.string()),
shortcutKey: z.string(),
enabled: z.boolean(),
configuring: z.boolean()
})
})
.catch(defaultShortcuts)

const result = shortcutsSchema.safeParse(initial.main.shortcuts)

if (result.success) {
const shortcuts = result.data

const updatedSummonShortcut = {
...shortcuts.summon,
modifierKeys: shortcuts.summon.modifierKeys.map(updateModifierKey).flat()
}

initial.main.shortcuts = {
...shortcuts,
summon: updatedSummonShortcut
}
} else {
log.error('Migration 37: Could not migrate shortcuts', result.error)
}

return initial
3 changes: 3 additions & 0 deletions resources/platform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isWindows() {
return process.platform === 'win32'
}
156 changes: 99 additions & 57 deletions test/main/store/migrations/index.test.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@ import log from 'electron-log'
import migrations from '../../../../main/store/migrations'
import { getDefaultAccountName } from '../../../../resources/domain/account'
import { capitalize } from '../../../../resources/utils'
import { withPlatform } from '../../../util'
import { isWindows } from '../../../../resources/platform'

jest.mock('../../../../resources/platform')

let state

@@ -1480,6 +1482,9 @@ describe('migration 35', () => {
describe('migration 37', () => {
beforeEach(() => {
state.main._version = 36
state.main.shortcuts = {
summon: { modifierKeys: ['Alt'], shortcutKey: 'Slash', enabled: true, configuring: false }
}
})

describe('when altGr is a modifier', () => {
@@ -1490,75 +1495,112 @@ describe('migration 37', () => {
})

it('should update the summon shortcut on Linux', () => {
withPlatform('linux', () => {
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
isWindows.mockReturnValue(false)

const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})

it('should update the summon shortcut on Windows', () => {
withPlatform('win32', () => {
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt', 'Control'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
isWindows.mockReturnValue(true)

const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt', 'Control'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})
})

describe('when altGr is not a modifier', () => {
beforeEach(() => {
state.main.shortcuts = {
summon: { modifierKeys: ['Alt'], shortcutKey: 'Slash', enabled: true, configuring: false }
it('should not update the summon shortcut on Windows', () => {
isWindows.mockReturnValue(true)

const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})

it('should not update the summon shortcut on Windows', () => {
withPlatform('win32', () => {
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})
it('should not update the summon shortcut on Linux', () => {
isWindows.mockReturnValue(false)

const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})

it('should handle missing shortcuts', () => {
delete state.main.shortcuts
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})

it('should not update the summon shortcut on Linux', () => {
withPlatform('linux', () => {
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})
it('should handle missing shortcuts.summon', () => {
delete state.main.shortcuts.summon
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})

it('should handle missing shortcuts.summon.modifierKeys', () => {
delete state.main.shortcuts.summon.modifierKeys
const updatedState = migrations.apply(state, 37)
const { shortcuts } = updatedState.main

expect(shortcuts).toStrictEqual({
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: true,
configuring: false
}
})
})
})
13 changes: 0 additions & 13 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -2,16 +2,3 @@ import { intToHex } from '@ethereumjs/util'

export const gweiToHex = (gwei) => intToHex(gwei * 1e9)
export const flushPromises = () => new Promise(jest.requireActual('timers').setImmediate)

export async function withPlatform(platform, test) {
const originalPlatform = process.platform
Object.defineProperty(process, 'platform', {
value: platform
})

await test()

Object.defineProperty(process, 'platform', {
value: originalPlatform
})
}

0 comments on commit 3af513c

Please sign in to comment.