|
27 | 27 | */ |
28 | 28 | import { beforeEach, describe, expect, it } from 'vitest'; |
29 | 29 | import { ObjectQL } from './engine.js'; |
| 30 | +import { resolveRecordTitle, resolveServedRecordTitle, servedRecordTitle } from './record-title.js'; |
30 | 31 |
|
31 | 32 | type Rec = Record<string, unknown>; |
32 | 33 |
|
@@ -66,13 +67,13 @@ function makeDriver() { |
66 | 67 | for (const f of fields) if (r[f] !== undefined) out[f] = r[f]; |
67 | 68 | return out; |
68 | 69 | }; |
69 | | - const calls: Array<{ object: string; ast: any }> = []; |
| 70 | + const calls: Array<{ object: string; ast: any; options?: any }> = []; |
70 | 71 | let n = 0; |
71 | 72 | const driver: any = { |
72 | 73 | name: 'memory', version: '0.0.0', supports: {}, |
73 | 74 | async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
74 | | - async find(object: string, ast: any) { |
75 | | - calls.push({ object, ast }); |
| 75 | + async find(object: string, ast: any, options?: any) { |
| 76 | + calls.push({ object, ast, options }); |
76 | 77 | const rows = Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where)); |
77 | 78 | // Hold the caller's page: `offset` then `limit`, as a real driver does. |
78 | 79 | const offset = typeof ast?.offset === 'number' ? ast.offset : 0; |
@@ -339,4 +340,84 @@ describe('#24197 — a formula field reads one hop through a reference', () => { |
339 | 340 | engine.find('lib_loan', { orderBy: [{ field: 'title', order: 'asc' }], context: READER }), |
340 | 341 | ).rejects.toMatchObject({ code: 'INVALID_SORT', status: 400, field: 'title' }); |
341 | 342 | }); |
| 343 | + |
| 344 | + // [#24197] The record a hook fires on was never read back, so nothing served |
| 345 | + // its title: `serveFormulaField` is the read path's evaluation over a record |
| 346 | + // in hand, reached through a channel (`ScopedContext`) that carries the |
| 347 | + // reader's own context. The hook body's `ctx.title()` is built on it. |
| 348 | + describe('serveFormulaField — a record in hand, served as a read serves it', () => { |
| 349 | + const LOAN_SCHEMA = { name: 'lib_loan', nameField: 'title' }; |
| 350 | + |
| 351 | + it('reads the hop through the IN-HAND foreign keys, one batched read per reference field', async () => { |
| 352 | + d.calls.length = 0; |
| 353 | + // Stored l1 names b1; the in-flight state names b2. |
| 354 | + const value = await engine.createContext(READER).serveFormulaField('lib_loan', { id: 'l1', book: 'b2', reader: 'r1' }, 'title'); |
| 355 | + expect(value).toBe('Emma · Ann'); |
| 356 | + expect(relatedReads('lib_book')).toBe(1); |
| 357 | + expect(relatedReads('lib_reader')).toBe(1); |
| 358 | + expect(relatedReads('lib_loan')).toBe(0); |
| 359 | + expect(d.calls.find((c) => c.object === 'lib_book')!.ast.where).toEqual({ id: { $in: ['b2'] } }); |
| 360 | + }); |
| 361 | + |
| 362 | + it('answers the title a read of the same record answers, where the synchronous evaluator answers none', async () => { |
| 363 | + const read = await engine.findOne('lib_loan', { where: { id: 'l3' }, context: READER }); |
| 364 | + const inHand = { id: 'l3', book: 'b3', reader: 'r2' }; |
| 365 | + expect(await resolveServedRecordTitle(LOAN_SCHEMA, inHand, engine.createContext(READER))).toBe(read?.title); |
| 366 | + expect(read?.title).toBe('Ulysses · Bo'); |
| 367 | + // The before-state of this seam: no hop, no title. |
| 368 | + expect(resolveRecordTitle({ ...LOAN_SCHEMA, fields: engine.getObject('lib_loan')!.fields }, inHand)).toBeUndefined(); |
| 369 | + }); |
| 370 | + |
| 371 | + it('a formula over the record\'s own columns reads nothing', async () => { |
| 372 | + d.calls.length = 0; |
| 373 | + expect(await engine.createContext(READER).serveFormulaField('lib_book', { id: 'b1', title: 'dune' }, 'shout')).toBe('DUNE'); |
| 374 | + expect(d.calls).toEqual([]); |
| 375 | + }); |
| 376 | + |
| 377 | + it('a field that is not a formula answers undefined, so the stored column is read instead', async () => { |
| 378 | + expect(await engine.createContext(READER).serveFormulaField('lib_loan', { id: 'l1', book: 'b1' }, 'book')).toBeUndefined(); |
| 379 | + }); |
| 380 | + |
| 381 | + it('reads the hop on the transaction the channel carries', async () => { |
| 382 | + const trx = { __trx: 'body' }; |
| 383 | + d.calls.length = 0; |
| 384 | + await engine.createContext({ ...READER, transaction: trx }).serveFormulaField('lib_loan', { id: 'l1', book: 'b1', reader: 'r1' }, 'title'); |
| 385 | + const hops = d.calls.filter((c) => c.object === 'lib_book' || c.object === 'lib_reader'); |
| 386 | + expect(hops).toHaveLength(2); |
| 387 | + for (const c of hops) expect(c.options?.transaction).toBe(trx); |
| 388 | + }); |
| 389 | + |
| 390 | + it('a related record the channel may not read leaves it null with the read\'s reason, and leaks nothing', async () => { |
| 391 | + engine.registerMiddleware(async (ctx: any, next: () => Promise<void>) => { |
| 392 | + if (ctx.object === 'lib_reader' && ['find', 'findOne'].includes(ctx.operation) && !ctx.context?.isSystem) { |
| 393 | + const scoped = { visible: true }; |
| 394 | + ctx.ast = { ...(ctx.ast ?? {}), where: ctx.ast?.where ? { $and: [ctx.ast.where, scoped] } : scoped }; |
| 395 | + } |
| 396 | + await next(); |
| 397 | + }); |
| 398 | + const inHand = { id: 'lx', book: 'b1', reader: 'r_hidden' }; |
| 399 | + expect(await engine.createContext(READER).serveFormulaField('lib_loan', inHand, 'title')).toBeNull(); |
| 400 | + expect(await resolveServedRecordTitle(LOAN_SCHEMA, inHand, engine.createContext(READER))).toBeUndefined(); |
| 401 | + expect(faultsOn('lib_loan', 'title')[0].message).toContain('was not found by this read'); |
| 402 | + // CONTROL — the SYSTEM channel reads the reader. |
| 403 | + expect(await engine.createContext({ isSystem: true }).serveFormulaField('lib_loan', inHand, 'title')).toBe('Dune · Hidden Person'); |
| 404 | + }); |
| 405 | + |
| 406 | + it('a second hop reached through the related object\'s formula is not served', async () => { |
| 407 | + registerLoan('record.book.shelf_code'); |
| 408 | + d.calls.length = 0; |
| 409 | + expect(await engine.createContext(READER).serveFormulaField('lib_loan', { id: 'l1', book: 'b1' }, 'title')).toBeNull(); |
| 410 | + expect(relatedReads('lib_shelf')).toBe(0); |
| 411 | + }); |
| 412 | + |
| 413 | + it('servedRecordTitle reads the title a read served off the row, never re-derived', async () => { |
| 414 | + const book = { name: 'lib_book', nameField: 'shelf_code', fields: engine.getObject('lib_book')!.fields }; |
| 415 | + // A row a read returned with its one-hop title served… |
| 416 | + expect(servedRecordTitle(book, { id: 'b1', title: 'Dune', shelf: 's1', shelf_code: 'A-1' })).toBe('A-1'); |
| 417 | + // …which the synchronous re-derivation throws away. |
| 418 | + expect(resolveRecordTitle(book, { id: 'b1', title: 'Dune', shelf: 's1', shelf_code: 'A-1' })).toBeUndefined(); |
| 419 | + // A row the read served no title on has none. |
| 420 | + expect(servedRecordTitle(book, { id: 'b1', title: 'Dune', shelf: 's1', shelf_code: null })).toBeUndefined(); |
| 421 | + }); |
| 422 | + }); |
342 | 423 | }); |
0 commit comments