|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #16095 — which authoring commands can SEE a hook authored as an inline |
| 5 | + * `handler` function, measured over the real CLI, one door per leg, with a |
| 6 | + * control beside every leg. |
| 7 | + * |
| 8 | + * The family (`hook-api-update-readonly-*`, `hook-body-*`) opens on |
| 9 | + * `body.language === 'js'`. Whether a handler-authored hook reaches it is a |
| 10 | + * property of the DOOR — what each command hands the rule registry — not of |
| 11 | + * the rule, so a claim measured through one command says nothing about the |
| 12 | + * others (#16109's lesson, applied here). The doors: |
| 13 | + * |
| 14 | + * `os build` lowers inline handlers to a metadata body BEFORE the parse |
| 15 | + * (`lowerCallables`) and judges the lowered stack — the family |
| 16 | + * reached handler-authored hooks here all along. |
| 17 | + * `os lint` used to judge the un-lowered normalized stack; since #16095 |
| 18 | + * `lintConfig` hands the registry's `parsed` tier the same |
| 19 | + * lowered view `os build` judges. This file's RED leg. |
| 20 | + * `os validate` parses the normalized stack WITHOUT lowering, so a |
| 21 | + * handler-authored hook carries no body there and the family |
| 22 | + * does not fire. Recorded below as a MEASUREMENT of that door, |
| 23 | + * not as a contract: an author who runs `os validate` alone is |
| 24 | + * not told. Closing it changes what `os validate` refuses and |
| 25 | + * is its own decision (see the card's report). |
| 26 | + * |
| 27 | + * The fixture is the card's own: a readonly `is_escalated` written through |
| 28 | + * `ctx.api.object('crm_case').update(…)` from an `afterUpdate` hook — the write |
| 29 | + * the engine strips on a non-system context while the call reports success. |
| 30 | + * The control authors the identical statement as an explicit `body`, which |
| 31 | + * fired on every door before this change. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 35 | +import { execFile } from 'node:child_process'; |
| 36 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 37 | +import { tmpdir } from 'node:os'; |
| 38 | +import { join, resolve } from 'node:path'; |
| 39 | +import { fileURLToPath } from 'node:url'; |
| 40 | +import { childEnv } from './helpers/serve-process.js'; |
| 41 | + |
| 42 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 43 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 44 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 45 | + |
| 46 | +const READONLY_RULE = 'hook-api-update-readonly-field'; |
| 47 | + |
| 48 | +interface Run { |
| 49 | + code: number; |
| 50 | + stdout: string; |
| 51 | + stderr: string; |
| 52 | +} |
| 53 | + |
| 54 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 55 | + return new Promise((resolvePromise) => { |
| 56 | + execFile( |
| 57 | + TSX, |
| 58 | + [CLI, ...args], |
| 59 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 60 | + (err, stdout, stderr) => { |
| 61 | + resolvePromise({ |
| 62 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 63 | + stdout: String(stdout), |
| 64 | + stderr: String(stderr), |
| 65 | + }); |
| 66 | + }, |
| 67 | + ); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +const OBJECT = `{ |
| 72 | + name: 'crm_case', |
| 73 | + label: 'Case', |
| 74 | + sharingModel: 'private', |
| 75 | + fields: { |
| 76 | + title: { type: 'text', label: 'Title' }, |
| 77 | + is_escalated: { type: 'boolean', label: 'Escalated', readonly: true }, |
| 78 | + }, |
| 79 | + }`; |
| 80 | + |
| 81 | +/** INTAKE: the reference app's shape — an inline handler, no `body`. */ |
| 82 | +const CONFIG_HANDLER = ` |
| 83 | +export default { |
| 84 | + manifest: { id: 'com.example.reach_handler', name: 'reach_handler', version: '1.0.0', type: 'app' }, |
| 85 | + objects: [${OBJECT}], |
| 86 | + hooks: [{ |
| 87 | + name: 'escalate', |
| 88 | + object: 'crm_case', |
| 89 | + events: ['afterUpdate'], |
| 90 | + handler: async (ctx: any) => { |
| 91 | + await ctx.api.object('crm_case').update({ id: ctx.input.id, is_escalated: true }); |
| 92 | + }, |
| 93 | + }], |
| 94 | +}; |
| 95 | +`; |
| 96 | + |
| 97 | +/** CONTROL: the identical statement authored as an explicit `body`. */ |
| 98 | +const CONFIG_BODY = ` |
| 99 | +export default { |
| 100 | + manifest: { id: 'com.example.reach_body', name: 'reach_body', version: '1.0.0', type: 'app' }, |
| 101 | + objects: [${OBJECT}], |
| 102 | + hooks: [{ |
| 103 | + name: 'escalate', |
| 104 | + object: 'crm_case', |
| 105 | + events: ['afterUpdate'], |
| 106 | + body: { |
| 107 | + language: 'js', |
| 108 | + source: "await ctx.api.object('crm_case').update({ id: ctx.input.id, is_escalated: true });", |
| 109 | + }, |
| 110 | + }], |
| 111 | +}; |
| 112 | +`; |
| 113 | + |
| 114 | +const dirs: Record<string, string> = {}; |
| 115 | + |
| 116 | +function project(key: string, source: string): string { |
| 117 | + const dir = mkdtempSync(join(tmpdir(), `os-reach-${key}-`)); |
| 118 | + writeFileSync(join(dir, 'objectstack.config.ts'), source); |
| 119 | + dirs[key] = dir; |
| 120 | + return dir; |
| 121 | +} |
| 122 | + |
| 123 | +beforeAll(() => { |
| 124 | + project('handler', CONFIG_HANDLER); |
| 125 | + project('body', CONFIG_BODY); |
| 126 | +}); |
| 127 | + |
| 128 | +afterAll(() => { |
| 129 | + for (const dir of Object.values(dirs)) rmSync(dir, { recursive: true, force: true }); |
| 130 | +}); |
| 131 | + |
| 132 | +/** Rule ids named anywhere in a `--json` payload's issue/finding lists. */ |
| 133 | +function rulesIn(run: Run): string[] { |
| 134 | + const json = JSON.parse(run.stdout); |
| 135 | + const lists: unknown[] = [json.issues, json.errors, json.warnings, json.findings].filter(Array.isArray); |
| 136 | + const ids: string[] = []; |
| 137 | + for (const list of lists) { |
| 138 | + for (const entry of list as Array<Record<string, unknown>>) { |
| 139 | + if (typeof entry?.rule === 'string') ids.push(entry.rule); |
| 140 | + } |
| 141 | + } |
| 142 | + return ids; |
| 143 | +} |
| 144 | + |
| 145 | +const label = (run: Run) => `exit ${run.code}\nstdout:\n${run.stdout}\nstderr:\n${run.stderr}`; |
| 146 | + |
| 147 | +describe('#16095 — door: `os lint`', () => { |
| 148 | + it('INTAKE — a handler-authored hook writing a readonly field is refused (error, exit 1)', async () => { |
| 149 | + const run = await runCli(['lint', 'objectstack.config.ts', '--json'], dirs.handler); |
| 150 | + expect(run.code, label(run)).toBe(1); |
| 151 | + expect(rulesIn(run)).toContain(READONLY_RULE); |
| 152 | + }, 60_000); |
| 153 | + |
| 154 | + it('CONTROL — the same statement as an explicit body is refused identically', async () => { |
| 155 | + const run = await runCli(['lint', 'objectstack.config.ts', '--json'], dirs.body); |
| 156 | + expect(run.code, label(run)).toBe(1); |
| 157 | + expect(rulesIn(run)).toContain(READONLY_RULE); |
| 158 | + }, 60_000); |
| 159 | +}); |
| 160 | + |
| 161 | +describe('#16095 — door: `os build` (the door that never had the gap)', () => { |
| 162 | + it('INTAKE — the lowered handler is refused at build, exit 1 — unchanged by this card', async () => { |
| 163 | + const run = await runCli(['build', 'objectstack.config.ts', '--json'], dirs.handler); |
| 164 | + expect(run.code, label(run)).toBe(1); |
| 165 | + expect(rulesIn(run)).toContain(READONLY_RULE); |
| 166 | + }, 90_000); |
| 167 | + |
| 168 | + it('CONTROL — the explicit body is refused at build identically', async () => { |
| 169 | + const run = await runCli(['build', 'objectstack.config.ts', '--json'], dirs.body); |
| 170 | + expect(run.code, label(run)).toBe(1); |
| 171 | + expect(rulesIn(run)).toContain(READONLY_RULE); |
| 172 | + }, 90_000); |
| 173 | +}); |
| 174 | + |
| 175 | +describe('#16095 — door: `os validate` (measured, NOT lowered)', () => { |
| 176 | + // A reading of the door as it stands, so a change to it is a change someone |
| 177 | + // chose: `os validate` parses the normalized stack without lowering, and the |
| 178 | + // handler-authored hook carries no body there. If this leg starts failing |
| 179 | + // because `os validate` began lowering, the intake row becomes the control |
| 180 | + // row — update the ledger in the file header, do not delete the pin. |
| 181 | + it('INTAKE — the handler-authored hook is NOT seen by the family here (exit 0, no finding)', async () => { |
| 182 | + const run = await runCli(['validate', 'objectstack.config.ts', '--json'], dirs.handler); |
| 183 | + expect(run.code, label(run)).toBe(0); |
| 184 | + expect(rulesIn(run)).not.toContain(READONLY_RULE); |
| 185 | + }, 60_000); |
| 186 | + |
| 187 | + it('CONTROL — the explicit body IS refused here, so the silence above is the door, not the rule', async () => { |
| 188 | + const run = await runCli(['validate', 'objectstack.config.ts', '--json'], dirs.body); |
| 189 | + expect(run.code, label(run)).toBe(1); |
| 190 | + expect(rulesIn(run)).toContain(READONLY_RULE); |
| 191 | + }, 60_000); |
| 192 | +}); |
0 commit comments