|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 10 | +import { ObjectStackAdapter, clearSharedDiscoveryCache } from './index'; |
| 11 | + |
| 12 | +/** |
| 13 | + * `listImportMappings(objectName)` feeds the import wizard's "saved mapping" |
| 14 | + * selector (framework #2611). The selector is FEATURE-DETECTED: an empty list |
| 15 | + * hides it, and the adapter deliberately degrades every failure to an empty |
| 16 | + * list — so a filter reading the wrong depth of the served item would not |
| 17 | + * fail anywhere; it would just make the selector never appear, on every |
| 18 | + * deployment, silently (objectui#14026). |
| 19 | + * |
| 20 | + * This pin therefore does not stub the SDK. The `fetch` stub answers the |
| 21 | + * discovery probe and `GET /api/v1/meta/mapping` with the body the framework's |
| 22 | + * REST list door was MEASURED to emit — `RestServer`'s `GET /meta/:type` |
| 23 | + * driven by a real `ObjectStackProtocolImplementation` over a registered |
| 24 | + * `mapping` artifact (objectstack `packages/rest`, probe run 2026-09-05): |
| 25 | + * raw spec documents decorated with `_packageId` / `_provenance` / |
| 26 | + * `_diagnostics`, carrying `targetObject` at the TOP LEVEL, inside the spec's |
| 27 | + * `{ type, items }` envelope with no `{ success, data }` wrapper. The real |
| 28 | + * `@objectstack/client` `meta.getItems` then unwraps it, and the adapter's |
| 29 | + * own filter runs on what the SDK hands back. |
| 30 | + */ |
| 31 | + |
| 32 | +/** Verbatim `GET /api/v1/meta/mapping` body from the framework probe. */ |
| 33 | +const WIRE_BODY = { |
| 34 | + type: 'mapping', |
| 35 | + items: [ |
| 36 | + { |
| 37 | + name: 'task_feed_import', |
| 38 | + label: 'Task feed import (manifest path)', |
| 39 | + sourceFormat: 'csv', |
| 40 | + targetObject: 'task', |
| 41 | + fieldMapping: [ |
| 42 | + { source: 'ID', target: 'id', transform: 'none' }, |
| 43 | + { source: 'Task Title', target: 'title', transform: 'none' }, |
| 44 | + ], |
| 45 | + mode: 'upsert', |
| 46 | + upsertKey: ['id'], |
| 47 | + _packageId: 'probe_pkg', |
| 48 | + _provenance: 'package', |
| 49 | + _diagnostics: { valid: true }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'user_only_mapping', |
| 53 | + label: 'User feed import', |
| 54 | + sourceFormat: 'json', |
| 55 | + targetObject: 'user', |
| 56 | + fieldMapping: [{ source: 'id', target: 'id', transform: 'none' }], |
| 57 | + mode: 'insert', |
| 58 | + _diagnostics: { valid: true }, |
| 59 | + }, |
| 60 | + ], |
| 61 | +}; |
| 62 | + |
| 63 | +const BASE_URL = 'http://list-import-mappings-pin.local'; |
| 64 | + |
| 65 | +function json(body: unknown, status = 200) { |
| 66 | + return new Response(JSON.stringify(body), { |
| 67 | + status, |
| 68 | + headers: { 'Content-Type': 'application/json' }, |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +function makeDS() { |
| 73 | + const requested: string[] = []; |
| 74 | + const fetchImpl = vi.fn(async (input: RequestInfo | URL) => { |
| 75 | + const url = String(input); |
| 76 | + requested.push(url); |
| 77 | + if (url.endsWith('/api/v1/discovery')) { |
| 78 | + return json({ success: true, data: { capabilities: {}, routes: {} } }); |
| 79 | + } |
| 80 | + if (url.endsWith('/api/v1/meta/mapping')) { |
| 81 | + return json(WIRE_BODY); |
| 82 | + } |
| 83 | + return json({ success: false, error: { code: 'NOT_FOUND', message: `unexpected ${url}` } }, 404); |
| 84 | + }); |
| 85 | + const ds = new ObjectStackAdapter({ baseUrl: BASE_URL, fetch: fetchImpl, autoReconnect: false }); |
| 86 | + return { ds, requested }; |
| 87 | +} |
| 88 | + |
| 89 | +describe('ObjectStackDataSource.listImportMappings (objectui#14026)', () => { |
| 90 | + beforeEach(() => { |
| 91 | + clearSharedDiscoveryCache(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns a registered mapping artifact targeting the object, read through the real SDK', async () => { |
| 95 | + const { ds, requested } = makeDS(); |
| 96 | + |
| 97 | + const mappings = await ds.listImportMappings('task'); |
| 98 | + |
| 99 | + // The SDK route the adapter reads — not a stubbed client method. |
| 100 | + expect(requested).toContain(`${BASE_URL}/api/v1/meta/mapping`); |
| 101 | + // The artifact targeting `task` is served; the one targeting `user` is not. |
| 102 | + expect(mappings.map((m: any) => m.name)).toEqual(['task_feed_import']); |
| 103 | + // …and it is the served document itself, decorations included, so the |
| 104 | + // wizard's `asSavedMapping` predicate (`name` string, `targetObject` |
| 105 | + // string, `fieldMapping` array) accepts it as-is. |
| 106 | + const [m] = mappings; |
| 107 | + expect(typeof m.name).toBe('string'); |
| 108 | + expect(m.targetObject).toBe('task'); |
| 109 | + expect(Array.isArray(m.fieldMapping)).toBe(true); |
| 110 | + expect(m.mode).toBe('upsert'); |
| 111 | + expect(m.upsertKey).toEqual(['id']); |
| 112 | + }); |
| 113 | + |
| 114 | + it('answers an empty list, not a match, for an object no artifact targets', async () => { |
| 115 | + const { ds } = makeDS(); |
| 116 | + expect(await ds.listImportMappings('project')).toEqual([]); |
| 117 | + }); |
| 118 | +}); |
0 commit comments