refactor(ipc): harden channels and unify IPC contracts
This commit is contained in:
17
tests/unit/file-ipc-paths.test.ts
Normal file
17
tests/unit/file-ipc-paths.test.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import path from 'path'
|
||||
import { isPathWithinAllowedRoots } from '../../src/main/ipc/file-handler'
|
||||
|
||||
describe('File IPC Path Guard', () => {
|
||||
it('accepts path under allowed root', () => {
|
||||
const root = path.resolve('C:/safe/root')
|
||||
const file = path.join(root, 'nested', 'file.txt')
|
||||
expect(isPathWithinAllowedRoots(file, [root])).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects path outside allowed roots', () => {
|
||||
const root = path.resolve('C:/safe/root')
|
||||
const outside = path.resolve('C:/other/location/file.txt')
|
||||
expect(isPathWithinAllowedRoots(outside, [root])).toBe(false)
|
||||
})
|
||||
})
|
||||
21
tests/unit/ipc-index.test.ts
Normal file
21
tests/unit/ipc-index.test.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { withErrorHandling } from '../../src/main/ipc'
|
||||
import { ValidationError } from '../../src/main/types/errors'
|
||||
|
||||
describe('IPC Result Wrapper', () => {
|
||||
it('wraps successful values into IpcResult.data', async () => {
|
||||
const result = await withErrorHandling(async () => ({ value: 42 }), 'test:success')
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.data).toEqual({ value: 42 })
|
||||
})
|
||||
|
||||
it('wraps exceptions into IpcResult.error/code', async () => {
|
||||
const result = await withErrorHandling(async () => {
|
||||
throw new ValidationError('Invalid input', 'VAL_INVALID_INPUT')
|
||||
}, 'test:failure')
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe('Invalid input')
|
||||
expect(result.code).toBe('VAL_INVALID_INPUT')
|
||||
})
|
||||
})
|
||||
11
tests/unit/preload-surface.test.ts
Normal file
11
tests/unit/preload-surface.test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
describe('Preload API Surface', () => {
|
||||
it('does not expose ipcRenderer in window typings', () => {
|
||||
const dtsPath = path.resolve(process.cwd(), 'src/preload/index.d.ts')
|
||||
const content = fs.readFileSync(dtsPath, 'utf-8')
|
||||
expect(content.includes('ipcRenderer:')).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user