|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#38995] The record page payload's `dataLock` part: the data layer's one lock |
| 5 | + * read (ADR-0162 D9, `GET /data/:object/:id/lock`), answered inside the one |
| 6 | + * request a record page opens with, so the page learns a closed record without |
| 7 | + * a second request. |
| 8 | + * |
| 9 | + * What each pin holds: |
| 10 | + * |
| 11 | + * - the part is the data protocol's `getRecordLock`, asked as the caller, and |
| 12 | + * it answers exactly what the single lock door answers that caller, for each |
| 13 | + * combination the read names (parity, both doors driven over one protocol); |
| 14 | + * - nothing the engine might add rides along: the part is the door's own |
| 15 | + * projection, field by field, so a reader whose field permissions hide the |
| 16 | + * state field is told `closed_state` and nothing about the field or value; |
| 17 | + * - a protocol without the read is the door's `501`, carried as the part's |
| 18 | + * refusal, never "unlocked"; a throwing read is that read's refusal, and the |
| 19 | + * other parts still answer; |
| 20 | + * - the approval part's own `lock` is a different read and is unchanged; |
| 21 | + * - a body that does not ask for the part gets none, and asks nothing. |
| 22 | + * |
| 23 | + * The engine's half (the combinations, the hidden state field, the master |
| 24 | + * line) is `plugin-approvals/src/record-lock-one-read-37770.integration.test.ts`. |
| 25 | + */ |
| 26 | + |
| 27 | +import { describe, it, expect, vi } from 'vitest'; |
| 28 | +import { RecordPageResponseSchema } from '@objectstack/spec/api'; |
| 29 | +import { RestServer } from './rest-server.js'; |
| 30 | +// The server reaches `@objectstack/objectql` only through a dynamic import inside a |
| 31 | +// handler; paid here at module scope so the first load lands during collection, |
| 32 | +// not inside a clocked window (`pnpm check:test-source-alias`). |
| 33 | +import '@objectstack/objectql'; |
| 34 | + |
| 35 | +const ACCOUNT = { |
| 36 | + name: 'account', |
| 37 | + label: 'Account', |
| 38 | + fields: { id: { name: 'id', type: 'text' }, name: { name: 'name', type: 'text' }, stage: { name: 'stage', type: 'text' } }, |
| 39 | +}; |
| 40 | +const CTX = { userId: 'u1', permissions: ['member_default'] }; |
| 41 | +const RECORD = { id: 'a1', name: 'Acme' }; |
| 42 | + |
| 43 | +function mockServer() { |
| 44 | + const noop = () => { /* routes are driven directly, never through the adapter */ }; |
| 45 | + return { |
| 46 | + get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, |
| 47 | + listen: async () => { /* never started */ }, |
| 48 | + close: async () => { /* never started */ }, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function mockRes() { |
| 53 | + const res: any = { |
| 54 | + statusCode: 200, |
| 55 | + header: vi.fn(function (this: any) { return this; }), |
| 56 | + setHeader: vi.fn(function (this: any) { return this; }), |
| 57 | + json: vi.fn(function (this: any, body: any) { this._body = body; return this; }), |
| 58 | + status: vi.fn(function (this: any, code: number) { this.statusCode = code; return this; }), |
| 59 | + }; |
| 60 | + return res; |
| 61 | +} |
| 62 | + |
| 63 | +interface BootOpts { |
| 64 | + /** The protocol's lock read; absent leaves the protocol without one. */ |
| 65 | + getRecordLock?: (req: any) => Promise<unknown>; |
| 66 | + security?: unknown; |
| 67 | + approvals?: unknown; |
| 68 | +} |
| 69 | + |
| 70 | +function boot(opts: BootOpts = {}) { |
| 71 | + const getData = vi.fn(async () => ({ object: 'account', id: 'a1', record: RECORD })); |
| 72 | + const findData = vi.fn(async (req: any) => ({ object: req.object, records: [], total: 0 })); |
| 73 | + const getRecordLock = opts.getRecordLock ? vi.fn(opts.getRecordLock) : undefined; |
| 74 | + const protocol: any = { |
| 75 | + getDiscovery: vi.fn().mockResolvedValue({ |
| 76 | + version: 'v0', routes: { data: '', metadata: '', ui: '', auth: '/auth' }, |
| 77 | + }), |
| 78 | + getMetaTypes: vi.fn().mockResolvedValue([]), |
| 79 | + getMetaItems: vi.fn().mockResolvedValue({ items: [ACCOUNT] }), |
| 80 | + getMetaItem: vi.fn(async ({ name }: any) => (name === 'account' ? { type: 'object', name, item: ACCOUNT } : null)), |
| 81 | + getData, |
| 82 | + findData, |
| 83 | + ...(getRecordLock ? { getRecordLock } : {}), |
| 84 | + }; |
| 85 | + const rest = new RestServer(mockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 86 | + (rest as any).resolveExecCtx = async () => CTX; |
| 87 | + (rest as any).resolveSecurityService = async () => opts.security; |
| 88 | + (rest as any).approvalsServiceProvider = opts.approvals === undefined ? undefined : async () => opts.approvals; |
| 89 | + rest.registerRoutes(); |
| 90 | + const routes = (rest as any).getRoutes(); |
| 91 | + const page = routes.find((r: any) => r.method === 'POST' && r.path === '/api/v1/data/:object/:id/page'); |
| 92 | + const door = routes.find((r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object/:id/lock'); |
| 93 | + const drive = async (route: any, req: Record<string, unknown>) => { |
| 94 | + if (!route) throw new Error('route not registered'); |
| 95 | + const res = mockRes(); |
| 96 | + await route.handler({ query: {}, headers: {}, ...req } as any, res); |
| 97 | + return { status: res.statusCode, body: res.json.mock.calls.at(-1)?.[0] }; |
| 98 | + }; |
| 99 | + const callPage = (body: unknown) => drive(page, { method: 'POST', params: { object: 'account', id: 'a1' }, body }); |
| 100 | + const callDoor = () => drive(door, { method: 'GET', params: { object: 'account', id: 'a1' }, body: {} }); |
| 101 | + return { callPage, callDoor, getRecordLock, getData }; |
| 102 | +} |
| 103 | + |
| 104 | +describe('[#38995] the record page\'s dataLock part is the data lock read', () => { |
| 105 | + // What the protocol's `getRecordLock` answers in each combination ADR-0162 |
| 106 | + // D9 names. `field` / `value` / `requestId` stand for anything an engine |
| 107 | + // might add; neither door may serve them. |
| 108 | + const CASES: Array<[string, unknown]> = [ |
| 109 | + ['a record closed by its own state', { locked: true, lockSource: 'record', lockReason: 'closed_state', field: 'stage', value: 'done' }], |
| 110 | + ['a line of a closed master', { locked: true, lockSource: 'master', lockReason: 'closed_state' }], |
| 111 | + ['a record held by its own approval', { locked: true, lockSource: 'record', lockReason: 'approval', requestId: 'ar1' }], |
| 112 | + ['a line of a held master', { locked: true, lockSource: 'master', lockReason: 'approval' }], |
| 113 | + ['an unlocked record', { locked: false }], |
| 114 | + ]; |
| 115 | + |
| 116 | + for (const [name, served] of CASES) { |
| 117 | + it(`${name}: the part equals GET /data/:object/:id/lock for the same caller`, async () => { |
| 118 | + const { callPage, callDoor, getRecordLock } = boot({ getRecordLock: async () => served }); |
| 119 | + const page = await callPage({ dataLock: true }); |
| 120 | + expect(page.status).toBe(200); |
| 121 | + const composite = RecordPageResponseSchema.parse(page.body.data).dataLock; |
| 122 | + |
| 123 | + const single = await callDoor(); |
| 124 | + expect(single.status).toBe(200); |
| 125 | + |
| 126 | + expect(composite).toEqual(single.body.data); |
| 127 | + expect(Object.keys(composite as object).sort()).toEqual( |
| 128 | + (served as any).locked ? ['lockReason', 'lockSource', 'locked'] : ['locked'], |
| 129 | + ); |
| 130 | + // Both doors asked the one read, as the caller, for this record. |
| 131 | + expect(getRecordLock!.mock.calls.map(([req]) => req)).toEqual([ |
| 132 | + { object: 'account', id: 'a1', context: CTX }, |
| 133 | + { object: 'account', id: 'a1', context: CTX }, |
| 134 | + ]); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + it('a reader whose field permissions hide the state field is told closed_state, and nothing about the field', async () => { |
| 139 | + const getRecordFieldAccess = vi.fn().mockResolvedValue({ |
| 140 | + object: 'account', recordId: 'a1', readable: ['id', 'name'], editable: [], writable: false, |
| 141 | + }); |
| 142 | + const { callPage } = boot({ |
| 143 | + security: { getRecordFieldAccess }, |
| 144 | + getRecordLock: async () => ({ locked: true, lockSource: 'record', lockReason: 'closed_state', field: 'stage', value: 'done' }), |
| 145 | + }); |
| 146 | + const page = await callPage({ dataLock: true, fieldAccess: true }); |
| 147 | + expect(page.status).toBe(200); |
| 148 | + const data = RecordPageResponseSchema.parse(page.body.data); |
| 149 | + expect((data.fieldAccess as any).readable).not.toContain('stage'); |
| 150 | + expect(data.dataLock).toEqual({ locked: true, lockSource: 'record', lockReason: 'closed_state' }); |
| 151 | + expect(JSON.stringify(data.dataLock)).not.toMatch(/stage|done/); |
| 152 | + }); |
| 153 | + |
| 154 | + it('a protocol without the read is the door\'s 501, carried as the part\'s refusal, never "unlocked"', async () => { |
| 155 | + const { callPage, callDoor } = boot(); |
| 156 | + const page = await callPage({ dataLock: true }); |
| 157 | + expect(page.status).toBe(200); |
| 158 | + const data = RecordPageResponseSchema.parse(page.body.data); |
| 159 | + expect(data.dataLock).toMatchObject({ error: { code: 'NOT_IMPLEMENTED', status: 501 } }); |
| 160 | + expect(data.record).toEqual(RECORD); |
| 161 | + const door = await callDoor(); |
| 162 | + expect([door.status, door.body?.error?.code]).toEqual([501, 'NOT_IMPLEMENTED']); |
| 163 | + }); |
| 164 | + |
| 165 | + it('a read that answers nothing (undefined) is the same 501', async () => { |
| 166 | + const { callPage } = boot({ getRecordLock: async () => undefined }); |
| 167 | + const data = RecordPageResponseSchema.parse((await callPage({ dataLock: true })).body.data); |
| 168 | + expect(data.dataLock).toMatchObject({ error: { code: 'NOT_IMPLEMENTED', status: 501 } }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('a read that refuses carries its own refusal, and the other parts answer', async () => { |
| 172 | + const getRecordFieldAccess = vi.fn().mockResolvedValue({ |
| 173 | + object: 'account', recordId: 'a1', readable: ['id', 'name', 'stage'], editable: ['name'], writable: true, |
| 174 | + }); |
| 175 | + const { callPage } = boot({ |
| 176 | + security: { getRecordFieldAccess }, |
| 177 | + getRecordLock: async () => { |
| 178 | + throw Object.assign(new Error('[Security] Access denied: read on account'), { code: 'PERMISSION_DENIED', status: 403 }); |
| 179 | + }, |
| 180 | + }); |
| 181 | + const page = await callPage({ dataLock: true, fieldAccess: true }); |
| 182 | + expect(page.status).toBe(200); |
| 183 | + const data = RecordPageResponseSchema.parse(page.body.data); |
| 184 | + expect(data.dataLock).toMatchObject({ error: { code: 'PERMISSION_DENIED', status: 403 } }); |
| 185 | + expect((data.fieldAccess as any).readable).toContain('stage'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('a record the read calls unreadable (null) is the by-id read\'s 404, as a part refusal', async () => { |
| 189 | + const { callPage } = boot({ getRecordLock: async () => null }); |
| 190 | + const data = RecordPageResponseSchema.parse((await callPage({ dataLock: true })).body.data); |
| 191 | + expect(data.dataLock).toMatchObject({ error: { code: 'RECORD_NOT_FOUND', status: 404 } }); |
| 192 | + }); |
| 193 | + |
| 194 | + it('the approval part keeps its own lock: a closed record the approvals read calls unlocked stays unlocked there', async () => { |
| 195 | + const approvals = { |
| 196 | + listRequests: vi.fn().mockResolvedValue([]), |
| 197 | + getRecordLock: vi.fn().mockResolvedValue({ locked: false }), |
| 198 | + }; |
| 199 | + const { callPage } = boot({ |
| 200 | + approvals, |
| 201 | + getRecordLock: async () => ({ locked: true, lockSource: 'record', lockReason: 'closed_state' }), |
| 202 | + }); |
| 203 | + const data = RecordPageResponseSchema.parse((await callPage({ dataLock: true, approvals: true })).body.data); |
| 204 | + expect(data.approvals).toEqual({ requests: [], lock: { locked: false } }); |
| 205 | + expect(data.dataLock).toEqual({ locked: true, lockSource: 'record', lockReason: 'closed_state' }); |
| 206 | + }); |
| 207 | + |
| 208 | + it('a body that does not ask for the part gets none, and the read is not asked', async () => { |
| 209 | + const { callPage, getRecordLock } = boot({ getRecordLock: async () => ({ locked: false }) }); |
| 210 | + const page = await callPage({}); |
| 211 | + expect(page.status).toBe(200); |
| 212 | + expect(page.body.data).not.toHaveProperty('dataLock'); |
| 213 | + expect(getRecordLock).not.toHaveBeenCalled(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('a dataLock that is not a boolean is refused by the strict body, before anything is read', async () => { |
| 217 | + const { callPage, getData, getRecordLock } = boot({ getRecordLock: async () => ({ locked: false }) }); |
| 218 | + const page = await callPage({ dataLock: 'yes' }); |
| 219 | + expect(page.status).toBe(400); |
| 220 | + expect(page.body.error.code).toBe('VALIDATION_FAILED'); |
| 221 | + expect(getData).not.toHaveBeenCalled(); |
| 222 | + expect(getRecordLock).not.toHaveBeenCalled(); |
| 223 | + }); |
| 224 | +}); |
0 commit comments