|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// ADR-0085 semantic roles — runtime proof over the SERVED pipeline. |
| 4 | +// |
| 5 | +// @proof: semantic-roles-served |
| 6 | +// The object semantic roles (`highlightFields` / `stageField` / |
| 7 | +// `fieldGroups.collapse`) are exhaustively unit-tested at parse time in |
| 8 | +// `@objectstack/spec`, but a parse-level green says nothing about the pipeline |
| 9 | +// a real renderer consumes: defineStack → artifact → registry → REST |
| 10 | +// serialization. That pipeline is exactly where the pre-ADR-0085 dialects |
| 11 | +// silently died (spec-authored `defaultExpanded` never reached the form; |
| 12 | +// `views.form.sections` never existed at all), and where a serializer |
| 13 | +// whitelist or a boot-cached merge can strip a key without any static check |
| 14 | +// noticing. These assertions run against the real Hono app over HTTP. |
| 15 | +// |
| 16 | +// Fixtures: `showcase_semantic_zoo` (canonical spellings) and |
| 17 | +// `showcase_semantic_zoo_legacy` (deprecated spellings) in |
| 18 | +// `examples/app-showcase/src/objects/semantic-zoo.object.ts`. |
| 19 | + |
| 20 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 21 | +import showcaseStack from '@objectstack/example-showcase'; |
| 22 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 23 | +import { deriveFieldGroupLayout } from '@objectstack/spec/data'; |
| 24 | + |
| 25 | +let stack: VerifyStack; |
| 26 | +let token: string; |
| 27 | + |
| 28 | +async function servedObject(name: string): Promise<Record<string, any>> { |
| 29 | + const res = await stack.apiAs(token, 'GET', `/meta/objects/${name}`); |
| 30 | + expect(res.status).toBe(200); |
| 31 | + const body = await res.json(); |
| 32 | + return (body as any).data ?? body; |
| 33 | +} |
| 34 | + |
| 35 | +beforeAll(async () => { |
| 36 | + stack = await bootStack(showcaseStack); |
| 37 | + token = await stack.signIn(); |
| 38 | +}); |
| 39 | + |
| 40 | +afterAll(async () => { |
| 41 | + await stack?.stop(); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('ADR-0085 semantic roles survive the served pipeline', () => { |
| 45 | + it('canonical spellings are served verbatim, with the compactLayout transition mirror', async () => { |
| 46 | + const def = await servedObject('showcase_semantic_zoo'); |
| 47 | + |
| 48 | + expect(def.highlightFields).toEqual(['name', 'status', 'amount']); |
| 49 | + // Transition mirror for pre-11.7 renderers (ObjectGrid & friends read the |
| 50 | + // old spelling). Deliberately asserted: when the deprecated alias is |
| 51 | + // retired, this expectation is DELETED IN THE SAME PR — a red here after |
| 52 | + // that removal means the retirement missed a consumer. |
| 53 | + expect(def.compactLayout).toEqual(['name', 'status', 'amount']); |
| 54 | + |
| 55 | + expect(def.stageField).toBe('status'); |
| 56 | + |
| 57 | + expect(def.fieldGroups).toEqual([ |
| 58 | + { key: 'basics', label: 'Basics', collapse: 'none' }, |
| 59 | + { key: 'money', label: 'Money', collapse: 'collapsed' }, |
| 60 | + ]); |
| 61 | + expect(def.fields?.status?.group).toBe('basics'); |
| 62 | + expect(def.fields?.amount?.group).toBe('money'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('deprecated spellings alias onto the canonical keys when served', async () => { |
| 66 | + const def = await servedObject('showcase_semantic_zoo_legacy'); |
| 67 | + |
| 68 | + // compactLayout (deprecated) must surface as highlightFields… |
| 69 | + expect(def.highlightFields).toEqual(['name', 'amount']); |
| 70 | + // …and stay readable under the old name (ADR-0079 alias pattern). |
| 71 | + expect(def.compactLayout).toEqual(['name', 'amount']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('stageField:false survives serialization as a strict false', async () => { |
| 75 | + const def = await servedObject('showcase_semantic_zoo_legacy'); |
| 76 | + |
| 77 | + // `false` is the only "this status-shaped field is NOT a lifecycle" signal. |
| 78 | + // It has to arrive as a STRICT false: a serializer that drops falsy values |
| 79 | + // (or a renderer falsy-check) silently turns the stepper back on — the |
| 80 | + // exact bug PR objectui#2168 fixed. The fixture's `status` field is named |
| 81 | + // to trip the heuristic if this suppression ever leaks. |
| 82 | + expect(def.stageField).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it('the served metadata composes with the shared fieldGroups derivation', async () => { |
| 86 | + const def = await servedObject('showcase_semantic_zoo'); |
| 87 | + |
| 88 | + // Every renderer (form / modal / detail) is a thin adapter over this one |
| 89 | + // function (ADR-0085 §5), so served-def × derivation IS the layout |
| 90 | + // contract: declared order, collapse passthrough, trailing untitled |
| 91 | + // bucket for ungrouped fields. |
| 92 | + const sections = deriveFieldGroupLayout(def); |
| 93 | + expect(sections).not.toBeNull(); |
| 94 | + expect(sections!.map((s) => s.key)).toEqual(['basics', 'money', undefined]); |
| 95 | + expect(sections![0].fields).toContain('status'); |
| 96 | + expect(sections![1]).toMatchObject({ key: 'money', collapse: 'collapsed', fields: ['amount'] }); |
| 97 | + // Ungrouped trailing bucket keeps `notes` visible (never silently dropped). |
| 98 | + expect(sections![2].fields).toContain('notes'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('the fixture objects are real, writable objects (not metadata ghosts)', async () => { |
| 102 | + const created = await stack.apiAs(token, 'POST', '/data/showcase_semantic_zoo', { |
| 103 | + name: 'roles-roundtrip', |
| 104 | + status: 'active', |
| 105 | + amount: 7, |
| 106 | + }); |
| 107 | + expect(created.status, `create failed: ${created.status}`).toBeLessThan(300); |
| 108 | + const createdBody = (await created.json()) as any; |
| 109 | + const id = createdBody.id ?? createdBody.record?.id ?? createdBody.data?.id; |
| 110 | + expect(id, 'no id returned from create').toBeTruthy(); |
| 111 | + |
| 112 | + const got = await stack.apiAs(token, 'GET', `/data/showcase_semantic_zoo/${id}`); |
| 113 | + expect(got.status).toBe(200); |
| 114 | + const gotBody = (await got.json()) as any; |
| 115 | + const rec = gotBody.record ?? gotBody.data ?? gotBody; |
| 116 | + expect(rec.status).toBe('active'); |
| 117 | + expect(rec.amount).toBe(7); |
| 118 | + }); |
| 119 | +}); |
0 commit comments