|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20481] A `date` string is written in its `YYYY-MM-DD` form or it is refused |
| 5 | + * — `VALIDATION_FAILED` with the field's `invalid_date` code, on insert, |
| 6 | + * update, a multi-row update and the dry-run `validate`, before any driver |
| 7 | + * write. |
| 8 | + * |
| 9 | + * "Its `YYYY-MM-DD` form" is the `date` storage rule's own reading of a |
| 10 | + * string (`@objectstack/core`'s `temporalStorageForm`): a leading `YYYY-MM-DD` |
| 11 | + * after trimming, collapsed to that day. The write door asks the SAME |
| 12 | + * predicate the temporal-comparand door asks (`isUninterpretableTemporalComparand`), |
| 13 | + * so the two doors cannot disagree about which `date` strings the rule reads — |
| 14 | + * the last block below holds them to that. |
| 15 | + * |
| 16 | + * Measured on the base (`0bbe4005e`) through REST, a create then a read-back, |
| 17 | + * the process in America/New_York and PostgreSQL 16 at Asia/Shanghai with |
| 18 | + * `DateStyle` `ISO, MDY`: |
| 19 | + * |
| 20 | + * | written to a `date` | memory | SQLite | PostgreSQL | now | |
| 21 | + * |:--|:--|:--|:--|:--| |
| 22 | + * | `"2026/07/15"`, `"07/15/2026"`, `"15 July 2026"`, `"2026-7-15"`, `"2026.07.15"`, `"July 15, 2026"` | 201, read back verbatim | 201, read back verbatim | 201, `"2026-07-15"` (its `DateStyle` reading) | 400 | |
| 23 | + * | `"07/08/2026"` | 201, verbatim | 201, verbatim | 201, `"2026-07-08"` (August 7 under DMY) | 400 | |
| 24 | + * | `"+002026-07-15"` | 201, verbatim | 201, verbatim | 500 | 400 | |
| 25 | + * | `"2026-07-15T10:00:00Z"`, `"2026-07-15 10:00"`, `" 2026-07-15"`, `"2026-07-15"` | 201, `"2026-07-15"` | 201, `"2026-07-15"` | 201, `"2026-07-15"` | unchanged | |
| 26 | + * | `"20260715"`, `"15/07/2026"` (no `Date.parse` reading) | 400 | 400 | 400 | unchanged | |
| 27 | + * | an epoch-millisecond number | 400 | 400 | 400 | unchanged | |
| 28 | + * | a `Date` | 201, its UTC day | 201, its UTC day | 201, its UTC day | unchanged | |
| 29 | + * |
| 30 | + * No other spelling is canonicalised, on purpose: `07/08/2026` names two days, |
| 31 | + * and a guess stores the wrong one silently. The REST door over real drivers is |
| 32 | + * `packages/rest/src/data-date-write-iso-only.test.ts`; this file's driver |
| 33 | + * records writes and stores nothing, because the refusal sits in front of |
| 34 | + * every driver. |
| 35 | + */ |
| 36 | + |
| 37 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 38 | +import { ObjectQL } from './engine.js'; |
| 39 | + |
| 40 | +const ledger = { |
| 41 | + name: 'ledger', |
| 42 | + label: 'Ledger', |
| 43 | + fields: { |
| 44 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 45 | + customer_id: { name: 'customer_id', type: 'text' as const }, |
| 46 | + placed_on: { name: 'placed_on', type: 'date' as const }, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +/** `Date.parse`-readable, no leading `YYYY-MM-DD` — each a 201 stored verbatim on memory and SQLite at the base. */ |
| 51 | +const REFUSED: readonly string[] = [ |
| 52 | + '2026/07/15', |
| 53 | + '07/15/2026', |
| 54 | + '07/08/2026', |
| 55 | + '15 July 2026', |
| 56 | + 'July 15, 2026', |
| 57 | + '2026-7-15', |
| 58 | + '2026.07.15', |
| 59 | + '+002026-07-15', |
| 60 | +]; |
| 61 | + |
| 62 | +/** Refused at the base already — kept refused. */ |
| 63 | +const STILL_REFUSED: ReadonlyArray<readonly [string, unknown]> = [ |
| 64 | + ['no Date.parse reading', '20260715'], |
| 65 | + ['a day-first spelling Date.parse cannot read', '15/07/2026'], |
| 66 | + ['a leading day shape with no reading', '2026-13-45'], |
| 67 | + ['a {placeholder}', '{today}'], |
| 68 | + ['an epoch-millisecond number', Date.UTC(2026, 6, 15)], |
| 69 | +]; |
| 70 | + |
| 71 | +/** A leading `YYYY-MM-DD` — the rule collapses each to `2026-07-15`, and a `Date` keeps its UTC day. */ |
| 72 | +const ACCEPTED: ReadonlyArray<readonly [string, unknown]> = [ |
| 73 | + ['a bare day', '2026-07-15'], |
| 74 | + ['an ISO instant', '2026-07-15T10:00:00Z'], |
| 75 | + ['a zone-naive wall clock', '2026-07-15 10:00'], |
| 76 | + ['a leading blank', ' 2026-07-15'], |
| 77 | + ['a Date', new Date(Date.UTC(2026, 6, 15, 10))], |
| 78 | +]; |
| 79 | + |
| 80 | +/** A driver that records every read and write, and answers none. */ |
| 81 | +function makeRecordingDriver() { |
| 82 | + const reads: unknown[] = []; |
| 83 | + const writes: Record<string, unknown>[] = []; |
| 84 | + const driver: any = { |
| 85 | + name: 'recording', version: '0.0.0', supports: {}, |
| 86 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 87 | + async find(_o: string, ast: unknown) { reads.push(ast); return []; }, |
| 88 | + async findOne(_o: string, ast: unknown) { reads.push(ast); return { id: 'r1' }; }, |
| 89 | + async count(_o: string, ast: unknown) { reads.push(ast); return 0; }, |
| 90 | + async aggregate(_o: string, ast: unknown) { reads.push(ast); return []; }, |
| 91 | + async create(_o: string, data: Record<string, unknown>) { writes.push(data); return { ...data }; }, |
| 92 | + async update(_o: string, id: string, data: Record<string, unknown>) { writes.push(data); return { ...data, id }; }, |
| 93 | + async updateMany(_o: string, _ast: unknown, data: Record<string, unknown>) { writes.push(data); return 0; }, |
| 94 | + async delete() { return true; }, |
| 95 | + async deleteMany() { return 0; }, |
| 96 | + async bulkCreate(_o: string, batch: Record<string, unknown>[]) { writes.push(...batch); return batch; }, |
| 97 | + async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; }, |
| 98 | + async commit() {}, async rollback() {}, |
| 99 | + }; |
| 100 | + return { driver, reads, writes }; |
| 101 | +} |
| 102 | + |
| 103 | +const refusalOf = async (p: Promise<unknown>) => |
| 104 | + p.then(() => null, (e: any) => e as Error & { code?: string; status?: number; fields?: Array<{ field: string; code: string }> }); |
| 105 | + |
| 106 | +const labelOf = (value: unknown) => (value instanceof Date ? `Date ${value.toISOString()}` : JSON.stringify(value)); |
| 107 | + |
| 108 | +describe('[#20481] the write door — a date string is written in its YYYY-MM-DD form, or it is VALIDATION_FAILED before any write', () => { |
| 109 | + let engine: ObjectQL; |
| 110 | + let reads: unknown[]; |
| 111 | + let writes: Record<string, unknown>[]; |
| 112 | + |
| 113 | + beforeEach(async () => { |
| 114 | + const rec = makeRecordingDriver(); |
| 115 | + reads = rec.reads; |
| 116 | + writes = rec.writes; |
| 117 | + engine = new ObjectQL(); |
| 118 | + engine.registerDriver(rec.driver, true); |
| 119 | + await engine.init(); |
| 120 | + engine.registry.registerObject(ledger, 'test'); |
| 121 | + }); |
| 122 | + |
| 123 | + const doors = (value: unknown) => [ |
| 124 | + ['insert', () => engine.insert('ledger', { id: 'n1', customer_id: 'c1', placed_on: value })], |
| 125 | + ['update', () => engine.update('ledger', { id: 'r1', placed_on: value })], |
| 126 | + ['multi-row update', () => engine.update('ledger', { placed_on: value }, { where: { customer_id: 'c1' }, multi: true })], |
| 127 | + ] as const; |
| 128 | + |
| 129 | + it('refuses every other spelling on insert, update and a multi-row update, with the field and invalid_date — and writes nothing', async () => { |
| 130 | + for (const value of [...REFUSED, ...STILL_REFUSED.map(([, v]) => v)]) { |
| 131 | + for (const [door, call] of doors(value)) { |
| 132 | + const err = await refusalOf(call()); |
| 133 | + expect(err, `${door}, ${labelOf(value)}`).not.toBeNull(); |
| 134 | + expect(err!.code, `${door}, ${labelOf(value)}`).toBe('VALIDATION_FAILED'); |
| 135 | + expect(err!.fields, `${door}, ${labelOf(value)}`).toEqual([expect.objectContaining({ field: 'placed_on', code: 'invalid_date' })]); |
| 136 | + } |
| 137 | + } |
| 138 | + expect(writes, 'no write — every refusal precedes the driver').toHaveLength(0); |
| 139 | + }); |
| 140 | + |
| 141 | + it('the dry-run validate predicts each refusal — and each accepted value as valid', async () => { |
| 142 | + for (const value of [...REFUSED, ...STILL_REFUSED.map(([, v]) => v)]) { |
| 143 | + const verdict = await engine.validate('ledger', { placed_on: value }); |
| 144 | + expect(verdict.valid, labelOf(value)).toBe(false); |
| 145 | + expect(verdict.results[0]!.errors, labelOf(value)).toEqual([expect.objectContaining({ field: 'placed_on', code: 'invalid_date' })]); |
| 146 | + } |
| 147 | + for (const [name, value] of ACCEPTED) { |
| 148 | + expect((await engine.validate('ledger', { placed_on: value })).valid, name).toBe(true); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + it('accepts a leading YYYY-MM-DD and a Date on every door — the POSITIVE CONTROL', async () => { |
| 153 | + for (const [name, value] of ACCEPTED) { |
| 154 | + for (const [door, call] of doors(value)) { |
| 155 | + const before = writes.length; |
| 156 | + await expect(call(), `${door}, ${name}`).resolves.toBeDefined(); |
| 157 | + expect(writes.length, `${door}, ${name} reached the driver`).toBe(before + 1); |
| 158 | + } |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + it('one reading at both doors: a date string is refused as a written value exactly when it is refused as a comparand', async () => { |
| 163 | + const strings = [...REFUSED, ...ACCEPTED.map(([, v]) => v).filter((v): v is string => typeof v === 'string')]; |
| 164 | + for (const value of strings) { |
| 165 | + const written = (await engine.validate('ledger', { placed_on: value })).valid; |
| 166 | + const err = await refusalOf(engine.find('ledger', { where: { placed_on: { $gte: value } } })); |
| 167 | + if (err) expect(err, `where ${JSON.stringify(value)}`).toMatchObject({ code: 'INVALID_FILTER', status: 400 }); |
| 168 | + expect({ written, compared: err === null }, JSON.stringify(value)).toEqual({ written: !REFUSED.includes(value), compared: !REFUSED.includes(value) }); |
| 169 | + } |
| 170 | + expect(reads, 'a read for each accepted comparand, none for a refused one').toHaveLength(strings.length - REFUSED.length); |
| 171 | + }); |
| 172 | +}); |
0 commit comments