|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The showcase's hook-REFUSAL fixture: `showcase_guard_task_reopen` refuses the |
| 5 | + * write that reopens a finished task, with a sentence addressed to the user, |
| 6 | + * and `showcase_reopen_task` is the script action whose write reaches it. |
| 7 | + * |
| 8 | + * Platform checklist item `records-forms.script-action-hook-refusal-toast` |
| 9 | + * drives this pair in the console (4xx on the action route, exactly one error |
| 10 | + * toast carrying the sentence, record unchanged). This file pins the two halves |
| 11 | + * the item stands on, so a run that FAILS is about the console and the action |
| 12 | + * route, never about a fixture that quietly stopped refusing: |
| 13 | + * |
| 14 | + * 1. on the real engine with the app's real hooks, the reopening write is |
| 15 | + * refused with the sentence and the stored row is unchanged — while an |
| 16 | + * edit of the same finished task that leaves `done` alone still lands; |
| 17 | + * 2. the action's body writes exactly the reopening shape (`done: false` on |
| 18 | + * its own record), so a click reaches the hook's condition. |
| 19 | + * |
| 20 | + * The action route's own 4xx for a nested sandboxed refusal is pinned in |
| 21 | + * `packages/runtime/src/sandbox/nested-hook-refusal-is-a-rejection.test.ts`; |
| 22 | + * it is not restated here. |
| 23 | + * |
| 24 | + * Harness: the production one `hook-body-persisted-writes.test.ts` uses — real |
| 25 | + * `ObjectQL`, real `SqlDriver` (better-sqlite3), real `QuickJSScriptRunner` |
| 26 | + * behind `hookBodyRunnerFactory`, the app's real objects and hooks. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 30 | +import { ObjectQL } from '@objectstack/objectql'; |
| 31 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 32 | +import { |
| 33 | + QuickJSScriptRunner, |
| 34 | + actionBodyRunnerFactory, |
| 35 | + hookBodyRunnerFactory, |
| 36 | +} from '@objectstack/runtime'; |
| 37 | + |
| 38 | +import { Account, Project, Task } from '../src/data/objects/index.js'; |
| 39 | +import { allHooks, TASK_REOPEN_REFUSAL } from '../src/data/hooks/index.js'; |
| 40 | +import { ReopenTaskAction } from '../src/ui/actions/index.js'; |
| 41 | + |
| 42 | +const APP_ID = 'com.objectstack.showcase'; |
| 43 | +const PACKAGE_ID = `app:${APP_ID}`; |
| 44 | + |
| 45 | +const openEngines: ObjectQL[] = []; |
| 46 | +afterEach(async () => { |
| 47 | + while (openEngines.length) { |
| 48 | + try { await openEngines.pop()?.destroy(); } catch { /* noop */ } |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +async function bootShowcase(hooks: unknown[] = allHooks): Promise<ObjectQL> { |
| 53 | + const driver = new SqlDriver({ |
| 54 | + client: 'better-sqlite3', |
| 55 | + connection: { filename: ':memory:' }, |
| 56 | + useNullAsDefault: true, |
| 57 | + }); |
| 58 | + await driver.connect(); |
| 59 | + |
| 60 | + const engine = new ObjectQL(); |
| 61 | + openEngines.push(engine); |
| 62 | + engine.registerDriver(driver as never, true); |
| 63 | + await engine.init(); |
| 64 | + for (const def of [Account, Project, Task]) { |
| 65 | + engine.registry.registerObject(def as never, PACKAGE_ID, 'showcase'); |
| 66 | + } |
| 67 | + await engine.syncSchemas(); |
| 68 | + engine.bindHooks(hooks as never[], { |
| 69 | + packageId: PACKAGE_ID, |
| 70 | + bodyRunner: hookBodyRunnerFactory(new QuickJSScriptRunner(), { ql: engine, appId: APP_ID }), |
| 71 | + }); |
| 72 | + return engine; |
| 73 | +} |
| 74 | + |
| 75 | +const ctx = { context: { userId: 'u_showcase', isSystem: true } }; |
| 76 | + |
| 77 | +const readBack = async (engine: ObjectQL, id: string) => |
| 78 | + (await engine.find('showcase_task', { where: { id } }, ctx as never))[0] as any; |
| 79 | + |
| 80 | +/** A finished task on a real project chain: done = true, progress = 100. */ |
| 81 | +async function finishedTask(engine: ObjectQL): Promise<string> { |
| 82 | + const account: any = await engine.insert('showcase_account', { name: 'Initech', status: 'active' }, ctx as never); |
| 83 | + const project: any = await engine.insert( |
| 84 | + 'showcase_project', |
| 85 | + { name: 'Platform', account: String(account.id), status: 'planned' }, |
| 86 | + ctx as never, |
| 87 | + ); |
| 88 | + const task: any = await engine.insert( |
| 89 | + 'showcase_task', |
| 90 | + { title: 'Audit current IA', project: String(project.id), status: 'backlog' }, |
| 91 | + ctx as never, |
| 92 | + ); |
| 93 | + const id = String(task.id); |
| 94 | + await engine.update('showcase_task', { id, done: true, progress: 100 }, ctx as never); |
| 95 | + expect((await readBack(engine, id)).done).toBeTruthy(); |
| 96 | + return id; |
| 97 | +} |
| 98 | + |
| 99 | +/** Every string an error carries that a client could be shown. */ |
| 100 | +function textOf(err: any): string { |
| 101 | + return [err?.message, err?.innerMessage, err?.cause?.message].filter(Boolean).join(' | '); |
| 102 | +} |
| 103 | + |
| 104 | +describe('showcase_guard_task_reopen — the hook refuses reopening a finished task', () => { |
| 105 | + it('the reopening write is refused with the sentence, and the stored row is unchanged', async () => { |
| 106 | + const engine = await bootShowcase(); |
| 107 | + const id = await finishedTask(engine); |
| 108 | + |
| 109 | + const err = await engine |
| 110 | + .update('showcase_task', { id, done: false }, ctx as never) |
| 111 | + .then(() => null, (e: unknown) => e); |
| 112 | + |
| 113 | + expect(err, 'expected the reopening write to be refused, but it resolved').not.toBeNull(); |
| 114 | + expect(textOf(err)).toContain(TASK_REOPEN_REFUSAL); |
| 115 | + // The refusal is a business sentence, not a script fault: no native |
| 116 | + // error-class name leads it (`TypeError: …` is the fault shape #7543 keeps |
| 117 | + // off the wire). |
| 118 | + expect(String((err as any).innerMessage ?? '')).not.toMatch(/^(TypeError|ReferenceError|SyntaxError):/); |
| 119 | + |
| 120 | + const stored = await readBack(engine, id); |
| 121 | + expect(stored.done).toBeTruthy(); |
| 122 | + expect(stored.progress).toBe(100); |
| 123 | + }, 30000); |
| 124 | + |
| 125 | + it('an edit of the finished task that leaves `done` alone still lands', async () => { |
| 126 | + // The two-root condition: `previous.done == true && record.done != true`. |
| 127 | + // A guard collapsed to `previous.done == true` would refuse this too and |
| 128 | + // make every finished task read-only. |
| 129 | + const engine = await bootShowcase(); |
| 130 | + const id = await finishedTask(engine); |
| 131 | + |
| 132 | + await engine.update('showcase_task', { id, priority: 'high' }, ctx as never); |
| 133 | + |
| 134 | + const stored = await readBack(engine, id); |
| 135 | + expect(stored.priority).toBe('high'); |
| 136 | + expect(stored.done).toBeTruthy(); |
| 137 | + }, 30000); |
| 138 | + |
| 139 | + it('REVERSE: with the hooks unbound the same write lands — the refusal is the hook', async () => { |
| 140 | + const engine = await bootShowcase([]); |
| 141 | + const id = await finishedTask(engine); |
| 142 | + |
| 143 | + await engine.update('showcase_task', { id, done: false }, ctx as never); |
| 144 | + |
| 145 | + expect((await readBack(engine, id)).done).toBeFalsy(); |
| 146 | + }, 30000); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('showcase_reopen_task — the script action writes the shape the hook refuses', () => { |
| 150 | + it("the body updates its own record with done: false and nothing else", async () => { |
| 151 | + let written: { object: string; data: Record<string, unknown> } | undefined; |
| 152 | + const ql = { |
| 153 | + object: (object: string) => ({ |
| 154 | + update: async (data: Record<string, unknown>) => { |
| 155 | + written = { object, data }; |
| 156 | + return { id: data.id }; |
| 157 | + }, |
| 158 | + }), |
| 159 | + }; |
| 160 | + |
| 161 | + const handler = actionBodyRunnerFactory(new QuickJSScriptRunner(), { ql, appId: 'showcase' })( |
| 162 | + ReopenTaskAction as never, |
| 163 | + ); |
| 164 | + expect(typeof handler).toBe('function'); |
| 165 | + |
| 166 | + await handler!({ |
| 167 | + recordId: 'task_1', |
| 168 | + record: { id: 'task_1', done: true, progress: 100 }, |
| 169 | + params: {}, |
| 170 | + user: { id: 'u1' }, |
| 171 | + }); |
| 172 | + |
| 173 | + expect(written).toEqual({ object: 'showcase_task', data: { id: 'task_1', done: false } }); |
| 174 | + }); |
| 175 | + |
| 176 | + it('is offered only on finished tasks', () => { |
| 177 | + expect(ReopenTaskAction.type).toBe('script'); |
| 178 | + expect(ReopenTaskAction.execution).toBe('perRecord'); |
| 179 | + const visible = ReopenTaskAction.visible as unknown; |
| 180 | + const source = typeof visible === 'string' ? visible : (visible as { source?: string }).source; |
| 181 | + expect(source).toBe('has(record.done) && record.done == true'); |
| 182 | + }); |
| 183 | +}); |
0 commit comments