-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuserState.test.ts
More file actions
315 lines (264 loc) · 9.88 KB
/
Copy pathuserState.test.ts
File metadata and controls
315 lines (264 loc) · 9.88 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* 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.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { createObjectStackUserStateAdapter } from './userState';
import type { DataSource } from '@object-ui/types';
function mockDataSource(overrides: Partial<DataSource<any>> = {}): DataSource<any> {
return {
find: vi.fn().mockResolvedValue({ data: [] }),
findOne: vi.fn().mockResolvedValue(null),
create: vi.fn().mockResolvedValue({}),
update: vi.fn().mockResolvedValue({}),
delete: vi.fn().mockResolvedValue(true),
...overrides,
} as unknown as DataSource<any>;
}
describe('createObjectStackUserStateAdapter', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
describe('load', () => {
it('returns [] when no row exists for the (user, key) pair', async () => {
const ds = mockDataSource();
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u1',
key: 'ui.favorites',
});
const items = await adapter.load();
expect(items).toEqual([]);
expect(ds.find).toHaveBeenCalledWith('sys_user_preference', {
$filter: { user_id: 'u1', key: 'ui.favorites' },
$top: 1,
});
});
it('returns parsed value when row exists (array value)', async () => {
const value = [{ id: 'object:contact', label: 'Contact' }];
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({
data: [{ id: 'row-1', user_id: 'u1', key: 'ui.recent', value }],
}) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u1',
key: 'ui.recent',
});
await expect(adapter.load()).resolves.toEqual(value);
});
it('parses string-encoded JSON values', async () => {
const items = [{ id: 'a' }, { id: 'b' }];
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({
data: [{ id: 1, value: JSON.stringify(items) }],
}) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
});
await expect(adapter.load()).resolves.toEqual(items);
});
it('returns [] when value is malformed JSON', async () => {
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({
data: [{ id: 1, value: '{not json' }],
}) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
});
await expect(adapter.load()).resolves.toEqual([]);
});
it('swallows errors and returns [] when find() throws', async () => {
const onError = vi.fn();
const ds = mockDataSource({
find: vi.fn().mockRejectedValue(new Error('boom')) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
onError,
});
await expect(adapter.load()).resolves.toEqual([]);
expect(onError).toHaveBeenCalledWith('load', expect.any(Error));
});
it('uses a custom resource name when provided', async () => {
const ds = mockDataSource();
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
resource: 'my_prefs',
});
await adapter.load();
expect(ds.find).toHaveBeenCalledWith('my_prefs', expect.any(Object));
});
});
describe('save', () => {
it('creates a new row when none exists', async () => {
const created = { id: 'new-row' };
const ds = mockDataSource({
create: vi.fn().mockResolvedValue(created) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u1',
key: 'ui.favorites',
});
await adapter.save([{ id: 'a' } as any]);
expect(ds.create).toHaveBeenCalledWith('sys_user_preference', {
user_id: 'u1',
key: 'ui.favorites',
value: [{ id: 'a' }],
});
});
it('updates the existing row when one exists', async () => {
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({
data: [{ id: 'row-99', user_id: 'u', key: 'k', value: [] }],
}) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
});
await adapter.save([{ id: 'x' } as any]);
expect(ds.update).toHaveBeenCalledWith('sys_user_preference', 'row-99', {
value: [{ id: 'x' }],
});
expect(ds.create).not.toHaveBeenCalled();
});
// #3794 — `updated_at` is server-managed. A caller-supplied value is
// stripped (framework #2948) and reported back as a dropped field, which
// the console turns into a "Some fields were not saved" toast. Stamping it
// here fired that warning on every recents/favorites write, about a field
// no user ever touched — noise on the exact surface that is supposed to
// tell a user their edit did not land.
it('never sends the server-managed updated_at column', async () => {
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({ data: [] }) as any,
create: vi.fn().mockResolvedValue({ id: 'row-1' }) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'ui.recent',
});
await adapter.save([{ id: 'a' } as any]);
await adapter.save([{ id: 'b' } as any]);
for (const call of [...(ds.create as any).mock.calls, ...(ds.update as any).mock.calls]) {
expect(call[call.length - 1]).not.toHaveProperty('updated_at');
}
});
it('uses the cached row id from a previous load on subsequent saves', async () => {
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({
data: [{ id: 'row-42', value: [] }],
}) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
});
await adapter.load();
(ds.find as any).mockClear();
await adapter.save([{ id: 'b' } as any]);
// No second find() — went straight to update via cached id.
expect(ds.find).not.toHaveBeenCalled();
expect(ds.update).toHaveBeenCalledWith('sys_user_preference', 'row-42', expect.any(Object));
});
it('falls back to insert when cached row update fails (row deleted server-side)', async () => {
const ds = mockDataSource({
find: vi.fn()
.mockResolvedValueOnce({ data: [{ id: 'stale', value: [] }] })
.mockResolvedValueOnce({ data: [] }) as any,
update: vi.fn().mockRejectedValue(new Error('not found')) as any,
create: vi.fn().mockResolvedValue({ id: 'new' }) as any,
});
const onError = vi.fn();
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
onError,
});
await adapter.load(); // primes cachedRowId = 'stale'
await adapter.save([{ id: 'z' } as any]);
expect(ds.update).toHaveBeenCalledTimes(1);
expect(ds.create).toHaveBeenCalledWith('sys_user_preference', expect.objectContaining({
user_id: 'u',
key: 'k',
value: [{ id: 'z' }],
}));
});
it('recovers from a UNIQUE(user_id, key) insert failure by updating in place', async () => {
// First findExisting() (no cached id) misses; create() loses the race and
// throws; the recovery findExisting() now sees the row → update.
const ds = mockDataSource({
find: vi.fn()
.mockResolvedValueOnce({ data: [] })
.mockResolvedValueOnce({ data: [{ id: 'row-7', user_id: 'u', key: 'k', value: [] }] }) as any,
create: vi.fn().mockRejectedValue(new Error('UNIQUE constraint failed')) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
});
await adapter.save([{ id: 'q' } as any]);
expect(ds.create).toHaveBeenCalledTimes(1);
expect(ds.update).toHaveBeenCalledWith('sys_user_preference', 'row-7', {
value: [{ id: 'q' }],
});
});
it('serializes concurrent saves so only one insert happens', async () => {
// Both saves start before either resolves. Without serialization both
// would findExisting()→[]→create() and the second would trip the
// UNIQUE constraint. With the save chain, the second waits, sees the
// cached row id from the first, and updates.
const ds = mockDataSource({
find: vi.fn().mockResolvedValue({ data: [] }) as any,
create: vi.fn().mockResolvedValue({ id: 'row-1' }) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'ui.recent',
});
await Promise.all([
adapter.save([{ id: 'a' } as any]),
adapter.save([{ id: 'a' }, { id: 'b' }] as any),
]);
expect(ds.create).toHaveBeenCalledTimes(1);
expect(ds.update).toHaveBeenCalledWith('sys_user_preference', 'row-1', {
value: [{ id: 'a' }, { id: 'b' }],
});
});
it('swallows errors when save() throws', async () => {
const onError = vi.fn();
const ds = mockDataSource({
find: vi.fn().mockRejectedValue(new Error('network down')) as any,
});
const adapter = createObjectStackUserStateAdapter({
dataSource: ds,
userId: 'u',
key: 'k',
onError,
});
await expect(adapter.save([{ id: 'a' } as any])).resolves.toBeUndefined();
expect(onError).toHaveBeenCalledWith('save', expect.any(Error));
});
});
});