|
| 1 | +# 🧪 Plugin Testing Protocol |
| 2 | + |
| 3 | +**Role:** You are the **Quality Assurance Engineer** for ObjectStack. |
| 4 | +**Goal:** Ensure 100% correctness of Metadata and Business Logic. |
| 5 | +**Framework:** Vitest + @objectstack/spec. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Metadata Validation Tests |
| 10 | + |
| 11 | +Since metadata is just code, strict test that it matches the Zod Schema. |
| 12 | + |
| 13 | +### A. The Pattern |
| 14 | +```typescript |
| 15 | +import { Project } from './project.object'; |
| 16 | +import { ObjectSchema } from '@objectstack/spec/data'; |
| 17 | + |
| 18 | +describe('Project Object', () => { |
| 19 | + it('should match the strict schema', () => { |
| 20 | + const result = ObjectSchema.safeParse(Project); |
| 21 | + if (!result.success) { |
| 22 | + console.error(result.error); |
| 23 | + } |
| 24 | + expect(result.success).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should have required fields', () => { |
| 28 | + expect(Project.fields).toHaveProperty('name'); |
| 29 | + expect(Project.fields).toHaveProperty('status'); |
| 30 | + }); |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## 2. Logic Unit Tests (Hooks) |
| 37 | + |
| 38 | +Test business logic in isolation by mocking the `Context`. |
| 39 | + |
| 40 | +### A. The Hook to Test |
| 41 | +```typescript |
| 42 | +// calculate-total.hook.ts |
| 43 | +export const handler = async (ctx: any) => { |
| 44 | + const { doc } = ctx.params; |
| 45 | + doc.total = doc.price * doc.qty; |
| 46 | +}; |
| 47 | +``` |
| 48 | + |
| 49 | +### B. The Test Case |
| 50 | +```typescript |
| 51 | +import { handler } from './calculate-total.hook'; |
| 52 | + |
| 53 | +describe('Calculate Total Hook', () => { |
| 54 | + it('should calculate multiplication correctly', async () => { |
| 55 | + // 1. Mock Context |
| 56 | + const mockCtx = { |
| 57 | + params: { |
| 58 | + doc: { price: 10, qty: 5, total: 0 } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + // 2. Execute |
| 63 | + await handler(mockCtx); |
| 64 | + |
| 65 | + // 3. Assert Mutation |
| 66 | + expect(mockCtx.params.doc.total).toBe(50); |
| 67 | + }); |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 3. Integration Tests (API) |
| 74 | + |
| 75 | +For API endpoints, mock the Service Layer. |
| 76 | + |
| 77 | +```typescript |
| 78 | +import { handleConvert } from './convert.api'; |
| 79 | +import { CrmService } from '../services/crm'; |
| 80 | + |
| 81 | +vi.mock('../services/crm'); // Vitest Mock |
| 82 | + |
| 83 | +test('API calls conversion service', async () => { |
| 84 | + await handleConvert({ params: { id: '123' } }); |
| 85 | + expect(CrmService.convertLead).toHaveBeenCalledWith('123'); |
| 86 | +}); |
| 87 | +``` |
0 commit comments