|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#33894] The connector pull door refuses an input key the read action does |
| 5 | + * not declare, as the `connector_action` node does: every door that |
| 6 | + * dispatches a connector action judges its input alike. The undeclared key |
| 7 | + * the author wrote is pinned beside the door's other input refusals in |
| 8 | + * `connector-pull.test.ts`; this file pins the key the door ADDS itself — |
| 9 | + * the watermark parameter, sent as `query[watermark.param]` once the target |
| 10 | + * holds a value — which only the action can declare (for an `openapi` |
| 11 | + * connector, its OpenAPI document). |
| 12 | + * |
| 13 | + * The schemas below are the shapes the shipped producers emit: the `openapi` |
| 14 | + * generator's `{ path, query, header, body }` sections, where a location the |
| 15 | + * operation declares no parameter in is left out, and the `rest` connector's |
| 16 | + * `request`, whose `query` lists no `properties` and so declares open input. |
| 17 | + * |
| 18 | + * Every refusal asserts the ADR-0112 pair (`code` + `status`), the executor's |
| 19 | + * `reason`, the named key, and that neither the action nor a write ran. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, vi } from 'vitest'; |
| 23 | +import { |
| 24 | + pullConnectorSource, |
| 25 | + ConnectorPullError, |
| 26 | + type ConnectorPullDeps, |
| 27 | +} from './connector-pull.js'; |
| 28 | + |
| 29 | +const silent = { debug() {}, info() {}, warn() {}, error() {} } as unknown as ConnectorPullDeps['logger']; |
| 30 | + |
| 31 | +const contactObject = { |
| 32 | + name: 'contact', |
| 33 | + fields: { |
| 34 | + external_id: { name: 'external_id', type: 'text' }, |
| 35 | + name: { name: 'name', type: 'text' }, |
| 36 | + synced_at: { name: 'synced_at', type: 'text' }, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +/** An incremental mapping: `updated_at` is copied onto `synced_at`, and sent back as `query.since`. */ |
| 41 | +function incrementalMapping(input: Record<string, unknown>) { |
| 42 | + return { |
| 43 | + name: 'crm_contacts', |
| 44 | + targetObject: 'contact', |
| 45 | + fieldMapping: [ |
| 46 | + { source: 'id', target: 'external_id' }, |
| 47 | + { source: 'name', target: 'name' }, |
| 48 | + { source: 'updated_at', target: 'synced_at' }, |
| 49 | + ], |
| 50 | + mode: 'upsert', |
| 51 | + upsertKey: ['external_id'], |
| 52 | + connectorSource: { |
| 53 | + connector: 'crm_api', action: 'list_contacts', input, |
| 54 | + watermark: { field: 'updated_at', param: 'since' }, |
| 55 | + }, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +/** An `openapi` operation declaring `query.limit` only — the document never names `since`. */ |
| 60 | +const OPENAPI_LIMIT_ONLY = { |
| 61 | + type: 'object', |
| 62 | + properties: { query: { type: 'object', properties: { limit: { type: 'integer' } } } }, |
| 63 | +}; |
| 64 | + |
| 65 | +/** An `openapi` operation declaring only a header parameter: the generator emits no `query` section at all. */ |
| 66 | +const OPENAPI_NO_QUERY = { |
| 67 | + type: 'object', |
| 68 | + properties: { header: { type: 'object', properties: { 'X-Tenant': { type: 'string' } } } }, |
| 69 | +}; |
| 70 | + |
| 71 | +/** The same operation once the document declares `since` as an `in: query` parameter. */ |
| 72 | +const OPENAPI_SINCE_DECLARED = { |
| 73 | + type: 'object', |
| 74 | + properties: { |
| 75 | + query: { type: 'object', properties: { limit: { type: 'integer' }, since: { type: 'string' } } }, |
| 76 | + }, |
| 77 | +}; |
| 78 | + |
| 79 | +/** The `rest` connector's `request` action: `query` lists no `properties`, so every query key is open. */ |
| 80 | +const REST_REQUEST = { |
| 81 | + type: 'object', |
| 82 | + properties: { |
| 83 | + method: { type: 'string' }, |
| 84 | + path: { type: 'string' }, |
| 85 | + headers: { type: 'object' }, |
| 86 | + query: { type: 'object' }, |
| 87 | + body: {}, |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +const STORED = [{ synced_at: '2026-01-02T00:00:00.000Z' }]; |
| 92 | + |
| 93 | +function harness(opts: { |
| 94 | + /** Absent ⇒ the action declares no `inputSchema`. */ |
| 95 | + inputSchema: Record<string, unknown> | undefined; |
| 96 | + input: Record<string, unknown>; |
| 97 | + stored?: Array<Record<string, unknown>>; |
| 98 | +}) { |
| 99 | + const writes: unknown[] = []; |
| 100 | + const handler = vi.fn(async (_input: Record<string, unknown>) => ({ status: 200, ok: true, body: [] }) as Record<string, unknown>); |
| 101 | + const mapping = incrementalMapping(opts.input); |
| 102 | + const deps: ConnectorPullDeps = { |
| 103 | + protocol: { |
| 104 | + async getMetaItem({ type, name }) { |
| 105 | + if (type === 'mapping' && name === mapping.name) return { type, name, item: mapping }; |
| 106 | + if (type === 'object' && name === 'contact') return { type, name, item: contactObject }; |
| 107 | + return undefined; |
| 108 | + }, |
| 109 | + async findData() { return { records: opts.stored ?? [] }; }, |
| 110 | + async createData(args) { writes.push(args); return { id: 'new' }; }, |
| 111 | + async updateData(args) { writes.push(args); return { id: 'upd' }; }, |
| 112 | + }, |
| 113 | + registry: { |
| 114 | + resolveConnectorAction: (connector, action) => |
| 115 | + connector === 'crm_api' && action === 'list_contacts' ? handler : undefined, |
| 116 | + getConnectorOrigin: (name) => (name === 'crm_api' ? 'declarative' : undefined), |
| 117 | + getConnectorDegradedReason: () => undefined, |
| 118 | + getConnectorDescriptors: () => [{ |
| 119 | + name: 'crm_api', label: 'CRM', type: 'api', origin: 'declarative', state: 'ready', |
| 120 | + actions: [{ key: 'list_contacts', label: 'List contacts', inputSchema: opts.inputSchema }], |
| 121 | + }], |
| 122 | + }, |
| 123 | + providerOf: (name) => (name === 'crm_api' ? 'openapi' : undefined), |
| 124 | + logger: silent, |
| 125 | + }; |
| 126 | + return { deps, handler, writes }; |
| 127 | +} |
| 128 | + |
| 129 | +async function refusal(h: ReturnType<typeof harness>): Promise<ConnectorPullError> { |
| 130 | + const err = await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }).then( |
| 131 | + () => undefined, |
| 132 | + (e: unknown) => e, |
| 133 | + ); |
| 134 | + expect(err, 'the pull must refuse, not resolve').toBeInstanceOf(ConnectorPullError); |
| 135 | + const e = err as ConnectorPullError; |
| 136 | + expect({ code: e.code, status: e.status, reason: e.reason, mapping: e.mapping }).toEqual({ |
| 137 | + code: 'VALIDATION_ERROR', status: 400, reason: 'input_schema_mismatch', mapping: 'crm_contacts', |
| 138 | + }); |
| 139 | + expect(h.handler).toHaveBeenCalledTimes(0); |
| 140 | + expect(h.writes).toEqual([]); |
| 141 | + return e; |
| 142 | +} |
| 143 | + |
| 144 | +describe('[#33894] connector pull — the watermark parameter the door adds is judged like any other input key', () => { |
| 145 | + it('a watermark parameter the operation does not declare is refused, naming it and the OpenAPI fix', async () => { |
| 146 | + const h = harness({ inputSchema: OPENAPI_LIMIT_ONLY, input: { query: { limit: 50 } }, stored: STORED }); |
| 147 | + const err = await refusal(h); |
| 148 | + expect(err.message).toContain("'query.since' is not an input the action declares"); |
| 149 | + expect(err.message).toContain("watermark parameter 'since'"); |
| 150 | + expect(err.message).toContain('OpenAPI document'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('an operation that declares no query parameter at all refuses the watermark the same way', async () => { |
| 154 | + const h = harness({ inputSchema: OPENAPI_NO_QUERY, input: {}, stored: STORED }); |
| 155 | + const err = await refusal(h); |
| 156 | + expect(err.message).toContain("'query' is not an input the action declares"); |
| 157 | + expect(err.message).toContain("watermark parameter 'since'"); |
| 158 | + }); |
| 159 | + |
| 160 | + it('an undeclared key the author wrote does not blame the watermark when the watermark is declared', async () => { |
| 161 | + const h = harness({ |
| 162 | + inputSchema: OPENAPI_SINCE_DECLARED, |
| 163 | + input: { query: { limit: 50, stale: 'x' } }, |
| 164 | + stored: STORED, |
| 165 | + }); |
| 166 | + const err = await refusal(h); |
| 167 | + expect(err.message).toContain("'query.stale' is not an input the action declares"); |
| 168 | + expect(err.message).not.toContain('watermark parameter'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('CONTROL: once the document declares the watermark parameter, the pull sends it', async () => { |
| 172 | + const h = harness({ inputSchema: OPENAPI_SINCE_DECLARED, input: { query: { limit: 50 } }, stored: STORED }); |
| 173 | + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); |
| 174 | + expect(h.handler).toHaveBeenCalledTimes(1); |
| 175 | + expect(h.handler.mock.calls[0][0]).toEqual({ query: { limit: 50, since: '2026-01-02T00:00:00.000Z' } }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('CONTROL: the first pull, with nothing stored yet, sends no watermark and so is not refused for it', async () => { |
| 179 | + const h = harness({ inputSchema: OPENAPI_LIMIT_ONLY, input: { query: { limit: 50 } } }); |
| 180 | + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); |
| 181 | + expect(h.handler).toHaveBeenCalledTimes(1); |
| 182 | + expect(h.handler.mock.calls[0][0]).toEqual({ query: { limit: 50 } }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('CONTROL: an openapi operation with no parameter and no body declares no inputSchema, so nothing is judged and the watermark is sent', async () => { |
| 186 | + const h = harness({ inputSchema: undefined, input: {}, stored: STORED }); |
| 187 | + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); |
| 188 | + expect(h.handler).toHaveBeenCalledTimes(1); |
| 189 | + expect(h.handler.mock.calls[0][0]).toEqual({ query: { since: '2026-01-02T00:00:00.000Z' } }); |
| 190 | + }); |
| 191 | + |
| 192 | + it('CONTROL: a rest connector\'s open query section takes the watermark parameter as before', async () => { |
| 193 | + const h = harness({ inputSchema: REST_REQUEST, input: { method: 'GET', path: '/contacts' }, stored: STORED }); |
| 194 | + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); |
| 195 | + expect(h.handler).toHaveBeenCalledTimes(1); |
| 196 | + expect(h.handler.mock.calls[0][0]).toEqual({ |
| 197 | + method: 'GET', path: '/contacts', query: { since: '2026-01-02T00:00:00.000Z' }, |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments