|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Setup's four identity objects — `sys_organization`, `sys_team`, |
| 5 | + * `sys_business_unit` and `sys_user` — declare `enable.feeds: false`, so a |
| 6 | + * comment cannot be created on their records (objectui#12614 item 1: the |
| 7 | + * record page drew a Discussion feed with a composer on each of them). |
| 8 | + * |
| 9 | + * The gate itself is generic and pinned elsewhere: `audit-writers.test.ts` |
| 10 | + * (#2707, insert) and `capability-gate-update-verb.test.ts` (#10170, the |
| 11 | + * update verb and the grandfathered body edit). What this file pins is the |
| 12 | + * COMPOSITION — the REAL platform-object definitions, imported rather than |
| 13 | + * restated, registered in a REAL `ObjectQL` with the real `installAuditWriters` |
| 14 | + * hooks — so a `feeds: false` dropped from any one of the four reds its own |
| 15 | + * case below. `@objectstack/platform-objects/identity` resolves to source |
| 16 | + * (this package's vitest alias), so the verdict is about the checkout, not |
| 17 | + * about `platform-objects/dist`. |
| 18 | + * |
| 19 | + * Every refusal asserts the ADR-0112 envelope (`code` AND `status`) plus the |
| 20 | + * object the gate named, and the CONTROL proves the harness does not refuse |
| 21 | + * everything: a business object that never mentions the flag still takes a |
| 22 | + * comment. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect } from 'vitest'; |
| 26 | +import { ObjectQL } from '@objectstack/objectql'; |
| 27 | +import type { EngineQueryOptions, EngineUpdateOptions } from '@objectstack/spec/data'; |
| 28 | +import { |
| 29 | + SysBusinessUnit, |
| 30 | + SysOrganization, |
| 31 | + SysTeam, |
| 32 | + SysUser, |
| 33 | +} from '@objectstack/platform-objects/identity'; |
| 34 | +import { installAuditWriters } from './audit-writers.js'; |
| 35 | + |
| 36 | +type Row = Record<string, unknown>; |
| 37 | + |
| 38 | +const text = (name: string, primaryKey = false) => ({ |
| 39 | + name, |
| 40 | + label: name, |
| 41 | + type: 'text' as const, |
| 42 | + ...(primaryKey ? { primaryKey: true } : {}), |
| 43 | +}); |
| 44 | + |
| 45 | +const fieldMap = (...names: string[]) => |
| 46 | + Object.fromEntries(names.map((n) => [n, text(n, n === 'id')])); |
| 47 | + |
| 48 | +/** |
| 49 | + * A stub driver with just enough storage for the write paths under test. Its |
| 50 | + * matcher implements the operators these reads send and REFUSES any other, so |
| 51 | + * it can never answer a query it does not understand silently wrong |
| 52 | + * (`check:where-matcher-conformance`); `find` applies the caller's `limit` by |
| 53 | + * presence, after the filter (`check:objectql-double-limit`). |
| 54 | + */ |
| 55 | +function makeStubDriver() { |
| 56 | + const stores = new Map<string, Map<string, Row>>(); |
| 57 | + const storeFor = (o: string) => { |
| 58 | + let s = stores.get(o); |
| 59 | + if (!s) { |
| 60 | + s = new Map(); |
| 61 | + stores.set(o, s); |
| 62 | + } |
| 63 | + return s; |
| 64 | + }; |
| 65 | + let nextId = 0; |
| 66 | + const matches = (row: Row, where: unknown): boolean => { |
| 67 | + if (!where || typeof where !== 'object') return true; |
| 68 | + for (const [k, v] of Object.entries(where as Record<string, unknown>)) { |
| 69 | + if (k === '$and' && Array.isArray(v)) { |
| 70 | + if (!v.every((sub) => matches(row, sub))) return false; |
| 71 | + continue; |
| 72 | + } |
| 73 | + if (k === '$or' && Array.isArray(v)) { |
| 74 | + if (!v.some((sub) => matches(row, sub))) return false; |
| 75 | + continue; |
| 76 | + } |
| 77 | + if (k.startsWith('$')) throw new Error(`stub driver: unsupported where operator '${k}'`); |
| 78 | + if (v && typeof v === 'object') { |
| 79 | + const ops = Object.keys(v); |
| 80 | + if (ops.length === 1 && ops[0] === '$eq') { |
| 81 | + if ((row[k] ?? null) !== ((v as { $eq: unknown }).$eq ?? null)) return false; |
| 82 | + continue; |
| 83 | + } |
| 84 | + if (ops.length === 1 && ops[0] === '$in' && Array.isArray((v as { $in: unknown }).$in)) { |
| 85 | + if (!(v as { $in: unknown[] }).$in.includes(row[k])) return false; |
| 86 | + continue; |
| 87 | + } |
| 88 | + throw new Error(`stub driver: unsupported where predicate on '${k}': ${JSON.stringify(v)}`); |
| 89 | + } |
| 90 | + if ((row[k] ?? null) !== (v ?? null)) return false; |
| 91 | + } |
| 92 | + return true; |
| 93 | + }; |
| 94 | + const driver: any = { |
| 95 | + name: 'memory', |
| 96 | + version: '0.0.0', |
| 97 | + supports: {}, |
| 98 | + async connect() {}, |
| 99 | + async disconnect() {}, |
| 100 | + async checkHealth() { |
| 101 | + return true; |
| 102 | + }, |
| 103 | + async execute() { |
| 104 | + return null; |
| 105 | + }, |
| 106 | + async syncSchema() {}, |
| 107 | + async find(o: string, ast: any) { |
| 108 | + const rows = Array.from(storeFor(o).values()).filter((r) => matches(r, ast?.where)); |
| 109 | + return typeof ast?.limit === 'number' ? rows.slice(0, ast.limit) : rows; |
| 110 | + }, |
| 111 | + async findOne(o: string, ast: any) { |
| 112 | + for (const r of storeFor(o).values()) if (matches(r, ast?.where)) return r; |
| 113 | + return null; |
| 114 | + }, |
| 115 | + async create(o: string, data: Row) { |
| 116 | + nextId += 1; |
| 117 | + const id = (data.id as string) ?? `r_${nextId}`; |
| 118 | + const row = { ...data, id }; |
| 119 | + storeFor(o).set(id, row); |
| 120 | + return row; |
| 121 | + }, |
| 122 | + async update(o: string, id: string, data: Row) { |
| 123 | + const s = storeFor(o); |
| 124 | + const cur = s.get(id); |
| 125 | + if (!cur) return null; |
| 126 | + const u = { ...cur, ...data, id }; |
| 127 | + s.set(id, u); |
| 128 | + return u; |
| 129 | + }, |
| 130 | + async delete() { |
| 131 | + return true; |
| 132 | + }, |
| 133 | + async count(o: string, ast: any) { |
| 134 | + return (await driver.find(o, { where: ast?.where })).length; |
| 135 | + }, |
| 136 | + async aggregate() { |
| 137 | + return []; |
| 138 | + }, |
| 139 | + }; |
| 140 | + return { driver, storeFor }; |
| 141 | +} |
| 142 | + |
| 143 | +/** The four Setup identity objects, as `platform-objects` declares them. */ |
| 144 | +const IDENTITY_OBJECTS = [SysOrganization, SysTeam, SysBusinessUnit, SysUser] as const; |
| 145 | + |
| 146 | +/** |
| 147 | + * Boot a real engine carrying the four REAL identity definitions, a business |
| 148 | + * object that never mentions `enable.feeds`, `sys_comment`, and the audit |
| 149 | + * sinks (registered so the `afterInsert` writers have somewhere real to land — |
| 150 | + * their failures are swallowed by design, and a swallowed failure would make |
| 151 | + * this harness lie about which write happened). |
| 152 | + */ |
| 153 | +async function boot() { |
| 154 | + const engine = new ObjectQL(); |
| 155 | + const { driver, storeFor } = makeStubDriver(); |
| 156 | + engine.registerDriver(driver, true); |
| 157 | + await engine.init(); |
| 158 | + |
| 159 | + const reg = engine.registry; |
| 160 | + for (const def of IDENTITY_OBJECTS) reg.registerObject(def as any, 'test.fixture'); |
| 161 | + reg.registerObject({ |
| 162 | + name: 'crm_account', |
| 163 | + label: 'Account', |
| 164 | + fields: fieldMap('id', 'name'), |
| 165 | + } as any, 'test.fixture'); |
| 166 | + reg.registerObject({ |
| 167 | + name: 'sys_comment', |
| 168 | + label: 'Comment', |
| 169 | + fields: fieldMap('id', 'thread_id', 'body'), |
| 170 | + } as any, 'test.fixture'); |
| 171 | + reg.registerObject({ |
| 172 | + name: 'sys_audit_log', |
| 173 | + label: 'Audit Log', |
| 174 | + fields: fieldMap( |
| 175 | + 'id', 'action', 'user_id', 'actor', 'object_name', 'record_id', 'old_value', 'new_value', 'tenant_id', |
| 176 | + ), |
| 177 | + } as any, 'test.fixture'); |
| 178 | + reg.registerObject({ |
| 179 | + name: 'sys_activity', |
| 180 | + label: 'Activity', |
| 181 | + fields: fieldMap( |
| 182 | + 'id', 'type', 'timestamp', 'summary', 'actor_id', 'object_name', 'record_id', 'record_label', 'metadata', |
| 183 | + ), |
| 184 | + } as any, 'test.fixture'); |
| 185 | + |
| 186 | + installAuditWriters(engine as any, 'test.audit'); |
| 187 | + return { engine, driver, storeFor }; |
| 188 | +} |
| 189 | + |
| 190 | +describe('Setup identity objects declare enable.feeds: false — a comment create on their records is refused', () => { |
| 191 | + it.each(IDENTITY_OBJECTS.map((def) => [def.name] as const))( |
| 192 | + '%s: a new comment on one of its records → 403 FEEDS_DISABLED, and no row lands', |
| 193 | + async (objectName) => { |
| 194 | + const { engine, storeFor } = await boot(); |
| 195 | + |
| 196 | + await expect( |
| 197 | + engine.insert('sys_comment', { thread_id: `${objectName}:rec-1`, body: 'hello' } as any), |
| 198 | + ).rejects.toMatchObject({ code: 'FEEDS_DISABLED', status: 403, object: objectName }); |
| 199 | + |
| 200 | + expect(storeFor('sys_comment').size).toBe(0); |
| 201 | + }, |
| 202 | + ); |
| 203 | + |
| 204 | + it('a comment row stored before the flag stays readable, and an ordinary body edit on it still saves', async () => { |
| 205 | + // The grandfather rule in `audit-writers.ts` (`enforceFeedsCapability`): |
| 206 | + // on update, an ABSENT `thread_id` is not a re-thread, so the narrowing |
| 207 | + // reaches new comments and re-points only — never a row that already |
| 208 | + // targets the object. The row is written straight through the driver, |
| 209 | + // below every hook, which is what a row stored before this change is. |
| 210 | + const { engine, driver } = await boot(); |
| 211 | + const stored = await driver.create('sys_comment', { thread_id: 'sys_user:usr-1', body: 'before the flag' }); |
| 212 | + const id = String(stored.id); |
| 213 | + |
| 214 | + // The gate is live on this very thread — so what follows is a grandfathered |
| 215 | + // row surviving the flag, not a harness in which the flag never applied. |
| 216 | + await expect( |
| 217 | + engine.insert('sys_comment', { thread_id: 'sys_user:usr-1', body: 'after the flag' } as any), |
| 218 | + ).rejects.toMatchObject({ code: 'FEEDS_DISABLED', status: 403, object: 'sys_user' }); |
| 219 | + |
| 220 | + const read: any[] = await engine.find('sys_comment', { |
| 221 | + where: { thread_id: 'sys_user:usr-1' }, |
| 222 | + } satisfies EngineQueryOptions); |
| 223 | + expect(read.map((r) => [r.id, r.body])).toEqual([[id, 'before the flag']]); |
| 224 | + |
| 225 | + await expect( |
| 226 | + engine.update('sys_comment', { body: 'edited after the flag' } as any, { |
| 227 | + where: { id }, |
| 228 | + } satisfies EngineUpdateOptions), |
| 229 | + ).resolves.toBeTruthy(); |
| 230 | + |
| 231 | + const after: any = await engine.findOne('sys_comment', { where: { id } } satisfies EngineQueryOptions); |
| 232 | + expect(after).toMatchObject({ id, thread_id: 'sys_user:usr-1', body: 'edited after the flag' }); |
| 233 | + }); |
| 234 | + |
| 235 | + it('CONTROL: a business object that never mentions enable.feeds still accepts a comment', async () => { |
| 236 | + const { engine, storeFor } = await boot(); |
| 237 | + |
| 238 | + await expect( |
| 239 | + engine.insert('sys_comment', { thread_id: 'crm_account:acc-1', body: 'hello' } as any), |
| 240 | + ).resolves.toBeTruthy(); |
| 241 | + |
| 242 | + expect(Array.from(storeFor('sys_comment').values()).map((r) => r.thread_id)).toEqual(['crm_account:acc-1']); |
| 243 | + }); |
| 244 | +}); |
0 commit comments