|
| 1 | +/** |
| 2 | + * MSW Server Setup for Tests |
| 3 | + * |
| 4 | + * This creates a complete ObjectStack environment for testing using MSW setupServer |
| 5 | + * instead of setupWorker (which is for browser only). |
| 6 | + */ |
| 7 | + |
| 8 | +import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime'; |
| 9 | +import { ObjectQLPlugin } from '@objectstack/objectql'; |
| 10 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
| 11 | +import { setupServer } from 'msw/node'; |
| 12 | +import { http, HttpResponse } from 'msw'; |
| 13 | +import appConfig from '../../objectstack.config'; |
| 14 | + |
| 15 | +let kernel: ObjectKernel | null = null; |
| 16 | +let driver: InMemoryDriver | null = null; |
| 17 | +let server: ReturnType<typeof setupServer> | null = null; |
| 18 | + |
| 19 | +export async function startMockServer() { |
| 20 | + if (kernel) { |
| 21 | + console.log('[MSW] ObjectStack Runtime already initialized'); |
| 22 | + return kernel; |
| 23 | + } |
| 24 | + |
| 25 | + console.log('[MSW] Starting ObjectStack Runtime (Test Mode)...'); |
| 26 | + |
| 27 | + driver = new InMemoryDriver(); |
| 28 | + |
| 29 | + // Create kernel |
| 30 | + kernel = new ObjectKernel(); |
| 31 | + |
| 32 | + kernel |
| 33 | + .use(new ObjectQLPlugin()) |
| 34 | + .use(new DriverPlugin(driver, 'memory')) |
| 35 | + .use(new AppPlugin(appConfig)); |
| 36 | + |
| 37 | + // Bootstrap kernel WITHOUT MSW plugin (we'll handle MSW separately for tests) |
| 38 | + await kernel.bootstrap(); |
| 39 | + |
| 40 | + // Load initial data from manifest |
| 41 | + const manifest = (appConfig as any).manifest; |
| 42 | + if (manifest && Array.isArray(manifest.data)) { |
| 43 | + console.log('[MSW] Loading initial data...'); |
| 44 | + for (const dataset of manifest.data) { |
| 45 | + if (dataset.object && Array.isArray(dataset.records)) { |
| 46 | + for (const record of dataset.records) { |
| 47 | + await driver.create(dataset.object, record); |
| 48 | + } |
| 49 | + console.log(`[MSW] Loaded ${dataset.records.length} records for ${dataset.object}`); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Create MSW handlers manually |
| 55 | + const baseUrl = 'http://localhost:3000/api/v1'; |
| 56 | + const handlers = createHandlers(baseUrl, kernel); |
| 57 | + |
| 58 | + // Setup MSW server for Node.js environment |
| 59 | + server = setupServer(...handlers); |
| 60 | + server.listen({ onUnhandledRequest: 'bypass' }); |
| 61 | + |
| 62 | + console.log('[MSW] ObjectStack Runtime ready'); |
| 63 | + return kernel; |
| 64 | +} |
| 65 | + |
| 66 | +export function stopMockServer() { |
| 67 | + if (server) { |
| 68 | + server.close(); |
| 69 | + server = null; |
| 70 | + } |
| 71 | + kernel = null; |
| 72 | + driver = null; |
| 73 | +} |
| 74 | + |
| 75 | +export function getKernel(): ObjectKernel | null { |
| 76 | + return kernel; |
| 77 | +} |
| 78 | + |
| 79 | +export function getDriver(): InMemoryDriver | null { |
| 80 | + return driver; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Create MSW request handlers for ObjectStack API |
| 85 | + */ |
| 86 | +function createHandlers(baseUrl: string, kernel: ObjectKernel) { |
| 87 | + const protocol = kernel.getService('protocol'); |
| 88 | + |
| 89 | + return [ |
| 90 | + // Discovery endpoint |
| 91 | + http.get(`${baseUrl}/`, async () => { |
| 92 | + const response = await protocol.handleDiscovery(); |
| 93 | + return HttpResponse.json(response, { status: 200 }); |
| 94 | + }), |
| 95 | + |
| 96 | + // Metadata endpoints |
| 97 | + http.get(`${baseUrl}/meta/objects`, async () => { |
| 98 | + const response = await protocol.handleMetadataListObjects(); |
| 99 | + return HttpResponse.json(response, { status: 200 }); |
| 100 | + }), |
| 101 | + |
| 102 | + http.get(`${baseUrl}/meta/objects/:objectName`, async ({ params }) => { |
| 103 | + const response = await protocol.handleMetadataGetObject({ objectName: params.objectName as string }); |
| 104 | + return HttpResponse.json(response, { status: 200 }); |
| 105 | + }), |
| 106 | + |
| 107 | + // Data endpoints - Find all |
| 108 | + http.get(`${baseUrl}/data/:objectName`, async ({ params, request }) => { |
| 109 | + const url = new URL(request.url); |
| 110 | + const query: any = {}; |
| 111 | + |
| 112 | + // Parse query parameters |
| 113 | + url.searchParams.forEach((value, key) => { |
| 114 | + try { |
| 115 | + query[key] = JSON.parse(value); |
| 116 | + } catch { |
| 117 | + query[key] = value; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + const response = await protocol.handleFind({ |
| 122 | + objectName: params.objectName as string, |
| 123 | + query |
| 124 | + }); |
| 125 | + return HttpResponse.json(response, { status: 200 }); |
| 126 | + }), |
| 127 | + |
| 128 | + // Data endpoints - Find by ID |
| 129 | + http.get(`${baseUrl}/data/:objectName/:id`, async ({ params }) => { |
| 130 | + const response = await protocol.handleFindById({ |
| 131 | + objectName: params.objectName as string, |
| 132 | + id: params.id as string |
| 133 | + }); |
| 134 | + return HttpResponse.json(response, { status: 200 }); |
| 135 | + }), |
| 136 | + |
| 137 | + // Data endpoints - Create |
| 138 | + http.post(`${baseUrl}/data/:objectName`, async ({ params, request }) => { |
| 139 | + const body = await request.json(); |
| 140 | + const response = await protocol.handleCreate({ |
| 141 | + objectName: params.objectName as string, |
| 142 | + data: body |
| 143 | + }); |
| 144 | + return HttpResponse.json(response, { status: 201 }); |
| 145 | + }), |
| 146 | + |
| 147 | + // Data endpoints - Update |
| 148 | + http.patch(`${baseUrl}/data/:objectName/:id`, async ({ params, request }) => { |
| 149 | + const body = await request.json(); |
| 150 | + const response = await protocol.handleUpdate({ |
| 151 | + objectName: params.objectName as string, |
| 152 | + id: params.id as string, |
| 153 | + data: body |
| 154 | + }); |
| 155 | + return HttpResponse.json(response, { status: 200 }); |
| 156 | + }), |
| 157 | + |
| 158 | + // Data endpoints - Delete |
| 159 | + http.delete(`${baseUrl}/data/:objectName/:id`, async ({ params }) => { |
| 160 | + const response = await protocol.handleDelete({ |
| 161 | + objectName: params.objectName as string, |
| 162 | + id: params.id as string |
| 163 | + }); |
| 164 | + return HttpResponse.json(response, { status: 200 }); |
| 165 | + }), |
| 166 | + ]; |
| 167 | +} |
0 commit comments