|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Sandboxed-hook error message format, end-to-end through the real stack: |
| 4 | +// QuickJS sandbox (SandboxError + innerMessage) → ObjectQL triggerHooks → |
| 5 | +// REST mapDataError → HTTP error body. |
| 6 | +// |
| 7 | +// A hook author writing `throw new Error('业务规则说明')` is expressing a |
| 8 | +// deliberate business rule (e.g. referential-integrity "记录被引用,删除被 |
| 9 | +// 阻断"). The console shows the REST body's `error` string verbatim in its |
| 10 | +// toast, so that string must be ONLY the author's message — not the sandbox |
| 11 | +// debug wrapper (`hook 'x' threw: Error: …`, which belongs in server logs) |
| 12 | +// and not a `code` field an older bundled @objectstack/client would prepend |
| 13 | +// as `[ObjectStack] CODE: …`. |
| 14 | +// |
| 15 | +// Non-default error names (`TypeError: …`) are deliberately KEPT: they mark |
| 16 | +// a genuine script bug rather than a thrown business rule. |
| 17 | + |
| 18 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 19 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 20 | +import { defineStack } from '@objectstack/spec'; |
| 21 | +import { ObjectSchema, Field } from '@objectstack/spec/data'; |
| 22 | + |
| 23 | +const BUSINESS_MSG = '制作基地被「项目主计划批次」引用(3 条),删除被阻断,请先解除引用'; |
| 24 | + |
| 25 | +const HefBase = ObjectSchema.create({ |
| 26 | + name: 'hef_base', |
| 27 | + label: '制作基地', |
| 28 | + fields: { |
| 29 | + name: Field.text({ label: '名称', required: true }), |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +const hefStack = defineStack({ |
| 34 | + manifest: { |
| 35 | + id: 'com.dogfood.hook_error_format', |
| 36 | + namespace: 'hef', |
| 37 | + version: '0.0.0', |
| 38 | + type: 'app', |
| 39 | + name: 'Hook Error Format Fixture', |
| 40 | + description: 'Sandboxed hooks throwing business-rule and script-bug errors.', |
| 41 | + }, |
| 42 | + objects: [HefBase], |
| 43 | + hooks: [ |
| 44 | + { |
| 45 | + // Mirrors the real-world referential-integrity guard (`pm_ref_base`) |
| 46 | + // that motivated the fix: a deliberate business rule thrown as a |
| 47 | + // default `Error`. |
| 48 | + name: 'hef_ref_guard', |
| 49 | + object: 'hef_base', |
| 50 | + events: ['beforeDelete'], |
| 51 | + body: { |
| 52 | + language: 'js', |
| 53 | + source: `throw new Error(${JSON.stringify(BUSINESS_MSG)});`, |
| 54 | + capabilities: [], |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + // A non-default error name signals a script bug, not a business rule — |
| 59 | + // the name must survive to the client as useful context. |
| 60 | + name: 'hef_buggy_guard', |
| 61 | + object: 'hef_base', |
| 62 | + events: ['beforeUpdate'], |
| 63 | + body: { |
| 64 | + language: 'js', |
| 65 | + source: `throw new TypeError('boom');`, |
| 66 | + capabilities: [], |
| 67 | + }, |
| 68 | + }, |
| 69 | + ], |
| 70 | +}); |
| 71 | + |
| 72 | +describe('objectstack verify: sandboxed hook error message format (#hef)', () => { |
| 73 | + let stack: VerifyStack; |
| 74 | + let token: string; |
| 75 | + let baseId: string; |
| 76 | + |
| 77 | + beforeAll(async () => { |
| 78 | + stack = await bootStack(hefStack); |
| 79 | + token = await stack.signIn(); |
| 80 | + |
| 81 | + const created = await stack.apiAs(token, 'POST', '/data/hef_base', { name: '华东制作基地' }); |
| 82 | + expect(created.status, `create: ${created.status} ${await created.clone().text()}`).toBeLessThan(300); |
| 83 | + const body = (await created.json()) as any; |
| 84 | + baseId = body.record?.id ?? body.id; |
| 85 | + expect(baseId).toBeTruthy(); |
| 86 | + }, 60_000); |
| 87 | + |
| 88 | + afterAll(async () => { |
| 89 | + await stack?.stop(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('DELETE blocked by a sandboxed hook returns ONLY the business message', async () => { |
| 93 | + const r = await stack.apiAs(token, 'DELETE', `/data/hef_base/${baseId}`); |
| 94 | + expect(r.status).toBe(400); |
| 95 | + |
| 96 | + const body = (await r.json()) as any; |
| 97 | + // The console toast renders this string verbatim — it must be exactly |
| 98 | + // what the hook author threw. |
| 99 | + expect(body.error).toBe(BUSINESS_MSG); |
| 100 | + // No sandbox debug wrapper, no branding, no code for old clients to prepend. |
| 101 | + expect(JSON.stringify(body)).not.toMatch(/threw:|hook '|\[ObjectStack\]/); |
| 102 | + expect(body.code).toBeUndefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('ground truth: the blocked delete did not remove the record', async () => { |
| 106 | + const r = await stack.apiAs(token, 'GET', `/data/hef_base/${baseId}`); |
| 107 | + expect(r.status).toBe(200); |
| 108 | + }); |
| 109 | + |
| 110 | + it('non-default error names (TypeError) survive as script-bug context', async () => { |
| 111 | + const r = await stack.apiAs(token, 'PATCH', `/data/hef_base/${baseId}`, { name: '改名' }); |
| 112 | + expect(r.status).toBe(400); |
| 113 | + |
| 114 | + const body = (await r.json()) as any; |
| 115 | + expect(body.error).toBe('TypeError: boom'); |
| 116 | + expect(JSON.stringify(body)).not.toMatch(/threw:|hook '/); |
| 117 | + }); |
| 118 | +}); |
0 commit comments