-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathonWriteWarning.test.ts
More file actions
172 lines (145 loc) · 6.82 KB
/
Copy pathonWriteWarning.test.ts
File metadata and controls
172 lines (145 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* ObjectStackAdapter must surface the framework's write-observability channel
* (#3431/#3455): when a create/update response carries `droppedFields` — fields
* the backend LEGALLY stripped (read-only / locked-by-state) — the adapter emits
* a WriteWarningEvent so the app shell can toast the user instead of letting the
* dropped value vanish silently. The write itself still succeeded.
*/
import { describe, it, expect, vi } from 'vitest';
import { ObjectStackAdapter } from './index';
import type { WriteWarningEvent } from './index';
function makeDS(stub: Record<string, any>) {
const ds: any = new ObjectStackAdapter({
baseUrl: 'http://test.local',
fetch: vi.fn(async () =>
new Response(JSON.stringify({ success: true, data: { capabilities: {}, routes: {} } }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}),
),
});
ds.connected = true;
ds.connectionState = 'connected';
ds.client = { data: stub };
return ds;
}
const ONE_DROP = [{ object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }];
describe('ObjectStackAdapter.onWriteWarning', () => {
it('emits a create write-warning carrying the dropped fields and the new id', async () => {
const create = vi.fn().mockResolvedValue({ record: { id: 'r1', title: 'A' }, droppedFields: ONE_DROP });
const ds = makeDS({ create });
const events: WriteWarningEvent[] = [];
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
const rec = await ds.create('approval_case', { title: 'A', approval_status: 'approved' });
// The write still returns the plain record (unchanged behaviour).
expect(rec).toEqual({ id: 'r1', title: 'A' });
expect(events).toEqual([
{ operation: 'create', resource: 'approval_case', id: 'r1', droppedFields: ONE_DROP },
]);
});
it('emits an update write-warning carrying the target id', async () => {
const update = vi.fn().mockResolvedValue({ record: { id: 'r1' }, droppedFields: ONE_DROP });
const ds = makeDS({ update });
const events: WriteWarningEvent[] = [];
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
await ds.update('approval_case', 'r1', { approval_status: 'approved' });
expect(events).toEqual([
{ operation: 'update', resource: 'approval_case', id: 'r1', droppedFields: ONE_DROP },
]);
});
it('does NOT emit when the response carries no droppedFields (the common case)', async () => {
const create = vi.fn().mockResolvedValue({ record: { id: 'r1', title: 'A' } });
const update = vi.fn().mockResolvedValue({ record: { id: 'r1' } });
const ds = makeDS({ create, update });
const events: WriteWarningEvent[] = [];
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
await ds.create('approval_case', { title: 'A' });
await ds.update('approval_case', 'r1', { title: 'B' });
expect(events).toEqual([]);
});
it('does NOT emit for an empty or malformed droppedFields payload', async () => {
const create = vi
.fn()
.mockResolvedValueOnce({ record: { id: 'r1' }, droppedFields: [] })
.mockResolvedValueOnce({ record: { id: 'r2' }, droppedFields: 'nope' })
.mockResolvedValueOnce({ record: { id: 'r3' }, droppedFields: [{ object: 'x', fields: [] }] });
const ds = makeDS({ create });
const events: WriteWarningEvent[] = [];
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
await ds.create('x', {});
await ds.create('x', {});
await ds.create('x', {}); // event present but its fields[] is empty → filtered out
expect(events).toEqual([]);
});
// ── cross-object batch (framework #3794) ─────────────────────────────────
//
// `batchTransaction` is the console record form's save path for a
// master-detail record, so this is where a `readonlyWhen` strip actually
// reaches a user editing a form. Its response tags each event with the index
// of the operation that produced it.
it('emits a write-warning per event on a cross-object batch, resolving the op', async () => {
const batchTransaction = vi.fn().mockResolvedValue({
results: [{ id: 'acc1' }, { id: 'inv1' }],
droppedFields: [
{ object: 'invoice', fields: ['tax_rate'], reason: 'readonly_when', index: 1 },
],
});
const ds = makeDS({ batchTransaction });
ds.atomicBatchCapability = true;
const events: WriteWarningEvent[] = [];
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
await ds.batchTransaction([
{ object: 'account', action: 'create', data: { name: 'Acme' } },
{ object: 'invoice', action: 'update', id: 'inv1', data: { status: 'paid', tax_rate: 9 } },
]);
expect(events).toEqual([
{
operation: 'update',
resource: 'invoice',
id: 'inv1',
droppedFields: [{ object: 'invoice', fields: ['tax_rate'], reason: 'readonly_when' }],
},
]);
});
it('does NOT emit for a clean batch or a malformed droppedFields list', async () => {
const batchTransaction = vi
.fn()
.mockResolvedValueOnce({ results: [{ id: 'a' }] })
.mockResolvedValueOnce({ results: [{ id: 'a' }], droppedFields: [{ object: 'x', fields: [], index: 0 }] });
const ds = makeDS({ batchTransaction });
ds.atomicBatchCapability = true;
const events: WriteWarningEvent[] = [];
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
await ds.batchTransaction([{ object: 'x', action: 'create', data: {} }]);
await ds.batchTransaction([{ object: 'x', action: 'create', data: {} }]);
expect(events).toEqual([]);
});
it('stops delivering after unsubscribe', async () => {
const create = vi.fn().mockResolvedValue({ record: { id: 'r1' }, droppedFields: ONE_DROP });
const ds = makeDS({ create });
const events: WriteWarningEvent[] = [];
const unsub = ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
await ds.create('approval_case', {});
unsub();
await ds.create('approval_case', {});
expect(events).toHaveLength(1);
});
it('isolates a throwing listener so the write still resolves and other listeners fire', async () => {
const update = vi.fn().mockResolvedValue({ record: { id: 'r1' }, droppedFields: ONE_DROP });
const ds = makeDS({ update });
const good = vi.fn();
ds.onWriteWarning(() => { throw new Error('boom'); });
ds.onWriteWarning(good);
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
await expect(ds.update('approval_case', 'r1', { approval_status: 'x' })).resolves.toBeTruthy();
expect(good).toHaveBeenCalledTimes(1);
warn.mockRestore();
});
});