|
| 1 | +import type { Server } from 'http'; |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import type { Express } from 'express'; |
| 5 | +import express from 'express'; |
| 6 | + |
| 7 | +import { setupServer } from './index.ts'; |
| 8 | + |
| 9 | +const pushMockState: Record<string, any> = {}; |
| 10 | + |
| 11 | +export function assertPushMockValid(expectedCount: number): void { |
| 12 | + if (Object.values(pushMockState).length !== expectedCount) { |
| 13 | + throw new Error('unexpected number of call to pushMock'); |
| 14 | + } |
| 15 | + for (const [lang, state] of Object.entries(pushMockState)) { |
| 16 | + let numberOfTestSuites = 1; |
| 17 | + |
| 18 | + if (lang === 'python') { |
| 19 | + numberOfTestSuites = 2; |
| 20 | + } |
| 21 | + |
| 22 | + expect(state).to.deep.equal({ |
| 23 | + saveObjectsWithTransformation: Number(numberOfTestSuites), |
| 24 | + partialUpdateObjectsWithTransformation: Number(numberOfTestSuites), |
| 25 | + }); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function addRoutes(app: Express): void { |
| 30 | + app.use(express.urlencoded({ extended: true })); |
| 31 | + app.use( |
| 32 | + express.json({ |
| 33 | + type: ['application/json', 'text/plain'], // the js client sends the body as text/plain |
| 34 | + }), |
| 35 | + ); |
| 36 | + |
| 37 | + app.post('/1/push/:indexName', (req, res) => { |
| 38 | + const match = req.params.indexName.match(/^cts_e2e_(\w+)_(.*)$/); |
| 39 | + const helper = match?.[1] as string; |
| 40 | + const lang = match?.[2] as string; |
| 41 | + |
| 42 | + if (!pushMockState[lang]) { |
| 43 | + pushMockState[lang] = {}; |
| 44 | + } |
| 45 | + |
| 46 | + pushMockState[lang][helper] = (pushMockState[lang][helper] ?? 0) + 1; |
| 47 | + switch (helper) { |
| 48 | + case 'saveObjectsWithTransformation': |
| 49 | + expect(req.body).to.deep.equal({ |
| 50 | + action: 'addObject', |
| 51 | + records: [ |
| 52 | + { objectID: '1', name: 'Adam' }, |
| 53 | + { objectID: '2', name: 'Benoit' }, |
| 54 | + ], |
| 55 | + }); |
| 56 | + |
| 57 | + res.json({ |
| 58 | + runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda', |
| 59 | + eventID: '113b2068-6337-4c85-b5c2-e7b213d82925', |
| 60 | + message: 'OK', |
| 61 | + createdAt: '2022-05-12T06:24:30.049Z', |
| 62 | + }); |
| 63 | + |
| 64 | + break; |
| 65 | + case 'partialUpdateObjectsWithTransformation': |
| 66 | + expect(req.body).to.deep.equal({ |
| 67 | + action: 'partialUpdateObject', |
| 68 | + records: [ |
| 69 | + { objectID: '1', name: 'Adam' }, |
| 70 | + { objectID: '2', name: 'Benoit' }, |
| 71 | + ], |
| 72 | + }); |
| 73 | + |
| 74 | + res.json({ |
| 75 | + runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda', |
| 76 | + eventID: '113b2068-6337-4c85-b5c2-e7b213d82925', |
| 77 | + message: 'OK', |
| 78 | + createdAt: '2022-05-12T06:24:30.049Z', |
| 79 | + }); |
| 80 | + break; |
| 81 | + default: |
| 82 | + throw new Error('unknown helper'); |
| 83 | + } |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +export function pushMockServer(): Promise<Server> { |
| 88 | + return setupServer('pushMock', 6689, addRoutes); |
| 89 | +} |
0 commit comments