hermes-agent/apps/desktop/electron/project-dir-ipc.test.cjs
Brooklyn Nicholson e167ed7bb1 refactor(desktop): extract project-dir + workspace settings IPC from main.cjs into project-dir-ipc.cjs
Sixth main.cjs cluster peel. The hermes:setting:defaultProjectDir:get/set/pick
handlers + hermes:workspace:sanitize move verbatim into
electron/project-dir-ipc.cjs behind a registerProjectDirIpc({ ipcMain,
readDefaultProjectDir, writeDefaultProjectDir, resolveHermesCwd,
sanitizeWorkspaceCwd }) registrar. The config readers/writers + cwd resolvers stay
in the main process and are injected.

Channel names unchanged → preload + renderer untouched. Adds
electron/project-dir-ipc.test.cjs (surface + set/sanitize behavior; get/pick touch
Electron app/dialog and are exercised in-app only).
2026-06-30 13:32:22 -05:00

63 lines
1.9 KiB
JavaScript

'use strict'
const assert = require('node:assert/strict')
const test = require('node:test')
const { registerProjectDirIpc } = require('./project-dir-ipc.cjs')
function fakeIpcMain() {
const handlers = new Map()
return {
handlers,
handle(channel, handler) {
assert.ok(!handlers.has(channel), `duplicate registration for ${channel}`)
handlers.set(channel, handler)
}
}
}
function deps(overrides = {}) {
return {
readDefaultProjectDir: () => '/projects',
resolveHermesCwd: () => '/cwd',
sanitizeWorkspaceCwd: cwd => `safe:${cwd}`,
writeDefaultProjectDir: () => {},
...overrides
}
}
test('registerProjectDirIpc wires the project-dir + workspace settings channels', () => {
const ipcMain = fakeIpcMain()
registerProjectDirIpc({ ipcMain, ...deps() })
assert.deepEqual([...ipcMain.handlers.keys()].sort(), [
'hermes:setting:defaultProjectDir:get',
'hermes:setting:defaultProjectDir:pick',
'hermes:setting:defaultProjectDir:set',
'hermes:workspace:sanitize'
])
})
// `get` / `pick` touch Electron's `app` / `dialog`, which are unavailable under
// `node --test` (require('electron') is a path stub), so they're exercised in-app
// only. The wiring of all four channels is covered by the surface test above.
test('set normalizes a blank dir to null and persists that (clears the override)', async () => {
const ipcMain = fakeIpcMain()
const writes = []
registerProjectDirIpc({ ipcMain, ...deps({ writeDefaultProjectDir: d => writes.push(d) }) })
assert.deepEqual(await ipcMain.handlers.get('hermes:setting:defaultProjectDir:set')({}, ' '), { dir: null })
assert.deepEqual(writes, [null])
})
test('workspace:sanitize delegates to the injected sanitizer', async () => {
const ipcMain = fakeIpcMain()
registerProjectDirIpc({ ipcMain, ...deps() })
assert.equal(await ipcMain.handlers.get('hermes:workspace:sanitize')({}, '/x'), 'safe:/x')
})