|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #26711 — the `view` write door refuses an unknown TOP-LEVEL key, as the |
| 5 | + * object, page and dashboard doors do. |
| 6 | + * |
| 7 | + * Three of `ViewMetadataSchema`'s four members — the ViewItem record and the |
| 8 | + * two flattened overlays — used to `.strip()` their top level. Since the save |
| 9 | + * stores the parsed body (#20051), a misspelt or retired key answered success |
| 10 | + * and was simply not stored. The container member was already closed. |
| 11 | + * |
| 12 | + * Pinned per member, because each is closed on its own: the refusal (code, |
| 13 | + * key, the member named), a retired key's prescription in both channels the |
| 14 | + * protocol has for it, the alias spellings the #20456 census found, and a |
| 15 | + * clean body of each shape still saving with what the console writes. |
| 16 | + */ |
| 17 | +import { describe, expect, it } from 'vitest'; |
| 18 | +import { z } from 'zod'; |
| 19 | + |
| 20 | +import { RETIRED_KEY_UPGRADE_GUIDE_SENTENCE } from '../shared/protocol-upgrade-guide'; |
| 21 | +import { ViewMetadataSchema, diagnoseViewMetadata, type ViewMetadataBranch } from './view.zod'; |
| 22 | + |
| 23 | +const DATA = { provider: 'object', object: 'crm_lead' } as const; |
| 24 | + |
| 25 | +/** One clean body per member the change closed, as the write path delivers it. */ |
| 26 | +const CLEAN: Record<'viewItem' | 'listOverlay' | 'formOverlay', { body: Record<string, unknown>; surface: string }> = { |
| 27 | + viewItem: { |
| 28 | + body: { |
| 29 | + name: 'crm_lead.mine', |
| 30 | + object: 'crm_lead', |
| 31 | + viewKind: 'list', |
| 32 | + label: 'Mine', |
| 33 | + config: { type: 'grid', data: DATA, columns: ['name'] }, |
| 34 | + isPinned: true, |
| 35 | + }, |
| 36 | + surface: 'this view item record', |
| 37 | + }, |
| 38 | + listOverlay: { |
| 39 | + body: { name: 'crm_lead.all', object: 'crm_lead', viewKind: 'list', type: 'grid', columns: ['name'], isPinned: true }, |
| 40 | + surface: 'this inline list view', |
| 41 | + }, |
| 42 | + formOverlay: { |
| 43 | + body: { name: 'crm_lead.edit', object: 'crm_lead', viewKind: 'form', type: 'simple' }, |
| 44 | + surface: 'this inline form view', |
| 45 | + }, |
| 46 | +}; |
| 47 | + |
| 48 | +const MEMBERS = Object.keys(CLEAN) as Array<keyof typeof CLEAN>; |
| 49 | + |
| 50 | +/** The claimed member's issues, read the way Studio and an MCP author get them. */ |
| 51 | +function refusal(body: unknown): { branch: ViewMetadataBranch | null; issues: readonly z.core.$ZodIssue[] } { |
| 52 | + const parsed = ViewMetadataSchema.safeParse(body); |
| 53 | + expect(parsed.success, 'expected the write door to refuse this body').toBe(false); |
| 54 | + const diagnosis = diagnoseViewMetadata(body); |
| 55 | + if (diagnosis.success) throw new Error('diagnoseViewMetadata accepted a body the union refused'); |
| 56 | + return { branch: diagnosis.branch, issues: diagnosis.issues }; |
| 57 | +} |
| 58 | + |
| 59 | +describe('a clean body of each closed member still saves', () => { |
| 60 | + it.each(MEMBERS)('%s', (member) => { |
| 61 | + const r = ViewMetadataSchema.safeParse(CLEAN[member].body); |
| 62 | + expect(r.success, JSON.stringify(r.success ? null : r.error.issues)).toBe(true); |
| 63 | + expect(diagnoseViewMetadata(CLEAN[member].body)).toMatchObject({ success: true, branch: member }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("the console's row ids are still removed before the parse, not refused", () => { |
| 67 | + const r = ViewMetadataSchema.safeParse({ |
| 68 | + ...CLEAN.listOverlay.body, |
| 69 | + sort: [{ id: 'row-1', field: 'name', order: 'asc' }], |
| 70 | + filter: [{ id: 'row-2', field: 'name', operator: 'equals', value: 'x' }], |
| 71 | + }); |
| 72 | + expect(r.success).toBe(true); |
| 73 | + const data = r.data as { sort: unknown[]; filter: unknown[] }; |
| 74 | + expect(data.sort).toEqual([{ field: 'name', order: 'asc' }]); |
| 75 | + expect(data.filter).toEqual([{ field: 'name', operator: 'equals', value: 'x' }]); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('an unknown top-level key is refused by name', () => { |
| 80 | + it.each(MEMBERS)('%s', (member) => { |
| 81 | + const { branch, issues } = refusal({ ...CLEAN[member].body, zzz_junk_key: true }); |
| 82 | + expect(branch).toBe(member); |
| 83 | + expect(issues).toHaveLength(1); |
| 84 | + expect(issues[0]).toMatchObject({ code: 'unrecognized_keys', path: [], keys: ['zzz_junk_key'] }); |
| 85 | + expect(issues[0]!.message).toMatch( |
| 86 | + new RegExp(`^Unrecognized key\\(s\\) on ${CLEAN[member].surface}: \`zzz_junk_key\`\\.`), |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('the container member was already closed, and still is', () => { |
| 91 | + const { branch, issues } = refusal({ object: 'crm_lead', list: { type: 'grid', data: DATA, columns: ['name'] }, zzz_junk_key: true }); |
| 92 | + expect(branch).toBe('container'); |
| 93 | + expect(issues[0]).toMatchObject({ code: 'unrecognized_keys', keys: ['zzz_junk_key'] }); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("a retired key gets the prescription its retirement kit gives it", () => { |
| 98 | + // `striped` was retired with the list view's other display keys, and its kit |
| 99 | + // has aged out (ADR-0087, amendment 2026-10-26): the refusal names the key and |
| 100 | + // points at the protocol upgrade guide, which holds its prescription now. |
| 101 | + // Before this change the overlay door answered success and dropped it. |
| 102 | + it.each(['listOverlay', 'viewItem'] as const)('`striped` (aged out) on %s: named, with the upgrade guide', (member) => { |
| 103 | + const { branch, issues } = refusal({ ...CLEAN[member].body, striped: true }); |
| 104 | + expect(branch).toBe(member); |
| 105 | + expect(issues[0]).toMatchObject({ code: 'unrecognized_keys', keys: ['striped'] }); |
| 106 | + expect(issues[0]!.message).toContain(RETIRED_KEY_UPGRADE_GUIDE_SENTENCE); |
| 107 | + }); |
| 108 | + |
| 109 | + // `filterableFields` still has its live tombstone on the list view shape, so |
| 110 | + // the overlay raises its own prescription at the key, and the union lifts it |
| 111 | + // to the top-level message an author at `PUT /api/v1/meta/view` reads. |
| 112 | + it('`filterableFields` (live tombstone) on the inline list view: its prescription, at the key', () => { |
| 113 | + const body = { ...CLEAN.listOverlay.body, filterableFields: ['name'] }; |
| 114 | + const { branch, issues } = refusal(body); |
| 115 | + expect(branch).toBe('listOverlay'); |
| 116 | + expect(issues[0]).toMatchObject({ code: 'invalid_type', expected: 'never', path: ['filterableFields'] }); |
| 117 | + const parsed = ViewMetadataSchema.safeParse(body); |
| 118 | + expect(parsed.success ? '' : parsed.error.issues[0]!.message).toMatch(/^`view\.list\.filterableFields` was removed in @objectstack\/spec 18/); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('the alias spellings the census found point at the declared key', () => { |
| 123 | + it.each(MEMBERS)('`objectName` on %s → `object`', (member) => { |
| 124 | + const { issues } = refusal({ ...CLEAN[member].body, objectName: 'crm_lead' }); |
| 125 | + expect(issues[0]).toMatchObject({ code: 'unrecognized_keys', keys: ['objectName'] }); |
| 126 | + expect(issues[0]!.message).toContain('Did you mean `objectName` → `object`?'); |
| 127 | + }); |
| 128 | + |
| 129 | + it.each(MEMBERS)('a top-level `id` on %s → `name`', (member) => { |
| 130 | + const { issues } = refusal({ ...CLEAN[member].body, id: 'crm_lead.mine' }); |
| 131 | + expect(issues[0]).toMatchObject({ code: 'unrecognized_keys', keys: ['id'] }); |
| 132 | + expect(issues[0]!.message).toContain('Did you mean `id` → `name`?'); |
| 133 | + }); |
| 134 | +}); |
0 commit comments