|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #25013 — a `Field.select` shorthand string that is no system identifier is |
| 5 | + * refused at the object save door, by the refusal an explicit option value |
| 6 | + * already meets. |
| 7 | + * |
| 8 | + * The shorthand (`packages/spec` `selectWithOptions`) used to delete every |
| 9 | + * character outside `[a-z0-9_]`, so `Field.select(['no-overlay'])` declared |
| 10 | + * the value `nooverlay` — a different value than the one written, accepted |
| 11 | + * without a word. It now only lower-cases and joins whitespace with `_`, so |
| 12 | + * the written value reaches `SelectOptionSchema.value` and `saveMetaItem` |
| 13 | + * answers `422 INVALID_METADATA` naming the option slot, exactly as for |
| 14 | + * `{ label: 'no-overlay', value: 'no-overlay' }`. |
| 15 | + */ |
| 16 | +import { describe, expect, it } from 'vitest'; |
| 17 | +import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core'; |
| 18 | +import { Field } from '@objectstack/spec/data'; |
| 19 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 20 | + |
| 21 | +/** The engine surface the repository write path touches (as the container-issue-descent harness). */ |
| 22 | +function makeProtocol() { |
| 23 | + const rows = new Map<string, Record<string, unknown>>(); |
| 24 | + let nextId = 0; |
| 25 | + const engine: any = { |
| 26 | + async findOne(object: string, query?: EngineFindOneQueryInput) { |
| 27 | + assertEngineFindOnePredicate(object, query); return null; }, |
| 28 | + async find() { return []; }, |
| 29 | + async insert(table: string, data: Record<string, unknown>) { |
| 30 | + nextId += 1; |
| 31 | + if (table === 'sys_metadata') rows.set(`${data.type}|${data.name}`, data); |
| 32 | + return { id: `r_${nextId}` }; |
| 33 | + }, |
| 34 | + async update(_t: string, data: Record<string, unknown>, opts?: Record<string, unknown>) { |
| 35 | + assertEngineUpdateDispatch(data, opts); |
| 36 | + return { id: null }; |
| 37 | + }, |
| 38 | + async delete(_t: string, opts?: Record<string, unknown>) { |
| 39 | + assertEngineDeleteDispatch(opts); |
| 40 | + return { deleted: 0 }; |
| 41 | + }, |
| 42 | + registry: { registerItem: () => {}, registerObject: () => {} }, |
| 43 | + }; |
| 44 | + const protocol: any = new ObjectStackProtocolImplementation(engine, () => new Map()); |
| 45 | + return { protocol, rows }; |
| 46 | +} |
| 47 | + |
| 48 | +async function rejection(promise: Promise<unknown>): Promise<any> { |
| 49 | + try { |
| 50 | + await promise; |
| 51 | + } catch (err) { |
| 52 | + return err; |
| 53 | + } |
| 54 | + throw new Error('expected the save to be rejected, but it resolved'); |
| 55 | +} |
| 56 | + |
| 57 | +const objectWith = (status: unknown) => ({ |
| 58 | + name: 'ticket', |
| 59 | + label: 'Ticket', |
| 60 | + sharingModel: 'private', |
| 61 | + fields: { status: { name: 'status', label: 'Status', ...(status as object) } }, |
| 62 | +}); |
| 63 | + |
| 64 | +describe('#25013 the save door refuses a shorthand option that is no system identifier', () => { |
| 65 | + it('`Field.select([\'open\', \'no-overlay\'])` answers 422 INVALID_METADATA at the option slot, and nothing is stored', async () => { |
| 66 | + const { protocol, rows } = makeProtocol(); |
| 67 | + |
| 68 | + const err = await rejection(protocol.saveMetaItem({ |
| 69 | + type: 'object', name: 'ticket', item: objectWith(Field.select(['open', 'no-overlay'])), |
| 70 | + })); |
| 71 | + |
| 72 | + expect(err.code).toBe('INVALID_METADATA'); |
| 73 | + expect(err.status).toBe(422); |
| 74 | + expect(err.issues).toEqual([ |
| 75 | + expect.objectContaining({ path: 'fields.status.options.1.value', code: 'invalid_format' }), |
| 76 | + ]); |
| 77 | + expect(rows.size).toBe(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it('the explicit `{ value: \'no-overlay\' }` meets the identical refusal — one refusal set, not two', async () => { |
| 81 | + const shorthand = await rejection(makeProtocol().protocol.saveMetaItem({ |
| 82 | + type: 'object', name: 'ticket', item: objectWith(Field.select(['open', 'no-overlay'])), |
| 83 | + })); |
| 84 | + const explicit = await rejection(makeProtocol().protocol.saveMetaItem({ |
| 85 | + type: 'object', name: 'ticket', |
| 86 | + item: objectWith(Field.select({ options: ['open', { label: 'no-overlay', value: 'no-overlay' }] })), |
| 87 | + })); |
| 88 | + |
| 89 | + expect([explicit.code, explicit.status]).toEqual(['INVALID_METADATA', 422]); |
| 90 | + expect(shorthand.issues).toEqual(explicit.issues); |
| 91 | + }); |
| 92 | + |
| 93 | + it('control: the documented transform still saves (`In Progress` → `in_progress`)', async () => { |
| 94 | + const { protocol, rows } = makeProtocol(); |
| 95 | + |
| 96 | + const result = await protocol.saveMetaItem({ |
| 97 | + type: 'object', name: 'ticket', item: objectWith(Field.select(['Open', 'In Progress'])), |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result.success).toBe(true); |
| 101 | + expect(rows.size).toBe(1); |
| 102 | + }); |
| 103 | +}); |
0 commit comments