|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { DevPlugin } from './dev-plugin'; |
| 3 | + |
| 4 | +describe('DevPlugin', () => { |
| 5 | + it('should have correct metadata', () => { |
| 6 | + const plugin = new DevPlugin(); |
| 7 | + expect(plugin.name).toBe('com.objectstack.plugin.dev'); |
| 8 | + expect(plugin.type).toBe('standard'); |
| 9 | + expect(plugin.version).toBe('1.0.0'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should accept default options', () => { |
| 13 | + const plugin = new DevPlugin(); |
| 14 | + expect(plugin).toBeDefined(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should accept custom options', () => { |
| 18 | + const plugin = new DevPlugin({ |
| 19 | + port: 4000, |
| 20 | + seedAdminUser: false, |
| 21 | + verbose: false, |
| 22 | + services: { auth: false }, |
| 23 | + }); |
| 24 | + expect(plugin).toBeDefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should init with mocked context and handle missing deps gracefully', async () => { |
| 28 | + const ctx: any = { |
| 29 | + logger: { |
| 30 | + info: vi.fn(), |
| 31 | + debug: vi.fn(), |
| 32 | + warn: vi.fn(), |
| 33 | + error: vi.fn(), |
| 34 | + }, |
| 35 | + getService: vi.fn().mockImplementation(() => { throw new Error('not found'); }), |
| 36 | + getServices: vi.fn().mockReturnValue(new Map()), |
| 37 | + registerService: vi.fn(), |
| 38 | + hook: vi.fn(), |
| 39 | + trigger: vi.fn(), |
| 40 | + getKernel: vi.fn(), |
| 41 | + }; |
| 42 | + |
| 43 | + // DevPlugin should not throw even if peer dependencies are missing |
| 44 | + const plugin = new DevPlugin({ seedAdminUser: false }); |
| 45 | + await expect(plugin.init(ctx)).resolves.not.toThrow(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should skip disabled services', async () => { |
| 49 | + const ctx: any = { |
| 50 | + logger: { |
| 51 | + info: vi.fn(), |
| 52 | + debug: vi.fn(), |
| 53 | + warn: vi.fn(), |
| 54 | + error: vi.fn(), |
| 55 | + }, |
| 56 | + getService: vi.fn().mockImplementation(() => { throw new Error('not found'); }), |
| 57 | + getServices: vi.fn().mockReturnValue(new Map()), |
| 58 | + registerService: vi.fn(), |
| 59 | + hook: vi.fn(), |
| 60 | + trigger: vi.fn(), |
| 61 | + getKernel: vi.fn(), |
| 62 | + }; |
| 63 | + |
| 64 | + const plugin = new DevPlugin({ |
| 65 | + seedAdminUser: false, |
| 66 | + services: { |
| 67 | + objectql: false, |
| 68 | + driver: false, |
| 69 | + auth: false, |
| 70 | + server: false, |
| 71 | + rest: false, |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + await plugin.init(ctx); |
| 76 | + |
| 77 | + // No child plugins should be registered when all disabled |
| 78 | + // Logger should show init with 0 services |
| 79 | + const lastInfoCall = ctx.logger.info.mock.calls.find( |
| 80 | + (call: any[]) => typeof call[0] === 'string' && call[0].includes('initialized'), |
| 81 | + ); |
| 82 | + expect(lastInfoCall).toBeDefined(); |
| 83 | + expect(lastInfoCall[0]).toContain('0 service'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should destroy without errors', async () => { |
| 87 | + const plugin = new DevPlugin(); |
| 88 | + await expect(plugin.destroy()).resolves.not.toThrow(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments