Skip to content

Commit 4172ad6

Browse files
committed
fix(approvals): refuse the publish with the documented 400, and make the gate test doubles drivable
The publish refusal carries VALIDATION_FAILED at 400, the status the error ledger documents for that code. The add-authoring-gate test loads @objectstack/lint at module top and its find double honours a caller's limit. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5nzgcVrZ8Neqqd1AFi
1 parent 4b91cd2 commit 4172ad6

5 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎.changeset/37766-publish-gate-approvals.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ At kernel:ready the plugin adds publish gates for `flow` and `object` beside the
1414
- **A flow.** It is judged as an approval's root, together with every flow that calls it through a `subflow` or a `map`, to any depth, with the written body in place. The walk up the callers ends on a flow that calls itself and on flows that call each other. It stops after 64 roots: past that, nothing is refused for the cap, and one warning says so.
1515
- **An object.** Every flow that can start on one of its records, or maps over them, is judged with the written object in place. Only gaps on the written object are reported.
1616

17-
The refusal is a 422 `VALIDATION_FAILED` that names the item, then gives one sentence per gap: the flow, the step, the object, the field and what is missing, in the first locale the write door states. These are the same sentences an approval's first request is refused with.
17+
The refusal is a 400 `VALIDATION_FAILED` that names the item, then gives one sentence per gap: the flow, the step, the object, the field and what is missing, in the first locale the write door states. These are the same sentences an approval's first request is refused with.
1818

1919
Not refused at publish:
2020
- a draft (it is judged when it is promoted);

‎packages/metadata-protocol/src/protocol.add-authoring-gate-37766.test.ts‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414

