|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 5 | + |
| 6 | +/** |
| 7 | + * #2555 — a console personalization PUT (grid column sort, inline edit, …) |
| 8 | + * sends only the raw view config: no top-level `viewKind`/`object`. Pre-fix, |
| 9 | + * `saveMetaItem` persisted it verbatim and `getMetaItems` replaced the |
| 10 | + * flattened package entry with the overlay row wholesale, so the identity |
| 11 | + * fields vanished and the view switcher endpoint (which filters on |
| 12 | + * `viewKind && object`) dropped the view permanently. |
| 13 | + * |
| 14 | + * Two independent guards are covered here end-to-end against a stubbed engine: |
| 15 | + * • write path — `saveMetaItem` inherits the identity fields from the |
| 16 | + * registry entry the overlay shadows before persisting; |
| 17 | + * • read path — `getMetaItems` heals identity-less rows already in the DB |
| 18 | + * (persisted by pre-fix saves) from the shadowed registry entry. |
| 19 | + */ |
| 20 | + |
| 21 | +// The flattened package entry `expandViewContainer` produces for the |
| 22 | +// showcase's default task grid — the entry the runtime overlay shadows. |
| 23 | +const flattened = { |
| 24 | + name: 'showcase_task.default', |
| 25 | + object: 'showcase_task', |
| 26 | + viewKind: 'list', |
| 27 | + label: 'All Tasks', |
| 28 | + scope: 'package', |
| 29 | + config: { type: 'grid', data: { provider: 'object', object: 'showcase_task' }, columns: ['title'] }, |
| 30 | +}; |
| 31 | + |
| 32 | +// What the console actually PUTs back on a column sort — the view's raw |
| 33 | +// config plus personalization state, no identity fields (captured from the |
| 34 | +// sys_metadata row in the 3777 repro). |
| 35 | +const personalization = { |
| 36 | + type: 'grid', |
| 37 | + data: { provider: 'object', object: 'showcase_task' }, |
| 38 | + columns: ['title'], |
| 39 | + sort: [{ id: '29200fa8-c416-471e-9ca3-913f9308ad89', field: 'estimate_hours', order: 'desc' }], |
| 40 | +}; |
| 41 | + |
| 42 | +interface Row { |
| 43 | + id: string; |
| 44 | + type: string; |
| 45 | + name: string; |
| 46 | + organization_id: string | null; |
| 47 | + state: string; |
| 48 | + metadata: string; |
| 49 | + package_id?: string | null; |
| 50 | +} |
| 51 | + |
| 52 | +function makeStubEngine(registryViews: Record<string, unknown> = {}) { |
| 53 | + const rows = new Map<string, Row>(); |
| 54 | + let nextId = 0; |
| 55 | + const keyOf = (w: Record<string, unknown>) => `${w.type}|${w.name}|${w.organization_id ?? '__env__'}`; |
| 56 | + const findRow = (w: Record<string, unknown>) => { |
| 57 | + if (w.id !== undefined) { |
| 58 | + for (const [k, r] of rows) if (r.id === w.id) return { key: k, row: r }; |
| 59 | + return null; |
| 60 | + } |
| 61 | + const r = rows.get(keyOf(w)); |
| 62 | + return r ? { key: keyOf(w), row: r } : null; |
| 63 | + }; |
| 64 | + const engine: any = { |
| 65 | + async findOne(_t: string, opts: { where: Record<string, unknown> }) { |
| 66 | + return findRow(opts.where)?.row ?? null; |
| 67 | + }, |
| 68 | + async find(_t: string, opts: { where: Record<string, unknown> }) { |
| 69 | + return Array.from(rows.values()).filter((r) => { |
| 70 | + if (opts.where.type && r.type !== opts.where.type) return false; |
| 71 | + if (opts.where.organization_id !== undefined && r.organization_id !== opts.where.organization_id) return false; |
| 72 | + if (opts.where.state && r.state !== opts.where.state) return false; |
| 73 | + return true; |
| 74 | + }); |
| 75 | + }, |
| 76 | + async insert(_t: string, data: Record<string, unknown>) { |
| 77 | + if (_t === 'sys_metadata_audit') return { id: 'audit_skip' }; |
| 78 | + nextId += 1; |
| 79 | + const row = { id: `r_${nextId}`, ...(data as any) } as Row; |
| 80 | + rows.set(keyOf(data), row); |
| 81 | + return { id: row.id }; |
| 82 | + }, |
| 83 | + async update(_t: string, data: Record<string, unknown>, opts: { where: Record<string, unknown> }) { |
| 84 | + const found = findRow(opts.where); |
| 85 | + if (!found) return { id: null }; |
| 86 | + rows.set(found.key, { ...found.row, ...(data as any) }); |
| 87 | + return { id: found.row.id }; |
| 88 | + }, |
| 89 | + async delete(_t: string, opts: { where: Record<string, unknown> }) { |
| 90 | + const found = findRow(opts.where); |
| 91 | + if (!found) return { deleted: 0 }; |
| 92 | + rows.delete(found.key); |
| 93 | + return { deleted: 1 }; |
| 94 | + }, |
| 95 | + registry: { |
| 96 | + registerItem: () => {}, |
| 97 | + registerObject: () => {}, |
| 98 | + getItem: (type: string, name: string) => (type === 'view' || type === 'views') ? registryViews[name] : undefined, |
| 99 | + listItems: (type: string) => (type === 'view' || type === 'views') ? Object.values(registryViews) : [], |
| 100 | + isPackageDisabled: () => false, |
| 101 | + }, |
| 102 | + }; |
| 103 | + return { engine, rows }; |
| 104 | +} |
| 105 | + |
| 106 | +describe('view overlay identity (#2555)', () => { |
| 107 | + it('write path: saveMetaItem inherits viewKind/object/label from the shadowed registry entry', async () => { |
| 108 | + const { engine, rows } = makeStubEngine({ 'showcase_task.default': flattened }); |
| 109 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 110 | + const result = await protocol.saveMetaItem({ |
| 111 | + type: 'view', |
| 112 | + name: 'showcase_task.default', |
| 113 | + item: { ...personalization }, |
| 114 | + }); |
| 115 | + expect(result.success).toBe(true); |
| 116 | + const row = Array.from(rows.values()).find((r) => r.type === 'view'); |
| 117 | + expect(row).toBeTruthy(); |
| 118 | + const persisted = JSON.parse(row!.metadata); |
| 119 | + // Identity inherited… |
| 120 | + expect(persisted.viewKind).toBe('list'); |
| 121 | + expect(persisted.object).toBe('showcase_task'); |
| 122 | + expect(persisted.label).toBe('All Tasks'); |
| 123 | + expect(persisted.name).toBe('showcase_task.default'); |
| 124 | + // …and the personalization survives untouched. |
| 125 | + expect(persisted.sort).toEqual(personalization.sort); |
| 126 | + }); |
| 127 | + |
| 128 | + it('read path: getMetaItems heals a pre-fix identity-less overlay row from the shadowed entry', async () => { |
| 129 | + const { engine } = makeStubEngine({ 'showcase_task.default': flattened }); |
| 130 | + // Seed the DB with a PRE-fix row: raw config + name, no identity. |
| 131 | + await engine.insert('sys_metadata', { |
| 132 | + type: 'view', |
| 133 | + name: 'showcase_task.default', |
| 134 | + organization_id: null, |
| 135 | + state: 'active', |
| 136 | + metadata: JSON.stringify({ ...personalization, name: 'showcase_task.default' }), |
| 137 | + }); |
| 138 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 139 | + const items = ((await protocol.getMetaItems({ type: 'view' })) as any).items as any[]; |
| 140 | + const item = items.find((i) => i?.name === 'showcase_task.default'); |
| 141 | + expect(item).toBeTruthy(); |
| 142 | + // The overlay still wins on content… |
| 143 | + expect(item.sort).toEqual(personalization.sort); |
| 144 | + // …but the identity fields the switcher filters on are back. |
| 145 | + expect(item.viewKind).toBe('list'); |
| 146 | + expect(item.object).toBe('showcase_task'); |
| 147 | + expect(item.label).toBe('All Tasks'); |
| 148 | + }); |
| 149 | + |
| 150 | + it("read path: an overlay's own identity fields are not clobbered by the shadowed entry", async () => { |
| 151 | + const { engine } = makeStubEngine({ 'showcase_task.default': flattened }); |
| 152 | + await engine.insert('sys_metadata', { |
| 153 | + type: 'view', |
| 154 | + name: 'showcase_task.default', |
| 155 | + organization_id: null, |
| 156 | + state: 'active', |
| 157 | + metadata: JSON.stringify({ |
| 158 | + ...personalization, |
| 159 | + name: 'showcase_task.default', |
| 160 | + viewKind: 'list', |
| 161 | + object: 'showcase_task', |
| 162 | + label: 'My Renamed Grid', |
| 163 | + }), |
| 164 | + }); |
| 165 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 166 | + const items = ((await protocol.getMetaItems({ type: 'view' })) as any).items as any[]; |
| 167 | + const item = items.find((i) => i?.name === 'showcase_task.default'); |
| 168 | + expect(item.label).toBe('My Renamed Grid'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('write path stays a plain name-stamp when the registry has no entry to inherit from', async () => { |
| 172 | + const { engine, rows } = makeStubEngine(); |
| 173 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 174 | + const result = await protocol.saveMetaItem({ |
| 175 | + type: 'view', |
| 176 | + name: 'adhoc.view', |
| 177 | + item: { ...personalization }, |
| 178 | + }); |
| 179 | + expect(result.success).toBe(true); |
| 180 | + const row = Array.from(rows.values()).find((r) => r.type === 'view'); |
| 181 | + const persisted = JSON.parse(row!.metadata); |
| 182 | + expect(persisted.name).toBe('adhoc.view'); |
| 183 | + expect('viewKind' in persisted).toBe(false); |
| 184 | + }); |
| 185 | +}); |
0 commit comments