|  | 
|  | 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 | +          requests: [ | 
|  | 51 | +            { action: 'addObject', body: { objectID: '1', name: 'Adam' } }, | 
|  | 52 | +            { action: 'addObject', body: { objectID: '2', name: 'Benoit' } }, | 
|  | 53 | +          ], | 
|  | 54 | +        }); | 
|  | 55 | + | 
|  | 56 | +        res.json({ | 
|  | 57 | +          runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda', | 
|  | 58 | +          eventID: '113b2068-6337-4c85-b5c2-e7b213d82925', | 
|  | 59 | +          message: 'OK', | 
|  | 60 | +          createdAt: '2022-05-12T06:24:30.049Z', | 
|  | 61 | +        }); | 
|  | 62 | + | 
|  | 63 | +        break; | 
|  | 64 | +      case 'partialUpdateObjectsWithTransformation': | 
|  | 65 | +        expect(req.body).to.deep.equal({ | 
|  | 66 | +          requests: [ | 
|  | 67 | +            { action: 'partialUpdateObject', body: { objectID: '1', name: 'Adam' } }, | 
|  | 68 | +            { action: 'partialUpdateObject', body: { objectID: '2', name: 'Benoit' } }, | 
|  | 69 | +          ], | 
|  | 70 | +        }); | 
|  | 71 | + | 
|  | 72 | +        res.json({ | 
|  | 73 | +          runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda', | 
|  | 74 | +          eventID: '113b2068-6337-4c85-b5c2-e7b213d82925', | 
|  | 75 | +          message: 'OK', | 
|  | 76 | +          createdAt: '2022-05-12T06:24:30.049Z', | 
|  | 77 | +        }); | 
|  | 78 | +        break; | 
|  | 79 | +      default: | 
|  | 80 | +        throw new Error('unknown helper'); | 
|  | 81 | +    } | 
|  | 82 | +  }); | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | +export function pushMockServer(): Promise<Server> { | 
|  | 86 | +  return setupServer('pushMock', 6689, addRoutes); | 
|  | 87 | +} | 
0 commit comments