|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { AuditPlugin } from './audit-plugin.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * #11374 route A, for the objects THIS PLUGIN registers — every text-family |
| 8 | + * column a declared index keys on must declare a `maxLength`, because a bound |
| 9 | + * is what lets the column be a key at all. |
| 10 | + * |
| 11 | + * ## Why a second copy of the pin lives here |
| 12 | + * |
| 13 | + * The original pin is `@objectstack/platform-objects`' |
| 14 | + * `platform-keyed-text-bounds.test.ts`, and it enumerates the objects THAT |
| 15 | + * package exports. Platform objects that moved out to plugins under ADR-0029 K2 |
| 16 | + * are outside it by construction, which is exactly how `sys_activity.record_id` |
| 17 | + * and `sys_audit_log.record_id` stayed unbounded through route A's sweep: the |
| 18 | + * pin could not see them, so nothing failed by name. |
| 19 | + * |
| 20 | + * That is the same failure the platform pin already survived once at a smaller |
| 21 | + * scale (it used to be scoped to `identity/`, and `sys_import_job.created_by` |
| 22 | + * in `audit/` escaped it). A pin scoped to a package polices a package, not the |
| 23 | + * defect class. The class-level repair — one walk over every package that ships |
| 24 | + * platform objects — is engine-lane work tracked separately; until it lands, |
| 25 | + * each shipping package carries its own copy so no keyed column is unpoliced. |
| 26 | + * |
| 27 | + * ## Why it drives `init()` instead of importing the objects |
| 28 | + * |
| 29 | + * This package's `package.json` declares only the `.` export and the root |
| 30 | + * barrel does not re-export `./objects`, so nothing outside the package can |
| 31 | + * import `SysActivity` at all — which is why the objects were never measured |
| 32 | + * live. Enumerating a hand-written list here would reproduce that blind spot in |
| 33 | + * miniature: the list, not the plugin, would define the surface. So the pin |
| 34 | + * drives the REAL registration path (`AuditPlugin.init` → the `manifest` |
| 35 | + * service's `register({ objects })`) and polices whatever the plugin actually |
| 36 | + * contributes to a kernel. An object added to that call is policed the moment |
| 37 | + * it is added, with no second edit here. |
| 38 | + * |
| 39 | + * ## What a red on this file means |
| 40 | + * |
| 41 | + * A new keyed text-family field arrived without a `maxLength`. Do not silence |
| 42 | + * it — derive a bound from the value's producer and declare it (route A's |
| 43 | + * shape: a NAMED producer, stated in the declaration so it is vetoable in |
| 44 | + * review), or extend the allowlist with a comment naming why no bound exists |
| 45 | + * and where the keyability debt is tracked. |
| 46 | + * |
| 47 | + * On MySQL the cost of a red is not theoretical: the unbounded column is |
| 48 | + * emitted `TEXT`, `ALTER TABLE … ADD INDEX` is refused with |
| 49 | + * `ER_BLOB_KEY_WITHOUT_LENGTH`, and the object lands registered-but-broken with |
| 50 | + * its declared index silently absent. |
| 51 | + */ |
| 52 | + |
| 53 | +const TEXT_FAMILY = new Set(['text', 'textarea', 'html', 'markdown']); |
| 54 | + |
| 55 | +/** |
| 56 | + * Keyed text-family columns with NO defensible bound. Every entry must name |
| 57 | + * why. Entries that stop matching a real keyed unbounded column fail the last |
| 58 | + * test, so the list cannot rot. Empty today, deliberately: all four of this |
| 59 | + * plugin's keyed text columns have a sourced bound. |
| 60 | + */ |
| 61 | +const UNBOUNDABLE: ReadonlySet<string> = new Set([]); |
| 62 | + |
| 63 | +type AnyObject = { |
| 64 | + name: string; |
| 65 | + fields: Record<string, { type?: string; maxLength?: unknown }>; |
| 66 | + indexes?: Array<{ fields?: string[]; unique?: boolean | string }>; |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * The objects `AuditPlugin` really contributes to a kernel, read off the |
| 71 | + * manifest registration it performs in `init()`. |
| 72 | + */ |
| 73 | +async function registeredObjects(): Promise<AnyObject[]> { |
| 74 | + const captured: AnyObject[] = []; |
| 75 | + const noop = () => {}; |
| 76 | + const logger = { |
| 77 | + info: noop, warn: noop, error: noop, debug: noop, |
| 78 | + child() { return logger; }, |
| 79 | + }; |
| 80 | + const ctx = { |
| 81 | + logger, |
| 82 | + getService(name: string) { |
| 83 | + if (name === 'manifest') { |
| 84 | + return { |
| 85 | + register(m: { objects?: AnyObject[] }) { |
| 86 | + for (const o of m?.objects ?? []) captured.push(o); |
| 87 | + }, |
| 88 | + }; |
| 89 | + } |
| 90 | + return undefined; |
| 91 | + }, |
| 92 | + registerService: noop, |
| 93 | + hook: noop, |
| 94 | + } as never; |
| 95 | + |
| 96 | + await new AuditPlugin().init(ctx); |
| 97 | + return captured; |
| 98 | +} |
| 99 | + |
| 100 | +function keyedTextColumns(o: AnyObject): Array<{ column: string; maxLength: unknown }> { |
| 101 | + const keyed = new Set<string>(); |
| 102 | + for (const ix of o.indexes ?? []) for (const f of ix.fields ?? []) keyed.add(f); |
| 103 | + return Object.entries(o.fields ?? {}) |
| 104 | + .filter(([name, def]) => keyed.has(name) && TEXT_FAMILY.has(def?.type ?? '')) |
| 105 | + .map(([column, def]) => ({ column: `${o.name}.${column}`, maxLength: def.maxLength })); |
| 106 | +} |
| 107 | + |
| 108 | +describe('plugin-audit keyed text-family columns declare their bound (#11374 route A)', () => { |
| 109 | + it('enumerates a real surface through the plugin registration path — the probe is not vacuous', async () => { |
| 110 | + // Positive control: if `init()` stops registering objects, or the field / |
| 111 | + // index spelling changes so this file stops seeing columns, fail loudly |
| 112 | + // instead of passing empty. An empty enumeration is the failure mode that |
| 113 | + // let these columns escape route A in the first place. |
| 114 | + const objects = await registeredObjects(); |
| 115 | + expect(objects.map((o) => o.name)).toEqual( |
| 116 | + expect.arrayContaining(['sys_audit_log', 'sys_activity', 'sys_comment']), |
| 117 | + ); |
| 118 | + |
| 119 | + // 5 is MEASURED off this registration surface, not a round number: |
| 120 | + // sys_audit_log.{object_name,record_id}, sys_activity.{object_name,record_id}, |
| 121 | + // sys_comment.thread_id. Every other index on these three objects keys on a |
| 122 | + // lookup, select or datetime column, which is not text-family. |
| 123 | + const all = objects.flatMap(keyedTextColumns); |
| 124 | + expect(all.length).toBeGreaterThanOrEqual(5); |
| 125 | + // Three names from THREE DIFFERENT objects, so a future narrowing of the |
| 126 | + // enumeration fails here by name rather than by quietly enumerating less. |
| 127 | + expect(all.map((c) => c.column)).toContain('sys_activity.record_id'); |
| 128 | + expect(all.map((c) => c.column)).toContain('sys_audit_log.record_id'); |
| 129 | + expect(all.map((c) => c.column)).toContain('sys_comment.thread_id'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('every keyed text-family column declares a positive integer maxLength, or is allowlisted by name', async () => { |
| 133 | + const objects = await registeredObjects(); |
| 134 | + const offenders: string[] = []; |
| 135 | + for (const o of objects) { |
| 136 | + for (const { column, maxLength } of keyedTextColumns(o)) { |
| 137 | + if (UNBOUNDABLE.has(column)) continue; |
| 138 | + const bounded = |
| 139 | + typeof maxLength === 'number' && Number.isInteger(maxLength) && maxLength > 0; |
| 140 | + if (!bounded) offenders.push(`${column} (maxLength: ${String(maxLength)})`); |
| 141 | + } |
| 142 | + } |
| 143 | + expect( |
| 144 | + offenders, |
| 145 | + `keyed text-family column(s) without a declared maxLength — on MySQL their ` + |
| 146 | + `declared index cannot be created and the object lands registered-but-broken. ` + |
| 147 | + `Declare a sourced bound or extend UNBOUNDABLE with a named reason: ` + |
| 148 | + offenders.join(', '), |
| 149 | + ).toEqual([]); |
| 150 | + }); |
| 151 | + |
| 152 | + it('the two ActivityPointer id columns carry the referenced-column bound, not just any bound', async () => { |
| 153 | + // The bound is not free-floating: 255 is the width of the physical `id` |
| 154 | + // column `driver-sql` creates (`table.string('id').primary()`, knex's |
| 155 | + // varchar(255) — the driver spells it `DEFAULT_STRING_VARCHAR_CHARS`), so a |
| 156 | + // column holding a record id is bounded by transitivity from the id itself. |
| 157 | + // Pinned by VALUE because a later edit that "tidies" one of these to a |
| 158 | + // narrower sibling convention (100, as plugin-sharing and plugin-approvals |
| 159 | + // chose) would silently make the column unable to hold ids that the id |
| 160 | + // column itself accepts. |
| 161 | + const byName = new Map((await registeredObjects()).map((o) => [o.name, o])); |
| 162 | + expect(byName.get('sys_activity')?.fields.record_id?.maxLength).toBe(255); |
| 163 | + expect(byName.get('sys_audit_log')?.fields.record_id?.maxLength).toBe(255); |
| 164 | + }); |
| 165 | + |
| 166 | + it('the UNBOUNDABLE allowlist matches only real, still-unbounded keyed columns', async () => { |
| 167 | + const real = new Map( |
| 168 | + (await registeredObjects()).flatMap(keyedTextColumns).map((c) => [c.column, c.maxLength]), |
| 169 | + ); |
| 170 | + for (const entry of UNBOUNDABLE) { |
| 171 | + expect(real.has(entry), `allowlist entry ${entry} is not a keyed text column any more — remove it`).toBe(true); |
| 172 | + expect( |
| 173 | + real.get(entry), |
| 174 | + `allowlist entry ${entry} now declares a bound — remove it from UNBOUNDABLE`, |
| 175 | + ).toBeUndefined(); |
| 176 | + } |
| 177 | + }); |
| 178 | +}); |
0 commit comments