Skip to content

Commit 3af513c

Browse files
authored
harden migration 37 (#1551)
1 parent b20ba9e commit 3af513c

File tree

4 files changed

+144
-76
lines changed

4 files changed

+144
-76
lines changed

main/store/migrations/index.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import log from 'electron-log'
22
import { v5 as uuidv5 } from 'uuid'
3+
import { z } from 'zod'
4+
35
import { accountNS, isDefaultAccountName } from '../../../resources/domain/account'
6+
import { isWindows } from '../../../resources/platform'
47

58
const migrations = {
69
4: (initial) => {
@@ -930,12 +933,45 @@ const migrations = {
930933
return initial
931934
},
932935
37: (initial) => {
933-
const isWindows = process.platform === 'win32'
934-
const { shortcuts } = initial.main || {}
935-
const altGrIndex = shortcuts.summon.modifierKeys.indexOf('AltGr')
936-
if (altGrIndex > -1) {
937-
const altGrReplacement = isWindows ? ['Alt', 'Control'] : ['Alt']
938-
initial.main.shortcuts.summon.modifierKeys.splice(altGrIndex, 1, ...altGrReplacement)
936+
const replaceAltGr = () => (isWindows() ? ['Alt', 'Control'] : ['Alt'])
937+
const updateModifierKey = (key) => (key === 'AltGr' ? replaceAltGr(key) : key)
938+
939+
const defaultShortcuts = {
940+
summon: {
941+
modifierKeys: ['Alt'],
942+
shortcutKey: 'Slash',
943+
enabled: true,
944+
configuring: false
945+
}
946+
}
947+
948+
const shortcutsSchema = z
949+
.object({
950+
summon: z.object({
951+
modifierKeys: z.array(z.string()),
952+
shortcutKey: z.string(),
953+
enabled: z.boolean(),
954+
configuring: z.boolean()
955+
})
956+
})
957+
.catch(defaultShortcuts)
958+
959+
const result = shortcutsSchema.safeParse(initial.main.shortcuts)
960+
961+
if (result.success) {
962+
const shortcuts = result.data
963+
964+
const updatedSummonShortcut = {
965+
...shortcuts.summon,
966+
modifierKeys: shortcuts.summon.modifierKeys.map(updateModifierKey).flat()
967+
}
968+
969+
initial.main.shortcuts = {
970+
...shortcuts,
971+
summon: updatedSummonShortcut
972+
}
973+
} else {
974+
log.error('Migration 37: Could not migrate shortcuts', result.error)
939975
}
940976

941977
return initial

resources/platform/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isWindows() {
2+
return process.platform === 'win32'
3+
}

test/main/store/migrations/index.test.js

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import log from 'electron-log'
33
import migrations from '../../../../main/store/migrations'
44
import { getDefaultAccountName } from '../../../../resources/domain/account'
55
import { capitalize } from '../../../../resources/utils'
6-
import { withPlatform } from '../../../util'
6+
import { isWindows } from '../../../../resources/platform'
7+
8+
jest.mock('../../../../resources/platform')
79

810
let state
911

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

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

14921497
it('should update the summon shortcut on Linux', () => {
1493-
withPlatform('linux', () => {
1494-
const updatedState = migrations.apply(state, 37)
1495-
const { shortcuts } = updatedState.main
1496-
1497-
expect(shortcuts).toStrictEqual({
1498-
summon: {
1499-
modifierKeys: ['Alt'],
1500-
shortcutKey: 'Slash',
1501-
enabled: true,
1502-
configuring: false
1503-
}
1504-
})
1498+
isWindows.mockReturnValue(false)
1499+
1500+
const updatedState = migrations.apply(state, 37)
1501+
const { shortcuts } = updatedState.main
1502+
1503+
expect(shortcuts).toStrictEqual({
1504+
summon: {
1505+
modifierKeys: ['Alt'],
1506+
shortcutKey: 'Slash',
1507+
enabled: true,
1508+
configuring: false
1509+
}
15051510
})
15061511
})
15071512

15081513
it('should update the summon shortcut on Windows', () => {
1509-
withPlatform('win32', () => {
1510-
const updatedState = migrations.apply(state, 37)
1511-
const { shortcuts } = updatedState.main
1512-
1513-
expect(shortcuts).toStrictEqual({
1514-
summon: {
1515-
modifierKeys: ['Alt', 'Control'],
1516-
shortcutKey: 'Slash',
1517-
enabled: true,
1518-
configuring: false
1519-
}
1520-
})
1514+
isWindows.mockReturnValue(true)
1515+
1516+
const updatedState = migrations.apply(state, 37)
1517+
const { shortcuts } = updatedState.main
1518+
1519+
expect(shortcuts).toStrictEqual({
1520+
summon: {
1521+
modifierKeys: ['Alt', 'Control'],
1522+
shortcutKey: 'Slash',
1523+
enabled: true,
1524+
configuring: false
1525+
}
15211526
})
15221527
})
15231528
})
15241529

1525-
describe('when altGr is not a modifier', () => {
1526-
beforeEach(() => {
1527-
state.main.shortcuts = {
1528-
summon: { modifierKeys: ['Alt'], shortcutKey: 'Slash', enabled: true, configuring: false }
1530+
it('should not update the summon shortcut on Windows', () => {
1531+
isWindows.mockReturnValue(true)
1532+
1533+
const updatedState = migrations.apply(state, 37)
1534+
const { shortcuts } = updatedState.main
1535+
1536+
expect(shortcuts).toStrictEqual({
1537+
summon: {
1538+
modifierKeys: ['Alt'],
1539+
shortcutKey: 'Slash',
1540+
enabled: true,
1541+
configuring: false
15291542
}
15301543
})
1544+
})
15311545

1532-
it('should not update the summon shortcut on Windows', () => {
1533-
withPlatform('win32', () => {
1534-
const updatedState = migrations.apply(state, 37)
1535-
const { shortcuts } = updatedState.main
1536-
1537-
expect(shortcuts).toStrictEqual({
1538-
summon: {
1539-
modifierKeys: ['Alt'],
1540-
shortcutKey: 'Slash',
1541-
enabled: true,
1542-
configuring: false
1543-
}
1544-
})
1545-
})
1546+
it('should not update the summon shortcut on Linux', () => {
1547+
isWindows.mockReturnValue(false)
1548+
1549+
const updatedState = migrations.apply(state, 37)
1550+
const { shortcuts } = updatedState.main
1551+
1552+
expect(shortcuts).toStrictEqual({
1553+
summon: {
1554+
modifierKeys: ['Alt'],
1555+
shortcutKey: 'Slash',
1556+
enabled: true,
1557+
configuring: false
1558+
}
1559+
})
1560+
})
1561+
1562+
it('should handle missing shortcuts', () => {
1563+
delete state.main.shortcuts
1564+
const updatedState = migrations.apply(state, 37)
1565+
const { shortcuts } = updatedState.main
1566+
1567+
expect(shortcuts).toStrictEqual({
1568+
summon: {
1569+
modifierKeys: ['Alt'],
1570+
shortcutKey: 'Slash',
1571+
enabled: true,
1572+
configuring: false
1573+
}
15461574
})
1575+
})
15471576

1548-
it('should not update the summon shortcut on Linux', () => {
1549-
withPlatform('linux', () => {
1550-
const updatedState = migrations.apply(state, 37)
1551-
const { shortcuts } = updatedState.main
1552-
1553-
expect(shortcuts).toStrictEqual({
1554-
summon: {
1555-
modifierKeys: ['Alt'],
1556-
shortcutKey: 'Slash',
1557-
enabled: true,
1558-
configuring: false
1559-
}
1560-
})
1561-
})
1577+
it('should handle missing shortcuts.summon', () => {
1578+
delete state.main.shortcuts.summon
1579+
const updatedState = migrations.apply(state, 37)
1580+
const { shortcuts } = updatedState.main
1581+
1582+
expect(shortcuts).toStrictEqual({
1583+
summon: {
1584+
modifierKeys: ['Alt'],
1585+
shortcutKey: 'Slash',
1586+
enabled: true,
1587+
configuring: false
1588+
}
1589+
})
1590+
})
1591+
1592+
it('should handle missing shortcuts.summon.modifierKeys', () => {
1593+
delete state.main.shortcuts.summon.modifierKeys
1594+
const updatedState = migrations.apply(state, 37)
1595+
const { shortcuts } = updatedState.main
1596+
1597+
expect(shortcuts).toStrictEqual({
1598+
summon: {
1599+
modifierKeys: ['Alt'],
1600+
shortcutKey: 'Slash',
1601+
enabled: true,
1602+
configuring: false
1603+
}
15621604
})
15631605
})
15641606
})

test/util.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,3 @@ import { intToHex } from '@ethereumjs/util'
22

33
export const gweiToHex = (gwei) => intToHex(gwei * 1e9)
44
export const flushPromises = () => new Promise(jest.requireActual('timers').setImmediate)
5-
6-
export async function withPlatform(platform, test) {
7-
const originalPlatform = process.platform
8-
Object.defineProperty(process, 'platform', {
9-
value: platform
10-
})
11-
12-
await test()
13-
14-
Object.defineProperty(process, 'platform', {
15-
value: originalPlatform
16-
})
17-
}

0 commit comments

Comments
 (0)