|
| 1 | +import statusCodes from '../fixtures/statusCodes.json' |
| 2 | +import endpoint from '../fixtures/endpoint.json' |
| 3 | +import input from '../fixtures/input.json' |
| 4 | + |
| 5 | +export default class UploadTextOrUrl { |
| 6 | + constructor() { |
| 7 | + this.endpoint = endpoint || {} |
| 8 | + this.input = input || {} |
| 9 | + this.statusCodes = statusCodes || {} |
| 10 | + |
| 11 | + this.expectedProductTerms = { |
| 12 | + cuttingBoard: ['Cutting Board'], |
| 13 | + storageContainer: ['4 Qt Container', 'Food Storage Container'] |
| 14 | + } |
| 15 | + |
| 16 | + this.inputMeta = null |
| 17 | + this.initInputMeta() |
| 18 | + } |
| 19 | + |
| 20 | + initInputMeta() { |
| 21 | + this.inputMeta = { |
| 22 | + input1: { |
| 23 | + apis: [ |
| 24 | + { endpointKey: 'api1', statusKey: 'SUCCESS' }, |
| 25 | + { endpointKey: 'api3', statusKey: 'NOT_FOUND' } |
| 26 | + ], |
| 27 | + expectedProductKey: 'cuttingBoard', |
| 28 | + }, |
| 29 | + input2: { |
| 30 | + apis: [ |
| 31 | + { endpointKey: 'api1', statusKey: 'SUCCESS' }, |
| 32 | + { endpointKey: 'api3', statusKey: 'NOT_FOUND' } |
| 33 | + ], |
| 34 | + expectedProductKey: 'storageContainer', |
| 35 | + }, |
| 36 | + input3: { |
| 37 | + apis: [ |
| 38 | + { endpointKey: 'api2', statusKey: 'SUCCESS' }, |
| 39 | + { endpointKey: 'api3', statusKey: 'NOT_FOUND' } |
| 40 | + ], |
| 41 | + expectedProductKey: 'cuttingBoard', |
| 42 | + }, |
| 43 | + input4: { |
| 44 | + apis: [ |
| 45 | + { endpointKey: 'api2', statusKey: 'SUCCESS' }, |
| 46 | + { endpointKey: 'api3', statusKey: 'NOT_FOUND' } |
| 47 | + ], |
| 48 | + expectedProductKey: 'storageContainer', |
| 49 | + }, |
| 50 | + input5: { |
| 51 | + apis: [ |
| 52 | + { endpointKey: 'api1', statusKey: 'BAD_REQUEST' }, |
| 53 | + { endpointKey: 'api2', statusKey: 'BAD_REQUEST' }, |
| 54 | + { endpointKey: 'api3', statusKey: 'NOT_FOUND' } |
| 55 | + ], |
| 56 | + expectedProductKey: null, |
| 57 | + }, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + upload(textUpload, failOnStatusCode = true, endpointKey) { |
| 62 | + const url = this.endpoint[endpointKey] |
| 63 | + if (!url) throw new Error(`Unknown endpoint key "${endpointKey}"`) |
| 64 | + if (textUpload === undefined || textUpload === null) { |
| 65 | + throw new Error(`Missing required text for endpoint "${endpointKey}"`) |
| 66 | + } |
| 67 | + |
| 68 | + const field = endpointKey === 'api2' ? 'url' : 'text' |
| 69 | + const requestBody = { |
| 70 | + [field]: textUpload, |
| 71 | + topK: 3, |
| 72 | + threshold: 0.8, |
| 73 | + enablePrivateLabelRanking: false, |
| 74 | + enableStockProductRanking: false, |
| 75 | + enableVendorRanking: false |
| 76 | + } |
| 77 | + |
| 78 | + return cy.postApi(url, requestBody, { failOnStatusCode }) |
| 79 | + } |
| 80 | + |
| 81 | + uploadAndValidateStatus(text, statusKey, failOnStatusCode = true, endpointKey) { |
| 82 | + const expected = this.statusCodes[statusKey] |
| 83 | + if (!expected) throw new Error(`Unknown statusKey "${statusKey}" in statusCodes`) |
| 84 | + |
| 85 | + return this.upload(text, failOnStatusCode, endpointKey).then(response => { |
| 86 | + expect(response.status).to.eq(expected.httpStatus) |
| 87 | + |
| 88 | + const body = response.body |
| 89 | + |
| 90 | + if (!body || Object.keys(body).length === 0) { |
| 91 | + cy.log('Response body is empty – skipping body field assertions') |
| 92 | + } else if (body.error_code !== undefined) { |
| 93 | + expect(body.error_code).to.eq(expected.apiStatus) |
| 94 | + expect(body.IsSuccess).to.eq(expected.isSuccess) |
| 95 | + } else if (body.status !== undefined && body.isSuccess !== undefined) { |
| 96 | + expect(body.status).to.eq(expected.apiStatus) |
| 97 | + expect(body.isSuccess).to.eq(expected.isSuccess) |
| 98 | + } else { |
| 99 | + cy.log('Unexpected response body structure:', JSON.stringify(body, null, 2)) |
| 100 | + } |
| 101 | + return cy.wrap(response) |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + uploadAndValidateMatchedProducts(text, expectedProductKey, statusKey, endpointKey) { |
| 106 | + const expectedTerms = this.expectedProductTerms[expectedProductKey] || [] |
| 107 | + |
| 108 | + return this.uploadAndValidateStatus(text, statusKey, true, endpointKey).then(response => { |
| 109 | + const matchedItems = response.body.result?.matchedItems || [] |
| 110 | + expect(matchedItems).to.be.an('array').that.is.not.empty |
| 111 | + |
| 112 | + const foundTerm = matchedItems.some(item => |
| 113 | + expectedTerms.some(term => |
| 114 | + item.productName.toLowerCase().includes(term.toLowerCase()) |
| 115 | + ) |
| 116 | + ) |
| 117 | + expect(foundTerm, 'Expected product terms not found in matched items').to.be.true |
| 118 | + |
| 119 | + return response |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + validateInputKey(inputKey, apiKey) { |
| 124 | + const endpoint = this.endpoint[apiKey] |
| 125 | + if (!endpoint) throw new Error(`Unknown endpoint key "${apiKey}"`) |
| 126 | + |
| 127 | + const textUpload = this.input[inputKey] |
| 128 | + if (textUpload === undefined) throw new Error(`Unknown input key "${inputKey}"`) |
| 129 | + |
| 130 | + const apiMeta = this.inputMeta[inputKey]?.apis.find(api => api.endpointKey === apiKey) |
| 131 | + const expectedStatusCode = apiMeta?.statusKey === 'SUCCESS' ? 200 : 400 |
| 132 | + const failOnStatusCode = expectedStatusCode === 200 |
| 133 | + |
| 134 | + return this.uploadAndValidateStatus(textUpload, apiMeta.statusKey, failOnStatusCode, apiKey) |
| 135 | + } |
| 136 | + |
| 137 | + getResolvedApiAndStatus(inputMeta, apiIndex = 0) { |
| 138 | + const api = inputMeta.apis[apiIndex] |
| 139 | + return { |
| 140 | + endpointKey: api.endpointKey, |
| 141 | + statusKey: api.statusKey, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + getValidInputApiCombinations() { |
| 146 | + if (!this.inputMeta) this.initInputMeta() |
| 147 | + |
| 148 | + const combinations = [] |
| 149 | + |
| 150 | + for (const inputKey in this.inputMeta) { |
| 151 | + const apis = this.inputMeta[inputKey].apis || [] |
| 152 | + apis.forEach(api => { |
| 153 | + if ( |
| 154 | + api.statusKey === 'SUCCESS' || |
| 155 | + api.statusKey === 'NOT_FOUND' || |
| 156 | + api.statusKey === 'BAD_REQUEST' |
| 157 | + ) { |
| 158 | + combinations.push([inputKey, api.endpointKey]) |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | + |
| 163 | + return combinations |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments