|
1 | 1 | import { strict as assert } from 'node:assert' |
2 | | -import { selectValidOutputs, readJsonCapped, MAX_OUTPUTS_PER_VAULT } from './webhook' |
| 2 | +import { mq } from 'lib' |
| 3 | +import { |
| 4 | + MAX_OUTPUT_GROUPS, |
| 5 | + MAX_OUTPUTS_PER_ADDRESS, |
| 6 | + MAX_TOTAL_OUTPUTS, |
| 7 | + readJsonCapped, |
| 8 | + selectValidOutputs, |
| 9 | + WebhookExtractor |
| 10 | +} from './webhook' |
3 | 11 | import type { Data } from './webhook' |
4 | 12 | import type { Output } from 'lib/types' |
5 | 13 | import type { WebhookSubscription } from 'lib/subscriptions' |
6 | 14 |
|
7 | 15 | const VAULT_A = '0x1111111111111111111111111111111111111111' |
8 | | -const VAULT_B = '0x2222222222222222222222222222222222222222' |
9 | 16 | const OUT_OF_SCOPE = '0x3333333333333333333333333333333333333333' |
10 | 17 |
|
11 | 18 | const subscription: WebhookSubscription = { |
@@ -40,35 +47,61 @@ function output(address: string, label = 'apr', chainId = 1): Output { |
40 | 47 | } |
41 | 48 |
|
42 | 49 | describe('selectValidOutputs', () => { |
43 | | - it('keeps outputs for requested vaults with allowed labels', () => { |
44 | | - const valid = selectValidOutputs([output(VAULT_A), output(VAULT_B)], data([VAULT_A, VAULT_B])) |
45 | | - assert.equal(valid.length, 2) |
| 50 | + it('keeps configured-label outputs for an address other than the triggering vault', () => { |
| 51 | + assert.deepEqual(selectValidOutputs([output(OUT_OF_SCOPE)], data([VAULT_A])), [output(OUT_OF_SCOPE)]) |
46 | 52 | }) |
47 | 53 |
|
48 | | - it('drops outputs for vaults that were not requested', () => { |
49 | | - const valid = selectValidOutputs([output(OUT_OF_SCOPE)], data([VAULT_A])) |
50 | | - assert.equal(valid.length, 0) |
| 54 | + it('drops outputs for a different chain than requested', () => { |
| 55 | + assert.deepEqual(selectValidOutputs([output(OUT_OF_SCOPE, 'apr', 10)], data([VAULT_A])), []) |
51 | 56 | }) |
52 | 57 |
|
53 | | - it('drops outputs for a different chain than requested', () => { |
54 | | - const valid = selectValidOutputs([output(VAULT_A, 'apr', 10)], data([VAULT_A], 1)) |
55 | | - assert.equal(valid.length, 0) |
| 58 | + it('drops a group with an unexpected label', () => { |
| 59 | + assert.deepEqual(selectValidOutputs([output(OUT_OF_SCOPE, 'not-allowed')], data([VAULT_A])), []) |
| 60 | + }) |
| 61 | + |
| 62 | + it('drops a group over the per-address cap', () => { |
| 63 | + const many = Array.from({ length: MAX_OUTPUTS_PER_ADDRESS + 1 }, () => output(OUT_OF_SCOPE)) |
| 64 | + assert.deepEqual(selectValidOutputs(many, data([VAULT_A])), []) |
56 | 65 | }) |
57 | 66 |
|
58 | | - it('matches vault addresses case-insensitively', () => { |
59 | | - const valid = selectValidOutputs([output(VAULT_A.toUpperCase().replace('0X', '0x'))], data([VAULT_A.toLowerCase()])) |
60 | | - assert.equal(valid.length, 1) |
| 67 | + it('drops a response over the total output cap', () => { |
| 68 | + const many = Array.from({ length: MAX_TOTAL_OUTPUTS + 1 }, () => output(OUT_OF_SCOPE)) |
| 69 | + assert.deepEqual(selectValidOutputs(many, data([VAULT_A])), []) |
61 | 70 | }) |
62 | 71 |
|
63 | | - it('drops a vault group with an unexpected label', () => { |
64 | | - const valid = selectValidOutputs([output(VAULT_A, 'not-allowed')], data([VAULT_A])) |
65 | | - assert.equal(valid.length, 0) |
| 72 | + it('drops a response over the output group cap', () => { |
| 73 | + const many = Array.from({ length: MAX_OUTPUT_GROUPS + 1 }, (_, i) => output(`0x${i.toString(16).padStart(40, '0')}`)) |
| 74 | + assert.deepEqual(selectValidOutputs(many, data([VAULT_A])), []) |
66 | 75 | }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe('WebhookExtractor', () => { |
| 79 | + it('loads a cross-address output without composition lookup', async () => { |
| 80 | + const originalFetch = globalThis.fetch |
| 81 | + const originalSecret = process.env.WEBHOOK_SECRET_S_TEST |
| 82 | + const responseOutput = { |
| 83 | + ...output(OUT_OF_SCOPE), |
| 84 | + blockNumber: '1', |
| 85 | + blockTime: '1' |
| 86 | + } |
| 87 | + const add = vi.spyOn(mq, 'add').mockResolvedValue({} as never) |
| 88 | + globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify([responseOutput]))) |
| 89 | + process.env.WEBHOOK_SECRET_S_TEST = 'secret' |
| 90 | + |
| 91 | + try { |
| 92 | + await new WebhookExtractor().extract(data([VAULT_A])) |
67 | 93 |
|
68 | | - it('drops a vault group over the per-vault cap', () => { |
69 | | - const many = Array.from({ length: MAX_OUTPUTS_PER_VAULT + 1 }, () => output(VAULT_A)) |
70 | | - const valid = selectValidOutputs(many, data([VAULT_A])) |
71 | | - assert.equal(valid.length, 0) |
| 94 | + assert.equal(add.mock.calls.length, 1) |
| 95 | + assert.deepEqual(add.mock.calls[0], [ |
| 96 | + mq.job.load.output, |
| 97 | + { batch: [output(OUT_OF_SCOPE)] } |
| 98 | + ]) |
| 99 | + } finally { |
| 100 | + add.mockRestore() |
| 101 | + globalThis.fetch = originalFetch |
| 102 | + if (originalSecret === undefined) delete process.env.WEBHOOK_SECRET_S_TEST |
| 103 | + else process.env.WEBHOOK_SECRET_S_TEST = originalSecret |
| 104 | + } |
72 | 105 | }) |
73 | 106 | }) |
74 | 107 |
|
|
0 commit comments