|
22 | 22 | * - [#28470] a related list's `expand` is its read's own `$expand`, and each |
23 | 23 | * stream part — the discussion (the thread, then its reactions), the |
24 | 24 | * activity and the attachments — is the `findData` the console's own read of |
25 | | - * it makes, behind that object's exposure gate, refused on its own. |
| 25 | + * it makes, behind that object's exposure gate, refused on its own; |
| 26 | + * - [#28737] the timeline part is a count of each source the body names, by |
| 27 | + * that source's own read as the caller, summed as the spec's timeline table |
| 28 | + * spells it. |
26 | 29 | * |
27 | 30 | * The end-to-end half, under row-level and field-level security as a member who |
28 | 31 | * is not an admin, is `record-page.dogfood.test.ts`. |
29 | 32 | */ |
30 | 33 |
|
31 | 34 | import { describe, it, expect, vi } from 'vitest'; |
32 | | -import { RecordPageResponseSchema } from '@objectstack/spec/api'; |
| 35 | +import { RECORD_TIMELINE_UNDRAWN_ACTIVITY_TYPES, RecordPageResponseSchema } from '@objectstack/spec/api'; |
33 | 36 | import { RestServer } from './rest-server.js'; |
34 | 37 | import { RECORD_PAGE_PARAMS } from './record-page-routes.js'; |
35 | 38 | import { unknownQueryParamMessage } from './query-allowlist.js'; |
@@ -522,3 +525,141 @@ describe('[#28470] the record\'s streams and the related rows\' names ride in th |
522 | 525 | expect(findData).not.toHaveBeenCalled(); |
523 | 526 | }); |
524 | 527 | }); |
| 528 | + |
| 529 | +describe('[#28737] the timeline part is the 动态 stream\'s size, counted by the caller\'s own reads', () => { |
| 530 | + const items = { ...ITEMS, sys_audit_log: systemObject('sys_audit_log') }; |
| 531 | + // Each count read answers its own total, so a sum that took one source's |
| 532 | + // count for another's would show. |
| 533 | + const countOf = (req: any) => { |
| 534 | + const where = req.query?.where ?? {}; |
| 535 | + if (req.object === 'sys_comment') return { records: [], total: 3 }; |
| 536 | + if (req.object === 'sys_audit_log') return { records: [], total: 40 }; |
| 537 | + if (req.object === 'sys_activity') return { records: [], total: where.type === 'updated' ? 30 : 250 }; |
| 538 | + return { records: [], total: 0 }; |
| 539 | + }; |
| 540 | + const ofRecord = { object_name: 'account', record_id: 'a1' }; |
| 541 | + |
| 542 | + it('counts each named source as the caller, and the updates step aside once a field change is counted', async () => { |
| 543 | + const { call, findData } = boot({ items, findData: async (req) => countOf(req) }); |
| 544 | + const answer = await call({ timeline: { discussion: true, activity: true, fieldChanges: true } }); |
| 545 | + expect(answer.status).toBe(200); |
| 546 | + const data = RecordPageResponseSchema.parse(answer.body.data); |
| 547 | + // 3 comments + 40 field changes + 250 drawn activity rows − their 30 updates. |
| 548 | + expect(data.timeline).toEqual({ total: 263 }); |
| 549 | + const reads = findData.mock.calls.map(([req]) => req); |
| 550 | + expect(reads).toEqual(expect.arrayContaining([ |
| 551 | + { object: 'sys_comment', query: { object: 'sys_comment', where: { thread_id: 'account:a1' }, limit: 0, count: true }, context: CTX }, |
| 552 | + { |
| 553 | + object: 'sys_audit_log', |
| 554 | + query: { object: 'sys_audit_log', where: { ...ofRecord, action: { $in: ['create', 'update', 'delete'] } }, limit: 0, count: true }, |
| 555 | + context: CTX, |
| 556 | + }, |
| 557 | + { |
| 558 | + object: 'sys_activity', |
| 559 | + query: { |
| 560 | + object: 'sys_activity', |
| 561 | + where: { ...ofRecord, type: { $nin: [...RECORD_TIMELINE_UNDRAWN_ACTIVITY_TYPES] } }, |
| 562 | + limit: 0, |
| 563 | + count: true, |
| 564 | + }, |
| 565 | + context: CTX, |
| 566 | + }, |
| 567 | + { |
| 568 | + object: 'sys_activity', |
| 569 | + query: { object: 'sys_activity', where: { ...ofRecord, type: 'updated' }, limit: 0, count: true }, |
| 570 | + context: CTX, |
| 571 | + }, |
| 572 | + ])); |
| 573 | + expect(reads).toHaveLength(4); |
| 574 | + }); |
| 575 | + |
| 576 | + it('counts only the sources named, and no update steps aside without a field change', async () => { |
| 577 | + const { call, findData } = boot({ items, findData: async (req) => countOf(req) }); |
| 578 | + const activityOnly = RecordPageResponseSchema.parse((await call({ timeline: { discussion: false, activity: true, fieldChanges: false } })).body.data); |
| 579 | + expect(activityOnly.timeline).toEqual({ total: 250 }); |
| 580 | + expect(findData.mock.calls.map(([req]) => req.object)).toEqual(['sys_activity']); |
| 581 | + |
| 582 | + const noChanges = boot({ |
| 583 | + items, |
| 584 | + findData: async (req) => (req.object === 'sys_audit_log' ? { records: [], total: 0 } : countOf(req)), |
| 585 | + }); |
| 586 | + const data = RecordPageResponseSchema.parse((await noChanges.call({ timeline: { discussion: true, activity: true, fieldChanges: true } })).body.data); |
| 587 | + expect(data.timeline).toEqual({ total: 253 }); |
| 588 | + }); |
| 589 | + |
| 590 | + it('a record with no entry counts 0', async () => { |
| 591 | + const { call } = boot({ items, findData: async () => ({ records: [], total: 0 }) }); |
| 592 | + const data = RecordPageResponseSchema.parse((await call({ timeline: { discussion: true, activity: true, fieldChanges: true } })).body.data); |
| 593 | + expect(data.timeline).toEqual({ total: 0 }); |
| 594 | + }); |
| 595 | + |
| 596 | + it('a refused comment or activity count refuses the part; the record and the other parts answer', async () => { |
| 597 | + for (const refused of ['sys_comment', 'sys_activity']) { |
| 598 | + const { call } = boot({ |
| 599 | + items, |
| 600 | + findData: async (req) => { |
| 601 | + if (req.object === refused) throw permissionDenied(); |
| 602 | + return countOf(req); |
| 603 | + }, |
| 604 | + }); |
| 605 | + const answer = await call({ attachments: { top: 5 }, timeline: { discussion: true, activity: true, fieldChanges: true } }); |
| 606 | + expect(answer.status, refused).toBe(200); |
| 607 | + const data = RecordPageResponseSchema.parse(answer.body.data); |
| 608 | + expect(data.timeline, refused).toEqual({ error: { code: 'PERMISSION_DENIED', status: 403, message: expect.any(String) } }); |
| 609 | + expect(data.record).toEqual(RECORD); |
| 610 | + expect(data.attachments).toEqual({ total: 0, records: [] }); |
| 611 | + } |
| 612 | + }); |
| 613 | + |
| 614 | + it('a refused ledger count is no field change, so the updates stay in the count', async () => { |
| 615 | + const { call } = boot({ |
| 616 | + items, |
| 617 | + findData: async (req) => { |
| 618 | + if (req.object === 'sys_audit_log') throw permissionDenied(); |
| 619 | + return countOf(req); |
| 620 | + }, |
| 621 | + }); |
| 622 | + const data = RecordPageResponseSchema.parse((await call({ timeline: { discussion: true, activity: true, fieldChanges: true } })).body.data); |
| 623 | + expect(data.timeline).toEqual({ total: 253 }); |
| 624 | + }); |
| 625 | + |
| 626 | + it('a source this deployment does not serve holds no entry', async () => { |
| 627 | + const withoutLedger = { ...items }; |
| 628 | + delete (withoutLedger as Record<string, unknown>).sys_audit_log; |
| 629 | + delete (withoutLedger as Record<string, unknown>).sys_comment; |
| 630 | + const { call } = boot({ |
| 631 | + items: withoutLedger, |
| 632 | + findData: async (req) => { |
| 633 | + if (req.object === 'sys_audit_log' || req.object === 'sys_comment') throw objectNotFound(req.object); |
| 634 | + return countOf(req); |
| 635 | + }, |
| 636 | + }); |
| 637 | + const data = RecordPageResponseSchema.parse((await call({ timeline: { discussion: true, activity: true, fieldChanges: true } })).body.data); |
| 638 | + expect(data.timeline).toEqual({ total: 250 }); |
| 639 | + }); |
| 640 | + |
| 641 | + it('a source on an object the API does not expose is refused as its list read is, and never read', async () => { |
| 642 | + const hidden = { ...items, sys_activity: { ...items.sys_activity, enable: { apiEnabled: false } } }; |
| 643 | + const { call, findData } = boot({ items: hidden, findData: async (req) => countOf(req) }); |
| 644 | + const data = RecordPageResponseSchema.parse((await call({ timeline: { discussion: false, activity: true, fieldChanges: false } })).body.data); |
| 645 | + expect(data.timeline).toEqual({ error: expect.objectContaining({ code: 'OBJECT_API_DISABLED', status: 404 }) }); |
| 646 | + expect(findData).not.toHaveBeenCalled(); |
| 647 | + }); |
| 648 | + |
| 649 | + it('the timeline body is strict: a missing or non-boolean source, or an undeclared key, is a 400 and nothing is read', async () => { |
| 650 | + const { call, getData, findData } = boot({ items }); |
| 651 | + for (const body of [ |
| 652 | + { timeline: true }, |
| 653 | + { timeline: {} }, |
| 654 | + { timeline: { discussion: true, activity: true } }, |
| 655 | + { timeline: { discussion: 'yes', activity: true, fieldChanges: true } }, |
| 656 | + { timeline: { discussion: true, activity: true, fieldChanges: true, top: 5 } }, |
| 657 | + ]) { |
| 658 | + const answer = await call(body); |
| 659 | + expect(answer.status, JSON.stringify(body)).toBe(400); |
| 660 | + expect(answer.body.error.code).toBe('VALIDATION_FAILED'); |
| 661 | + } |
| 662 | + expect(getData).not.toHaveBeenCalled(); |
| 663 | + expect(findData).not.toHaveBeenCalled(); |
| 664 | + }); |
| 665 | +}); |
0 commit comments