|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36511 — a permission set in the Studio's Access pillar saves with |
| 5 | + * the top bar's Save and Cancel, and a Save is live at once. |
| 6 | + * |
| 7 | + * - A grant clicked sends nothing: no draft write leaves before Save is |
| 8 | + * pressed, however long the page waits (the set's 1.5 s autosave is gone |
| 9 | + * from the Studio). |
| 10 | + * - Save writes the set's draft at its package, then publishes that one set at |
| 11 | + * the same package (`POST /meta/permission/NAME/publish?package=`). |
| 12 | + * - Cancel sends nothing and puts the matrix back as the server holds it. |
| 13 | + * - A refused publish keeps the grant unsaved, says why on the page, and takes |
| 14 | + * back the draft the Save wrote. |
| 15 | + * |
| 16 | + * The REAL `AccessPillar` inside the top bar's coordinator; the writes go |
| 17 | + * through a REAL `MetadataClient` whose transport records each one, so the pins |
| 18 | + * read the URL the server would read. The environment door (metadata-admin, |
| 19 | + * no package) keeps its own Save and live write: pinned in |
| 20 | + * `PermissionMatrixEditor.saveSurface-12725.test.tsx`. |
| 21 | + */ |
| 22 | + |
| 23 | +// Warmed at module top so the first load of the console's lazily loaded |
| 24 | +// authoring code is paid during collection, not inside a test's clock. |
| 25 | +import '@objectstack/lint'; |
| 26 | +import '@testing-library/jest-dom/vitest'; |
| 27 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 28 | +import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 29 | +import { MemoryRouter } from 'react-router-dom'; |
| 30 | +import { MetadataClient } from '@object-ui/data-objectstack'; |
| 31 | + |
| 32 | +const PKG = 'com.acme.crm'; |
| 33 | + |
| 34 | +const SET = { |
| 35 | + name: 'sales_perms', |
| 36 | + label: 'Sales', |
| 37 | + objects: { a_account: { allowRead: false, allowCreate: true } }, |
| 38 | + fields: {}, |
| 39 | +}; |
| 40 | + |
| 41 | +interface Write { |
| 42 | + method: string; |
| 43 | + url: URL; |
| 44 | + body: Record<string, unknown> | null; |
| 45 | +} |
| 46 | +const writes: Write[] = []; |
| 47 | +const door = { refusePublish: false }; |
| 48 | + |
| 49 | +const json = (status: number, body: unknown) => |
| 50 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 51 | + |
| 52 | +async function serve(input: RequestInfo | URL, init?: RequestInit): Promise<Response> { |
| 53 | + const url = new URL(String(input), 'http://console.test'); |
| 54 | + const method = (init?.method ?? 'GET').toUpperCase(); |
| 55 | + if (method === 'GET') return json(404, { error: { code: 'NOT_FOUND', message: 'absent' } }); |
| 56 | + writes.push({ method, url, body: init?.body ? JSON.parse(String(init.body)) : null }); |
| 57 | + if (method === 'POST' && door.refusePublish) { |
| 58 | + return json(422, { success: false, error: { code: 'INVALID_METADATA', message: 'The set grants on an object that is not live.' } }); |
| 59 | + } |
| 60 | + return json(200, { success: true, version: 'v2', state: url.searchParams.get('mode') ?? 'active' }); |
| 61 | +} |
| 62 | + |
| 63 | +const wire = new MetadataClient({ baseUrl: '', fetch: vi.fn(serve) as unknown as typeof fetch }); |
| 64 | + |
| 65 | +/** The reads the pillar makes, answered in memory; the writes are the real client's. */ |
| 66 | +const client = { |
| 67 | + withPreviewDrafts() { return this; }, |
| 68 | + list: async (type: string) => { |
| 69 | + if (type === 'permission') return [{ name: SET.name, label: SET.label }]; |
| 70 | + if (type === 'object') return [{ name: 'a_account', label: 'Account' }]; |
| 71 | + return []; |
| 72 | + }, |
| 73 | + listDrafts: async () => [], |
| 74 | + layered: async () => ({ effective: SET, code: null, overlay: null, overlayScope: null }), |
| 75 | + getDraft: async () => null, |
| 76 | + get: async () => null, |
| 77 | + save: (...args: Parameters<MetadataClient['save']>) => wire.save(...args), |
| 78 | + publish: (...args: Parameters<MetadataClient['publish']>) => wire.publish(...args), |
| 79 | + reset: (...args: Parameters<MetadataClient['reset']>) => wire.reset(...args), |
| 80 | +}; |
| 81 | + |
| 82 | +vi.mock('./useMetadata', async (importOriginal) => { |
| 83 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 84 | + return { |
| 85 | + ...actual, |
| 86 | + useMetadataClient: () => client, |
| 87 | + useMetadataTypes: () => ({ |
| 88 | + loading: false, |
| 89 | + error: null, |
| 90 | + entries: [{ type: 'permission', label: 'Permission', allowEnvironmentOverlay: true }], |
| 91 | + }), |
| 92 | + }; |
| 93 | +}); |
| 94 | +vi.mock('sonner', () => ({ |
| 95 | + toast: { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn(), message: vi.fn(), dismiss: vi.fn() }, |
| 96 | + Toaster: () => null, |
| 97 | +})); |
| 98 | +vi.mock('./AssignedUsersSection', () => ({ AssignedUsersSection: () => null })); |
| 99 | +vi.mock('./AccessExplainPanel', () => ({ AccessExplainPanel: () => null })); |
| 100 | +vi.mock('../../components/SuggestedBindingsPanel', () => ({ SuggestedBindingsPanel: () => null })); |
| 101 | +vi.mock('../studio-design/StudioAiCopilot', () => ({ StudioChatDock: () => null })); |
| 102 | + |
| 103 | +import { toast } from 'sonner'; |
| 104 | +import { AccessPillar } from '../studio-design/StudioDesignSurface'; |
| 105 | +import { StudioSaveHarness, pressStudioCancel, pressStudioSave } from '../studio-design/__tests__/studioSaveHarness'; |
| 106 | +import { tFormat } from './i18n'; |
| 107 | + |
| 108 | +window.matchMedia = ((query: string) => ({ |
| 109 | + matches: false, |
| 110 | + media: query, |
| 111 | + onchange: null, |
| 112 | + addEventListener: () => {}, |
| 113 | + removeEventListener: () => {}, |
| 114 | + addListener: () => {}, |
| 115 | + removeListener: () => {}, |
| 116 | + dispatchEvent: () => false, |
| 117 | +})) as unknown as typeof window.matchMedia; |
| 118 | + |
| 119 | +const readBox = () => screen.getByRole('checkbox', { name: 'a_account Read' }); |
| 120 | +const describeWrite = (w: Write) => `${w.method} ${w.url.pathname}${w.url.search}`; |
| 121 | + |
| 122 | +/** Past the retired autosave's 1.5 s debounce and a round trip. */ |
| 123 | +async function waitPastTheOldAutosave(): Promise<void> { |
| 124 | + await act(async () => { |
| 125 | + await new Promise((r) => setTimeout(r, 2300)); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +async function mount(): Promise<void> { |
| 130 | + render( |
| 131 | + <StudioSaveHarness> |
| 132 | + <MemoryRouter> |
| 133 | + <AccessPillar packageId={PKG} /> |
| 134 | + </MemoryRouter> |
| 135 | + </StudioSaveHarness>, |
| 136 | + ); |
| 137 | + await waitFor(() => expect(readBox()).toBeInTheDocument(), { timeout: 8000 }); |
| 138 | + expect(readBox()).not.toBeChecked(); |
| 139 | +} |
| 140 | + |
| 141 | +beforeEach(() => { |
| 142 | + window.confirm = vi.fn(() => true) as unknown as typeof window.confirm; |
| 143 | +}); |
| 144 | +afterEach(() => { |
| 145 | + cleanup(); |
| 146 | + writes.length = 0; |
| 147 | + door.refusePublish = false; |
| 148 | + vi.mocked(toast.success).mockClear(); |
| 149 | + vi.mocked(toast.error).mockClear(); |
| 150 | +}); |
| 151 | + |
| 152 | +describe('Access — a permission set saves with the top bar, live at once (objectstack#36511)', () => { |
| 153 | + it('a grant sends nothing until Save; Save writes the draft, then publishes that set at its package', async () => { |
| 154 | + await mount(); |
| 155 | + expect(screen.queryByTestId('studio-save')).toBeNull(); |
| 156 | + fireEvent.click(readBox()); |
| 157 | + expect(await screen.findByTestId('studio-save')).toBeInTheDocument(); |
| 158 | + await waitPastTheOldAutosave(); |
| 159 | + expect(writes.map(describeWrite), 'no write before Save').toEqual([]); |
| 160 | + |
| 161 | + await pressStudioSave(); |
| 162 | + await waitFor(() => expect(writes).toHaveLength(2), { timeout: 4000 }); |
| 163 | + const [draft, publish] = writes; |
| 164 | + expect(draft.method).toBe('PUT'); |
| 165 | + expect(draft.url.pathname).toBe('/api/v1/meta/permission/sales_perms'); |
| 166 | + expect(draft.url.searchParams.get('mode')).toBe('draft'); |
| 167 | + expect(draft.url.searchParams.get('package')).toBe(PKG); |
| 168 | + expect((draft.body?.objects as Record<string, { allowRead?: boolean }>).a_account.allowRead).toBe(true); |
| 169 | + expect(publish.method).toBe('POST'); |
| 170 | + expect(publish.url.pathname).toBe('/api/v1/meta/permission/sales_perms/publish'); |
| 171 | + expect(publish.url.searchParams.get('package')).toBe(PKG); |
| 172 | + |
| 173 | + await waitFor(() => expect(screen.queryByTestId('studio-save')).toBeNull()); |
| 174 | + expect(readBox()).toBeChecked(); |
| 175 | + expect(toast.success).toHaveBeenCalledWith(tFormat('engine.studio.save.done', 'en', { items: 'Sales' }), { id: 'studio-save' }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('Cancel sends nothing and puts the matrix back as the server holds it', async () => { |
| 179 | + await mount(); |
| 180 | + fireEvent.click(readBox()); |
| 181 | + expect(readBox()).toBeChecked(); |
| 182 | + await pressStudioCancel(); |
| 183 | + await waitFor(() => expect(readBox()).not.toBeChecked()); |
| 184 | + expect(screen.queryByTestId('studio-save')).toBeNull(); |
| 185 | + await waitPastTheOldAutosave(); |
| 186 | + expect(writes.map(describeWrite)).toEqual([]); |
| 187 | + }); |
| 188 | + |
| 189 | + it('a refused publish keeps the grant unsaved, says why, and takes back the draft it wrote', async () => { |
| 190 | + door.refusePublish = true; |
| 191 | + await mount(); |
| 192 | + fireEvent.click(readBox()); |
| 193 | + await pressStudioSave(); |
| 194 | + await waitFor(() => expect(writes.map((w) => w.method)).toEqual(['PUT', 'POST', 'DELETE']), { timeout: 4000 }); |
| 195 | + const discard = writes[2]; |
| 196 | + expect(discard.url.pathname).toBe('/api/v1/meta/permission/sales_perms'); |
| 197 | + expect(discard.url.searchParams.get('state')).toBe('draft'); |
| 198 | + expect(discard.url.searchParams.get('package')).toBe(PKG); |
| 199 | + expect(readBox(), 'the grant is still on the page').toBeChecked(); |
| 200 | + expect(screen.getByTestId('studio-save'), 'and still offered to Save').toBeInTheDocument(); |
| 201 | + expect(await screen.findByText(/The set grants on an object that is not live\./)).toBeInTheDocument(); |
| 202 | + expect(toast.success).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | +}); |
0 commit comments