|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25609] An approval step's `excludeSubmitter` option, default off — the |
| 5 | + * protocol half of #25573 (ruling 6377731715 on #25496, letter B for #25446). |
| 6 | + * ADR-0019 made each approval STEP an approval NODE, so the step-level option |
| 7 | + * is a key on `ApprovalNodeConfigSchema`. |
| 8 | + * |
| 9 | + * ## What is pinned, and why each half |
| 10 | + * |
| 11 | + * - §1 THE KEY PARSES on the node config, `true` and `false`, the whole config |
| 12 | + * green beside a `field` and a `position` approver (the two types the ruling |
| 13 | + * names; the key is one switch for the whole slate, never per type). Omitted, |
| 14 | + * it stays ABSENT after the parse — no `false` materializes — so a node that |
| 15 | + * does not author it parses to the output it parsed to before the key |
| 16 | + * existed. A non-boolean is refused at the key's own path. |
| 17 | + * - §2 THE DOOR AUTHORS MEET: `FlowSchema` judges an approval node's `config` |
| 18 | + * against this contract whole, so the key is accepted inside a flow too — |
| 19 | + * the parse `os validate`, `compile` and the metadata save door run. |
| 20 | + * - §3 STEP-LEVEL ONLY: the same key written anywhere but the node config — on |
| 21 | + * an approver row, on the flow node beside `config`, on the flow — is still |
| 22 | + * refused, each at its own path. These are the controls that prove the |
| 23 | + * widening opened one coordinate and nothing else. |
| 24 | + * - §4 THE DESIGNER'S SCHEMA: the published JSON schema carries the key as a |
| 25 | + * plain optional boolean with no `default`, under the same |
| 26 | + * `additionalProperties: false`. |
| 27 | + * - §5 THE STATUS: the describe opens with the `[PLANNED` marker (the runtime |
| 28 | + * half has not landed, so the key is declared and not yet read) and names |
| 29 | + * the two subjects an author needs — `submitter_id` (who is removed) and |
| 30 | + * `onEmptyApprovers` (where an emptied slate goes). The prose itself is not |
| 31 | + * pinned. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, expect, it } from 'vitest'; |
| 35 | +import type { z } from 'zod'; |
| 36 | + |
| 37 | +import { |
| 38 | + ApprovalNodeApproverSchema, |
| 39 | + ApprovalNodeConfigSchema, |
| 40 | + getApprovalNodeConfigJsonSchema, |
| 41 | +} from './approval.zod'; |
| 42 | +import { FlowSchema } from './flow.zod'; |
| 43 | + |
| 44 | +type Door = { safeParse: (v: unknown) => z.ZodSafeParseResult<unknown> }; |
| 45 | + |
| 46 | +/** The issue codes and paths a refusal carries, so a refusal for the WRONG reason reds. */ |
| 47 | +function issues(door: Door, value: unknown): { code: string; path: string }[] { |
| 48 | + const result = door.safeParse(value); |
| 49 | + if (result.success) return []; |
| 50 | + return result.error.issues.map((i) => ({ code: i.code, path: i.path.join('.') })); |
| 51 | +} |
| 52 | + |
| 53 | +/** A category manager on the record, then a procurement director — the card's two-level shape. */ |
| 54 | +const SLATE = [ |
| 55 | + { type: 'field', value: 'category_manager_id' }, |
| 56 | + { type: 'position', value: 'procurement_director' }, |
| 57 | +]; |
| 58 | +const BASE = { approvers: SLATE }; |
| 59 | + |
| 60 | +/** start → the approval node → approve / reject ends; `node` and `top` spread where a key is planted. */ |
| 61 | +function flowWith(node: Record<string, unknown>, top: Record<string, unknown> = {}) { |
| 62 | + return { |
| 63 | + name: 'supplier_admission', |
| 64 | + label: 'Supplier admission', |
| 65 | + type: 'autolaunched', |
| 66 | + ...top, |
| 67 | + nodes: [ |
| 68 | + { id: 'start', type: 'start', label: 'Start' }, |
| 69 | + { id: 'gate', type: 'approval', label: 'Category manager', ...node }, |
| 70 | + { id: 'approved', type: 'end', label: 'Approved' }, |
| 71 | + { id: 'rejected', type: 'end', label: 'Rejected' }, |
| 72 | + ], |
| 73 | + edges: [ |
| 74 | + { id: 'e1', source: 'start', target: 'gate' }, |
| 75 | + { id: 'e2', source: 'gate', target: 'approved', label: 'approve' }, |
| 76 | + { id: 'e3', source: 'gate', target: 'rejected', label: 'reject' }, |
| 77 | + ], |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +// ─────────────────────────────────────────────────────────────────────────── |
| 82 | +// §1 the key parses on the node config |
| 83 | +// ─────────────────────────────────────────────────────────────────────────── |
| 84 | + |
| 85 | +describe('§1 excludeSubmitter parses on the approval node config, absent when off', () => { |
| 86 | + it.each([true, false])('excludeSubmitter: %s parses, whole config green, and is carried through', (value) => { |
| 87 | + const result = ApprovalNodeConfigSchema.safeParse({ ...BASE, excludeSubmitter: value }); |
| 88 | + expect(issues(ApprovalNodeConfigSchema, { ...BASE, excludeSubmitter: value })).toEqual([]); |
| 89 | + expect(result.success && result.data.excludeSubmitter).toBe(value); |
| 90 | + }); |
| 91 | + |
| 92 | + it('parses beside the empty-slate fallback it hands an emptied slate to', () => { |
| 93 | + const config = { |
| 94 | + ...BASE, |
| 95 | + excludeSubmitter: true, |
| 96 | + onEmptyApprovers: 'fallback', |
| 97 | + fallbackApprovers: [{ type: 'org_membership_level', value: 'owner' }], |
| 98 | + }; |
| 99 | + expect(issues(ApprovalNodeConfigSchema, config)).toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('omitted, stays absent after the parse: no default materializes', () => { |
| 103 | + const parsed = ApprovalNodeConfigSchema.parse(BASE); |
| 104 | + expect(Object.prototype.hasOwnProperty.call(parsed, 'excludeSubmitter')).toBe(false); |
| 105 | + }); |
| 106 | + |
| 107 | + it.each(['yes', 1, null])('refuses the non-boolean %j at the key\'s own path', (value) => { |
| 108 | + expect(issues(ApprovalNodeConfigSchema, { ...BASE, excludeSubmitter: value })).toEqual([ |
| 109 | + { code: 'invalid_type', path: 'excludeSubmitter' }, |
| 110 | + ]); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +// ─────────────────────────────────────────────────────────────────────────── |
| 115 | +// §2 the flow door |
| 116 | +// ─────────────────────────────────────────────────────────────────────────── |
| 117 | + |
| 118 | +describe('§2 FlowSchema accepts it inside an approval node config', () => { |
| 119 | + it('a flow whose approval node sets excludeSubmitter: true parses whole', () => { |
| 120 | + expect(issues(FlowSchema, flowWith({ config: { ...BASE, excludeSubmitter: true } }))).toEqual([]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('control: the same flow without the key parses too', () => { |
| 124 | + expect(issues(FlowSchema, flowWith({ config: BASE }))).toEqual([]); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +// ─────────────────────────────────────────────────────────────────────────── |
| 129 | +// §3 step-level only — every other coordinate still refuses it |
| 130 | +// ─────────────────────────────────────────────────────────────────────────── |
| 131 | + |
| 132 | +describe('§3 the key is step-level only: written anywhere else it is refused', () => { |
| 133 | + it('on an approver row: the closed approver shape refuses it', () => { |
| 134 | + expect(issues(ApprovalNodeApproverSchema, { ...SLATE[0], excludeSubmitter: true })).toEqual([ |
| 135 | + { code: 'unrecognized_keys', path: '' }, |
| 136 | + ]); |
| 137 | + }); |
| 138 | + |
| 139 | + it('on an approver row inside a flow: refused at that row\'s path', () => { |
| 140 | + const config = { approvers: [{ ...SLATE[0], excludeSubmitter: true }, SLATE[1]] }; |
| 141 | + expect(issues(FlowSchema, flowWith({ config }))).toEqual([ |
| 142 | + { code: 'custom', path: 'nodes.1.config.approvers.0.excludeSubmitter' }, |
| 143 | + ]); |
| 144 | + }); |
| 145 | + |
| 146 | + it('on the flow node beside config: refused at the node', () => { |
| 147 | + expect(issues(FlowSchema, flowWith({ config: BASE, excludeSubmitter: true }))).toEqual([ |
| 148 | + { code: 'unrecognized_keys', path: 'nodes.1' }, |
| 149 | + ]); |
| 150 | + }); |
| 151 | + |
| 152 | + it('on the flow itself (no flow-level default was ruled): refused at the root', () => { |
| 153 | + expect(issues(FlowSchema, flowWith({ config: BASE }, { excludeSubmitter: true }))).toEqual([ |
| 154 | + { code: 'unrecognized_keys', path: '' }, |
| 155 | + ]); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +// ─────────────────────────────────────────────────────────────────────────── |
| 160 | +// §4 the JSON schema the designer and registerFlow() read |
| 161 | +// ─────────────────────────────────────────────────────────────────────────── |
| 162 | + |
| 163 | +describe('§4 the published JSON schema carries it as an optional boolean with no default', () => { |
| 164 | + it('is a plain boolean property, not required, with no default, and the shape stays closed', () => { |
| 165 | + const js = getApprovalNodeConfigJsonSchema() as { |
| 166 | + properties: Record<string, Record<string, unknown>>; |
| 167 | + required?: string[]; |
| 168 | + additionalProperties?: unknown; |
| 169 | + }; |
| 170 | + expect(js.properties.excludeSubmitter?.type).toBe('boolean'); |
| 171 | + expect(js.properties.excludeSubmitter).not.toHaveProperty('default'); |
| 172 | + expect(js.required ?? []).not.toContain('excludeSubmitter'); |
| 173 | + expect(js.additionalProperties).toBe(false); |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +// ─────────────────────────────────────────────────────────────────────────── |
| 178 | +// §5 the status the describe declares |
| 179 | +// ─────────────────────────────────────────────────────────────────────────── |
| 180 | + |
| 181 | +describe('§5 the describe declares the key planned and names its two subjects', () => { |
| 182 | + // Read inside each test, never at collection: a key that went missing then |
| 183 | + // reds these two tests instead of erroring the file and hiding §1–§4. |
| 184 | + const doc = (): string => { |
| 185 | + const member = (ApprovalNodeConfigSchema.shape as Record<string, { description?: string } | undefined>) |
| 186 | + .excludeSubmitter; |
| 187 | + return member?.description ?? ''; |
| 188 | + }; |
| 189 | + |
| 190 | + it('opens with the planned marker: declared, not yet read by the approval engine', () => { |
| 191 | + expect(doc().startsWith('[PLANNED')).toBe(true); |
| 192 | + }); |
| 193 | + |
| 194 | + it('names who is removed (submitter_id) and where an emptied slate goes (onEmptyApprovers)', () => { |
| 195 | + expect(doc()).toContain('`submitter_id`'); |
| 196 | + expect(doc()).toContain('`onEmptyApprovers`'); |
| 197 | + }); |
| 198 | +}); |
0 commit comments