1515
import { describe, expect, it, vi } from 'vitest';
16+
// Pay the lazy `@objectstack/lint` load during collection, not inside a clocked case.
17+
import '@objectstack/lint';
1618
import {
1719
assertEngineDeleteDispatch,
1820
assertEngineUpdateDispatch,
@@ -103,15 +105,11 @@ function makeStubEngine() {
103105
assertEngineFindOnePredicate(table, opts);
104106
return findRow(table, opts.where)?.row ?? null;
105107
},
106-
async find(table: string, opts: { where: Record<string, unknown> }) {
107-
return Array.from(tableOf(table).values()).filter((r) => {
108-
if (opts.where.type && !whereValueMatches(r.type, opts.where.type)) return false;
109-
if (opts.where.name !== undefined && r.name !== opts.where.name) return false;
110-
if (opts.where.organization_id !== undefined
111-
&& r.organization_id !== opts.where.organization_id) return false;
112-
if (opts.where.state && r.state !== opts.where.state) return false;
113-
return true;
114-
});
108+
async find(table: string, opts: { where: Record<string, unknown>; limit?: number }) {
109+
// Every stated key of the `where` is matched (`$in` or equality), then the bound.
110+
const matched = Array.from(tableOf(table).values()).filter((r) =>
111+
Object.entries(opts.where).every(([k, v]) => v === undefined || whereValueMatches(r[k], v)));
112+
return typeof opts?.limit === 'number' ? matched.slice(0, opts.limit) : matched;
115113
},
116114
async insert(table: string, data: Record<string, unknown>) {
117115
nextId += 1;
@@ -225,10 +223,10 @@ describe('addAuthoringGate: a gate beside the owner gate (#37766)', () => {
225223
const { protocol } = makeProtocolOn();
226224
protocol.addAuthoringGate('object', () => {
227225
const err: any = new Error('refused');
228-
err.code = 'VALIDATION_FAILED'; err.status = 422;
226+
err.code = 'VALIDATION_FAILED'; err.status = 400;
229227
throw err;
230228
});
231-
await expect(protocol.runAuthoringGate(gateEvent())).rejects.toMatchObject({ code: 'VALIDATION_FAILED', status: 422 });
229+
await expect(protocol.runAuthoringGate(gateEvent())).rejects.toMatchObject({ code: 'VALIDATION_FAILED', status: 400 });
232230
});
233231

234232
it('a throw of the owner gate stops the write before any added gate runs', async () => {
@@ -268,11 +266,11 @@ describe('addAuthoringGate through the save and publish doors (#37766)', () => {
268266
const { protocol, rows } = makeProtocolOn();
269267
protocol.addAuthoringGate('flow', (ctx: any) => {
270268
if (ctx.state !== 'active') return;
271-
throw Object.assign(new Error('VALIDATION_FAILED: refused'), { code: 'VALIDATION_FAILED', status: 422 });
269+
throw Object.assign(new Error('VALIDATION_FAILED: refused'), { code: 'VALIDATION_FAILED', status: 400 });
272270
});
273271
await protocol.saveMetaItem({ type: 'flow', name: 'leave_approval', item: validApprovalFlow(), mode: 'draft' });
274272
const err = await protocol.publishMetaItem({ type: 'flow', name: 'leave_approval' }).catch((e: any) => e);
275-
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', status: 422 });
273+
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', status: 400 });
276274
expect(flowRows(rows).map((r) => r.state), 'no active row').not.toContain('active');
277275
});
278276

‎packages/plugins/plugin-approvals/src/approval-publish-check.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@ describe('the publish gates', () => {
245245
expect(registerApprovalPublishGates(undefined, refusing)).toBe(false);
246246
});
247247

248-
it('refuses an active write with a 422 VALIDATION_FAILED, in en and in the first locale the door states', async () => {
248+
it('refuses an active write with a 400 VALIDATION_FAILED, in en and in the first locale the door states', async () => {
249249
const p = protocolDouble();
250250
registerApprovalPublishGates(p, refusing);
251251
const err = await p.gates.get('flow')![0]!(ctx({ userTextLocales: ['zh-CN', 'en'] })).catch((e) => e);
252252
expect(err).toBeInstanceOf(ApprovalPublishRefusedError);
253-
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', status: 422, statusCode: 422 });
253+
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', status: 400, statusCode: 400 });
254254
expect(err.message).toMatch(/^flow F was not published: /);
255255
expect(err.message).toContain('Field status of invoice (step a1 of flow F) does not declare the value pending');
256256
expect(err.userMessage).toMatch(/^flow F 没有发布:/);

‎packages/plugins/plugin-approvals/src/approval-publish-gate-37766.integration.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* conditions, reported at the real metadata door: the approvals plugin adds
66
* its gates beside each type's owner gate (`addAuthoringGate`) on
77
* kernel:ready, and a flow or an object that goes `active` where an approval
8-
* could not open is refused with a 422 `VALIDATION_FAILED` that names what is
8+
* could not open is refused with a 400 `VALIDATION_FAILED` that names what is
99
* missing. A draft is not refused; its promotion is. The read itself, arm by
1010
* arm, is pinned over fixtures in `approval-publish-check.test.ts`.
1111
*
@@ -105,7 +105,7 @@ describe('[ADR-0162 item 7, N45] the opening check reported at publish', () => {
105105
type: 'flow', name: 'invoice_wait', item: approvalFlow('invoice_wait', 'crm_invoice', true),
106106
}).catch((e: any) => e);
107107
expect(err).toBeInstanceOf(Error);
108-
expect(err.status).toBe(422);
108+
expect(err.status).toBe(400);
109109
expect(err.code).toBe('VALIDATION_FAILED');
110110
expect(err.message).toContain('flow invoice_wait was not published');
111111
expect(err.message).toContain('Node hold (wait) of flow invoice_wait can pause the run between two approval steps of crm_invoice');
@@ -117,7 +117,7 @@ describe('[ADR-0162 item 7, N45] the opening check reported at publish', () => {
117117
const err = await protocol.saveMetaItem({
118118
type: 'flow', name: 'invoice_wait', item: approvalFlow('invoice_wait', 'crm_invoice', true), userTextLocales: ['zh-CN'],
119119
}).catch((e: any) => e);
120-
expect(err.status).toBe(422);
120+
expect(err.status).toBe(400);
121121
expect(err.userMessage).toMatch(/^flow invoice_wait 没有发布:/);
122122
});
123123

@@ -140,7 +140,7 @@ describe('[ADR-0162 item 7, N45] the opening check reported at publish', () => {
140140
});
141141
expect(saved.success).toBe(true);
142142
const err = await protocol.publishMetaItem({ type: 'flow', name: 'invoice_wait' }).catch((e: any) => e);
143-
expect(err.status).toBe(422);
143+
expect(err.status).toBe(400);
144144
expect(err.code).toBe('VALIDATION_FAILED');
145145
expect((await flowRows('invoice_wait')).filter((r) => r.state === 'active')).toEqual([]);
146146
});
@@ -156,7 +156,7 @@ describe('[ADR-0162 item 7, N45] the opening check reported at publish', () => {
156156
type: 'object', name: 'crm_note', item: { ...authored, closedStates: { approval_status: ['approved', 'rejected'] } },
157157
}).catch((e: any) => e);
158158
expect(err).toBeInstanceOf(Error);
159-
expect(err.status).toBe(422);
159+
expect(err.status).toBe(400);
160160
expect(err.code).toBe('VALIDATION_FAILED');
161161
expect(err.message).toContain('object crm_note was not published');
162162
expect(err.message).toContain('Node hold (wait) of flow note_wait can pause the run between two approval steps of crm_note');

‎packages/plugins/plugin-approvals/src/approval-publish-gate.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* save and restore the owner gate runs on, and on the promotion of a draft,
1212
* which the owner gate does not run on. It judges an `active` write only.
1313
*
14-
* The refusal is a 422 `VALIDATION_FAILED`: `message` in `en`, the
14+
* The refusal is a 400 `VALIDATION_FAILED`: `message` in `en`, the
1515
* diagnostic, and `userMessage` in the first locale the write door states
1616
* (`userTextLocales`), from the plugin's own catalog. A judge that fails is
1717
* logged and refuses nothing: the report is early notice, and the first
@@ -35,8 +35,8 @@ export interface ApprovalPublishGateContext {
3535
/** The publish of a flow or an object that an approval could not open on is refused. */
3636
export class ApprovalPublishRefusedError extends Error {
3737
readonly code = 'VALIDATION_FAILED';
38-
readonly status = 422;
39-
readonly statusCode = 422;
38+
readonly status = 400;
39+
readonly statusCode = 400;
4040
/** The refusal addressed to the author, in the first locale the write door states; `message` stays in `en`. */
4141
readonly userMessage: string;
4242
readonly gaps: readonly OpeningGap[];

0 commit comments

Comments
 (0)