|
| 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 | + * A plain list read of an object the server does not know must reach the |
| 11 | + * surface as a failure, not as "no data yet". |
| 12 | + * |
| 13 | + * `find()`'s SDK path (no `$expand`, no `$search`) degraded every 404 to |
| 14 | + * `{ data: [], total: 0 }` and memoised the resource in `missingResources`. |
| 15 | + * That memo exists for OPTIONAL platform collections (`sys_presence`, |
| 16 | + * `sys_activity`, …), whose absence means "this deployment does not install |
| 17 | + * the feature". It also swallowed the 404 of a list's OWN object: an object |
| 18 | + * that failed to register answered 404 `OBJECT_NOT_FOUND`, the list showed its |
| 19 | + * empty state, and every later read of that object short-circuited to empty |
| 20 | + * without asking again. |
| 21 | + * |
| 22 | + * The error code cannot split the two cases: an uninstalled `sys_presence` and |
| 23 | + * an unregistered `crm_invoice` both answer 404 `OBJECT_NOT_FOUND`. The split |
| 24 | + * is by name, against the platform's curated registry |
| 25 | + * (`isPlatformProvidedObjectName`, @objectstack/spec): a registry name keeps |
| 26 | + * the empty answer and the memo; any other object's 404 is rethrown and not |
| 27 | + * memoised. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, vi } from 'vitest'; |
| 31 | +import { isPlatformProvidedObjectName } from '@objectstack/spec/system'; |
| 32 | +import { ObjectStackAdapter } from './index'; |
| 33 | + |
| 34 | +/** An error shaped the way `@objectstack/client`'s fetch wrapper throws one. */ |
| 35 | +function clientError(message: string, httpStatus: number, code?: string) { |
| 36 | + const err = new Error(message) as Error & { httpStatus: number; code?: string }; |
| 37 | + err.httpStatus = httpStatus; |
| 38 | + if (code) err.code = code; |
| 39 | + return err; |
| 40 | +} |
| 41 | + |
| 42 | +function makeDS(find: any) { |
| 43 | + const ds: any = new ObjectStackAdapter({ |
| 44 | + baseUrl: 'http://test.local', |
| 45 | + fetch: vi.fn(async () => |
| 46 | + new Response(JSON.stringify({ success: true, data: { capabilities: {}, routes: {} } }), { |
| 47 | + status: 200, |
| 48 | + headers: { 'Content-Type': 'application/json' }, |
| 49 | + })), |
| 50 | + }); |
| 51 | + ds.connected = true; |
| 52 | + ds.connectionState = 'connected'; |
| 53 | + ds.client = { data: { find } }; |
| 54 | + return ds; |
| 55 | +} |
| 56 | + |
| 57 | +const MISSING = 'crm_invoice'; |
| 58 | +const NOT_FOUND_SENTENCE = "Object 'crm_invoice' is not registered"; |
| 59 | + |
| 60 | +describe('find — a 404 on a non-platform object is rethrown, not emptied', () => { |
| 61 | + it('the names these tests lean on sit on the right side of the registry', () => { |
| 62 | + expect(isPlatformProvidedObjectName(MISSING)).toBe(false); |
| 63 | + expect(isPlatformProvidedObjectName('sys_presence')).toBe(true); |
| 64 | + expect(isPlatformProvidedObjectName('sys_activity')).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('REJECTS a plain find answered 404 OBJECT_NOT_FOUND, with the code, status and sentence', async () => { |
| 68 | + const find = vi.fn().mockRejectedValue(clientError(NOT_FOUND_SENTENCE, 404, 'OBJECT_NOT_FOUND')); |
| 69 | + const ds = makeDS(find); |
| 70 | + |
| 71 | + await expect(ds.find(MISSING, { $top: 0 })).rejects.toMatchObject({ |
| 72 | + code: 'OBJECT_NOT_FOUND', |
| 73 | + httpStatus: 404, |
| 74 | + message: NOT_FOUND_SENTENCE, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not memoise it: a second find asks the server again and rejects again', async () => { |
| 79 | + const find = vi.fn().mockRejectedValue(clientError(NOT_FOUND_SENTENCE, 404, 'OBJECT_NOT_FOUND')); |
| 80 | + const ds = makeDS(find); |
| 81 | + |
| 82 | + await expect(ds.find(MISSING)).rejects.toMatchObject({ code: 'OBJECT_NOT_FOUND' }); |
| 83 | + await expect(ds.find(MISSING, { $top: 25 })).rejects.toMatchObject({ code: 'OBJECT_NOT_FOUND' }); |
| 84 | + expect(find).toHaveBeenCalledTimes(2); |
| 85 | + }); |
| 86 | + |
| 87 | + it('a bare 404 with no code on a non-platform object is rethrown too', async () => { |
| 88 | + const find = vi.fn().mockRejectedValue(clientError('Not found', 404)); |
| 89 | + const ds = makeDS(find); |
| 90 | + |
| 91 | + await expect(ds.find(MISSING)).rejects.toMatchObject({ httpStatus: 404 }); |
| 92 | + await expect(ds.find(MISSING)).rejects.toMatchObject({ httpStatus: 404 }); |
| 93 | + expect(find).toHaveBeenCalledTimes(2); |
| 94 | + }); |
| 95 | + |
| 96 | + it('over the real client: the wrapped 404 envelope reaches the caller as code + status', async () => { |
| 97 | + const dataUrls: string[] = []; |
| 98 | + const fetchImpl = vi.fn(async (url: RequestInfo | URL) => { |
| 99 | + const href = String(url); |
| 100 | + if (href.includes('/data/')) { |
| 101 | + dataUrls.push(href); |
| 102 | + return new Response( |
| 103 | + JSON.stringify({ success: false, error: { code: 'OBJECT_NOT_FOUND', message: NOT_FOUND_SENTENCE } }), |
| 104 | + { status: 404, headers: { 'Content-Type': 'application/json' } }, |
| 105 | + ); |
| 106 | + } |
| 107 | + return new Response(JSON.stringify({ success: true, data: { capabilities: {}, routes: {} } }), { |
| 108 | + status: 200, |
| 109 | + headers: { 'Content-Type': 'application/json' }, |
| 110 | + }); |
| 111 | + }); |
| 112 | + const ds: any = new ObjectStackAdapter({ baseUrl: 'http://test.local', fetch: fetchImpl }); |
| 113 | + ds.connected = true; |
| 114 | + ds.connectionState = 'connected'; |
| 115 | + |
| 116 | + await expect(ds.find(MISSING, { $top: 0 })).rejects.toMatchObject({ |
| 117 | + code: 'OBJECT_NOT_FOUND', |
| 118 | + httpStatus: 404, |
| 119 | + }); |
| 120 | + await expect(ds.find(MISSING, { $top: 0 })).rejects.toMatchObject({ code: 'OBJECT_NOT_FOUND' }); |
| 121 | + expect(dataUrls).toHaveLength(2); |
| 122 | + expect(dataUrls.every((u) => u.includes(`/data/${MISSING}`))).toBe(true); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('find — CONTROLS: the platform optional-collection half is unchanged', () => { |
| 127 | + it('CONTROL: a registry name answered 404 OBJECT_NOT_FOUND degrades to empty and is memoised', async () => { |
| 128 | + const find = vi.fn().mockRejectedValue(clientError("Object 'sys_presence' not found", 404, 'OBJECT_NOT_FOUND')); |
| 129 | + const ds = makeDS(find); |
| 130 | + |
| 131 | + await expect(ds.find('sys_presence')).resolves.toMatchObject({ data: [], total: 0 }); |
| 132 | + // Short-circuited: the second call never reaches the server. |
| 133 | + await expect(ds.find('sys_presence', { $top: 5 })).resolves.toMatchObject({ data: [], total: 0 }); |
| 134 | + expect(find).toHaveBeenCalledTimes(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it('CONTROL: the memo is per resource: an emptied platform name does not empty a missing object', async () => { |
| 138 | + const find = vi.fn(async (resource: string) => { |
| 139 | + throw clientError(`Object '${resource}' not found`, 404, 'OBJECT_NOT_FOUND'); |
| 140 | + }); |
| 141 | + const ds = makeDS(find); |
| 142 | + |
| 143 | + await expect(ds.find('sys_activity')).resolves.toMatchObject({ data: [], total: 0 }); |
| 144 | + await expect(ds.find(MISSING)).rejects.toMatchObject({ code: 'OBJECT_NOT_FOUND' }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('CONTROL: an enable-block denial still rethrows, on a platform name and on any other', async () => { |
| 148 | + const find = vi.fn().mockRejectedValue(clientError('Object API is disabled', 404, 'OBJECT_API_DISABLED')); |
| 149 | + const ds = makeDS(find); |
| 150 | + |
| 151 | + await expect(ds.find('sys_jwks')).rejects.toMatchObject({ code: 'OBJECT_API_DISABLED', httpStatus: 404 }); |
| 152 | + await expect(ds.find(MISSING)).rejects.toMatchObject({ code: 'OBJECT_API_DISABLED', httpStatus: 404 }); |
| 153 | + // Neither is memoised: the platform name is asked again. |
| 154 | + await expect(ds.find('sys_jwks')).rejects.toMatchObject({ code: 'OBJECT_API_DISABLED' }); |
| 155 | + expect(find).toHaveBeenCalledTimes(3); |
| 156 | + }); |
| 157 | + |
| 158 | + it('CONTROL: a 2xx with zero rows on a non-platform object is still an ordinary empty result', async () => { |
| 159 | + const find = vi.fn().mockResolvedValue({ records: [], total: 0 }); |
| 160 | + const ds = makeDS(find); |
| 161 | + |
| 162 | + await expect(ds.find(MISSING)).resolves.toMatchObject({ data: [], total: 0 }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments