|
| 1 | +/** |
| 2 | + * @file src/main/ipc/__tests__/stacks.ipc.test.ts |
| 3 | + * |
| 4 | + * @created 07.03.2026 |
| 5 | + * @modified 07.03.2026 |
| 6 | + * |
| 7 | + * @author Christian Blank <christianblank91@protonmail.com> |
| 8 | + * @copyright 2026 |
| 9 | + * |
| 10 | + * @description Unit tests for stacks IPC handlers. DB repos and feature gates |
| 11 | + * are backed by an in-memory SQLite database so tests verify real persistence |
| 12 | + * behavior without touching the filesystem. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 16 | +import { createTestDb } from '@main/db/__tests__/helpers' |
| 17 | +import { getDatabase } from '@main/db/connection' |
| 18 | +import type { McpStack } from '@shared/channels' |
| 19 | + |
| 20 | +// ─── Module Mocks ───────────────────────────────────────────────────────────── |
| 21 | + |
| 22 | +vi.mock('electron', () => ({ |
| 23 | + ipcMain: { handle: vi.fn() }, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('electron-log', () => ({ |
| 27 | + default: { debug: vi.fn(), info: vi.fn(), error: vi.fn(), warn: vi.fn() }, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('@main/db/connection', () => ({ getDatabase: vi.fn() })) |
| 31 | + |
| 32 | +vi.mock('@main/licensing/feature-gates', () => ({ |
| 33 | + checkGate: vi.fn().mockImplementation((key: string) => { |
| 34 | + if (key === 'stackExport') return true |
| 35 | + return true |
| 36 | + }), |
| 37 | +})) |
| 38 | + |
| 39 | +// ─── Helpers ────────────────────────────────────────────────────────────────── |
| 40 | + |
| 41 | +import { ipcMain } from 'electron' |
| 42 | +import { checkGate } from '@main/licensing/feature-gates' |
| 43 | +import { registerStacksIpc } from '../stacks.ipc' |
| 44 | + |
| 45 | +type IpcHandler = (_event: unknown, ...args: unknown[]) => unknown |
| 46 | + |
| 47 | +const getHandler = (channel: string): IpcHandler => { |
| 48 | + const calls = vi.mocked(ipcMain.handle).mock.calls |
| 49 | + const call = calls.find(([ch]) => ch === channel) |
| 50 | + if (!call) throw new Error(`No handler registered for "${channel}"`) |
| 51 | + return call[1] as IpcHandler |
| 52 | +} |
| 53 | + |
| 54 | +const makeStack = (overrides: Partial<McpStack> = {}): string => |
| 55 | + JSON.stringify({ |
| 56 | + name: 'My Stack', |
| 57 | + description: '', |
| 58 | + version: '1.0.0', |
| 59 | + servers: [ |
| 60 | + { |
| 61 | + name: 'test-server', |
| 62 | + type: 'stdio', |
| 63 | + command: 'npx', |
| 64 | + args: ['-y', '@test/mcp'], |
| 65 | + env: {}, |
| 66 | + enabled: true, |
| 67 | + tags: [], |
| 68 | + notes: '', |
| 69 | + createdAt: new Date().toISOString(), |
| 70 | + updatedAt: new Date().toISOString(), |
| 71 | + }, |
| 72 | + ], |
| 73 | + rules: [ |
| 74 | + { |
| 75 | + name: 'test-rule', |
| 76 | + description: '', |
| 77 | + content: '# Test rule', |
| 78 | + category: 'general', |
| 79 | + tags: [], |
| 80 | + enabled: true, |
| 81 | + priority: 'normal', |
| 82 | + scope: 'global', |
| 83 | + fileGlobs: [], |
| 84 | + alwaysApply: false, |
| 85 | + tokenEstimate: 5, |
| 86 | + createdAt: new Date().toISOString(), |
| 87 | + updatedAt: new Date().toISOString(), |
| 88 | + }, |
| 89 | + ], |
| 90 | + exportedAt: new Date().toISOString(), |
| 91 | + ...overrides, |
| 92 | + } satisfies McpStack) |
| 93 | + |
| 94 | +// ─── Tests ──────────────────────────────────────────────────────────────────── |
| 95 | + |
| 96 | +describe('stacks IPC handlers', () => { |
| 97 | + beforeEach(() => { |
| 98 | + vi.clearAllMocks() |
| 99 | + const db = createTestDb() |
| 100 | + vi.mocked(getDatabase).mockReturnValue(db) |
| 101 | + vi.mocked(checkGate).mockImplementation((key: string) => { |
| 102 | + if (key === 'stackExport') return true |
| 103 | + if (key === 'maxServers') return Infinity |
| 104 | + return true |
| 105 | + }) |
| 106 | + registerStacksIpc() |
| 107 | + }) |
| 108 | + |
| 109 | + describe('stacks:export', () => { |
| 110 | + it('returns a JSON string with the correct structure', async () => { |
| 111 | + const handler = getHandler('stacks:export') |
| 112 | + // No servers/rules seeded — exporting empty selection is valid. |
| 113 | + const json = (await handler(null, [], [], 'Empty Stack')) as string |
| 114 | + |
| 115 | + const stack = JSON.parse(json) as McpStack |
| 116 | + expect(stack.name).toBe('Empty Stack') |
| 117 | + expect(stack.version).toBe('1.0.0') |
| 118 | + expect(stack.servers).toEqual([]) |
| 119 | + expect(stack.rules).toEqual([]) |
| 120 | + expect(typeof stack.exportedAt).toBe('string') |
| 121 | + }) |
| 122 | + |
| 123 | + it('throws when the stackExport gate is false', async () => { |
| 124 | + vi.mocked(checkGate).mockReturnValue(false) |
| 125 | + |
| 126 | + const handler = getHandler('stacks:export') |
| 127 | + await expect(handler(null, [], [], 'Stack')).rejects.toThrow('Pro') |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + describe('stacks:import', () => { |
| 132 | + it('imports servers and rules from a valid stack JSON', async () => { |
| 133 | + const handler = getHandler('stacks:import') |
| 134 | + const result = (await handler(null, makeStack())) as { |
| 135 | + imported: number |
| 136 | + skipped: number |
| 137 | + errors: string[] |
| 138 | + } |
| 139 | + |
| 140 | + expect(result.imported).toBe(2) // 1 server + 1 rule |
| 141 | + expect(result.skipped).toBe(0) |
| 142 | + expect(result.errors).toHaveLength(0) |
| 143 | + }) |
| 144 | + |
| 145 | + it('skips duplicate server names on second import', async () => { |
| 146 | + const handler = getHandler('stacks:import') |
| 147 | + await handler(null, makeStack()) |
| 148 | + const result = (await handler(null, makeStack())) as { imported: number; skipped: number } |
| 149 | + |
| 150 | + expect(result.skipped).toBe(2) |
| 151 | + expect(result.imported).toBe(0) |
| 152 | + }) |
| 153 | + |
| 154 | + it('returns an error for invalid JSON', async () => { |
| 155 | + const handler = getHandler('stacks:import') |
| 156 | + const result = (await handler(null, 'not-json')) as { imported: number; errors: string[] } |
| 157 | + |
| 158 | + expect(result.imported).toBe(0) |
| 159 | + expect(result.errors[0]).toMatch(/Invalid JSON/) |
| 160 | + }) |
| 161 | + |
| 162 | + it('returns an error for JSON missing servers/rules arrays', async () => { |
| 163 | + const handler = getHandler('stacks:import') |
| 164 | + const result = (await handler(null, JSON.stringify({ name: 'bad' }))) as { errors: string[] } |
| 165 | + |
| 166 | + expect(result.errors[0]).toMatch(/Invalid stack format/) |
| 167 | + }) |
| 168 | + }) |
| 169 | +}) |
0 commit comments