|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#31196] The cached by-name `/meta` read (`getMetaItemCached`) answers |
| 5 | + * `lastModified` — the REST door's `Last-Modified` — as the item's own last |
| 6 | + * change, never the time of the read. |
| 7 | + * |
| 8 | + * 1. Two reads of one unchanged stored item answer the same `lastModified`: |
| 9 | + * the stored row's `updated_at`, the time of the save that wrote it. |
| 10 | + * 2. A save and a publish of that item each move it to their own time. |
| 11 | + * 3. An item no stored row serves (a code-shipped definition from the |
| 12 | + * registry) has no known change time and answers no `lastModified` key. |
| 13 | + * 4. CONTROL: the ETag and `cacheControl` (`private, no-cache`) are as they |
| 14 | + * were — freshness stays the ETag's. |
| 15 | + * |
| 16 | + * The clock is fake and moved between steps, so a read stamped with the |
| 17 | + * read time (the defect) differs from the save time by whole minutes, never |
| 18 | + * by a sub-second accident. |
| 19 | + * |
| 20 | + * Ablation (one-shot, recorded in the PR): putting |
| 21 | + * `lastModified: new Date().toISOString()` back in |
| 22 | + * `protocol-search-and-cache.ts` turns §1, §2 and §3 red. |
| 23 | + */ |
| 24 | +// Pays the lint root's first load at collection, never inside a clocked case. |
| 25 | +import './lint-root-preload.testkit.js'; |
| 26 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 27 | +import { |
| 28 | + assertEngineDeleteDispatch, |
| 29 | + assertEngineFindOnePredicate, |
| 30 | + assertEngineUpdateDispatch, |
| 31 | +} from '@objectstack/metadata-core'; |
| 32 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 33 | + |
| 34 | +type Row = Record<string, any>; |
| 35 | + |
| 36 | +/** [#24371] One `where` value as the store reads it: `{ $in: [...] }` or equality. */ |
| 37 | +const whereValueMatches = (actual: unknown, want: unknown): boolean => |
| 38 | + want !== null && typeof want === 'object' && Array.isArray((want as { $in?: unknown }).$in) |
| 39 | + ? (want as { $in: unknown[] }).$in.includes(actual) |
| 40 | + : actual === want; |
| 41 | + |
| 42 | +function matches(r: Row, where: Record<string, unknown> = {}): boolean { |
| 43 | + for (const [k, v] of Object.entries(where)) { |
| 44 | + // A combinator is refused, never read as a field name. |
| 45 | + if (k.startsWith('$')) throw new Error(`matches: unsupported combinator ${k}`); |
| 46 | + if (v === undefined) continue; |
| 47 | + if (!whereValueMatches(r[k] ?? null, v)) return false; |
| 48 | + } |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +/** A `sys_metadata` store; every other table answers empty. `code` is the registry's code-shipped items. */ |
| 53 | +function makeStubEngine(code: Record<string, Row> = {}) { |
| 54 | + const rows: Row[] = []; |
| 55 | + let nextId = 0; |
| 56 | + const engine: any = { |
| 57 | + async findOne(t: string, opts: { where: Record<string, unknown> }) { |
| 58 | + assertEngineFindOnePredicate(t, opts); |
| 59 | + if (t !== 'sys_metadata') return null; |
| 60 | + return rows.find((r) => matches(r, opts.where)) ?? null; |
| 61 | + }, |
| 62 | + async find(t: string, opts?: { where?: Record<string, unknown>; limit?: number }) { |
| 63 | + if (t !== 'sys_metadata') return []; |
| 64 | + const found = rows.filter((r) => matches(r, opts?.where)); |
| 65 | + return typeof opts?.limit === 'number' ? found.slice(0, opts.limit) : found; |
| 66 | + }, |
| 67 | + async count(t: string, opts?: { where?: Record<string, unknown> }) { |
| 68 | + if (t !== 'sys_metadata') return 0; |
| 69 | + return rows.filter((r) => matches(r, opts?.where)).length; |
| 70 | + }, |
| 71 | + async insert(t: string, data: Row) { |
| 72 | + if (t !== 'sys_metadata') return { id: 'side_table' }; |
| 73 | + const row = { id: `r_${++nextId}`, ...data }; |
| 74 | + rows.push(row); |
| 75 | + return { id: row.id }; |
| 76 | + }, |
| 77 | + async update(t: string, data: Row, opts: { where: Record<string, unknown> }) { |
| 78 | + assertEngineUpdateDispatch(data, opts); |
| 79 | + if (t !== 'sys_metadata') return { id: null }; |
| 80 | + const found = rows.find((r) => matches(r, opts.where)); |
| 81 | + if (!found) return { id: null }; |
| 82 | + Object.assign(found, data); |
| 83 | + return { id: found.id }; |
| 84 | + }, |
| 85 | + async delete(_t: string, opts?: Record<string, unknown>) { |
| 86 | + assertEngineDeleteDispatch(opts); |
| 87 | + return { deleted: 0 }; |
| 88 | + }, |
| 89 | + async transaction<T>(cb: (ctx: unknown, info: { owned: boolean }) => Promise<T>): Promise<T> { |
| 90 | + return cb(undefined, { owned: true }); |
| 91 | + }, |
| 92 | + async syncObjectSchema() { /* no DDL in this stub */ }, |
| 93 | + registry: { |
| 94 | + listItems: () => [], |
| 95 | + isPackageDisabled: () => false, |
| 96 | + getItem: (type: string, name: string) => (type === 'object' ? code[name] : undefined), |
| 97 | + registerItem: () => {}, |
| 98 | + registerObject: () => {}, |
| 99 | + getPackage: () => undefined, |
| 100 | + }, |
| 101 | + }; |
| 102 | + return { engine, rows }; |
| 103 | +} |
| 104 | + |
| 105 | +/** A runtime-created business object (no artifact), so the save door writes a stored row. */ |
| 106 | +const authored = (name: string, label = 'Invoice') => ({ |
| 107 | + name, |
| 108 | + label, |
| 109 | + sharingModel: 'private', |
| 110 | + fields: { amount: { type: 'currency', label: 'Amount' } }, |
| 111 | +}); |
| 112 | + |
| 113 | +const T_SAVE = '2026-11-07T08:00:00.000Z'; |
| 114 | +const T_READ_1 = '2026-11-07T08:05:00.000Z'; |
| 115 | +const T_READ_2 = '2026-11-07T08:09:00.000Z'; |
| 116 | +const T_RESAVE = '2026-11-07T09:00:00.000Z'; |
| 117 | +const T_DRAFT = '2026-11-07T10:00:00.000Z'; |
| 118 | +const T_PUBLISH = '2026-11-07T10:30:00.000Z'; |
| 119 | +const T_READ_3 = '2026-11-07T11:00:00.000Z'; |
| 120 | + |
| 121 | +describe('[#31196] the cached by-name read answers the item\'s own change time, never the read time', () => { |
| 122 | + beforeEach(() => { |
| 123 | + vi.useFakeTimers({ toFake: ['Date'] }); |
| 124 | + }); |
| 125 | + afterEach(() => { |
| 126 | + vi.useRealTimers(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('§1 two reads of one unchanged stored item answer the same lastModified — the save time', async () => { |
| 130 | + const { engine } = makeStubEngine(); |
| 131 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 132 | + vi.setSystemTime(new Date(T_SAVE)); |
| 133 | + await protocol.saveMetaItem({ type: 'object', name: 'invoice', item: authored('invoice') }); |
| 134 | + |
| 135 | + vi.setSystemTime(new Date(T_READ_1)); |
| 136 | + const first = await protocol.getMetaItemCached({ type: 'object', name: 'invoice' }); |
| 137 | + vi.setSystemTime(new Date(T_READ_2)); |
| 138 | + const second = await protocol.getMetaItemCached({ type: 'object', name: 'invoice' }); |
| 139 | + |
| 140 | + expect(first.lastModified).toBe(T_SAVE); |
| 141 | + expect(second.lastModified).toBe(T_SAVE); |
| 142 | + // CONTROL (§4): the same document answers the same validator and the |
| 143 | + // same freshness rule as before. |
| 144 | + expect(second.etag).toEqual(first.etag); |
| 145 | + expect(first.cacheControl).toEqual({ directives: ['private', 'no-cache'] }); |
| 146 | + expect(first.notModified).toBe(false); |
| 147 | + }); |
| 148 | + |
| 149 | + it('§2 a save moves it, and so does a publish', async () => { |
| 150 | + const { engine } = makeStubEngine(); |
| 151 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 152 | + vi.setSystemTime(new Date(T_SAVE)); |
| 153 | + await protocol.saveMetaItem({ type: 'object', name: 'invoice', item: authored('invoice') }); |
| 154 | + vi.setSystemTime(new Date(T_READ_1)); |
| 155 | + const before = await protocol.getMetaItemCached({ type: 'object', name: 'invoice' }); |
| 156 | + |
| 157 | + vi.setSystemTime(new Date(T_RESAVE)); |
| 158 | + await protocol.saveMetaItem({ type: 'object', name: 'invoice', item: authored('invoice', 'Bill') }); |
| 159 | + vi.setSystemTime(new Date(T_READ_2)); |
| 160 | + const afterSave = await protocol.getMetaItemCached({ type: 'object', name: 'invoice' }); |
| 161 | + |
| 162 | + vi.setSystemTime(new Date(T_DRAFT)); |
| 163 | + await protocol.saveMetaItem({ type: 'object', name: 'invoice', item: authored('invoice', 'Receipt'), mode: 'draft' }); |
| 164 | + // A pending draft is not the served item: the active row's time stands. |
| 165 | + const withDraft = await protocol.getMetaItemCached({ type: 'object', name: 'invoice' }); |
| 166 | + vi.setSystemTime(new Date(T_PUBLISH)); |
| 167 | + await protocol.publishMetaItem({ type: 'object', name: 'invoice' }); |
| 168 | + vi.setSystemTime(new Date(T_READ_3)); |
| 169 | + const afterPublish = await protocol.getMetaItemCached({ type: 'object', name: 'invoice' }); |
| 170 | + |
| 171 | + expect(before.lastModified).toBe(T_SAVE); |
| 172 | + expect(afterSave.lastModified).toBe(T_RESAVE); |
| 173 | + expect(withDraft.lastModified).toBe(T_RESAVE); |
| 174 | + expect((afterPublish.data as { label?: string }).label).toBe('Receipt'); |
| 175 | + expect(afterPublish.lastModified).toBe(T_PUBLISH); |
| 176 | + // CONTROL (§4): each change also moved the validator. |
| 177 | + expect(afterSave.etag).not.toEqual(before.etag); |
| 178 | + expect(afterPublish.etag).not.toEqual(afterSave.etag); |
| 179 | + }); |
| 180 | + |
| 181 | + it('§3 an item no stored row serves has no known change time: no lastModified key', async () => { |
| 182 | + const { engine } = makeStubEngine({ shipped: { ...authored('shipped', 'Shipped'), _packageId: 'com.example.code' } }); |
| 183 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 184 | + vi.setSystemTime(new Date(T_READ_1)); |
| 185 | + const read = await protocol.getMetaItemCached({ type: 'object', name: 'shipped' }); |
| 186 | + |
| 187 | + expect((read.data as { label?: string }).label).toBe('Shipped'); |
| 188 | + expect(Object.keys(read)).not.toContain('lastModified'); |
| 189 | + // CONTROL (§4): it still carries its validator and freshness rule. |
| 190 | + expect(read.etag?.value).toEqual(expect.any(String)); |
| 191 | + expect(read.cacheControl).toEqual({ directives: ['private', 'no-cache'] }); |
| 192 | + }); |
| 193 | + |
| 194 | + it('§4 the change time stays off the by-name read\'s published envelope', async () => { |
| 195 | + const { engine } = makeStubEngine(); |
| 196 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 197 | + vi.setSystemTime(new Date(T_SAVE)); |
| 198 | + await protocol.saveMetaItem({ type: 'object', name: 'invoice', item: authored('invoice') }); |
| 199 | + const envelope = await protocol.getMetaItem({ type: 'object', name: 'invoice' }); |
| 200 | + |
| 201 | + expect(JSON.stringify(envelope)).not.toContain(T_SAVE); |
| 202 | + expect(Object.keys(envelope)).not.toContain('lastModified'); |
| 203 | + }); |
| 204 | +}); |
0 commit comments