|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30251] `onFailedRead` on {@link IOwnershipQueueService}: the call-time |
| 5 | + * signal ADR-0142 D2 (as amended) asks each consumer to send, so a recompute |
| 6 | + * consumer reached only through this contract (the D6 approver slate in |
| 7 | + * `@objectstack/plugin-approvals`) can ask for a refusal on a failed read. |
| 8 | + * |
| 9 | + * Pinned at the type level, under `check:test-typecheck` (this file compiles |
| 10 | + * under `tsconfig.test.json`): |
| 11 | + * |
| 12 | + * 1. **A widening, not a narrowing.** A call with no options, and the |
| 13 | + * three-argument `isMember` call, compile exactly as before; so does an |
| 14 | + * implementation written before the key existed. |
| 15 | + * 2. **Both members read one value type.** `expandMembers`' and `isMember`'s |
| 16 | + * `onFailedRead` are both {@link OwnershipQueueFailedReadAnswer}, which is |
| 17 | + * exactly `'nobody' | 'refuse'`; an unknown value does not compile. |
| 18 | + * 3. **Independent of `purpose`.** The two keys sit side by side; neither |
| 19 | + * value set leaks into the other. |
| 20 | + * |
| 21 | + * ⛔ Not pinned: what any implementation DOES under `'refuse'` — that is the |
| 22 | + * producer half (#29604, `@objectstack/plugin-sharing`). |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect } from 'vitest'; |
| 26 | + |
| 27 | +import type { |
| 28 | + IOwnershipQueueService, |
| 29 | + OwnershipQueueFailedReadAnswer, |
| 30 | + QueueExpansionPurpose, |
| 31 | +} from './ownership-queue-service'; |
| 32 | + |
| 33 | +/** Type-level identity: true iff A and B are the same type. */ |
| 34 | +type Eq<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; |
| 35 | +/** Compile error when the argument is not `true`. */ |
| 36 | +type Assert<T extends true> = T; |
| 37 | + |
| 38 | +type ExpandOptions = NonNullable<Parameters<IOwnershipQueueService['expandMembers']>[2]>; |
| 39 | +type IsMemberOptions = NonNullable<Parameters<IOwnershipQueueService['isMember']>[3]>; |
| 40 | + |
| 41 | +export type _AnswerIsExactlyTwoValues = Assert<Eq<OwnershipQueueFailedReadAnswer, 'nobody' | 'refuse'>>; |
| 42 | +export type _ExpandReadsTheAnswerType = Assert<Eq<ExpandOptions['onFailedRead'], OwnershipQueueFailedReadAnswer | undefined>>; |
| 43 | +export type _IsMemberReadsTheAnswerType = Assert<Eq<IsMemberOptions['onFailedRead'], OwnershipQueueFailedReadAnswer | undefined>>; |
| 44 | +export type _PurposeUnchanged = Assert<Eq<ExpandOptions['purpose'], QueueExpansionPurpose | undefined>>; |
| 45 | +export type _ExpandOptionKeys = Assert<Eq<keyof ExpandOptions, 'purpose' | 'onFailedRead'>>; |
| 46 | +export type _IsMemberOptionKeys = Assert<Eq<keyof IsMemberOptions, 'onFailedRead'>>; |
| 47 | +/** The axes stay apart: no failed-read answer is a purpose, and no purpose is a failed-read answer. */ |
| 48 | +export type _AxesDisjoint = Assert<Eq<Extract<OwnershipQueueFailedReadAnswer, QueueExpansionPurpose>, never>>; |
| 49 | + |
| 50 | +/** An implementation written before the key existed still satisfies the contract (method bivariance on an optional trailing argument). */ |
| 51 | +const legacy: IOwnershipQueueService = { |
| 52 | + resolveQueue: async () => null, |
| 53 | + expandMembers: async (_name: string, _org: string | null, _options?: { purpose?: QueueExpansionPurpose }) => [], |
| 54 | + isMember: async (_user: string, _name: string, _org: string | null) => false, |
| 55 | + queuesOf: async () => [], |
| 56 | +}; |
| 57 | + |
| 58 | +/** Every call shape the card's acceptance names; the unknown values must NOT compile. */ |
| 59 | +async function callShapes(svc: IOwnershipQueueService): Promise<unknown[]> { |
| 60 | + return [ |
| 61 | + await svc.expandMembers('q', 'org_a'), |
| 62 | + await svc.expandMembers('q', null, {}), |
| 63 | + await svc.expandMembers('q', 'org_a', { purpose: 'addressing' }), |
| 64 | + await svc.expandMembers('q', 'org_a', { onFailedRead: 'refuse' }), |
| 65 | + await svc.expandMembers('q', 'org_a', { purpose: 'addressing', onFailedRead: 'refuse' }), |
| 66 | + await svc.expandMembers('q', 'org_a', { onFailedRead: 'nobody' }), |
| 67 | + await svc.isMember('u', 'q', 'org_a'), |
| 68 | + await svc.isMember('u', 'q', 'org_a', { onFailedRead: 'refuse' }), |
| 69 | + // @ts-expect-error — not a failed-read answer |
| 70 | + await svc.expandMembers('q', 'org_a', { onFailedRead: 'throw' }), |
| 71 | + // @ts-expect-error — not a failed-read answer |
| 72 | + await svc.isMember('u', 'q', 'org_a', { onFailedRead: 'empty' }), |
| 73 | + // @ts-expect-error — `purpose` is the other axis, and `isMember` does not take it |
| 74 | + await svc.isMember('u', 'q', 'org_a', { purpose: 'addressing' }), |
| 75 | + // @ts-expect-error — a purpose value is not a failed-read answer |
| 76 | + await svc.expandMembers('q', 'org_a', { onFailedRead: 'addressing' }), |
| 77 | + ]; |
| 78 | +} |
| 79 | + |
| 80 | +describe('IOwnershipQueueService onFailedRead (#30251)', () => { |
| 81 | + it('every call shape compiles against an implementation written before the key, and answers as it did', async () => { |
| 82 | + expect(await callShapes(legacy)).toEqual([[], [], [], [], [], [], false, false, [], false, false, []]); |
| 83 | + }); |
| 84 | +}); |
0 commit comments