|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The lock banner must never render a headline-less amber box — objectui#5024. |
| 5 | + * |
| 6 | + * ## The defect |
| 7 | + * |
| 8 | + * The banner's title was three independent `&&` branches with no `else` and no |
| 9 | + * fallback: |
| 10 | + * |
| 11 | + * {layered?.lock === 'full' && t('engine.edit.lockFull', locale)} |
| 12 | + * {layered?.lock === 'no-overlay' && t('engine.edit.lockNoOverlay', locale)} |
| 13 | + * {layered?.lock === 'no-delete' && t('engine.edit.lockNoDelete', locale)} |
| 14 | + * |
| 15 | + * while the switch that OPENS the banner is `layered?.lock && lock !== 'none'` |
| 16 | + * — true for *any* non-`none` value. So a lock state outside the ADR-0010 §3.6 |
| 17 | + * four opens the amber box, draws the padlock and the border, and leaves the |
| 18 | + * title `<div>` empty: a locked-looking banner that never says what is locked |
| 19 | + * or why. |
| 20 | + * |
| 21 | + * ## Why this is reachable today, not only "the day a fifth state lands" |
| 22 | + * |
| 23 | + * The card framed it as dormant, on the reading that both hand-written unions |
| 24 | + * list the same four values. The unions are not the gate. `MetadataClient. |
| 25 | + * layered()` passes the wire value through with an unchecked cast — |
| 26 | + * |
| 27 | + * ...(body.lock !== undefined ? { lock: body.lock as MetadataLayered['lock'] } : {}), |
| 28 | + * |
| 29 | + * — over a `res.json()` body. There is no Zod parse, no allowlist, no default |
| 30 | + * on this path. The union constrains what this repo may *write*; it constrains |
| 31 | + * nothing about what a server may *send*. A backend that grows a fifth state |
| 32 | + * reaches this banner with zero code change here, which is why the fix has to |
| 33 | + * be a runtime fallback and not only a compile-time exhaustiveness check: a |
| 34 | + * `satisfies` assertion is satisfied by the current four either way and would |
| 35 | + * have left the blank banner exactly as it was. |
| 36 | + * |
| 37 | + * ## What this suite pins |
| 38 | + * |
| 39 | + * The four known states keep their existing sentences (this half passes before |
| 40 | + * the fix — it is the control that says a red fifth-value case is the defect |
| 41 | + * and not a broken harness), and an out-of-vocabulary state renders a loud, |
| 42 | + * non-empty fallback that also surfaces the raw token so the state a server |
| 43 | + * actually sent is diagnosable from the screen. |
| 44 | + */ |
| 45 | + |
| 46 | +import '@testing-library/jest-dom/vitest'; |
| 47 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 48 | +import { render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 49 | +import { MemoryRouter } from 'react-router-dom'; |
| 50 | + |
| 51 | +const objectDef = { |
| 52 | + name: 'showcase_account', |
| 53 | + label: 'Account', |
| 54 | + fields: [{ name: 'title', label: 'Title', type: 'text' }], |
| 55 | +}; |
| 56 | + |
| 57 | +const layeredImpl = { current: vi.fn() }; |
| 58 | + |
| 59 | +const mockClient = { |
| 60 | + list: vi.fn(async () => []), |
| 61 | + listDrafts: vi.fn(async () => []), |
| 62 | + get: vi.fn(async () => null), |
| 63 | + getDraft: vi.fn(async () => null), |
| 64 | + references: vi.fn(async () => []), |
| 65 | + layered: vi.fn(async (...args: unknown[]) => layeredImpl.current(...args)), |
| 66 | +}; |
| 67 | + |
| 68 | +vi.mock('./useMetadata', async (importOriginal) => { |
| 69 | + const mod = await importOriginal<typeof import('./useMetadata')>(); |
| 70 | + return { |
| 71 | + ...mod, |
| 72 | + useMetadataClient: () => mockClient, |
| 73 | + useMetadataTypes: () => ({ |
| 74 | + loading: false, |
| 75 | + error: null, |
| 76 | + entries: [ |
| 77 | + { |
| 78 | + type: 'object', |
| 79 | + name: 'object', |
| 80 | + label: 'Object', |
| 81 | + allowOrgOverride: false, |
| 82 | + allowRuntimeCreate: true, |
| 83 | + schema: { |
| 84 | + type: 'object', |
| 85 | + properties: { name: { type: 'string' }, label: { type: 'string' } }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }), |
| 90 | + }; |
| 91 | +}); |
| 92 | + |
| 93 | +import { MetadataResourceEditPage } from './ResourceEditPage'; |
| 94 | + |
| 95 | +afterEach(() => { |
| 96 | + cleanup(); |
| 97 | + vi.clearAllMocks(); |
| 98 | +}); |
| 99 | + |
| 100 | +/** |
| 101 | + * Mount the editor over a layered envelope carrying `lock`. `provenance: 'org'` |
| 102 | + * keeps the separate installed-package notice out of the way so the assertions |
| 103 | + * below read the LOCK banner and nothing else. |
| 104 | + */ |
| 105 | +async function renderWithLock(lock: unknown) { |
| 106 | + layeredImpl.current = vi.fn(async () => ({ |
| 107 | + code: { ...objectDef, _packageId: 'com.example.base', _provenance: 'org' }, |
| 108 | + overlay: null, |
| 109 | + overlayScope: null, |
| 110 | + effective: objectDef, |
| 111 | + provenance: 'org', |
| 112 | + packageId: 'com.example.base', |
| 113 | + editable: true, |
| 114 | + lock, |
| 115 | + })); |
| 116 | + render( |
| 117 | + <MemoryRouter initialEntries={['/metadata/object/showcase_account']}> |
| 118 | + <MetadataResourceEditPage type="object" name="showcase_account" /> |
| 119 | + </MemoryRouter>, |
| 120 | + ); |
| 121 | + await waitFor(() => expect(mockClient.layered).toHaveBeenCalled()); |
| 122 | + return waitFor(() => { |
| 123 | + const el = screen.queryByTestId('lock-banner-title'); |
| 124 | + expect(el).not.toBeNull(); |
| 125 | + return el as HTMLElement; |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +describe('ResourceEditPage lock banner — every state that opens the banner also titles it (#5024)', () => { |
| 130 | + // The control half: these pass before the fix as well as after. If the |
| 131 | + // out-of-vocabulary case below goes red while these stay green, the red is |
| 132 | + // the defect rather than a harness that cannot mount the page. |
| 133 | + it.each([ |
| 134 | + ['full', 'This item is locked and cannot be edited or deleted.'], |
| 135 | + ['no-overlay', 'This item is locked and cannot be edited.'], |
| 136 | + ['no-delete', 'This item is locked and cannot be deleted.'], |
| 137 | + ])('known state %s keeps its existing sentence', async (lock, sentence) => { |
| 138 | + const el = await renderWithLock(lock); |
| 139 | + expect(el.textContent?.trim()).toBe(sentence); |
| 140 | + }); |
| 141 | + |
| 142 | + it('an out-of-vocabulary state from the wire still gets a title', async () => { |
| 143 | + // The whole defect in one assertion: pre-fix this element exists (the |
| 144 | + // banner opened) and is EMPTY. |
| 145 | + const el = await renderWithLock('no-publish'); |
| 146 | + expect(el.textContent?.trim()).not.toBe(''); |
| 147 | + }); |
| 148 | + |
| 149 | + it('an out-of-vocabulary state names the raw token it could not read', async () => { |
| 150 | + // "Loudly", per the triage ruling: a generic "this is locked" that hides |
| 151 | + // WHICH state arrived would leave an operator with nothing to report. |
| 152 | + const el = await renderWithLock('no-publish'); |
| 153 | + expect(el.textContent).toContain('no-publish'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('a non-string lock value cannot crash or blank the banner', async () => { |
| 157 | + // `layered()` casts whatever JSON held; a malformed body is the same class |
| 158 | + // of unchecked input as an unknown state name. |
| 159 | + const el = await renderWithLock(42); |
| 160 | + expect(el.textContent?.trim()).not.toBe(''); |
| 161 | + }); |
| 162 | +}); |
0 commit comments