|
| 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 aciState: Record< |
| 10 | + string, |
| 11 | + { |
| 12 | + setSettingsCount: number; |
| 13 | + getSettingsCount: number; |
| 14 | + saveRulesCount: number; |
| 15 | + browseRulesCount: number; |
| 16 | + saveSynonymsCount: number; |
| 17 | + browseSynonymsCount: number; |
| 18 | + saveObjectsCount: number; |
| 19 | + browseObjectsCount: number; |
| 20 | + waitTaskCount: number; |
| 21 | + successful: boolean; |
| 22 | + } |
| 23 | +> = {}; |
| 24 | + |
| 25 | +export function assertValidAccountCopyIndex(expectedCount: number): void { |
| 26 | + expect(Object.keys(aciState)).to.have.length(expectedCount); |
| 27 | + for (const lang in aciState) { |
| 28 | + expect(aciState[lang].waitTaskCount).to.equal(4); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function addRoutes(app: Express): void { |
| 33 | + app.use(express.urlencoded({ extended: true })); |
| 34 | + app.use( |
| 35 | + express.json({ |
| 36 | + type: ['application/json', 'text/plain'], // the js client sends the body as text/plain |
| 37 | + }), |
| 38 | + ); |
| 39 | + |
| 40 | + app.get('/1/indexes/:indexName/settings', (req, res) => { |
| 41 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_(source|destination)_(.*)$/)?.[2] as string; |
| 42 | + |
| 43 | + if (!aciState[lang] || aciState[lang].successful) { |
| 44 | + aciState[lang] = { |
| 45 | + setSettingsCount: 0, |
| 46 | + getSettingsCount: 0, |
| 47 | + saveRulesCount: 0, |
| 48 | + browseRulesCount: 0, |
| 49 | + saveSynonymsCount: 0, |
| 50 | + browseSynonymsCount: 0, |
| 51 | + saveObjectsCount: 0, |
| 52 | + browseObjectsCount: 0, |
| 53 | + waitTaskCount: 0, |
| 54 | + successful: false, |
| 55 | + }; |
| 56 | + } else { |
| 57 | + expect(aciState).to.include.keys(lang); |
| 58 | + } |
| 59 | + |
| 60 | + aciState[lang].getSettingsCount++; |
| 61 | + |
| 62 | + if (req.params.indexName.includes('destination')) { |
| 63 | + res.status(404).json({ message: 'Index not found' }); |
| 64 | + } else { |
| 65 | + res.status(200).json({ |
| 66 | + minWordSizefor1Typo: 4, |
| 67 | + minWordSizefor2Typos: 8, |
| 68 | + hitsPerPage: 100, |
| 69 | + maxValuesPerFacet: 100, |
| 70 | + paginationLimitedTo: 10, |
| 71 | + exactOnSingleWordQuery: 'attribute', |
| 72 | + ranking: ['typo', 'geo', 'words', 'filters', 'proximity', 'attribute', 'exact', 'custom'], |
| 73 | + separatorsToIndex: '', |
| 74 | + removeWordsIfNoResults: 'none', |
| 75 | + queryType: 'prefixLast', |
| 76 | + highlightPreTag: '<em>', |
| 77 | + highlightPostTag: '</em>', |
| 78 | + alternativesAsExact: ['ignorePlurals', 'singleWordSynonym'], |
| 79 | + }); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + app.put('/1/indexes/:indexName/settings', (req, res) => { |
| 84 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_destination_(.*)$/)?.[1] as string; |
| 85 | + expect(aciState).to.include.keys(lang); |
| 86 | + |
| 87 | + aciState[lang].setSettingsCount++; |
| 88 | + |
| 89 | + res.json({ taskID: 123 + aciState[lang].setSettingsCount, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 90 | + }); |
| 91 | + |
| 92 | + app.post('/1/indexes/:indexName/rules/search', (req, res) => { |
| 93 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_source_(.*)$/)?.[1] as string; |
| 94 | + expect(aciState).to.include.keys(lang); |
| 95 | + |
| 96 | + aciState[lang].browseRulesCount++; |
| 97 | + |
| 98 | + res.json({ |
| 99 | + hits: [ |
| 100 | + { |
| 101 | + conditions: [ |
| 102 | + { |
| 103 | + alternatives: true, |
| 104 | + anchoring: 'contains', |
| 105 | + pattern: 'zorro', |
| 106 | + }, |
| 107 | + ], |
| 108 | + consequence: { |
| 109 | + params: { |
| 110 | + ignorePlurals: 'true', |
| 111 | + }, |
| 112 | + filterPromotes: true, |
| 113 | + promote: [ |
| 114 | + { |
| 115 | + objectIDs: ['\u00C6on Flux'], |
| 116 | + position: 0, |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | + description: 'test_rule', |
| 121 | + enabled: true, |
| 122 | + objectID: 'qr-1725004648916', |
| 123 | + }, |
| 124 | + ], |
| 125 | + nbHits: 1, |
| 126 | + nbPages: 1, |
| 127 | + page: 0, |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + app.post('/1/indexes/:indexName/rules/batch', (req, res) => { |
| 132 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_destination_(.*)$/)?.[1] as string; |
| 133 | + expect(aciState).to.include.keys(lang); |
| 134 | + |
| 135 | + aciState[lang].saveRulesCount++; |
| 136 | + |
| 137 | + res.json({ taskID: 456 + aciState[lang].saveRulesCount, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 138 | + }); |
| 139 | + |
| 140 | + app.post('/1/indexes/:indexName/synonyms/search', (req, res) => { |
| 141 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_source_(.*)$/)?.[1] as string; |
| 142 | + expect(aciState).to.include.keys(lang); |
| 143 | + |
| 144 | + aciState[lang].browseSynonymsCount++; |
| 145 | + |
| 146 | + res.json({ |
| 147 | + hits: [ |
| 148 | + { |
| 149 | + objectID: 'foo', |
| 150 | + }, |
| 151 | + ], |
| 152 | + nbHits: 1, |
| 153 | + nbPages: 1, |
| 154 | + page: 0, |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + app.post('/1/indexes/:indexName/synonyms/batch', (req, res) => { |
| 159 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_destination_(.*)$/)?.[1] as string; |
| 160 | + expect(aciState).to.include.keys(lang); |
| 161 | + |
| 162 | + aciState[lang].saveSynonymsCount++; |
| 163 | + |
| 164 | + res.json({ taskID: 789 + aciState[lang].saveSynonymsCount, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 165 | + }); |
| 166 | + |
| 167 | + app.post('/1/indexes/:indexName/browse', (req, res) => { |
| 168 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_source_(.*)$/)?.[1] as string; |
| 169 | + expect(aciState).to.include.keys(lang); |
| 170 | + |
| 171 | + aciState[lang].browseObjectsCount++; |
| 172 | + |
| 173 | + res.json({ |
| 174 | + page: 0, |
| 175 | + nbHits: 1, |
| 176 | + nbPages: 1, |
| 177 | + hitsPerPage: 1000, |
| 178 | + query: '', |
| 179 | + params: '', |
| 180 | + hits: [{ objectID: 'bar' }], |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + app.post('/1/indexes/:indexName/batch', (req, res) => { |
| 185 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_destination_(.*)$/)?.[1] as string; |
| 186 | + expect(aciState).to.include.keys(lang); |
| 187 | + |
| 188 | + aciState[lang].saveObjectsCount++; |
| 189 | + |
| 190 | + res.json({ taskID: 10 + aciState[lang].saveObjectsCount, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 191 | + }); |
| 192 | + |
| 193 | + app.get('/1/indexes/:indexName/task/:taskID', (req, res) => { |
| 194 | + const lang = req.params.indexName.match(/^cts_e2e_account_copy_index_destination_(.*)$/)?.[1] as string; |
| 195 | + expect(aciState).to.include.keys(lang); |
| 196 | + |
| 197 | + aciState[lang].waitTaskCount++; |
| 198 | + |
| 199 | + res.json({ status: 'published', updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 200 | + }); |
| 201 | +} |
| 202 | + |
| 203 | +export function accountCopyIndexServer(): Promise<Server> { |
| 204 | + // this server is used to simulate the responses for the accountCopyIndex method, |
| 205 | + // and uses a state machine to determine if the logic is correct. |
| 206 | + return setupServer('accountCopyIndex', 6687, addRoutes); |
| 207 | +} |
0 commit comments