|
| 1 | +import { computeQuotesErrors } from "./computeQuotesErrors"; |
| 2 | +import type { RawQuoteError } from "./service/types"; |
| 3 | + |
| 4 | +function makeProviderError(overrides: Partial<RawQuoteError> = {}): RawQuoteError { |
| 5 | + return { |
| 6 | + code: "amount_off_limits", |
| 7 | + type: "float", |
| 8 | + provider: "lifi", |
| 9 | + message: "amount out of range", |
| 10 | + parameter: {}, |
| 11 | + ...overrides, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +describe("computeQuotesErrors", () => { |
| 16 | + it("returns an empty list when at least one successful quote came back, regardless of providerErrors", () => { |
| 17 | + const result = computeQuotesErrors({ |
| 18 | + successfulQuotesCount: 1, |
| 19 | + providerErrors: [ |
| 20 | + makeProviderError({ parameter: { minAmount: "10" } }), |
| 21 | + makeProviderError({ parameter: { maxAmount: "1" } }), |
| 22 | + ], |
| 23 | + amountFrom: "5", |
| 24 | + }); |
| 25 | + |
| 26 | + expect(result).toEqual([]); |
| 27 | + }); |
| 28 | + |
| 29 | + it("emits only `noQuotes` when no successful quotes and no rejection rows", () => { |
| 30 | + const result = computeQuotesErrors({ |
| 31 | + successfulQuotesCount: 0, |
| 32 | + providerErrors: [], |
| 33 | + amountFrom: "5", |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result).toEqual([{ code: "noQuotes" }]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("stacks `amountTooLow` on top of `noQuotes` when a min-bound row brackets amountFrom", () => { |
| 40 | + const result = computeQuotesErrors({ |
| 41 | + successfulQuotesCount: 0, |
| 42 | + providerErrors: [makeProviderError({ parameter: { minAmount: "10" } })], |
| 43 | + amountFrom: "5", |
| 44 | + }); |
| 45 | + |
| 46 | + expect(result).toEqual([ |
| 47 | + { code: "noQuotes" }, |
| 48 | + { code: "amountTooLow", minAmount: "10" }, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("stacks `amountTooHigh` on top of `noQuotes` when a max-bound row brackets amountFrom", () => { |
| 53 | + const result = computeQuotesErrors({ |
| 54 | + successfulQuotesCount: 0, |
| 55 | + providerErrors: [makeProviderError({ parameter: { maxAmount: "100" } })], |
| 56 | + amountFrom: "200", |
| 57 | + }); |
| 58 | + |
| 59 | + expect(result).toEqual([ |
| 60 | + { code: "noQuotes" }, |
| 61 | + { code: "amountTooHigh", maxAmount: "100" }, |
| 62 | + ]); |
| 63 | + }); |
| 64 | + |
| 65 | + it("stacks all three when both bounds match", () => { |
| 66 | + const result = computeQuotesErrors({ |
| 67 | + successfulQuotesCount: 0, |
| 68 | + providerErrors: [ |
| 69 | + makeProviderError({ parameter: { minAmount: "10" } }), |
| 70 | + makeProviderError({ parameter: { maxAmount: "1" }, provider: "okx" }), |
| 71 | + ], |
| 72 | + amountFrom: "5", |
| 73 | + }); |
| 74 | + |
| 75 | + expect(result).toEqual([ |
| 76 | + { code: "noQuotes" }, |
| 77 | + { code: "amountTooLow", minAmount: "10" }, |
| 78 | + { code: "amountTooHigh", maxAmount: "1" }, |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it("picks the lowest reported `minAmount` across providers", () => { |
| 83 | + const result = computeQuotesErrors({ |
| 84 | + successfulQuotesCount: 0, |
| 85 | + providerErrors: [ |
| 86 | + makeProviderError({ provider: "lifi", parameter: { minAmount: "20" } }), |
| 87 | + makeProviderError({ provider: "okx", parameter: { minAmount: "12" } }), |
| 88 | + makeProviderError({ provider: "uniswap", parameter: { minAmount: "30" } }), |
| 89 | + ], |
| 90 | + amountFrom: "5", |
| 91 | + }); |
| 92 | + |
| 93 | + expect(result).toEqual([ |
| 94 | + { code: "noQuotes" }, |
| 95 | + { code: "amountTooLow", minAmount: "12" }, |
| 96 | + ]); |
| 97 | + }); |
| 98 | + |
| 99 | + it("picks the highest reported `maxAmount` across providers", () => { |
| 100 | + const result = computeQuotesErrors({ |
| 101 | + successfulQuotesCount: 0, |
| 102 | + providerErrors: [ |
| 103 | + makeProviderError({ provider: "lifi", parameter: { maxAmount: "100" } }), |
| 104 | + makeProviderError({ provider: "okx", parameter: { maxAmount: "150" } }), |
| 105 | + makeProviderError({ provider: "uniswap", parameter: { maxAmount: "50" } }), |
| 106 | + ], |
| 107 | + amountFrom: "200", |
| 108 | + }); |
| 109 | + |
| 110 | + expect(result).toEqual([ |
| 111 | + { code: "noQuotes" }, |
| 112 | + { code: "amountTooHigh", maxAmount: "150" }, |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("ignores `amount_off_limits` rows whose threshold does not bracket amountFrom (legacy filter)", () => { |
| 117 | + const result = computeQuotesErrors({ |
| 118 | + successfulQuotesCount: 0, |
| 119 | + providerErrors: [ |
| 120 | + // user input 5 is ABOVE this provider's minimum -> not a `tooLow` candidate |
| 121 | + makeProviderError({ parameter: { minAmount: "1" } }), |
| 122 | + // user input 5 is BELOW this provider's maximum -> not a `tooHigh` candidate |
| 123 | + makeProviderError({ parameter: { maxAmount: "10" }, provider: "okx" }), |
| 124 | + ], |
| 125 | + amountFrom: "5", |
| 126 | + }); |
| 127 | + |
| 128 | + expect(result).toEqual([{ code: "noQuotes" }]); |
| 129 | + }); |
| 130 | + |
| 131 | + it("treats edge case `minAmount === amountFrom` as below the limit (legacy `gte`)", () => { |
| 132 | + // Legacy filter: BigNumber(minAmount).gte(amountFrom) — equality counts. |
| 133 | + const result = computeQuotesErrors({ |
| 134 | + successfulQuotesCount: 0, |
| 135 | + providerErrors: [makeProviderError({ parameter: { minAmount: "5" } })], |
| 136 | + amountFrom: "5", |
| 137 | + }); |
| 138 | + |
| 139 | + expect(result).toEqual([ |
| 140 | + { code: "noQuotes" }, |
| 141 | + { code: "amountTooLow", minAmount: "5" }, |
| 142 | + ]); |
| 143 | + }); |
| 144 | + |
| 145 | + it("ignores rejection rows with codes other than `amount_off_limits`", () => { |
| 146 | + const result = computeQuotesErrors({ |
| 147 | + successfulQuotesCount: 0, |
| 148 | + providerErrors: [ |
| 149 | + // legacy never inspects these; they remain in providerErrors for the |
| 150 | + // consumer to surface directly if it wants to. |
| 151 | + makeProviderError({ code: "unknown_error", parameter: { minAmount: "10" } }), |
| 152 | + ], |
| 153 | + amountFrom: "5", |
| 154 | + }); |
| 155 | + |
| 156 | + expect(result).toEqual([{ code: "noQuotes" }]); |
| 157 | + }); |
| 158 | + |
| 159 | + it("ignores `amount_off_limits` rows missing both bound parameters", () => { |
| 160 | + const result = computeQuotesErrors({ |
| 161 | + successfulQuotesCount: 0, |
| 162 | + providerErrors: [makeProviderError({ parameter: {} })], |
| 163 | + amountFrom: "5", |
| 164 | + }); |
| 165 | + |
| 166 | + expect(result).toEqual([{ code: "noQuotes" }]); |
| 167 | + }); |
| 168 | +}); |
0 commit comments