|
| 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 | + * ObjectStackAdapter must surface the framework's write-observability channel |
| 11 | + * (#3431/#3455): when a create/update response carries `droppedFields` — fields |
| 12 | + * the backend LEGALLY stripped (read-only / locked-by-state) — the adapter emits |
| 13 | + * a WriteWarningEvent so the app shell can toast the user instead of letting the |
| 14 | + * dropped value vanish silently. The write itself still succeeded. |
| 15 | + */ |
| 16 | +import { describe, it, expect, vi } from 'vitest'; |
| 17 | +import { ObjectStackAdapter } from './index'; |
| 18 | +import type { WriteWarningEvent } from './index'; |
| 19 | + |
| 20 | +function makeDS(stub: Record<string, any>) { |
| 21 | + const ds: any = new ObjectStackAdapter({ |
| 22 | + baseUrl: 'http://test.local', |
| 23 | + fetch: vi.fn(async () => |
| 24 | + new Response(JSON.stringify({ success: true, data: { capabilities: {}, routes: {} } }), { |
| 25 | + status: 200, |
| 26 | + headers: { 'Content-Type': 'application/json' }, |
| 27 | + }), |
| 28 | + ), |
| 29 | + }); |
| 30 | + ds.connected = true; |
| 31 | + ds.connectionState = 'connected'; |
| 32 | + ds.client = { data: stub }; |
| 33 | + return ds; |
| 34 | +} |
| 35 | + |
| 36 | +const ONE_DROP = [{ object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }]; |
| 37 | + |
| 38 | +describe('ObjectStackAdapter.onWriteWarning', () => { |
| 39 | + it('emits a create write-warning carrying the dropped fields and the new id', async () => { |
| 40 | + const create = vi.fn().mockResolvedValue({ record: { id: 'r1', title: 'A' }, droppedFields: ONE_DROP }); |
| 41 | + const ds = makeDS({ create }); |
| 42 | + const events: WriteWarningEvent[] = []; |
| 43 | + ds.onWriteWarning((e: WriteWarningEvent) => events.push(e)); |
| 44 | + |
| 45 | + const rec = await ds.create('approval_case', { title: 'A', approval_status: 'approved' }); |
| 46 | + |
| 47 | + // The write still returns the plain record (unchanged behaviour). |
| 48 | + expect(rec).toEqual({ id: 'r1', title: 'A' }); |
| 49 | + expect(events).toEqual([ |
| 50 | + { operation: 'create', resource: 'approval_case', id: 'r1', droppedFields: ONE_DROP }, |
| 51 | + ]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('emits an update write-warning carrying the target id', async () => { |
| 55 | + const update = vi.fn().mockResolvedValue({ record: { id: 'r1' }, droppedFields: ONE_DROP }); |
| 56 | + const ds = makeDS({ update }); |
| 57 | + const events: WriteWarningEvent[] = []; |
| 58 | + ds.onWriteWarning((e: WriteWarningEvent) => events.push(e)); |
| 59 | + |
| 60 | + await ds.update('approval_case', 'r1', { approval_status: 'approved' }); |
| 61 | + |
| 62 | + expect(events).toEqual([ |
| 63 | + { operation: 'update', resource: 'approval_case', id: 'r1', droppedFields: ONE_DROP }, |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('does NOT emit when the response carries no droppedFields (the common case)', async () => { |
| 68 | + const create = vi.fn().mockResolvedValue({ record: { id: 'r1', title: 'A' } }); |
| 69 | + const update = vi.fn().mockResolvedValue({ record: { id: 'r1' } }); |
| 70 | + const ds = makeDS({ create, update }); |
| 71 | + const events: WriteWarningEvent[] = []; |
| 72 | + ds.onWriteWarning((e: WriteWarningEvent) => events.push(e)); |
| 73 | + |
| 74 | + await ds.create('approval_case', { title: 'A' }); |
| 75 | + await ds.update('approval_case', 'r1', { title: 'B' }); |
| 76 | + |
| 77 | + expect(events).toEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does NOT emit for an empty or malformed droppedFields payload', async () => { |
| 81 | + const create = vi |
| 82 | + .fn() |
| 83 | + .mockResolvedValueOnce({ record: { id: 'r1' }, droppedFields: [] }) |
| 84 | + .mockResolvedValueOnce({ record: { id: 'r2' }, droppedFields: 'nope' }) |
| 85 | + .mockResolvedValueOnce({ record: { id: 'r3' }, droppedFields: [{ object: 'x', fields: [] }] }); |
| 86 | + const ds = makeDS({ create }); |
| 87 | + const events: WriteWarningEvent[] = []; |
| 88 | + ds.onWriteWarning((e: WriteWarningEvent) => events.push(e)); |
| 89 | + |
| 90 | + await ds.create('x', {}); |
| 91 | + await ds.create('x', {}); |
| 92 | + await ds.create('x', {}); // event present but its fields[] is empty → filtered out |
| 93 | + |
| 94 | + expect(events).toEqual([]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('stops delivering after unsubscribe', async () => { |
| 98 | + const create = vi.fn().mockResolvedValue({ record: { id: 'r1' }, droppedFields: ONE_DROP }); |
| 99 | + const ds = makeDS({ create }); |
| 100 | + const events: WriteWarningEvent[] = []; |
| 101 | + const unsub = ds.onWriteWarning((e: WriteWarningEvent) => events.push(e)); |
| 102 | + |
| 103 | + await ds.create('approval_case', {}); |
| 104 | + unsub(); |
| 105 | + await ds.create('approval_case', {}); |
| 106 | + |
| 107 | + expect(events).toHaveLength(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it('isolates a throwing listener so the write still resolves and other listeners fire', async () => { |
| 111 | + const update = vi.fn().mockResolvedValue({ record: { id: 'r1' }, droppedFields: ONE_DROP }); |
| 112 | + const ds = makeDS({ update }); |
| 113 | + const good = vi.fn(); |
| 114 | + ds.onWriteWarning(() => { throw new Error('boom'); }); |
| 115 | + ds.onWriteWarning(good); |
| 116 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 117 | + |
| 118 | + await expect(ds.update('approval_case', 'r1', { approval_status: 'x' })).resolves.toBeTruthy(); |
| 119 | + expect(good).toHaveBeenCalledTimes(1); |
| 120 | + warn.mockRestore(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments