|
| 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 raowtState: Record< |
| 10 | + string, |
| 11 | + { |
| 12 | + copyCount: number; |
| 13 | + pushCount: number; |
| 14 | + tmpIndexName: string; |
| 15 | + waitTaskCount: number; |
| 16 | + waitingForFinalWaitTask: boolean; |
| 17 | + successful: boolean; |
| 18 | + } |
| 19 | +> = {}; |
| 20 | + |
| 21 | +export function assertValidReplaceAllObjectsWithTransformation(expectedCount: number): void { |
| 22 | + expect(Object.keys(raowtState)).to.have.length(expectedCount); |
| 23 | + for (const lang in raowtState) { |
| 24 | + expect(raowtState[lang].successful).to.equal(true); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function addRoutes(app: Express): void { |
| 29 | + app.use(express.urlencoded({ extended: true })); |
| 30 | + app.use( |
| 31 | + express.json({ |
| 32 | + type: ['application/json', 'text/plain'], // the js client sends the body as text/plain |
| 33 | + }), |
| 34 | + ); |
| 35 | + |
| 36 | + app.post('/1/indexes/:indexName/operation', (req, res) => { |
| 37 | + expect(req.params.indexName).to.match(/^cts_e2e_replace_all_objects_with_transformation_(.*)$/); |
| 38 | + |
| 39 | + switch (req.body.operation) { |
| 40 | + case 'copy': { |
| 41 | + expect(req.params.indexName).to.not.include('tmp'); |
| 42 | + expect(req.body.destination).to.include('tmp'); |
| 43 | + expect(req.body.scope).to.deep.equal(['settings', 'rules', 'synonyms']); |
| 44 | + |
| 45 | + const lang = req.params.indexName.replace('cts_e2e_replace_all_objects_with_transformation_', ''); |
| 46 | + if (!raowtState[lang] || raowtState[lang].successful) { |
| 47 | + raowtState[lang] = { |
| 48 | + copyCount: 1, |
| 49 | + pushCount: 0, |
| 50 | + waitTaskCount: 0, |
| 51 | + tmpIndexName: req.body.destination, |
| 52 | + waitingForFinalWaitTask: false, |
| 53 | + successful: false, |
| 54 | + }; |
| 55 | + } else { |
| 56 | + raowtState[lang].copyCount++; |
| 57 | + } |
| 58 | + |
| 59 | + res.json({ taskID: 123 + raowtState[lang].copyCount, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 60 | + break; |
| 61 | + } |
| 62 | + case 'move': { |
| 63 | + const lang = req.body.destination.replace('cts_e2e_replace_all_objects_with_transformation_', ''); |
| 64 | + expect(raowtState).to.include.keys(lang); |
| 65 | + expect(raowtState[lang]).to.deep.equal({ |
| 66 | + copyCount: 2, |
| 67 | + pushCount: 10, |
| 68 | + waitTaskCount: 2, |
| 69 | + tmpIndexName: req.params.indexName, |
| 70 | + waitingForFinalWaitTask: false, |
| 71 | + successful: false, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(req.body.scope).to.equal(undefined); |
| 75 | + |
| 76 | + raowtState[lang].waitingForFinalWaitTask = true; |
| 77 | + |
| 78 | + res.json({ taskID: 777, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 79 | + |
| 80 | + break; |
| 81 | + } |
| 82 | + default: |
| 83 | + res.status(400).json({ |
| 84 | + message: `invalid operation: ${req.body.operation}, body: ${JSON.stringify(req.body)}`, |
| 85 | + }); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + app.post('/1/push/:indexName', (req, res) => { |
| 90 | + const lang = req.params.indexName.match( |
| 91 | + /^cts_e2e_replace_all_objects_with_transformation_(.*)_tmp_\d+$/, |
| 92 | + )?.[1] as string; |
| 93 | + expect(raowtState).to.include.keys(lang); |
| 94 | + expect(req.body.action === 'addObject').to.equal(true); |
| 95 | + |
| 96 | + raowtState[lang].pushCount += req.body.records.length; |
| 97 | + |
| 98 | + res.json({ |
| 99 | + runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda', |
| 100 | + eventID: `113b2068-6337-4c85-b5c2-e7b213d8292${raowtState[lang].pushCount}`, |
| 101 | + message: 'OK', |
| 102 | + createdAt: '2022-05-12T06:24:30.049Z', |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + app.get('/1/runs/:runID/events/:eventID', (req, res) => { |
| 107 | + res.json({ status: 'finished' }); |
| 108 | + }); |
| 109 | + |
| 110 | + app.get('/1/indexes/:indexName/task/:taskID', (req, res) => { |
| 111 | + const lang = req.params.indexName.match( |
| 112 | + /^cts_e2e_replace_all_objects_with_transformation_(.*)_tmp_\d+$/, |
| 113 | + )?.[1] as string; |
| 114 | + expect(raowtState).to.include.keys(lang); |
| 115 | + |
| 116 | + raowtState[lang].waitTaskCount++; |
| 117 | + if (raowtState[lang].waitingForFinalWaitTask) { |
| 118 | + expect(req.params.taskID).to.equal('777'); |
| 119 | + expect(raowtState[lang].waitTaskCount).to.equal(3); |
| 120 | + |
| 121 | + raowtState[lang].successful = true; |
| 122 | + } |
| 123 | + |
| 124 | + res.json({ status: 'published', updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +export function replaceAllObjectsWithTransformationServer(): Promise<Server> { |
| 129 | + // this server is used to simulate the responses for the replaceAllObjectsWithTransformationServer method, |
| 130 | + // and uses a state machine to determine if the logic is correct. |
| 131 | + return setupServer('replaceAllObjectsWithTransformationServer', 6690, addRoutes); |
| 132 | +} |
0 commit comments