|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36750 — the list-view buffer does not ask `GET …/layers` for the |
| 5 | + * fallback default view id. |
| 6 | + * |
| 7 | + * With no view of the object flagged default, the buffer edits |
| 8 | + * `<object>.default` (`listViewTargetOf`), the name the panel's first edit |
| 9 | + * creates the view under. Nothing is published under it, so the server |
| 10 | + * answers its `/layers` with a 404 (objectstack#22397) and the browser logs a |
| 11 | + * failed request on every open of such an object. The guard that used to skip |
| 12 | + * this read covered draft-only objects only (objectui#12508); since |
| 13 | + * objectstack#36511 a new object is published at once, so it no longer |
| 14 | + * applies. The buffer already knows the id is the fallback: no view of the |
| 15 | + * object's merged `listViews` (which holds every view item the server serves, |
| 16 | + * under its qualified name) nor of this package's drafted list views is held |
| 17 | + * under it. |
| 18 | + * |
| 19 | + * The REAL hook over a REAL `MetadataClient`, whose transport is an in-memory |
| 20 | + * server answering as the framework does (404 for `/layers` with no layer). |
| 21 | + * |
| 22 | + * Pinned: |
| 23 | + * - the fallback id: no `/layers`, and the buffer reads "not created yet"; |
| 24 | + * - CONTROL: a published default view the merged `listViews` holds (flagged) |
| 25 | + * still reads its layer; |
| 26 | + * - CONTROL: a view held under `default` unflagged (the target falls back to |
| 27 | + * the id a view really has) still reads its layer; |
| 28 | + * - CONTROL: while the shell's objects are still loading, absence from them |
| 29 | + * says nothing, so the layer is read as before; once they answer, the |
| 30 | + * fallback stops being read; a context that reports no load status (a |
| 31 | + * hand-rolled one) is read as before too; |
| 32 | + * - the Customize door's served-override read (`useServedOverride`) skips |
| 33 | + * the layered read for an item flagged `unpublished`, answering as its 404 |
| 34 | + * did, and still reads the pair's draft first. |
| 35 | + */ |
| 36 | + |
| 37 | +import '@testing-library/jest-dom/vitest'; |
| 38 | +// Paid at collection, not inside a clocked case (check:test-source-alias): |
| 39 | +// the source under test loads `@objectstack/lint` lazily. |
| 40 | +import '@objectstack/lint'; |
| 41 | +import * as React from 'react'; |
| 42 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 43 | +import { act, cleanup, render } from '@testing-library/react'; |
| 44 | +import { MetadataCtx, type MetadataContextValue, type MetadataTypeStatus } from '@object-ui/react'; |
| 45 | +import { MetadataClient } from '@object-ui/data-objectstack'; |
| 46 | +import { useObjectListViewDraft, type ObjectListViewDraft } from './ObjectListViewInspector'; |
| 47 | +import { useServedOverride, type ServedOverride } from './StudioCustomizeControls'; |
| 48 | +import type { Surface } from './navSurface'; |
| 49 | + |
| 50 | +const PKG = 'com.acme.app'; |
| 51 | +const OBJECT = 'acme_ticket'; |
| 52 | +const FALLBACK = `${OBJECT}.default`; |
| 53 | + |
| 54 | +const server = { |
| 55 | + active: new Map<string, Record<string, unknown>>(), |
| 56 | + requests: [] as Array<{ method: string; path: string; status: number }>, |
| 57 | +}; |
| 58 | + |
| 59 | +const json = (status: number, body: unknown) => |
| 60 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 61 | + |
| 62 | +async function serve(input: RequestInfo | URL, init?: RequestInit): Promise<Response> { |
| 63 | + const method = (init?.method ?? 'GET').toUpperCase(); |
| 64 | + const url = new URL(String(input), 'http://studio.test'); |
| 65 | + let status = 200; |
| 66 | + let body: unknown = []; |
| 67 | + const meta = url.pathname.match(/^\/api\/v1\/meta\/(.+)$/); |
| 68 | + if (meta) { |
| 69 | + const seg = meta[1].split('/').map(decodeURIComponent); |
| 70 | + if (seg.length === 1 && seg[0] === '_drafts') { |
| 71 | + body = []; |
| 72 | + } else if (seg.length === 1) { |
| 73 | + body = []; |
| 74 | + } else if (seg.length === 3 && seg[2] === 'layers') { |
| 75 | + const row = server.active.get(`${seg[0]}/${seg[1]}`); |
| 76 | + if (row) body = { code: null, overlay: row, overlayScope: 'env', effective: row, draft: null }; |
| 77 | + else [status, body] = [404, { error: { code: 'NOT_FOUND', message: 'absent' } }]; |
| 78 | + } else if (seg.length === 2) { |
| 79 | + [status, body] = [404, { error: { code: 'NO_DRAFT', message: 'absent' } }]; |
| 80 | + } |
| 81 | + } |
| 82 | + server.requests.push({ method, path: url.pathname, status }); |
| 83 | + return json(status, body); |
| 84 | +} |
| 85 | + |
| 86 | +const client = new MetadataClient({ baseUrl: '', fetch: vi.fn(serve) as unknown as typeof fetch }); |
| 87 | + |
| 88 | +const listItem = (name: string, isDefault: boolean) => ({ |
| 89 | + name, |
| 90 | + object: OBJECT, |
| 91 | + viewKind: 'list', |
| 92 | + ...(isDefault ? { isDefault: true } : {}), |
| 93 | + config: { type: 'grid', data: { provider: 'object', object: OBJECT }, columns: ['title'] }, |
| 94 | +}); |
| 95 | + |
| 96 | +function metadataOf(listViews: Record<string, unknown> | undefined, status: MetadataTypeStatus | null): MetadataContextValue { |
| 97 | + return { |
| 98 | + apps: [], |
| 99 | + objects: [{ name: OBJECT, label: 'Ticket', fields: { title: { name: 'title', type: 'text' } }, ...(listViews ? { listViews } : {}) }], |
| 100 | + dashboards: [], |
| 101 | + reports: [], |
| 102 | + pages: [], |
| 103 | + loading: false, |
| 104 | + error: null, |
| 105 | + refresh: async () => {}, |
| 106 | + invalidate: () => {}, |
| 107 | + ensureType: async () => [], |
| 108 | + getItem: async () => null, |
| 109 | + getItemsByType: () => [], |
| 110 | + ...(status ? { getTypeStatus: () => status } : {}), |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +let latest: ObjectListViewDraft | null = null; |
| 115 | +function Probe() { |
| 116 | + const leaf: Surface = { type: 'object', name: OBJECT, label: OBJECT }; |
| 117 | + const d = useObjectListViewDraft({ client, packageId: PKG, leaf, publishNonce: 0 }); |
| 118 | + React.useLayoutEffect(() => { |
| 119 | + latest = d; |
| 120 | + }); |
| 121 | + return null; |
| 122 | +} |
| 123 | + |
| 124 | +const settle = () => |
| 125 | + act(async () => { |
| 126 | + for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 0)); |
| 127 | + }); |
| 128 | + |
| 129 | +async function open(metadata: MetadataContextValue) { |
| 130 | + const view = render( |
| 131 | + <MetadataCtx.Provider value={metadata}> |
| 132 | + <Probe /> |
| 133 | + </MetadataCtx.Provider>, |
| 134 | + ); |
| 135 | + await settle(); |
| 136 | + return { |
| 137 | + d: () => latest!, |
| 138 | + rerender: async (next: MetadataContextValue) => { |
| 139 | + view.rerender( |
| 140 | + <MetadataCtx.Provider value={next}> |
| 141 | + <Probe /> |
| 142 | + </MetadataCtx.Provider>, |
| 143 | + ); |
| 144 | + await settle(); |
| 145 | + }, |
| 146 | + }; |
| 147 | +} |
| 148 | + |
| 149 | +const layersAsked = (name: string) => |
| 150 | + server.requests.filter((r) => r.method === 'GET' && r.path === `/api/v1/meta/view/${name}/layers`).map((r) => r.status); |
| 151 | + |
| 152 | +beforeEach(() => { |
| 153 | + server.active.clear(); |
| 154 | + server.requests = []; |
| 155 | + latest = null; |
| 156 | +}); |
| 157 | +afterEach(cleanup); |
| 158 | + |
| 159 | +describe('useObjectListViewDraft — the fallback default view is not asked for /layers (objectstack#36750)', () => { |
| 160 | + it('no view flagged default and none held under the fallback id: no /layers, and the buffer reads "not created yet"', async () => { |
| 161 | + const { d } = await open(metadataOf(undefined, 'ready')); |
| 162 | + |
| 163 | + expect(d().target).toEqual({ objectName: OBJECT, viewId: FALLBACK, isDefault: true }); |
| 164 | + expect(d().targetIsFallback).toBe(true); |
| 165 | + expect(layersAsked(FALLBACK)).toEqual([]); |
| 166 | + expect(server.requests.filter((r) => r.status >= 400)).toEqual([]); |
| 167 | + expect(d().loadedFor).toBe(`listView:${FALLBACK}`); |
| 168 | + expect(d().row).toBeNull(); |
| 169 | + expect(d().failure).toBeNull(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('CONTROL: a published default view the merged listViews holds still reads its layer', async () => { |
| 173 | + const all = listItem(`${OBJECT}.all`, true); |
| 174 | + server.active.set(`view/${all.name}`, all); |
| 175 | + const { d } = await open(metadataOf({ [all.name]: { ...all.config, name: all.name, isDefault: true } }, 'ready')); |
| 176 | + |
| 177 | + expect(d().target?.viewId).toBe(`${OBJECT}.all`); |
| 178 | + expect(d().targetIsFallback).toBe(false); |
| 179 | + expect(layersAsked(`${OBJECT}.all`)).toEqual([200]); |
| 180 | + expect(d().row).toEqual(all); |
| 181 | + }); |
| 182 | + |
| 183 | + it('CONTROL: a view held under `default` unflagged is no fallback, and still reads its layer', async () => { |
| 184 | + const held = listItem(FALLBACK, false); |
| 185 | + server.active.set(`view/${FALLBACK}`, held); |
| 186 | + // An object document's own `listViews` keys its entries bare. |
| 187 | + const { d } = await open(metadataOf({ default: { ...held.config } }, 'ready')); |
| 188 | + |
| 189 | + expect(d().target).toEqual({ objectName: OBJECT, viewId: FALLBACK, isDefault: true }); |
| 190 | + expect(d().targetIsFallback).toBe(false); |
| 191 | + expect(layersAsked(FALLBACK)).toEqual([200]); |
| 192 | + expect(d().row).toEqual(held); |
| 193 | + }); |
| 194 | + |
| 195 | + it('CONTROL: while the shell\'s objects are loading the layer is read as before; once they answer, it is not read again', async () => { |
| 196 | + const { d, rerender } = await open(metadataOf(undefined, 'loading')); |
| 197 | + |
| 198 | + expect(d().targetIsFallback).toBe(false); |
| 199 | + expect(layersAsked(FALLBACK)).toEqual([404]); |
| 200 | + expect(d().row).toBeNull(); |
| 201 | + expect(d().failure).toBeNull(); |
| 202 | + |
| 203 | + await rerender(metadataOf(undefined, 'ready')); |
| 204 | + expect(d().targetIsFallback).toBe(true); |
| 205 | + expect(layersAsked(FALLBACK)).toEqual([404]); |
| 206 | + expect(d().loadedFor).toBe(`listView:${FALLBACK}`); |
| 207 | + expect(d().row).toBeNull(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('CONTROL: a context that reports no load status proves no absence, so the layer is read as before', async () => { |
| 211 | + const { d } = await open(metadataOf(undefined, null)); |
| 212 | + |
| 213 | + expect(d().targetIsFallback).toBe(false); |
| 214 | + expect(layersAsked(FALLBACK)).toEqual([404]); |
| 215 | + expect(d().row).toBeNull(); |
| 216 | + expect(d().failure).toBeNull(); |
| 217 | + }); |
| 218 | +}); |
| 219 | + |
| 220 | +describe('useServedOverride — an item flagged `unpublished` is not asked for its layers (objectstack#36750)', () => { |
| 221 | + let served: ServedOverride | null = null; |
| 222 | + function ServedProbe({ fake, unpublished, pair }: { fake: Parameters<typeof useServedOverride>[0]; unpublished: boolean; pair: string | null }) { |
| 223 | + const s = useServedOverride(fake, { type: 'view', name: FALLBACK, ...(unpublished ? { unpublished: true } : {}) }, 'com.acme.base', pair, 0); |
| 224 | + React.useLayoutEffect(() => { |
| 225 | + served = s; |
| 226 | + }); |
| 227 | + return null; |
| 228 | + } |
| 229 | + const fakeClient = () => ({ |
| 230 | + getDraft: vi.fn(async () => null), |
| 231 | + layered: vi.fn(async () => ({ code: null, overlay: null, overlayScope: null, effective: null })), |
| 232 | + }); |
| 233 | + |
| 234 | + it('skips the layered read and answers as its 404 did, after reading the pair\'s draft', async () => { |
| 235 | + const fake = fakeClient(); |
| 236 | + render(<ServedProbe fake={fake as never} unpublished pair="com.acme.base.custom" />); |
| 237 | + await settle(); |
| 238 | + |
| 239 | + expect(fake.layered).not.toHaveBeenCalled(); |
| 240 | + expect(fake.getDraft).toHaveBeenCalledTimes(1); |
| 241 | + expect(served).toEqual({ declaration: null, state: null, shipped: 'none' }); |
| 242 | + }); |
| 243 | + |
| 244 | + it('CONTROL: an item not flagged still reads its layers, with the same answer for nothing published', async () => { |
| 245 | + const fake = fakeClient(); |
| 246 | + render(<ServedProbe fake={fake as never} unpublished={false} pair={null} />); |
| 247 | + await settle(); |
| 248 | + |
| 249 | + expect(fake.layered).toHaveBeenCalledTimes(1); |
| 250 | + expect(served).toEqual({ declaration: null, state: null, shipped: 'none' }); |
| 251 | + }); |
| 252 | +}); |
0 commit comments