|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#39147 — Setup's app editor and the Studio's nav inspector (both |
| 5 | + * `AppNavInspector`) edit an entry's search keywords. |
| 6 | + * |
| 7 | + * A navigation entry declares `keywords` (objectstack#39052) and the command |
| 8 | + * palette finds the entry by them (objectstack#39055), but no editor offered a |
| 9 | + * field for them: an admin could set them only in source. |
| 10 | + * |
| 11 | + * - an admin adds, edits and removes keywords; the saved entry carries them, |
| 12 | + * parses, and the palette's matcher finds it by the new word; |
| 13 | + * - emptying the list removes the key, never writes `[]`; |
| 14 | + * - a 61-character keyword and a 21st keyword are refused with the spec's own |
| 15 | + * message, and nothing is written (CONTROL: 60 characters and a 20th pass); |
| 16 | + * - a separator is offered no keywords field, and a retype to a separator |
| 17 | + * drops them while a retype to another entry type keeps them. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { render, screen, cleanup, fireEvent } from '@testing-library/react'; |
| 23 | +import { MetadataCtx } from '@object-ui/react'; |
| 24 | +import { NavigationItemSchema } from '@objectstack/spec/ui'; |
| 25 | + |
| 26 | +// ONE client for the whole file, as the real hook hands out (see |
| 27 | +// `AppNavInspector.labelInherits-11196.test.tsx`). |
| 28 | +const client = vi.hoisted(() => ({ list: async () => [] })); |
| 29 | + |
| 30 | +vi.mock('../useMetadata', async (importOriginal) => ({ |
| 31 | + ...(await importOriginal<Record<string, unknown>>()), |
| 32 | + useMetadataClient: () => client, |
| 33 | +})); |
| 34 | + |
| 35 | +import { AppNavInspector } from './AppNavInspector'; |
| 36 | +import { navEntryOffersField, retypedNavEntry } from './nav-target'; |
| 37 | +import { navEntryKeywords, paletteNavEntryHit } from '../../../chrome/paletteNavKeywords'; |
| 38 | +import { t } from '../i18n'; |
| 39 | +// Paid at collection, not inside a clocked case (check:test-source-alias). |
| 40 | +import '@objectstack/lint'; |
| 41 | + |
| 42 | +afterEach(cleanup); |
| 43 | + |
| 44 | +const METADATA = { |
| 45 | + apps: [], |
| 46 | + objects: [{ name: 'lead', label: 'Leads' }], |
| 47 | + dashboards: [], |
| 48 | + reports: [], |
| 49 | + pages: [], |
| 50 | + loading: false, |
| 51 | + error: null, |
| 52 | + refresh: async () => {}, |
| 53 | + invalidate: () => {}, |
| 54 | + ensureType: async () => [], |
| 55 | + getItem: async () => null, |
| 56 | + getItemsByType: () => [], |
| 57 | + getTypeStatus: () => 'ready' as const, |
| 58 | +}; |
| 59 | + |
| 60 | +type Entry = Record<string, unknown>; |
| 61 | +type NavPatch = { navigation: Entry[] }; |
| 62 | + |
| 63 | +const L = 'en-US'; |
| 64 | +const PAGE_ENTRY: Entry = { id: 'nav_home', type: 'page', pageName: 'home', label: 'Home' }; |
| 65 | +const SEPARATOR: Entry = { id: 'nav_sep', type: 'separator' }; |
| 66 | + |
| 67 | +function inspect(navigation: Entry[], index = 0) { |
| 68 | + const onPatch = vi.fn<(patch: NavPatch) => void>(); |
| 69 | + render( |
| 70 | + <MetadataCtx.Provider value={METADATA as never}> |
| 71 | + <AppNavInspector |
| 72 | + type="app" |
| 73 | + name="crm" |
| 74 | + selection={{ kind: 'nav', id: `navigation[${index}]` }} |
| 75 | + draft={{ name: 'crm', navigation }} |
| 76 | + onPatch={onPatch as never} |
| 77 | + onClearSelection={() => {}} |
| 78 | + locale={L} |
| 79 | + readOnly={false} |
| 80 | + /> |
| 81 | + </MetadataCtx.Provider>, |
| 82 | + ); |
| 83 | + return onPatch; |
| 84 | +} |
| 85 | + |
| 86 | +/** What the save meets: `undefined`-valued keys erased, as JSON does. */ |
| 87 | +const wire = (entry: Entry) => JSON.parse(JSON.stringify(entry)) as Entry; |
| 88 | + |
| 89 | +function issuesOf(entry: Entry) { |
| 90 | + const r = NavigationItemSchema.safeParse(wire(entry)); |
| 91 | + return r.success ? [] : r.error.issues; |
| 92 | +} |
| 93 | + |
| 94 | +/** The keyword row inputs, in order. */ |
| 95 | +const rows = () => screen.queryAllByPlaceholderText(t('engine.inspector.appNav.keywordsItem', L)) as HTMLInputElement[]; |
| 96 | + |
| 97 | +function typeInto(input: HTMLInputElement, value: string) { |
| 98 | + fireEvent.change(input, { target: { value } }); |
| 99 | + fireEvent.blur(input); |
| 100 | +} |
| 101 | + |
| 102 | +function addKeyword(value: string) { |
| 103 | + fireEvent.click(screen.getByRole('button', { name: t('engine.inspector.appNav.keywordsAdd', L) })); |
| 104 | + const all = rows(); |
| 105 | + typeInto(all[all.length - 1], value); |
| 106 | +} |
| 107 | + |
| 108 | +/** The palette's own matcher, against the saved entry: its label is its term. */ |
| 109 | +const paletteHit = (query: string, entry: Entry) => |
| 110 | + paletteNavEntryHit(query, [String(entry.label ?? '')], navEntryKeywords(entry), []); |
| 111 | + |
| 112 | +describe('objectstack#39147 — the nav editor edits an entry\'s search keywords', () => { |
| 113 | + it('an admin adds a keyword: the saved entry carries it, parses, and the palette finds the entry by it', () => { |
| 114 | + const onPatch = inspect([PAGE_ENTRY]); |
| 115 | + expect(rows()).toEqual([]); |
| 116 | + expect(screen.getByText(t('engine.inspector.appNav.keywordsEmpty', L))).toBeInTheDocument(); |
| 117 | + addKeyword('pipeline'); |
| 118 | + expect(onPatch).toHaveBeenCalledTimes(1); |
| 119 | + const saved = onPatch.mock.calls[0][0].navigation[0]; |
| 120 | + expect(saved).toEqual({ ...PAGE_ENTRY, keywords: ['pipeline'] }); |
| 121 | + expect(issuesOf(saved)).toEqual([]); |
| 122 | + // NON-VACUITY: before the edit the palette does not find it by that word. |
| 123 | + expect(paletteHit('pipel', PAGE_ENTRY)).toEqual({ found: false }); |
| 124 | + expect(paletteHit('pipel', saved)).toEqual({ found: true, keyword: 'pipeline' }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('an admin edits a keyword: the saved entry carries the new word', () => { |
| 128 | + const entry = { ...PAGE_ENTRY, keywords: ['old name', 'deals'] }; |
| 129 | + const onPatch = inspect([entry]); |
| 130 | + expect(rows().map((r) => r.value)).toEqual(['old name', 'deals']); |
| 131 | + typeInto(rows()[0], 'opportunities'); |
| 132 | + expect(onPatch).toHaveBeenCalledTimes(1); |
| 133 | + const saved = onPatch.mock.calls[0][0].navigation[0]; |
| 134 | + expect(saved).toEqual({ ...entry, keywords: ['opportunities', 'deals'] }); |
| 135 | + expect(issuesOf(saved)).toEqual([]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('an admin removes a keyword; removing the last one removes the key, never writes []', () => { |
| 139 | + const entry = { ...PAGE_ENTRY, keywords: ['deals'] }; |
| 140 | + const onPatch = inspect([entry]); |
| 141 | + fireEvent.click(screen.getByRole('button', { name: t('engine.inspector.appNav.keywordsRemove', L) })); |
| 142 | + expect(onPatch).toHaveBeenCalledTimes(1); |
| 143 | + const saved = onPatch.mock.calls[0][0].navigation[0]; |
| 144 | + expect('keywords' in saved).toBe(false); |
| 145 | + expect(saved).toEqual(PAGE_ENTRY); |
| 146 | + }); |
| 147 | + |
| 148 | + it('leaving a keyword unchanged writes nothing', () => { |
| 149 | + const onPatch = inspect([{ ...PAGE_ENTRY, keywords: ['deals'] }]); |
| 150 | + typeInto(rows()[0], 'deals'); |
| 151 | + expect(onPatch).not.toHaveBeenCalled(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('refuses a 61-character keyword with the spec\'s message and writes nothing (CONTROL: 60 pass)', () => { |
| 155 | + const tooLong = 'k'.repeat(61); |
| 156 | + const specIssues = issuesOf({ ...PAGE_ENTRY, keywords: [tooLong] }); |
| 157 | + expect(specIssues.length).toBeGreaterThan(0); |
| 158 | + const onPatch = inspect([PAGE_ENTRY]); |
| 159 | + addKeyword(tooLong); |
| 160 | + expect(onPatch).not.toHaveBeenCalled(); |
| 161 | + expect(screen.getByRole('alert')).toHaveTextContent(specIssues[0].message); |
| 162 | + |
| 163 | + cleanup(); |
| 164 | + const control = inspect([PAGE_ENTRY]); |
| 165 | + addKeyword('k'.repeat(60)); |
| 166 | + expect(control).toHaveBeenCalledTimes(1); |
| 167 | + expect(issuesOf(control.mock.calls[0][0].navigation[0])).toEqual([]); |
| 168 | + expect(screen.queryByRole('alert')).toBeNull(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('refuses a 21st keyword with the spec\'s message and writes nothing (CONTROL: a 20th passes)', () => { |
| 172 | + const twenty = Array.from({ length: 20 }, (_, i) => `word${i}`); |
| 173 | + const specIssues = issuesOf({ ...PAGE_ENTRY, keywords: [...twenty, 'one more'] }); |
| 174 | + expect(specIssues.length).toBeGreaterThan(0); |
| 175 | + const onPatch = inspect([{ ...PAGE_ENTRY, keywords: twenty }]); |
| 176 | + addKeyword('one more'); |
| 177 | + expect(onPatch).not.toHaveBeenCalled(); |
| 178 | + expect(screen.getByRole('alert')).toHaveTextContent(specIssues[0].message); |
| 179 | + |
| 180 | + cleanup(); |
| 181 | + const nineteen = twenty.slice(0, 19); |
| 182 | + const control = inspect([{ ...PAGE_ENTRY, keywords: nineteen }]); |
| 183 | + addKeyword('word19'); |
| 184 | + expect(control).toHaveBeenCalledTimes(1); |
| 185 | + const saved = control.mock.calls[0][0].navigation[0]; |
| 186 | + expect(saved.keywords).toEqual(twenty); |
| 187 | + expect(issuesOf(saved)).toEqual([]); |
| 188 | + }); |
| 189 | + |
| 190 | + it('a separator is offered no keywords field', () => { |
| 191 | + expect(navEntryOffersField('separator', 'keywords')).toBe(false); |
| 192 | + inspect([PAGE_ENTRY, SEPARATOR], 1); |
| 193 | + expect(screen.queryByText(t('engine.inspector.appNav.keywords', L))).toBeNull(); |
| 194 | + expect(screen.queryByRole('button', { name: t('engine.inspector.appNav.keywordsAdd', L) })).toBeNull(); |
| 195 | + }); |
| 196 | + |
| 197 | + it('a retype to a separator drops the keywords; a retype to another entry type keeps them', () => { |
| 198 | + const entry = { ...PAGE_ENTRY, keywords: ['deals'] }; |
| 199 | + const separator = retypedNavEntry(entry, 'separator'); |
| 200 | + expect('keywords' in separator).toBe(false); |
| 201 | + expect(issuesOf(separator)).toEqual([]); |
| 202 | + expect(retypedNavEntry(entry, 'dashboard').keywords).toEqual(['deals']); |
| 203 | + }); |
| 204 | +}); |
| 205 | + |
| 206 | +describe('objectstack#39147 — a refused keywords draft stays with its own entry', () => { |
| 207 | + it('switching from an entry with a refused draft to another keyword-less entry shows an empty field and writes nothing to it', () => { |
| 208 | + // Both entries declare no `keywords`, so the field's `value` is |
| 209 | + // `undefined` for each: only a fresh field per entry resets its rows. |
| 210 | + const second: Entry = { id: 'nav_leads', type: 'page', pageName: 'leads', label: 'Leads' }; |
| 211 | + const navigation = [PAGE_ENTRY, second]; |
| 212 | + const onPatch = vi.fn<(patch: NavPatch) => void>(); |
| 213 | + const view = (index: number) => ( |
| 214 | + <MetadataCtx.Provider value={METADATA as never}> |
| 215 | + <AppNavInspector |
| 216 | + type="app" |
| 217 | + name="crm" |
| 218 | + selection={{ kind: 'nav', id: `navigation[${index}]` }} |
| 219 | + draft={{ name: 'crm', navigation }} |
| 220 | + onPatch={onPatch as never} |
| 221 | + onClearSelection={() => {}} |
| 222 | + locale={L} |
| 223 | + readOnly={false} |
| 224 | + /> |
| 225 | + </MetadataCtx.Provider> |
| 226 | + ); |
| 227 | + const { rerender } = render(view(0)); |
| 228 | + addKeyword('pipeline'); |
| 229 | + addKeyword('k'.repeat(61)); |
| 230 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 231 | + const writesOnA = onPatch.mock.calls.length; |
| 232 | + |
| 233 | + rerender(view(1)); |
| 234 | + expect(rows()).toEqual([]); |
| 235 | + expect(screen.getByText(t('engine.inspector.appNav.keywordsEmpty', L))).toBeInTheDocument(); |
| 236 | + expect(screen.queryByRole('alert')).toBeNull(); |
| 237 | + expect(onPatch).toHaveBeenCalledTimes(writesOnA); |
| 238 | + |
| 239 | + // The next edit on B writes B's own word, and nothing carried from A. |
| 240 | + addKeyword('leads'); |
| 241 | + expect(onPatch).toHaveBeenCalledTimes(writesOnA + 1); |
| 242 | + const saved = onPatch.mock.calls[writesOnA][0].navigation[1]; |
| 243 | + expect(saved).toEqual({ ...second, keywords: ['leads'] }); |
| 244 | + expect(issuesOf(saved)).toEqual([]); |
| 245 | + }); |
| 246 | +}); |
0 commit comments