|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #29212 — the dashboard save door refuses a dashboard that declares one |
| 5 | + * global filter key twice (the #29023 refusal), in every save mode. |
| 6 | + * |
| 7 | + * The rule lives in the spec parse (`duplicateGlobalFilterNames`, the |
| 8 | + * `globalFilters` refinement of `DashboardSchema`), and `saveMetaItem` parses |
| 9 | + * with that schema, so `PUT /api/v1/meta/dashboard/X` answers |
| 10 | + * `422 INVALID_METADATA` at the second declaration's key and stores nothing, |
| 11 | + * with no mode, with `mode: 'publish'` and with `mode: 'draft'` alike. This |
| 12 | + * pins that the door keeps reading the refusal; the v18 stored listing |
| 13 | + * (`refused-shapes.ts`) names a row already stored in the shape. |
| 14 | + */ |
| 15 | +import { describe, expect, it } from 'vitest'; |
| 16 | +import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core'; |
| 17 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 18 | + |
| 19 | +/** The engine surface the repository write path touches (as the container-issue-descent harness). */ |
| 20 | +function makeProtocol() { |
| 21 | + const rows = new Map<string, Record<string, unknown>>(); |
| 22 | + let nextId = 0; |
| 23 | + const engine: any = { |
| 24 | + async findOne(object: string, query?: EngineFindOneQueryInput) { |
| 25 | + assertEngineFindOnePredicate(object, query); return null; }, |
| 26 | + async find() { return []; }, |
| 27 | + async insert(table: string, data: Record<string, unknown>) { |
| 28 | + nextId += 1; |
| 29 | + if (table === 'sys_metadata') rows.set(`${data.type}|${data.name}`, data); |
| 30 | + return { id: `r_${nextId}` }; |
| 31 | + }, |
| 32 | + async update(_t: string, data: Record<string, unknown>, opts?: Record<string, unknown>) { |
| 33 | + assertEngineUpdateDispatch(data, opts); |
| 34 | + return { id: null }; |
| 35 | + }, |
| 36 | + async delete(_t: string, opts?: Record<string, unknown>) { |
| 37 | + assertEngineDeleteDispatch(opts); |
| 38 | + return { deleted: 0 }; |
| 39 | + }, |
| 40 | + registry: { registerItem: () => {}, registerObject: () => {} }, |
| 41 | + }; |
| 42 | + const protocol: any = new ObjectStackProtocolImplementation(engine, () => new Map()); |
| 43 | + return { protocol, rows }; |
| 44 | +} |
| 45 | + |
| 46 | +async function rejection(promise: Promise<unknown>): Promise<any> { |
| 47 | + try { |
| 48 | + await promise; |
| 49 | + } catch (err) { |
| 50 | + return err; |
| 51 | + } |
| 52 | + throw new Error('expected the save to be rejected, but it resolved'); |
| 53 | +} |
| 54 | + |
| 55 | +const MODES = [ |
| 56 | + ['no mode', {}], |
| 57 | + ['publish', { mode: 'publish' }], |
| 58 | + ['draft', { mode: 'draft' }], |
| 59 | +] as const; |
| 60 | + |
| 61 | +const boardWith = (globalFilters: unknown[]) => ({ |
| 62 | + name: 'dup_board', |
| 63 | + label: 'Dup Board', |
| 64 | + widgets: [], |
| 65 | + globalFilters, |
| 66 | +}); |
| 67 | + |
| 68 | +describe('#29212 the save door refuses a dashboard that declares one global filter key twice', () => { |
| 69 | + for (const [label, mode] of MODES) { |
| 70 | + it(`${label}: two filters named \`r\` answer 422 INVALID_METADATA at globalFilters.1.name, and nothing is stored`, async () => { |
| 71 | + const { protocol, rows } = makeProtocol(); |
| 72 | + |
| 73 | + const err = await rejection(protocol.saveMetaItem({ |
| 74 | + type: 'dashboard', name: 'dup_board', ...mode, item: boardWith([ |
| 75 | + { name: 'r', field: 'region', type: 'text' }, |
| 76 | + { name: 'r', field: 'status', type: 'text' }, |
| 77 | + ]), |
| 78 | + })); |
| 79 | + |
| 80 | + expect(err.code).toBe('INVALID_METADATA'); |
| 81 | + expect(err.status).toBe(422); |
| 82 | + expect(err.issues).toEqual([expect.objectContaining({ path: 'globalFilters.1.name', code: 'custom' })]); |
| 83 | + expect(err.issues[0].message.startsWith( |
| 84 | + "This dashboard declares the global filter 'r' twice in globalFilters: globalFilters[0] and globalFilters[1].", |
| 85 | + )).toBe(true); |
| 86 | + expect(rows.size).toBe(0); |
| 87 | + }); |
| 88 | + |
| 89 | + it(`${label}: control, the same dashboard with distinct filter names saves`, async () => { |
| 90 | + const { protocol, rows } = makeProtocol(); |
| 91 | + |
| 92 | + const result = await protocol.saveMetaItem({ |
| 93 | + type: 'dashboard', name: 'dup_board', ...mode, item: boardWith([ |
| 94 | + { name: 'r', field: 'region', type: 'text' }, |
| 95 | + { name: 's', field: 'status', type: 'text' }, |
| 96 | + ]), |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result.success).toBe(true); |
| 100 | + expect(rows.size).toBe(1); |
| 101 | + }); |
| 102 | + } |
| 103 | +}); |
0 commit comments