|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import { getBlockSchema } from '@/executor/utils/block-data' |
| 6 | +import { resolveBlockReference } from '@/executor/utils/block-reference' |
| 7 | +import type { SerializedBlock } from '@/serializer/types' |
| 8 | + |
| 9 | +/** |
| 10 | + * These assertions are about what the real block registry publishes, so the global stub — which |
| 11 | + * returns one mock block with no outputs — would make every case here pass vacuously. |
| 12 | + */ |
| 13 | +vi.unmock('@/blocks/registry') |
| 14 | + |
| 15 | +function triggerBlock(type: string, params: Record<string, unknown> = {}): SerializedBlock { |
| 16 | + return { |
| 17 | + id: 'trigger-1', |
| 18 | + metadata: { id: type, name: 'webhook1', category: 'triggers' }, |
| 19 | + position: { x: 0, y: 0 }, |
| 20 | + config: { tool: '', params }, |
| 21 | + inputs: {}, |
| 22 | + outputs: {}, |
| 23 | + enabled: true, |
| 24 | + } as unknown as SerializedBlock |
| 25 | +} |
| 26 | + |
| 27 | +function resolve( |
| 28 | + pathParts: string[], |
| 29 | + schema: ReturnType<typeof getBlockSchema> |
| 30 | +): ReturnType<typeof resolveBlockReference> { |
| 31 | + return resolveBlockReference( |
| 32 | + 'webhook1', |
| 33 | + pathParts, |
| 34 | + { |
| 35 | + blockNameMapping: { webhook1: 'trigger-1' }, |
| 36 | + blockData: { 'trigger-1': { query: { env: 'prod' } } }, |
| 37 | + blockOutputSchemas: schema ? { 'trigger-1': schema } : {}, |
| 38 | + } as never, |
| 39 | + {} as never |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +describe('generic webhook output schema', () => { |
| 44 | + /** |
| 45 | + * A generic webhook receives whatever the caller sends, so it must publish no schema at all. |
| 46 | + * `collectBlockData` registers any non-empty output declaration as exhaustive, which turns |
| 47 | + * every unlisted field into a hard `InvalidFieldError` rather than an absent value. |
| 48 | + */ |
| 49 | + it('publishes no output schema, leaving the block shape open', () => { |
| 50 | + expect(getBlockSchema(triggerBlock('generic_webhook'))).toBeUndefined() |
| 51 | + }) |
| 52 | + |
| 53 | + it.each([ |
| 54 | + [{}, 'no flags set'], |
| 55 | + [{ acceptOtherMethods: true, exposeRequestHeaders: true }, 'both request-metadata flags on'], |
| 56 | + ])('stays open with %o (%s)', (params) => { |
| 57 | + expect(getBlockSchema(triggerBlock('generic_webhook', params))).toBeUndefined() |
| 58 | + }) |
| 59 | + |
| 60 | + /** |
| 61 | + * The production regression this pins: a Slack interactive payload reaching a workflow that |
| 62 | + * reads `actions.0.selected_option.value`. When a delivery omits the field the reference must |
| 63 | + * resolve to `undefined` so the condition simply evaluates falsy — not abort the run. |
| 64 | + */ |
| 65 | + it('resolves an absent body field to undefined instead of throwing', () => { |
| 66 | + const schema = getBlockSchema(triggerBlock('generic_webhook')) |
| 67 | + |
| 68 | + expect(() => resolve(['actions', '0', 'selected_option', 'value'], schema)).not.toThrow() |
| 69 | + expect(resolve(['actions', '0', 'selected_option', 'value'], schema)?.value).toBeUndefined() |
| 70 | + }) |
| 71 | + |
| 72 | + it('still resolves request metadata the provider merges into the input', () => { |
| 73 | + const schema = getBlockSchema(triggerBlock('generic_webhook')) |
| 74 | + |
| 75 | + expect(resolve(['query', 'env'], schema)?.value).toBe('prod') |
| 76 | + }) |
| 77 | +}) |
0 commit comments