1616 * `200 { data: [] }` while a `lockRecord: true` approval runs. The page took
1717 * that for "no approval is running" and offered inline edit, and the save
1818 * answered `409 RECORD_LOCKED`. The page now asks the record's own lock
19- * (`client.approvals.getRecordLock`, answered to every reader) when the list
20- * finds no pending request.
19+ * (`GET /approvals/records/:object/:recordId/lock`, answered to every reader)
20+ * when the list finds no pending request.
21+ *
22+ * A composition without the approvals service answers that read 501, its
23+ * designed "no lock service": the page reads it silently and does not ask
24+ * again, while any other failure still logs (objectstack#26632).
2125 *
2226 * Same probe as `RecordDetailView.approvalLock-12220`: it prints what the page
2327 * threads into its `<InlineEditProvider>`. The record carries no
@@ -126,6 +130,7 @@ const pendingRequest = (over: Record<string, unknown> = {}) => ({
126130} ) ;
127131
128132let listGets : string [ ] ;
133+ let lockGets : string [ ] ;
129134
130135const jsonResponse = ( status : number , body : unknown ) =>
131136 new Response ( JSON . stringify ( body ) , { status, headers : { 'content-type' : 'application/json' } } ) ;
@@ -137,12 +142,31 @@ const jsonResponse = (status: number, body: unknown) =>
137142 */
138143type ListAnswer = { row : Record < string , unknown > | null } | { absent : number } ;
139144
140- function stubApprovalsApi ( answer : ListAnswer ) {
145+ /**
146+ * What the record-lock route answers: `{ locked }` in the declared envelope,
147+ * or a refusal carrying the route's status (501 when the approvals service is
148+ * absent, the body `respond501` sends).
149+ */
150+ type LockAnswer = { locked : boolean } | { refused : number } ;
151+
152+ const LOCK_PATH = `/approvals/records/${ OBJECT_NAME } /${ RECORD_ID } /lock` ;
153+
154+ function stubApprovalsApi ( answer : ListAnswer , lock : LockAnswer ) {
141155 listGets = [ ] ;
156+ lockGets = [ ] ;
142157 vi . stubGlobal (
143158 'fetch' ,
144159 vi . fn ( async ( url : string ) => {
145160 const u = String ( url ) ;
161+ if ( u . endsWith ( LOCK_PATH ) ) {
162+ lockGets . push ( u ) ;
163+ if ( 'refused' in lock ) {
164+ return lock . refused === 501
165+ ? jsonResponse ( 501 , { code : 'NOT_IMPLEMENTED' , message : 'Approvals service is not configured on this deployment' } )
166+ : jsonResponse ( lock . refused , { success : false , error : { code : 'INTERNAL_ERROR' , message : 'boom' } } ) ;
167+ }
168+ return jsonResponse ( 200 , { success : true , data : { locked : lock . locked } } ) ;
169+ }
146170 if ( u . includes ( '/approvals/requests?object=' ) ) {
147171 listGets . push ( u ) ;
148172 if ( 'absent' in answer ) return jsonResponse ( answer . absent , { code : 'NOT_FOUND' } ) ;
@@ -181,24 +205,20 @@ function makeDataSource() {
181205}
182206
183207/**
184- * The record-lock read as the SDK answers it: `{ locked }`, or a refusal
185- * carrying the route's status (501 when the approvals service is absent ).
208+ * The SDK's own record-lock read. The page must not use it: the client logs
209+ * every non-2xx as an error before the page sees the answer (objectstack#26632 ).
186210 */
187- type LockAnswer = { locked : boolean } | { refused : number } ;
188211let getRecordLock : ReturnType < typeof vi . fn > ;
189212
190- function makeAdapter ( answer : LockAnswer ) {
191- getRecordLock = vi . fn ( async ( ) => {
192- if ( 'refused' in answer ) throw Object . assign ( new Error ( `HTTP ${ answer . refused } ` ) , { status : answer . refused } ) ;
193- return { locked : answer . locked } ;
194- } ) ;
213+ function makeAdapter ( ) {
214+ getRecordLock = vi . fn ( async ( ) => ( { locked : false } ) ) ;
195215 return { getClient : ( ) => ( { approvals : { getRecordLock } } ) } as any ;
196216}
197217
198- function renderRecordPage ( lock : LockAnswer ) {
218+ function renderRecordPage ( ) {
199219 return render (
200220 < MemoryRouter initialEntries = { [ `/app/crm/${ OBJECT_NAME } /${ RECORD_ID } ` ] } >
201- < AdapterCtx . Provider value = { makeAdapter ( lock ) } >
221+ < AdapterCtx . Provider value = { makeAdapter ( ) } >
202222 < MetadataCtx . Provider value = { METADATA } >
203223 < RecordDetailView
204224 dataSource = { makeDataSource ( ) }
@@ -216,24 +236,32 @@ function renderRecordPage(lock: LockAnswer) {
216236
217237const attr = ( probe : HTMLElement , name : string ) => probe . getAttribute ( `data-${ name } ` ) ;
218238
239+ let consoleError : ReturnType < typeof vi . spyOn > ;
240+
219241beforeEach ( ( ) => {
220242 cleanup ( ) ;
221243 __resetRecordApprovalsStore ( ) ;
222244 authFetchSpy . mockClear ( ) ;
245+ consoleError = vi . spyOn ( console , 'error' ) ;
223246} ) ;
224247
225248afterEach ( ( ) => {
226249 vi . unstubAllGlobals ( ) ;
250+ consoleError . mockRestore ( ) ;
227251} ) ;
228252
253+ /** The `console.error` lines that name the record-lock read. */
254+ const lockErrorLines = ( ) =>
255+ consoleError . mock . calls . filter ( ( args ) => args . some ( ( a ) => String ( a ) . includes ( LOCK_PATH ) ) ) ;
256+
229257describe ( 'record page lock for a reader outside the approval (objectstack#25934)' , ( ) => {
230258 it ( 'a non-participant on a record held by a locking approval is offered no inline edit' , async ( ) => {
231- stubApprovalsApi ( { row : null } ) ;
232- renderRecordPage ( { locked : true } ) ;
259+ stubApprovalsApi ( { row : null } , { locked : true } ) ;
260+ renderRecordPage ( ) ;
233261 const probe = await screen . findByTestId ( 'inline-probe' ) ;
234262
235263 await waitFor ( ( ) => expect ( attr ( probe , 'locked' ) ) . toBe ( 'true' ) ) ;
236- expect ( getRecordLock ) . toHaveBeenCalledWith ( OBJECT_NAME , RECORD_ID ) ;
264+ expect ( lockGets ) . toHaveLength ( 1 ) ;
237265 expect ( attr ( probe , 'approval-read-answered' ) ) . toBe ( 'true' ) ;
238266 expect ( attr ( probe , 'approval-pending' ) ) . toBe ( 'true' ) ;
239267 expect ( attr ( probe , 'can-edit' ) ) . toBe ( 'false' ) ;
@@ -242,37 +270,72 @@ describe('record page lock for a reader outside the approval (objectstack#25934)
242270 } ) ;
243271
244272 it ( 'a record the server reports unlocked opens editable' , async ( ) => {
245- stubApprovalsApi ( { row : null } ) ;
246- renderRecordPage ( { locked : false } ) ;
273+ stubApprovalsApi ( { row : null } , { locked : false } ) ;
274+ renderRecordPage ( ) ;
247275 const probe = await screen . findByTestId ( 'inline-probe' ) ;
248276
249- await waitFor ( ( ) => expect ( getRecordLock ) . toHaveBeenCalled ( ) ) ;
277+ await waitFor ( ( ) => expect ( lockGets ) . toHaveLength ( 1 ) ) ;
250278 await waitFor ( ( ) => expect ( attr ( probe , 'approval-read-answered' ) ) . toBe ( 'true' ) ) ;
251279 expect ( attr ( probe , 'locked' ) ) . toBe ( 'false' ) ;
252280 expect ( attr ( probe , 'approval-pending' ) ) . toBe ( 'false' ) ;
253281 await waitFor ( ( ) => expect ( attr ( probe , 'can-edit' ) ) . toBe ( 'true' ) ) ;
254282 } ) ;
255283
256284 it ( 'without the approvals service (lock read 501) nothing changes: an answered empty read is not locked' , async ( ) => {
257- stubApprovalsApi ( { row : null } ) ;
258- renderRecordPage ( { refused : 501 } ) ;
285+ stubApprovalsApi ( { row : null } , { refused : 501 } ) ;
286+ renderRecordPage ( ) ;
259287 const probe = await screen . findByTestId ( 'inline-probe' ) ;
260288
261- await waitFor ( ( ) => expect ( getRecordLock ) . toHaveBeenCalled ( ) ) ;
289+ await waitFor ( ( ) => expect ( lockGets ) . toHaveLength ( 1 ) ) ;
262290 await waitFor ( ( ) => expect ( attr ( probe , 'approval-read-answered' ) ) . toBe ( 'true' ) ) ;
263291 expect ( attr ( probe , 'locked' ) ) . toBe ( 'false' ) ;
264292 expect ( attr ( probe , 'approval-pending' ) ) . toBe ( 'false' ) ;
265293 await waitFor ( ( ) => expect ( attr ( probe , 'can-edit' ) ) . toBe ( 'true' ) ) ;
266294 } ) ;
267295
296+ it ( 'the 501 of a composition without the approvals service logs nothing, and the lock is not read again (objectstack#26632)' , async ( ) => {
297+ stubApprovalsApi ( { row : null } , { refused : 501 } ) ;
298+ renderRecordPage ( ) ;
299+ const probe = await screen . findByTestId ( 'inline-probe' ) ;
300+ await waitFor ( ( ) => expect ( attr ( probe , 'approval-read-answered' ) ) . toBe ( 'true' ) ) ;
301+ await waitFor ( ( ) => expect ( lockGets ) . toHaveLength ( 1 ) ) ;
302+ expect ( lockErrorLines ( ) ) . toEqual ( [ ] ) ;
303+ expect ( getRecordLock ) . not . toHaveBeenCalled ( ) ;
304+
305+ // Open the record again: the list is read again, the lock is not.
306+ cleanup ( ) ;
307+ renderRecordPage ( ) ;
308+ await waitFor ( ( ) => expect ( listGets . length ) . toBeGreaterThanOrEqual ( 2 ) ) ;
309+ const again = await screen . findByTestId ( 'inline-probe' ) ;
310+ await waitFor ( ( ) => expect ( attr ( again , 'approval-read-answered' ) ) . toBe ( 'true' ) ) ;
311+ expect ( attr ( again , 'locked' ) ) . toBe ( 'false' ) ;
312+ expect ( lockGets ) . toHaveLength ( 1 ) ;
313+ expect ( lockErrorLines ( ) ) . toEqual ( [ ] ) ;
314+ } ) ;
315+
316+ it ( 'any other lock-read failure still logs, and the next open asks again' , async ( ) => {
317+ stubApprovalsApi ( { row : null } , { refused : 500 } ) ;
318+ renderRecordPage ( ) ;
319+ const probe = await screen . findByTestId ( 'inline-probe' ) ;
320+ await waitFor ( ( ) => expect ( attr ( probe , 'approval-read-answered' ) ) . toBe ( 'true' ) ) ;
321+ await waitFor ( ( ) => expect ( lockErrorLines ( ) ) . toHaveLength ( 1 ) ) ;
322+ expect ( String ( lockErrorLines ( ) [ 0 ] [ 0 ] ) ) . toContain ( `GET ` ) ;
323+ expect ( String ( lockErrorLines ( ) [ 0 ] [ 0 ] ) ) . toContain ( '-> 500' ) ;
324+ expect ( attr ( probe , 'locked' ) ) . toBe ( 'false' ) ;
325+
326+ cleanup ( ) ;
327+ renderRecordPage ( ) ;
328+ await waitFor ( ( ) => expect ( lockGets ) . toHaveLength ( 2 ) ) ;
329+ } ) ;
330+
268331 it ( 'a participant\'s pending request decides, and the record lock is not read' , async ( ) => {
269- stubApprovalsApi ( { row : pendingRequest ( { lock_record : false } ) } ) ;
270- renderRecordPage ( { locked : true } ) ;
332+ stubApprovalsApi ( { row : pendingRequest ( { lock_record : false } ) } , { locked : true } ) ;
333+ renderRecordPage ( ) ;
271334 const probe = await screen . findByTestId ( 'inline-probe' ) ;
272335
273336 await waitFor ( ( ) => expect ( attr ( probe , 'approval-pending' ) ) . toBe ( 'true' ) ) ;
274337 expect ( attr ( probe , 'locked' ) ) . toBe ( 'false' ) ;
275338 await waitFor ( ( ) => expect ( attr ( probe , 'can-edit' ) ) . toBe ( 'true' ) ) ;
276- expect ( getRecordLock ) . not . toHaveBeenCalled ( ) ;
339+ expect ( lockGets ) . toEqual ( [ ] ) ;
277340 } ) ;
278341} ) ;
0 commit comments