|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29596] The stored-row conversion notice names the item by the identity |
| 5 | + * its type uses, and the warn-once key separates rows by it. |
| 6 | + * |
| 7 | + * An `object_extension` body carries `extend` and no `name` |
| 8 | + * (`ObjectExtensionSchema` refuses one; its row is stored under the target's |
| 9 | + * name). Since #29589 a stored extension row replays the `objectExtensions[]` |
| 10 | + * conversions, so `convertStoredItem` warns for it. Reading `data.name`, it |
| 11 | + * warned every stale extension as `object_extension/<unnamed>`, and the key |
| 12 | + * `conversionId|object_extension|` collapsed every row to ONE notice per |
| 13 | + * conversion. Measured on a real store (SQLite + ObjectQL + the protocol's |
| 14 | + * save door, two published extensions made stale): six reads, `data.name` |
| 15 | + * absent on every one, and a single `<unnamed>` line for the two rows. |
| 16 | + * |
| 17 | + * Driven through the module's own `convertStoredItem` over the real |
| 18 | + * conversion chain (`convertStoredItemDetailed`), with the warn-once set the |
| 19 | + * class hands it. |
| 20 | + */ |
| 21 | + |
| 22 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 23 | +import { convertStoredItem, convertStoredItemDetailed } from './protocol-stored-item-conversion.js'; |
| 24 | + |
| 25 | +/** A host shaped like the one the class builds: the real chain, a fresh warn-once set. */ |
| 26 | +const host = () => ({ convertStoredItemDetailed, storedConversionWarned: new Set<string>() }); |
| 27 | + |
| 28 | +/** A stale extension: the `Z`-suffixed `time` default `time-default-utc-suffix-dropped` converts. */ |
| 29 | +const staleExtension = (target: string) => ({ |
| 30 | + extend: target, |
| 31 | + fields: { opens_at__c: { type: 'time', label: 'Opens', defaultValue: '12:00Z' } }, |
| 32 | +}); |
| 33 | + |
| 34 | +let warn: ReturnType<typeof vi.spyOn>; |
| 35 | +beforeEach(() => { |
| 36 | + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 37 | +}); |
| 38 | +afterEach(() => { |
| 39 | + warn.mockRestore(); |
| 40 | +}); |
| 41 | + |
| 42 | +/** The `stored TYPE/NAME` subject of each conversion notice, in order. */ |
| 43 | +const subjects = () => warn.mock.calls |
| 44 | + .map((c) => String(c[0])) |
| 45 | + .filter((line) => line.includes('carries a pre-protocol shape')) |
| 46 | + .map((line) => /stored (\S+) carries/.exec(line)?.[1]); |
| 47 | + |
| 48 | +describe('[#29596] a stored conversion notice names the item by its identity', () => { |
| 49 | + it('two stale extension rows give two notices, each naming its target', () => { |
| 50 | + const h = host(); |
| 51 | + const account = convertStoredItem.call(h, 'object_extension', staleExtension('crm_account')); |
| 52 | + const contact = convertStoredItem.call(h, 'object_extension', staleExtension('crm_contact')); |
| 53 | + |
| 54 | + expect(subjects()).toEqual(['object_extension/crm_account', 'object_extension/crm_contact']); |
| 55 | + // The rows still convert, and the body is not given a `name`. |
| 56 | + expect(account).toEqual({ extend: 'crm_account', fields: { opens_at__c: { type: 'time', label: 'Opens', defaultValue: '12:00' } } }); |
| 57 | + expect(contact).not.toHaveProperty('name'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('the same extension row read again warns once — the dedupe still holds per row', () => { |
| 61 | + const h = host(); |
| 62 | + convertStoredItem.call(h, 'object_extension', staleExtension('crm_account')); |
| 63 | + convertStoredItem.call(h, 'object_extension', staleExtension('crm_account')); |
| 64 | + convertStoredItem.call(h, 'object_extension', staleExtension('crm_contact')); |
| 65 | + |
| 66 | + expect(subjects()).toEqual(['object_extension/crm_account', 'object_extension/crm_contact']); |
| 67 | + }); |
| 68 | + |
| 69 | + it('CONTROL — a non-extension type is still named by its `name`, and a nameless body is still `<unnamed>`', () => { |
| 70 | + const h = host(); |
| 71 | + const object = (name?: string) => ({ |
| 72 | + ...(name === undefined ? {} : { name }), |
| 73 | + fields: { opens_at: { type: 'time', defaultValue: '12:00Z' } }, |
| 74 | + }); |
| 75 | + convertStoredItem.call(h, 'object', object('shift')); |
| 76 | + convertStoredItem.call(h, 'object', object('rota')); |
| 77 | + convertStoredItem.call(h, 'object', object()); |
| 78 | + |
| 79 | + expect(subjects()).toEqual(['object/shift', 'object/rota', 'object/<unnamed>']); |
| 80 | + }); |
| 81 | + |
| 82 | + it('CONTROL — an `extend` key on another type does not name it', () => { |
| 83 | + const h = host(); |
| 84 | + convertStoredItem.call(h, 'object', { extend: 'crm_account', fields: { opens_at: { type: 'time', defaultValue: '12:00Z' } } }); |
| 85 | + |
| 86 | + expect(subjects()).toEqual(['object/<unnamed>']); |
| 87 | + }); |
| 88 | +}); |
0 commit comments