|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#17620] `ActionEngineFacade.delete` no longer swallows a NULLISH element. |
| 5 | + * |
| 6 | + * ## What was here, and who could reach it |
| 7 | + * |
| 8 | + * The `delete` arm of {@link buildActionEngineFacade} normalises its argument |
| 9 | + * to a list and issues one `ql.delete` per id. It used to open that loop with |
| 10 | + * `if (id != null)`, so a nullish element was **silently skipped**: nothing |
| 11 | + * refused it, nothing warned, and the call resolved as though the deletion had |
| 12 | + * happened — a silent no-op on a destructive verb, which is the one failure a |
| 13 | + * caller cannot detect. |
| 14 | + * |
| 15 | + * The declared type is `string | string[]` (#15117), so **no typed caller ever |
| 16 | + * reached the guard** — the population is UNTYPED hosts: a JS host, or a |
| 17 | + * `registerAction` handler whose slot is still `(ctx: any)`. That is also why |
| 18 | + * this file is not a correction of #15117 / PR #17608: that card's contract |
| 19 | + * sentence is true, and this arm's declared behaviour is unchanged by the |
| 20 | + * removal. |
| 21 | + * |
| 22 | + * ## Why removing the guard is enough to make it loud |
| 23 | + * |
| 24 | + * Every id now reaches `ql.delete(object, { where: { id }, context })` as |
| 25 | + * written, and the engine's own dispatch predicate answers that call: a |
| 26 | + * `where.id` that is not a TRUTHY SCALAR is neither `by-id` nor (absent |
| 27 | + * `multi`) a bulk intent, so `ObjectQL.delete` throws |
| 28 | + * {@link ENGINE_DELETE_REJECT_MESSAGE}. The refusal is the producer's, not a |
| 29 | + * second copy of it — which is why the double below opens with |
| 30 | + * {@link assertEngineDeleteDispatch} rather than a hand-rolled id check: a |
| 31 | + * double looser than the engine would keep this file green against a facade |
| 32 | + * that still swallowed the value. |
| 33 | + * |
| 34 | + * ⚠️ The pin is the exported MESSAGE CONSTANT, compared exactly. This refusal |
| 35 | + * is a plain `Error` — it carries no ADR-0112 `code`/`status` — so a bare |
| 36 | + * `toThrow()` here would stay green against any unnamed `Error` at all, which |
| 37 | + * is precisely what an unfixed arm would have to produce to be believed. |
| 38 | + * |
| 39 | + * @see packages/runtime/src/action-execution.ts — `buildActionEngineFacade`. |
| 40 | + * @see packages/spec/src/ui/action-params.test.ts — the declaration's own pin, |
| 41 | + * whose `@ts-expect-error` reads «"delete nothing" is the EMPTY ARRAY, |
| 42 | + * never a null id». |
| 43 | + */ |
| 44 | + |
| 45 | +import { describe, it, expect } from 'vitest'; |
| 46 | +import { ENGINE_DELETE_REJECT_MESSAGE, assertEngineDeleteDispatch } from '@objectstack/metadata-core'; |
| 47 | +import { buildActionEngineFacade } from './action-execution.js'; |
| 48 | + |
| 49 | +const deps: any = { resolveService: () => undefined, getObjectQL: async () => undefined }; |
| 50 | + |
| 51 | +/** |
| 52 | + * An engine double whose `delete` is bound to the REAL engine's dispatch |
| 53 | + * contract: one call to the producer's own predicate, never a mirrored `if`. |
| 54 | + * Everything it accepts, a running server accepts; everything it refuses, a |
| 55 | + * running server refuses (`scripts/check-engine-double-contract.mjs`). |
| 56 | + */ |
| 57 | +function makeEngine() { |
| 58 | + const deleted: Array<{ object: string; id: unknown; context: unknown }> = []; |
| 59 | + const ql: any = { |
| 60 | + deleted, |
| 61 | + async insert(_object: string, data: Record<string, unknown>) { |
| 62 | + return { id: (data as Record<string, unknown>)?.id ?? 'rec_new' }; |
| 63 | + }, |
| 64 | + async find(_object: string, _options?: Record<string, unknown>) { |
| 65 | + return []; |
| 66 | + }, |
| 67 | + async count(_object: string, _options?: Record<string, unknown>) { |
| 68 | + return 0; |
| 69 | + }, |
| 70 | + async delete(object: string, options?: Record<string, unknown>) { |
| 71 | + assertEngineDeleteDispatch(options); |
| 72 | + const where = (options as { where?: Record<string, unknown> } | undefined)?.where; |
| 73 | + deleted.push({ object, id: where?.id, context: (options as { context?: unknown } | undefined)?.context }); |
| 74 | + return { ok: true }; |
| 75 | + }, |
| 76 | + }; |
| 77 | + return ql; |
| 78 | +} |
| 79 | + |
| 80 | +/** Drive the arm and hand back whatever it rejected with, or `undefined`. */ |
| 81 | +async function rejection(run: Promise<unknown>): Promise<unknown> { |
| 82 | + return run.then(() => undefined, (e: unknown) => e); |
| 83 | +} |
| 84 | + |
| 85 | +describe('#17620 — ActionEngineFacade.delete refuses a nullish id', () => { |
| 86 | + it('refuses a nullish ELEMENT of the array form instead of skipping it', async () => { |
| 87 | + const ql = makeEngine(); |
| 88 | + const engine = buildActionEngineFacade(deps, ql, { userId: 'u1' }); |
| 89 | + |
| 90 | + const err = await rejection(engine.delete('crm_case', [null])); |
| 91 | + |
| 92 | + expect(err).toBeInstanceOf(Error); |
| 93 | + expect((err as Error).message).toBe(ENGINE_DELETE_REJECT_MESSAGE); |
| 94 | + // …and it is loud INSTEAD of deleting, not as well as: nothing landed. |
| 95 | + expect(ql.deleted).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('refuses a nullish SINGLE id (the non-array spelling) the same way', async () => { |
| 99 | + const ql = makeEngine(); |
| 100 | + const engine = buildActionEngineFacade(deps, ql, { userId: 'u1' }); |
| 101 | + |
| 102 | + for (const nullish of [null, undefined]) { |
| 103 | + const err = await rejection(engine.delete('crm_case', nullish)); |
| 104 | + expect(err).toBeInstanceOf(Error); |
| 105 | + expect((err as Error).message).toBe(ENGINE_DELETE_REJECT_MESSAGE); |
| 106 | + } |
| 107 | + expect(ql.deleted).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('stops AT the nullish element — ids before it are deleted, ids after it untouched', async () => { |
| 111 | + const ql = makeEngine(); |
| 112 | + const engine = buildActionEngineFacade(deps, ql, { userId: 'u1' }); |
| 113 | + |
| 114 | + const err = await rejection(engine.delete('crm_case', ['case_1', null, 'case_3'])); |
| 115 | + |
| 116 | + expect((err as Error).message).toBe(ENGINE_DELETE_REJECT_MESSAGE); |
| 117 | + // The declared partial-progress shape, unchanged: "a failure part-way |
| 118 | + // through leaves the ids before it deleted and the ids after it |
| 119 | + // untouched" (`ActionEngineFacade.delete`'s member doc). |
| 120 | + expect(ql.deleted.map((d: { id: unknown }) => d.id)).toEqual(['case_1']); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('#17620 — controls: the declared contract is untouched', () => { |
| 125 | + it('a well-formed single id still deletes', async () => { |
| 126 | + const ql = makeEngine(); |
| 127 | + const engine = buildActionEngineFacade(deps, ql, { userId: 'u1', tenantId: 'org_acme' }); |
| 128 | + |
| 129 | + await expect(engine.delete('crm_case', 'case_1')).resolves.toBeUndefined(); |
| 130 | + |
| 131 | + expect(ql.deleted).toHaveLength(1); |
| 132 | + expect(ql.deleted[0]).toMatchObject({ object: 'crm_case', id: 'case_1' }); |
| 133 | + // the elevated caller envelope still rides every call (#3914) |
| 134 | + expect(ql.deleted[0].context).toMatchObject({ isSystem: true, userId: 'u1', tenantId: 'org_acme' }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('the declared ARRAY form still deletes every id, in order, one call each', async () => { |
| 138 | + const ql = makeEngine(); |
| 139 | + const engine = buildActionEngineFacade(deps, ql, { userId: 'u1' }); |
| 140 | + |
| 141 | + await expect(engine.delete('crm_case', ['case_1', 'case_2', 'case_3'])).resolves.toBeUndefined(); |
| 142 | + |
| 143 | + expect(ql.deleted.map((d: { id: unknown }) => d.id)).toEqual(['case_1', 'case_2', 'case_3']); |
| 144 | + }); |
| 145 | + |
| 146 | + it('an empty array still deletes nothing and resolves', async () => { |
| 147 | + const ql = makeEngine(); |
| 148 | + const engine = buildActionEngineFacade(deps, ql, { userId: 'u1' }); |
| 149 | + |
| 150 | + await expect(engine.delete('crm_case', [])).resolves.toBeUndefined(); |
| 151 | + |
| 152 | + expect(ql.deleted).toEqual([]); |
| 153 | + }); |
| 154 | +}); |
0 commit comments