|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20481] What the `date` write door admits, this driver stores as its day. |
| 5 | + * |
| 6 | + * The engine's record validator now admits a `date` string only when it carries |
| 7 | + * a leading `YYYY-MM-DD` — the `date` storage rule's own reading |
| 8 | + * (`@objectstack/core`'s `temporalStorageForm`) — and refuses every other |
| 9 | + * spelling with `VALIDATION_FAILED` / `invalid_date` before any driver write |
| 10 | + * (`packages/objectql/src/engine-date-write-iso-only.test.ts`). The refusal is |
| 11 | + * therefore not this driver's; what it owes is the other half: every spelling |
| 12 | + * the door admits is STORED as the day it names and found by it, beside a |
| 13 | + * `Date`. |
| 14 | + * |
| 15 | + * Measured on the base through REST over this driver: `"2026/07/15"`, |
| 16 | + * `"07/15/2026"`, `"15 July 2026"` and `"2026-7-15"` were each a 201 read back |
| 17 | + * verbatim — a non-day that compares as text beside real days. The engine |
| 18 | + * refuses them now; the REST door over SQL is |
| 19 | + * `packages/rest/src/data-date-write-iso-only.test.ts`. |
| 20 | + */ |
| 21 | + |
| 22 | +import { beforeAll, describe, expect, it } from 'vitest'; |
| 23 | +import { InMemoryDriver } from './memory-driver.js'; |
| 24 | + |
| 25 | +const OBJECT = 'ledger_date_20481'; |
| 26 | +const FIELDS = { placed_on: { type: 'date' } }; |
| 27 | + |
| 28 | +/** Each spelling the write door admits, and a `Date` — every one names 2026-07-15. */ |
| 29 | +const ADMITTED: ReadonlyArray<readonly [string, unknown]> = [ |
| 30 | + ['bare', '2026-07-15'], |
| 31 | + ['iso', '2026-07-15T10:00:00Z'], |
| 32 | + ['naive', '2026-07-15 10:00'], |
| 33 | + ['blank', ' 2026-07-15'], |
| 34 | + ['date', new Date(Date.UTC(2026, 6, 15, 10))], |
| 35 | +]; |
| 36 | + |
| 37 | +describe('[#20481] every date spelling the write door admits is stored as its day', () => { |
| 38 | + let driver: InMemoryDriver; |
| 39 | + |
| 40 | + beforeAll(async () => { |
| 41 | + driver = new InMemoryDriver({}); |
| 42 | + await driver.connect(); |
| 43 | + await driver.syncSchema(OBJECT, { name: OBJECT, fields: FIELDS }); |
| 44 | + for (const [id, placed_on] of ADMITTED) await driver.create(OBJECT, { id, placed_on }); |
| 45 | + await driver.create(OBJECT, { id: 'before', placed_on: '2026-07-14' }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('reads each one back as 2026-07-15', async () => { |
| 49 | + for (const [id] of ADMITTED) { |
| 50 | + expect((await driver.findOne(OBJECT, { where: { id } }))?.placed_on, id).toBe('2026-07-15'); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('finds each one by the day, and orders it after 2026-07-14', async () => { |
| 55 | + const ids = async (where: Record<string, unknown>) => |
| 56 | + (await driver.find(OBJECT, { where })).map((r) => r.id as string).sort(); |
| 57 | + const all = ADMITTED.map(([id]) => id).sort(); |
| 58 | + expect(await ids({ placed_on: { $eq: '2026-07-15' } })).toEqual(all); |
| 59 | + expect(await ids({ placed_on: { $gt: '2026-07-14' } })).toEqual(all); |
| 60 | + expect(await ids({ placed_on: { $lt: '2026-07-15' } })).toEqual(['before']); |
| 61 | + }); |
| 62 | +}); |
0 commit comments