|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * An `api` action's `target` is interpolated before a REGISTERED `api` handler |
| 5 | + * receives it, through the same substitution a `url` target goes through. |
| 6 | + * |
| 7 | + * The console registers its own `api` handler, which replaces the built-in |
| 8 | + * `executeAPI` wholesale. Only `executeAPI` called `interpolateTarget`, so on a |
| 9 | + * record page `target: "/api/v1/data/gapdata_item/${ctx.recordId}"` reached the |
| 10 | + * wire as `…/$%7Bctx.recordId%7D` for PATCH and POST alike, while a `url` |
| 11 | + * action with the same token opened the resolved address. |
| 12 | + * |
| 13 | + * Pinned here: what the handler receives for PATCH and POST, that the encoding |
| 14 | + * is the url target's (one opaque segment per value), that the built-in |
| 15 | + * executor sends the same path, that a `url` action is unchanged, and that a |
| 16 | + * non-`api` handler still receives its target as authored. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 20 | +import { ActionRunner, type ActionContext, type ActionDef, type ActionRunnerHandler } from '../ActionRunner'; |
| 21 | + |
| 22 | +const RECORD_PAGE: ActionContext = { |
| 23 | + record: { id: 'gd_001', name: 'Gap item' }, |
| 24 | + objectName: 'gapdata_item', |
| 25 | +}; |
| 26 | + |
| 27 | +const TARGET = '/api/v1/data/gapdata_item/${ctx.recordId}'; |
| 28 | + |
| 29 | +function recordingApiHandler() { |
| 30 | + const seen: ActionDef[] = []; |
| 31 | + const handler: ActionRunnerHandler = async (action) => { |
| 32 | + seen.push(action); |
| 33 | + return { success: true, silent: true }; |
| 34 | + }; |
| 35 | + return { seen, handler }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('api targets interpolate before a registered api handler (objectstack#26722)', () => { |
| 39 | + afterEach(() => { |
| 40 | + vi.unstubAllGlobals(); |
| 41 | + }); |
| 42 | + |
| 43 | + it.each(['PATCH', 'POST'])('a %s api action hands the handler the resolved record path', async (method) => { |
| 44 | + const runner = new ActionRunner(RECORD_PAGE); |
| 45 | + const { seen, handler } = recordingApiHandler(); |
| 46 | + runner.registerHandler('api', handler); |
| 47 | + |
| 48 | + const result = await runner.execute({ type: 'api', name: 'call_api', target: TARGET, method }); |
| 49 | + |
| 50 | + expect(result.success).toBe(true); |
| 51 | + expect(seen).toHaveLength(1); |
| 52 | + expect(seen[0].target).toBe('/api/v1/data/gapdata_item/gd_001'); |
| 53 | + expect(seen[0].method).toBe(method); |
| 54 | + }); |
| 55 | + |
| 56 | + it('encodes each value the way a url target does', async () => { |
| 57 | + const context: ActionContext = { record: { id: 'a/b c' }, objectName: 'gapdata_item' }; |
| 58 | + const runner = new ActionRunner(context); |
| 59 | + const { seen, handler } = recordingApiHandler(); |
| 60 | + runner.registerHandler('api', handler); |
| 61 | + const navigate = vi.fn(); |
| 62 | + runner.setNavigationHandler(navigate); |
| 63 | + |
| 64 | + await runner.execute({ type: 'api', name: 'call_api', target: TARGET, method: 'PATCH' }); |
| 65 | + await runner.execute({ type: 'url', name: 'open', target: TARGET }); |
| 66 | + |
| 67 | + expect(seen[0].target).toBe('/api/v1/data/gapdata_item/a%2Fb%20c'); |
| 68 | + expect(navigate.mock.calls[0][0]).toContain('/api/v1/data/gapdata_item/a%2Fb%20c'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('the built-in executor sends the same resolved path', async () => { |
| 72 | + const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({}) }); |
| 73 | + vi.stubGlobal('fetch', fetchMock); |
| 74 | + const runner = new ActionRunner(RECORD_PAGE); |
| 75 | + |
| 76 | + await runner.execute({ type: 'api', name: 'export_tab', target: TARGET, method: 'POST' }); |
| 77 | + |
| 78 | + expect(fetchMock).toHaveBeenCalledWith( |
| 79 | + '/api/v1/data/gapdata_item/gd_001', |
| 80 | + expect.objectContaining({ method: 'POST' }), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('a url action with the same token is unchanged', async () => { |
| 85 | + const runner = new ActionRunner(RECORD_PAGE); |
| 86 | + const navigate = vi.fn(); |
| 87 | + runner.setNavigationHandler(navigate); |
| 88 | + |
| 89 | + const result = await runner.execute({ type: 'url', name: 'open', target: TARGET }); |
| 90 | + |
| 91 | + expect(result.success).toBe(true); |
| 92 | + expect(navigate).toHaveBeenCalledTimes(1); |
| 93 | + expect(navigate.mock.calls[0][0]).toContain('/api/v1/data/gapdata_item/gd_001'); |
| 94 | + expect(navigate.mock.calls[0][0]).not.toContain('${'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('a registered non-api handler still receives its target as authored', async () => { |
| 98 | + const runner = new ActionRunner(RECORD_PAGE); |
| 99 | + const seen: ActionDef[] = []; |
| 100 | + runner.registerHandler('flow', async (action) => { |
| 101 | + seen.push(action); |
| 102 | + return { success: true, silent: true }; |
| 103 | + }); |
| 104 | + |
| 105 | + await runner.execute({ type: 'flow', name: 'run_flow', target: 'flow_${ctx.recordId}' }); |
| 106 | + |
| 107 | + expect(seen[0].target).toBe('flow_${ctx.recordId}'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments