|
| 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 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { isSubmitterOf, type ApprovalRequestLite } from '../useRecordApprovals'; |
| 11 | + |
| 12 | +/** |
| 13 | + * `isSubmitterOf` — the ONE derivation of "did this viewer submit this |
| 14 | + * approval" (objectui#6464). |
| 15 | + * |
| 16 | + * Two surfaces gate on the answer: the approvals panel's Remind button and the |
| 17 | + * record band's Recall button. Both are server-authorized on submitter |
| 18 | + * identity, so two client-side copies of the derivation would be two |
| 19 | + * definitions of who submitted — the drift `utils/approverIdentity` already |
| 20 | + * exists to prevent on the display side. |
| 21 | + * |
| 22 | + * What the cells below actually protect is the SOURCE ORDER. The server's |
| 23 | + * `viewer.is_submitter` (framework#3310) is the resolution the endpoint will |
| 24 | + * enforce; the id comparison exists only for backends that predate the `viewer` |
| 25 | + * block. Joining them with `||` instead of `??` would look identical on every |
| 26 | + * agreeing row and quietly overturn the server on the one row where it says no. |
| 27 | + */ |
| 28 | + |
| 29 | +const row = (over: Partial<ApprovalRequestLite> = {}): ApprovalRequestLite => ({ |
| 30 | + id: 'req_1', |
| 31 | + process_name: 'flow:budget_review', |
| 32 | + object_name: 'budget', |
| 33 | + record_id: 'B1', |
| 34 | + status: 'pending', |
| 35 | + submitter_id: 'u_alice', |
| 36 | + ...over, |
| 37 | +}); |
| 38 | + |
| 39 | +describe('isSubmitterOf — server-resolved first (objectui#6464)', () => { |
| 40 | + it('believes the server when it says the viewer IS the submitter', () => { |
| 41 | + expect(isSubmitterOf(row({ viewer: { can_act: false, is_submitter: true } }), 'u_bob')).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('believes the server when it says the viewer is NOT the submitter', () => { |
| 45 | + expect(isSubmitterOf(row({ viewer: { can_act: false, is_submitter: false } }), 'u_bob')).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + /** |
| 49 | + * The cell that separates `??` from `||`. The server resolved `false` while |
| 50 | + * the raw `submitter_id` matches the signed-in id — a real shape whenever the |
| 51 | + * server's answer accounts for something the bare column does not (a |
| 52 | + * delegated or system-rewritten submission). `||` would re-litigate the |
| 53 | + * server's refusal client-side and return `true`, lighting a lever the recall |
| 54 | + * endpoint then rejects: the very defect this gate exists to close. |
| 55 | + */ |
| 56 | + it('does not let a matching submitter_id overturn a server `false`', () => { |
| 57 | + expect( |
| 58 | + isSubmitterOf(row({ submitter_id: 'u_alice', viewer: { can_act: false, is_submitter: false } }), 'u_alice'), |
| 59 | + ).toBe(false); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('isSubmitterOf — the id fallback for pre-`viewer` backends', () => { |
| 64 | + it('matches the submitter by id when the server sends no viewer block', () => { |
| 65 | + expect(isSubmitterOf(row({ submitter_id: 'u_alice' }), 'u_alice')).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it('rejects a non-submitter by id when the server sends no viewer block', () => { |
| 69 | + expect(isSubmitterOf(row({ submitter_id: 'u_alice' }), 'u_bob')).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + /** |
| 73 | + * Fallback-also-absent: an older server AND no signed-in id to compare |
| 74 | + * against. This resolves to a definite `false`, NOT to "unknown" — the |
| 75 | + * authoritative row is in hand and nothing in it identifies this viewer as |
| 76 | + * the submitter. It is also the same answer the Remind gate has always given |
| 77 | + * in this shape, which is the point of there being one derivation. |
| 78 | + */ |
| 79 | + it('resolves to false when there is no id to compare against', () => { |
| 80 | + expect(isSubmitterOf(row({ submitter_id: 'u_alice' }), undefined)).toBe(false); |
| 81 | + expect(isSubmitterOf(row({ submitter_id: 'u_alice' }), null)).toBe(false); |
| 82 | + expect(isSubmitterOf(row({ submitter_id: 'u_alice' }), '')).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it('resolves to false when the row itself names no submitter', () => { |
| 86 | + // Both sides absent must not collapse into `undefined === undefined`. |
| 87 | + expect(isSubmitterOf(row({ submitter_id: null }), undefined)).toBe(false); |
| 88 | + expect(isSubmitterOf(row({ submitter_id: undefined }), undefined)).toBe(false); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('isSubmitterOf — "unknown" is its own answer', () => { |
| 93 | + /** |
| 94 | + * No request to consult is NOT a denial. Callers feeding a feedback gate keep |
| 95 | + * their prior behaviour on `undefined`; collapsing it to `false` here would |
| 96 | + * hide the affordance on every backend that exposes no approvals API at all. |
| 97 | + * Asserted with `toBeUndefined`, since `toBeFalsy` passes for both. |
| 98 | + */ |
| 99 | + it('returns undefined — not false — with no request', () => { |
| 100 | + expect(isSubmitterOf(null, 'u_alice')).toBeUndefined(); |
| 101 | + expect(isSubmitterOf(undefined, 'u_alice')).toBeUndefined(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments