|
| 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 | +/** |
| 10 | + * objectstack#26711 — the platform's `view` write door refuses a top-level key |
| 11 | + * the body's shape does not declare. `createView` defaulted a top-level `data` |
| 12 | + * onto every spec it sent. On a flat list view `data` is declared; on a |
| 13 | + * ViewItem envelope (`{ name, object, viewKind, config }`) it is declared only |
| 14 | + * inside `config`, so the save was refused with `unrecognized_keys` naming |
| 15 | + * `data` (before, the door dropped it unread). Each body the adapter sends is |
| 16 | + * judged here by the platform's own `ViewMetadataSchema`. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, vi } from 'vitest'; |
| 20 | +import { ViewMetadataSchema } from '@objectstack/spec/ui'; |
| 21 | +import { ObjectStackAdapter } from './index'; |
| 22 | + |
| 23 | +function makeDS() { |
| 24 | + const saveItem = vi.fn(async (_type: string, _name: string, item: any) => ({ success: true, item })); |
| 25 | + const ds: any = new ObjectStackAdapter({ |
| 26 | + baseUrl: 'http://test.local', |
| 27 | + fetch: vi.fn(async () => |
| 28 | + new Response(JSON.stringify({ success: true, data: { capabilities: {}, routes: {} } }), { |
| 29 | + status: 200, |
| 30 | + headers: { 'Content-Type': 'application/json' }, |
| 31 | + })), |
| 32 | + }); |
| 33 | + ds.connected = true; |
| 34 | + ds.connectionState = 'connected'; |
| 35 | + ds.client = { meta: { saveItem } }; |
| 36 | + return { ds, saveItem }; |
| 37 | +} |
| 38 | + |
| 39 | +const sent = (saveItem: ReturnType<typeof vi.fn>) => saveItem.mock.calls[0]![2] as Record<string, unknown>; |
| 40 | + |
| 41 | +describe('createView sends a body the view write door accepts (objectstack#26711)', () => { |
| 42 | + it('a ViewItem envelope is sent without a top-level `data`, and the write door accepts it', async () => { |
| 43 | + const { ds, saveItem } = makeDS(); |
| 44 | + await ds.createView('crm_lead', { |
| 45 | + name: 'crm_lead.probe', |
| 46 | + object: 'crm_lead', |
| 47 | + viewKind: 'list', |
| 48 | + label: 'Probe', |
| 49 | + config: { type: 'grid', data: { provider: 'object', object: 'crm_lead' }, columns: ['name'] }, |
| 50 | + }); |
| 51 | + const body = sent(saveItem); |
| 52 | + expect(body).not.toHaveProperty('data'); |
| 53 | + const parsed = ViewMetadataSchema.safeParse(body); |
| 54 | + expect(parsed.success, JSON.stringify(parsed.success ? null : parsed.error.issues)).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('CONTROL: the envelope WITH the old top-level `data` is refused, naming the key', () => { |
| 58 | + const parsed = ViewMetadataSchema.safeParse({ |
| 59 | + name: 'crm_lead.probe', |
| 60 | + object: 'crm_lead', |
| 61 | + viewKind: 'list', |
| 62 | + label: 'Probe', |
| 63 | + config: { type: 'grid', data: { provider: 'object', object: 'crm_lead' }, columns: ['name'] }, |
| 64 | + data: { provider: 'object', object: 'crm_lead' }, |
| 65 | + }); |
| 66 | + expect(parsed.success).toBe(false); |
| 67 | + expect(JSON.stringify(parsed.success ? [] : parsed.error.issues)).toContain('"keys":["data"]'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('a flat list view still gets its default `data`, where the flat shape declares it', async () => { |
| 71 | + const { ds, saveItem } = makeDS(); |
| 72 | + await ds.createView('crm_lead', { name: 'crm_lead.custom', viewKind: 'list', label: 'Custom', type: 'grid', columns: ['name'] }); |
| 73 | + const body = sent(saveItem); |
| 74 | + expect(body.data).toEqual({ provider: 'object', object: 'crm_lead' }); |
| 75 | + const parsed = ViewMetadataSchema.safeParse(body); |
| 76 | + expect(parsed.success, JSON.stringify(parsed.success ? null : parsed.error.issues)).toBe(true); |
| 77 | + }); |
| 78 | +}); |
0 commit comments