|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { getInitialPageWebhooks, getWebhook } from '../index' |
| 4 | + |
| 5 | +// Use a version that's guaranteed to exist in the data directory. |
| 6 | +const VERSION = 'free-pro-team@latest' |
| 7 | + |
| 8 | +// Pick a webhook category whose data file has body parameters with |
| 9 | +// non-empty childParamsGroups so we can verify they survive the |
| 10 | +// initial-page stripping. |
| 11 | +const CATEGORY = 'projects_v2_item' |
| 12 | + |
| 13 | +describe('getInitialPageWebhooks does not corrupt the getWebhook cache', () => { |
| 14 | + it('strips childParamsGroups from the initial-page data', async () => { |
| 15 | + const initial = await getInitialPageWebhooks(VERSION) |
| 16 | + const initialWebhook = initial.find((w) => w.name === CATEGORY) |
| 17 | + expect(initialWebhook).toBeDefined() |
| 18 | + |
| 19 | + for (const bp of initialWebhook!.data.bodyParameters ?? []) { |
| 20 | + if (bp.childParamsGroups) { |
| 21 | + expect(bp.childParamsGroups, `initial ${bp.name} should be stripped`).toHaveLength(0) |
| 22 | + } |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + it('preserves childParamsGroups in the getWebhook cache after getInitialPageWebhooks runs', async () => { |
| 27 | + // Seed the cache and record original childParamsGroups lengths. |
| 28 | + const before = await getWebhook(VERSION, CATEGORY) |
| 29 | + expect(before).toBeDefined() |
| 30 | + |
| 31 | + const originalLengths: Record<string, number> = {} |
| 32 | + for (const [action, actionData] of Object.entries(before!)) { |
| 33 | + for (const bp of actionData.bodyParameters ?? []) { |
| 34 | + if (bp.childParamsGroups && bp.childParamsGroups.length > 0) { |
| 35 | + originalLengths[`${action}.${bp.name}`] = bp.childParamsGroups.length |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + expect(Object.keys(originalLengths).length).toBeGreaterThan(0) |
| 40 | + |
| 41 | + // This intentionally empties childParamsGroups for the initial page render. |
| 42 | + await getInitialPageWebhooks(VERSION) |
| 43 | + |
| 44 | + // getWebhook returns cached data — it must NOT have been mutated. |
| 45 | + const after = await getWebhook(VERSION, CATEGORY) |
| 46 | + expect(after).toBeDefined() |
| 47 | + |
| 48 | + for (const [key, expectedLen] of Object.entries(originalLengths)) { |
| 49 | + const [action, paramName] = key.split('.') |
| 50 | + const bp = after![action]?.bodyParameters?.find( |
| 51 | + (p: { name?: string }) => p.name === paramName, |
| 52 | + ) |
| 53 | + expect( |
| 54 | + bp?.childParamsGroups?.length, |
| 55 | + `${key}.childParamsGroups should still have ${expectedLen} entries`, |
| 56 | + ).toBe(expectedLen) |
| 57 | + } |
| 58 | + }) |
| 59 | +}) |
0 commit comments