|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The write-path inverse of the connector read projection, for the keys a |
| 4 | +// connector does not declare: a save of the served form keeps what the read |
| 5 | +// withheld (top level, `retryConfig`, and an `actions` element addressed by |
| 6 | +// its `key`), and a container the author removed is never minted back. |
| 7 | +// |
| 8 | +// Every value below is an obviously-fake placeholder. |
| 9 | + |
| 10 | +import { describe, it, expect } from 'vitest'; |
| 11 | +import { carryForwardRedactedValues, redactMetadataItem } from './metadata-redaction.js'; |
| 12 | + |
| 13 | +const P = (n: number) => `TEST_PLACEHOLDER_UNDECLARED_${n}`; |
| 14 | + |
| 15 | +const stored = { |
| 16 | + name: 'vendor_catalog', |
| 17 | + label: 'Vendor catalog', |
| 18 | + type: 'api', |
| 19 | + apiKey: P(1), |
| 20 | + retryConfig: { strategy: 'exponential_backoff', secret: P(2) }, |
| 21 | + actions: [ |
| 22 | + { key: 'get_invoice', label: 'Get invoice', token: P(3) }, |
| 23 | + { key: 'list_invoices', label: 'List invoices' }, |
| 24 | + ], |
| 25 | +}; |
| 26 | + |
| 27 | +/** What a read exit serves for `stored`: the projection, as a client would send it back. */ |
| 28 | +const served = () => JSON.parse(JSON.stringify(redactMetadataItem('connector', stored))) as Record<string, any>; |
| 29 | + |
| 30 | +describe('carry-forward of the keys a connector does not declare', () => { |
| 31 | + it('the served form withholds them (positive control for every case below)', () => { |
| 32 | + const body = served(); |
| 33 | + expect(JSON.stringify(body)).not.toContain('TEST_PLACEHOLDER_UNDECLARED_'); |
| 34 | + expect(body.retryConfig).toEqual({ strategy: 'exponential_backoff' }); |
| 35 | + expect(body.actions[0]).toEqual({ key: 'get_invoice', label: 'Get invoice' }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('a save of the served form keeps every withheld value, the edited label included', () => { |
| 39 | + const out = carryForwardRedactedValues('connector', { ...served(), label: 'Vendor catalog (edited)' }, stored); |
| 40 | + expect(out).toEqual({ ...stored, label: 'Vendor catalog (edited)' }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('an `actions` element is followed by its `key` when the author reorders the list', () => { |
| 44 | + const body = served(); |
| 45 | + body.actions.reverse(); |
| 46 | + const out = carryForwardRedactedValues('connector', body, stored) as Record<string, any>; |
| 47 | + expect(out.actions).toEqual([ |
| 48 | + { key: 'list_invoices', label: 'List invoices' }, |
| 49 | + { key: 'get_invoice', label: 'Get invoice', token: P(3) }, |
| 50 | + ]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('the author\'s own value at a withheld nested position wins', () => { |
| 54 | + const body = served(); |
| 55 | + body.retryConfig.secret = 'author-value'; |
| 56 | + const out = carryForwardRedactedValues('connector', body, stored) as Record<string, any>; |
| 57 | + expect(out.retryConfig.secret).toBe('author-value'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('a removed `retryConfig` or `actions` element is not minted back', () => { |
| 61 | + const body = served(); |
| 62 | + delete body.retryConfig; |
| 63 | + body.actions = body.actions.filter((a: { key: string }) => a.key !== 'get_invoice'); |
| 64 | + const out = carryForwardRedactedValues('connector', body, stored) as Record<string, any>; |
| 65 | + expect(out).not.toHaveProperty('retryConfig'); |
| 66 | + expect(out.actions).toEqual([{ key: 'list_invoices', label: 'List invoices' }]); |
| 67 | + expect(out.apiKey).toBe(P(1)); |
| 68 | + }); |
| 69 | + |
| 70 | + it('two `actions` elements sharing a `key` cannot be told apart: neither is carried', () => { |
| 71 | + const twin = { ...stored, actions: [stored.actions[0], { key: 'get_invoice', label: 'Twin' }] }; |
| 72 | + const body = JSON.parse(JSON.stringify(redactMetadataItem('connector', twin))); |
| 73 | + const out = carryForwardRedactedValues('connector', body, twin) as Record<string, any>; |
| 74 | + expect(JSON.stringify(out.actions)).not.toContain(P(3)); |
| 75 | + }); |
| 76 | +}); |
0 commit comments