|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #12015 — the storage/presentation split itself, pinned. |
| 5 | + * |
| 6 | + * The diagnostic in `SqlDriver` fires on what this module decides, so the |
| 7 | + * decision is worth more than the plumbing around it. Two claims live here: |
| 8 | + * |
| 9 | + * ① **The classification is exhaustive over `FieldSchema`.** A key added to |
| 10 | + * the spec later fails the first case until someone classifies it — the |
| 11 | + * whole point of keeping the table in one place. At runtime an unclassified |
| 12 | + * key is silent (a diagnostic must never invent a warning it cannot |
| 13 | + * justify), so without this pin an added key would default into silence |
| 14 | + * with nothing to notice it. |
| 15 | + * |
| 16 | + * ② **Delivered means delivered.** A declared storage attribute the platform's |
| 17 | + * own column already provides is NOT a disagreement and must not be |
| 18 | + * reported as one — that is the whole content of the 2026-08-25 narrowing, |
| 19 | + * and the case that makes the message true again. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect } from 'vitest'; |
| 23 | +import { FieldSchema } from '@objectstack/spec/data'; |
| 24 | +import { |
| 25 | + FIELD_KEY_STORAGE_CLASS, |
| 26 | + BUILTIN_COLUMN_DELIVERY, |
| 27 | + undeliveredStorageAttributes, |
| 28 | +} from './builtin-column-collision.js'; |
| 29 | + |
| 30 | +/** Just the keys, for readability in the assertions below. */ |
| 31 | +const keysOf = (attrs: ReturnType<typeof undeliveredStorageAttributes>) => attrs.map((a) => a.key); |
| 32 | + |
| 33 | +describe('the FieldSchema storage/presentation classification (#12015)', () => { |
| 34 | + it('classifies EVERY FieldSchema key, and invents none', () => { |
| 35 | + const declared = Object.keys(FieldSchema.shape).sort(); |
| 36 | + const classified = Object.keys(FIELD_KEY_STORAGE_CLASS).sort(); |
| 37 | + |
| 38 | + // A spec key with no classification: it would be silent at runtime, which |
| 39 | + // is safe but undeliberate. Classify it in `FIELD_KEY_STORAGE_CLASS`. |
| 40 | + expect(declared.filter((k) => !classified.includes(k)), 'unclassified FieldSchema key(s)').toEqual([]); |
| 41 | + // A classification with no spec key: dead weight that reads as coverage. |
| 42 | + expect(classified.filter((k) => !declared.includes(k)), 'classified key(s) FieldSchema does not declare').toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('puts `required` on the PRESENTATION side — ADR-0113 makes it the WRITE contract, not a column constraint', () => { |
| 46 | + // The load-bearing classification: `required: true` appears on nearly every |
| 47 | + // platform object's `id`, the engine enforces it there exactly as anywhere |
| 48 | + // else, and calling it "discarded" is the false sentence this card removed. |
| 49 | + expect(FIELD_KEY_STORAGE_CLASS.required).toBe('presentation'); |
| 50 | + // Its ADR-0113 sibling — the one that IS the column constraint. |
| 51 | + expect(FIELD_KEY_STORAGE_CLASS.storage).toBe('storage'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('puts the honoured half on the presentation side and the column shape on the storage side', () => { |
| 55 | + for (const key of ['label', 'readonly', 'searchable', 'description', 'inlineHelpText', 'group', 'name']) { |
| 56 | + expect(FIELD_KEY_STORAGE_CLASS[key], `${key} is honoured on a builtin column`).toBe('presentation'); |
| 57 | + } |
| 58 | + for (const key of ['type', 'maxLength', 'unique', 'defaultValue', 'multiple', 'expression']) { |
| 59 | + expect(FIELD_KEY_STORAGE_CLASS[key], `${key} shapes the physical column`).toBe('storage'); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('records what each builtin column actually delivers, read off the emitting lines', () => { |
| 64 | + // `table.string('id').primary()` — varchar(255), NOT NULL, unique, no default. |
| 65 | + expect(BUILTIN_COLUMN_DELIVERY.id).toMatchObject({ |
| 66 | + type: 'string', maxLength: 255, unique: true, notNull: true, defaultValue: null, |
| 67 | + }); |
| 68 | + // `createAuditTimestampColumn` — a timestamp defaulted to the DB clock, left NULLABLE. |
| 69 | + for (const column of ['created_at', 'updated_at']) { |
| 70 | + expect(BUILTIN_COLUMN_DELIVERY[column]).toMatchObject({ |
| 71 | + type: 'datetime', unique: false, notNull: false, defaultValue: 'NOW()', |
| 72 | + }); |
| 73 | + } |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('what a declaration on a builtin column name loses (#12015)', () => { |
| 78 | + it('FIRES on the author error the card was filed for', () => { |
| 79 | + // `id: { type: 'number' }` — an author expecting a numeric key. |
| 80 | + expect(keysOf(undeliveredStorageAttributes('id', { type: 'number' }))).toEqual(['type']); |
| 81 | + // The #11456 fixture's shape. |
| 82 | + expect(keysOf(undeliveredStorageAttributes('id', { type: 'text', name: 'id' }))).toEqual(['type']); |
| 83 | + // …and names what the column really is, not just that something was lost. |
| 84 | + expect(undeliveredStorageAttributes('id', { type: 'text' })[0]).toMatchObject({ |
| 85 | + key: 'type', declared: 'text', delivered: 'string', |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('is SILENT for a presentation-only declaration — the platform honours that half', () => { |
| 90 | + // `sys_presence.id`, verbatim in shape: the population the pre-narrowing |
| 91 | + // warning was false about. |
| 92 | + expect( |
| 93 | + undeliveredStorageAttributes('id', { type: 'string', label: 'Presence ID', required: true, readonly: true }), |
| 94 | + ).toEqual([]); |
| 95 | + expect( |
| 96 | + undeliveredStorageAttributes('created_at', { |
| 97 | + type: 'datetime', label: 'Created At', defaultValue: 'NOW()', readonly: true, |
| 98 | + }), |
| 99 | + ).toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('is SILENT for a storage attribute the column already delivers', () => { |
| 103 | + expect(undeliveredStorageAttributes('id', { type: 'string', maxLength: 255 })).toEqual([]); |
| 104 | + expect(undeliveredStorageAttributes('id', { type: 'string', unique: true })).toEqual([]); // the PK is unique |
| 105 | + expect(undeliveredStorageAttributes('id', { type: 'string', storage: { notNull: true } })).toEqual([]); // the PK is NOT NULL |
| 106 | + expect(undeliveredStorageAttributes('created_at', { type: 'datetime', defaultValue: 'now()' })).toEqual([]); // token, case-insensitive |
| 107 | + }); |
| 108 | + |
| 109 | + it('FIRES for a storage attribute the column does NOT deliver, one entry each', () => { |
| 110 | + expect(keysOf(undeliveredStorageAttributes('id', { type: 'string', maxLength: 12 }))).toEqual(['maxLength']); |
| 111 | + expect(keysOf(undeliveredStorageAttributes('id', { type: 'string', defaultValue: 'NOW()' }))).toEqual(['defaultValue']); |
| 112 | + // created_at IS nullable and NOT unique — asking for either is a real disagreement. |
| 113 | + expect(keysOf(undeliveredStorageAttributes('created_at', { type: 'datetime', unique: true }))).toEqual(['unique']); |
| 114 | + expect(keysOf(undeliveredStorageAttributes('created_at', { type: 'datetime', storage: { notNull: true } }))) |
| 115 | + .toEqual(['storage.notNull']); |
| 116 | + // Several at once, in declaration order. |
| 117 | + expect(keysOf(undeliveredStorageAttributes('id', { type: 'text', maxLength: 12, unique: false }))) |
| 118 | + .toEqual(['type', 'maxLength']); // `unique: false` asks for nothing |
| 119 | + }); |
| 120 | + |
| 121 | + it('ignores a field that is not a builtin column name at all', () => { |
| 122 | + expect(undeliveredStorageAttributes('region', { type: 'text', maxLength: 12 })).toEqual([]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('stays silent — never throws — on a key it does not know, and on a malformed declaration', () => { |
| 126 | + // Forward compatibility: an unclassified key cannot invent a warning. The |
| 127 | + // exhaustiveness case above is what makes its arrival visible. |
| 128 | + expect(undeliveredStorageAttributes('id', { type: 'string', someFutureKey: 'x' } as any)).toEqual([]); |
| 129 | + expect(undeliveredStorageAttributes('id', undefined)).toEqual([]); |
| 130 | + expect(undeliveredStorageAttributes('id', null as any)).toEqual([]); |
| 131 | + }); |
| 132 | +}); |
0 commit comments