|
| 1 | +/** |
| 2 | + * Plugin Factory Tests |
| 3 | + * |
| 4 | + * Tests the main plugin initialization and hook wiring. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, mock, beforeEach } from 'bun:test' |
| 8 | +import type { Plugin } from '@opencode-ai/plugin' |
| 9 | +import { createPlugin } from '../../src/core/plugin' |
| 10 | + |
| 11 | +describe('createPlugin', () => { |
| 12 | + // Mock context object matching OpenCode SDK structure |
| 13 | + const createMockContext = (): any => ({ |
| 14 | + client: { |
| 15 | + app: { |
| 16 | + log: mock(async () => {}), |
| 17 | + }, |
| 18 | + session: { |
| 19 | + prompt: mock(async () => {}), |
| 20 | + }, |
| 21 | + } as any, |
| 22 | + project: { |
| 23 | + id: 'test-project', |
| 24 | + worktree: '/tmp/test-project', |
| 25 | + vcs: 'git', |
| 26 | + name: 'Test Project', |
| 27 | + }, |
| 28 | + $: mock(() => ({})) as any, |
| 29 | + directory: '/tmp/test-project', |
| 30 | + worktree: '/tmp/test-project', |
| 31 | + serverUrl: new URL('http://localhost:8080'), |
| 32 | + }) |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + // Reset mocks between tests |
| 36 | + mock.restore() |
| 37 | + }) |
| 38 | + |
| 39 | + it('should be a valid Plugin function', () => { |
| 40 | + expect(typeof createPlugin).toBe('function') |
| 41 | + }) |
| 42 | + |
| 43 | + it('should initialize plugin and return hooks object', async () => { |
| 44 | + const mockContext = createMockContext() |
| 45 | + |
| 46 | + const hooks = await createPlugin(mockContext) |
| 47 | + |
| 48 | + expect(hooks).toBeTypeOf('object') |
| 49 | + expect(hooks).not.toBeNull() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should register permission.asked hook', async () => { |
| 53 | + const mockContext = createMockContext() |
| 54 | + |
| 55 | + const hooks = await createPlugin(mockContext) |
| 56 | + |
| 57 | + // Check hooks exist using property access instead of hasProperty |
| 58 | + expect('permission.asked' in hooks).toBe(true) |
| 59 | + expect(typeof hooks['permission.asked']).toBe('function') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should register event hook for session events', async () => { |
| 63 | + const mockContext = createMockContext() |
| 64 | + |
| 65 | + const hooks = await createPlugin(mockContext) |
| 66 | + |
| 67 | + expect(hooks).toHaveProperty('event') |
| 68 | + expect(typeof hooks.event).toBe('function') |
| 69 | + }) |
| 70 | + |
| 71 | + it('should register experimental.chat.system.transform hook', async () => { |
| 72 | + const mockContext = createMockContext() |
| 73 | + |
| 74 | + const hooks = await createPlugin(mockContext) |
| 75 | + |
| 76 | + expect('experimental.chat.system.transform' in hooks).toBe(true) |
| 77 | + expect(typeof hooks['experimental.chat.system.transform']).toBe('function') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should register experimental.session.compacting hook', async () => { |
| 81 | + const mockContext = createMockContext() |
| 82 | + |
| 83 | + const hooks = await createPlugin(mockContext) |
| 84 | + |
| 85 | + expect('experimental.session.compacting' in hooks).toBe(true) |
| 86 | + expect(typeof hooks['experimental.session.compacting']).toBe('function') |
| 87 | + }) |
| 88 | + |
| 89 | + it('should load config from worktree directory', async () => { |
| 90 | + const mockContext = createMockContext() |
| 91 | + |
| 92 | + // Config loading happens during initialization |
| 93 | + await createPlugin(mockContext) |
| 94 | + |
| 95 | + // Config is loaded (verified by no errors thrown) |
| 96 | + // Actual config behavior is tested in config.test.ts |
| 97 | + expect(true).toBe(true) |
| 98 | + }) |
| 99 | + |
| 100 | + it('should log initialization message', async () => { |
| 101 | + const mockContext = createMockContext() |
| 102 | + const logMock = mockContext.client.app.log |
| 103 | + |
| 104 | + await createPlugin(mockContext) |
| 105 | + |
| 106 | + // Should log initialization (called at least once) |
| 107 | + expect(logMock).toHaveBeenCalled() |
| 108 | + }) |
| 109 | + |
| 110 | + it('should handle config load errors gracefully', async () => { |
| 111 | + const mockContext = createMockContext() |
| 112 | + // Use invalid directory to trigger config load warning |
| 113 | + mockContext.project.worktree = '/nonexistent/directory/that/does/not/exist' |
| 114 | + mockContext.worktree = '/nonexistent/directory/that/does/not/exist' |
| 115 | + |
| 116 | + // Should not throw - graceful degradation |
| 117 | + const hooks = await createPlugin(mockContext) |
| 118 | + |
| 119 | + // Hooks should still be registered (enabled defaults to true) |
| 120 | + expect('permission.asked' in hooks).toBe(true) |
| 121 | + expect('event' in hooks).toBe(true) |
| 122 | + expect('experimental.chat.system.transform' in hooks).toBe(true) |
| 123 | + }) |
| 124 | + |
| 125 | + it('should return empty object when plugin is disabled via config', async () => { |
| 126 | + const mockContext = createMockContext() |
| 127 | + |
| 128 | + // Create opencode.json with enabled: false |
| 129 | + const configPath = `${mockContext.project.worktree}/opencode.json` |
| 130 | + await Bun.write(configPath, JSON.stringify({ |
| 131 | + blockerDiverter: { |
| 132 | + enabled: false |
| 133 | + } |
| 134 | + })) |
| 135 | + |
| 136 | + const hooks = await createPlugin(mockContext) |
| 137 | + |
| 138 | + // Should return empty hooks object when disabled |
| 139 | + expect(Object.keys(hooks).length).toBe(0) |
| 140 | + |
| 141 | + // Cleanup |
| 142 | + await Bun.write(configPath, '') |
| 143 | + }) |
| 144 | + |
| 145 | + it('should pass context to permission hook', async () => { |
| 146 | + const mockContext = createMockContext() |
| 147 | + |
| 148 | + const hooks = await createPlugin(mockContext) |
| 149 | + |
| 150 | + // Permission hook should exist and be callable |
| 151 | + expect(hooks['permission.asked']).toBeDefined() |
| 152 | + |
| 153 | + // Mock permission input |
| 154 | + const mockInput = { |
| 155 | + sessionID: 'test-session', |
| 156 | + type: 'bash', |
| 157 | + title: 'Test Permission', |
| 158 | + metadata: { tool: 'bash' } |
| 159 | + } |
| 160 | + const mockOutput = { status: 'ask' as const } |
| 161 | + |
| 162 | + // Should not throw when called |
| 163 | + await expect( |
| 164 | + hooks['permission.asked']!(mockInput as any, mockOutput) |
| 165 | + ).resolves.toBeUndefined() |
| 166 | + }) |
| 167 | + |
| 168 | + it('should pass context to session hook', async () => { |
| 169 | + const mockContext = createMockContext() |
| 170 | + |
| 171 | + const hooks = await createPlugin(mockContext) |
| 172 | + |
| 173 | + // Session hook should exist and be callable |
| 174 | + expect(hooks.event).toBeDefined() |
| 175 | + |
| 176 | + // Mock session event |
| 177 | + const mockEvent = { |
| 178 | + event: { |
| 179 | + type: 'session.created', |
| 180 | + session_id: 'test-session' |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // Should not throw when called |
| 185 | + await expect( |
| 186 | + hooks.event!(mockEvent as any) |
| 187 | + ).resolves.toBeUndefined() |
| 188 | + }) |
| 189 | + |
| 190 | + it('should pass config to system prompt hook', async () => { |
| 191 | + const mockContext = createMockContext() |
| 192 | + |
| 193 | + const hooks = await createPlugin(mockContext) |
| 194 | + |
| 195 | + // System prompt hook should exist and be callable |
| 196 | + expect(hooks['experimental.chat.system.transform']).toBeDefined() |
| 197 | + |
| 198 | + // Mock system prompt input/output |
| 199 | + const mockInput = { |
| 200 | + sessionID: 'test-session', |
| 201 | + model: { id: 'claude-3' } |
| 202 | + } |
| 203 | + const mockOutput = { system: [] as string[] } |
| 204 | + |
| 205 | + // Should not throw when called |
| 206 | + await expect( |
| 207 | + hooks['experimental.chat.system.transform']!(mockInput as any, mockOutput) |
| 208 | + ).resolves.toBeUndefined() |
| 209 | + }) |
| 210 | +}) |
0 commit comments