|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26840 — the package door's Access matrix lists the package's |
| 5 | + * DRAFT objects too, so a new package can be secured before its first |
| 6 | + * publish. |
| 7 | + * |
| 8 | + * The fake client behaves like the server's two reads of one package: |
| 9 | + * |
| 10 | + * • the published door (`list` / `get`) knows only the published object; |
| 11 | + * • the draft-overlaid door (`withPreviewDrafts(true)`, `?preview=draft`) |
| 12 | + * also serves each pending draft, with its `_draft` mark. |
| 13 | + * |
| 14 | + * The draft-only row carries the object picker's draft mark, its fields come |
| 15 | + * from its draft, and its grants write into the set's draft. The environment |
| 16 | + * door keeps the published list and its rows carry no mark. |
| 17 | + */ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 21 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 22 | +import { MemoryRouter } from 'react-router-dom'; |
| 23 | + |
| 24 | +interface Server { |
| 25 | + set: Record<string, unknown>; |
| 26 | + saved: Array<Record<string, unknown>>; |
| 27 | + savedOpts: Array<Record<string, unknown> | undefined>; |
| 28 | + listCalls: Array<{ preview: boolean; opts?: Record<string, unknown> }>; |
| 29 | +} |
| 30 | + |
| 31 | +const PUBLISHED = { name: 'a_account', label: 'Account', fields: { name: { label: 'Name' } } }; |
| 32 | +const DRAFT_ONLY = { |
| 33 | + name: 'a_invoice', |
| 34 | + label: 'Invoice', |
| 35 | + fields: { amount: { label: 'Amount' } }, |
| 36 | + _draft: true, |
| 37 | + _packageId: 'app.a', |
| 38 | +}; |
| 39 | + |
| 40 | +function makeClient(server: Server, preview = false): any { |
| 41 | + return { |
| 42 | + withPreviewDrafts: (on: boolean) => makeClient(server, on), |
| 43 | + layered: async () => ({ effective: server.set, code: null, overlay: null, overlayScope: null }), |
| 44 | + getDraft: async () => null, |
| 45 | + list: async (type: string, opts?: Record<string, unknown>) => { |
| 46 | + if (type !== 'object') return []; |
| 47 | + server.listCalls.push({ preview, opts }); |
| 48 | + return preview ? [PUBLISHED, DRAFT_ONLY] : [PUBLISHED]; |
| 49 | + }, |
| 50 | + get: async (type: string, name: string) => { |
| 51 | + if (type !== 'object') return null; |
| 52 | + if (name === PUBLISHED.name) return PUBLISHED; |
| 53 | + // A draft-only object has no published definition. |
| 54 | + return preview && name === DRAFT_ONLY.name ? DRAFT_ONLY : null; |
| 55 | + }, |
| 56 | + save: async (_type: string, _name: string, payload: Record<string, unknown>, opts?: Record<string, unknown>) => { |
| 57 | + server.saved.push(payload); |
| 58 | + server.savedOpts.push(opts); |
| 59 | + server.set = payload; |
| 60 | + return payload; |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +let clientImpl: any; |
| 66 | + |
| 67 | +vi.mock('./useMetadata', () => ({ |
| 68 | + useMetadataClient: () => clientImpl, |
| 69 | + useMetadataTypes: () => ({ |
| 70 | + loading: false, |
| 71 | + error: null, |
| 72 | + entries: [{ type: 'permission', label: 'Permission', allowEnvironmentOverlay: true }], |
| 73 | + }), |
| 74 | +})); |
| 75 | + |
| 76 | +vi.mock('./AssignedUsersSection', () => ({ AssignedUsersSection: () => null })); |
| 77 | + |
| 78 | +import { PermissionMatrixEditPage } from './PermissionMatrixEditor'; |
| 79 | + |
| 80 | +afterEach(cleanup); |
| 81 | + |
| 82 | +function freshServer(): Server { |
| 83 | + return { |
| 84 | + set: { name: 'sales_perms', label: 'Sales', objects: {}, fields: {} }, |
| 85 | + saved: [], |
| 86 | + savedOpts: [], |
| 87 | + listCalls: [], |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function renderMatrix(opts: { packageId?: string; studio?: boolean } = {}) { |
| 92 | + return render( |
| 93 | + <MemoryRouter> |
| 94 | + <PermissionMatrixEditPage |
| 95 | + type="permission" |
| 96 | + name="sales_perms" |
| 97 | + packageId={opts.packageId} |
| 98 | + studio={opts.studio ? { tab: 'objects', onTabChange: () => {} } : undefined} |
| 99 | + /> |
| 100 | + </MemoryRouter>, |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +const rowOf = (text: string) => screen.getByText(text).closest('tr')!; |
| 105 | +const draftMarks = (row: HTMLElement) => row.querySelectorAll('[data-draft-marker]'); |
| 106 | + |
| 107 | +describe('PermissionMatrixEditPage — draft objects at the package door (objectstack#26840)', () => { |
| 108 | + it('lists a draft-only object badged as a draft, and a published one without the badge', async () => { |
| 109 | + clientImpl = makeClient(freshServer()); |
| 110 | + renderMatrix({ packageId: 'app.a' }); |
| 111 | + |
| 112 | + await screen.findByText('Invoice'); |
| 113 | + const invoiceMarks = draftMarks(rowOf('Invoice')); |
| 114 | + expect(invoiceMarks).toHaveLength(1); |
| 115 | + expect(invoiceMarks[0]).toHaveTextContent('(draft)'); |
| 116 | + expect(draftMarks(rowOf('Account'))).toHaveLength(0); |
| 117 | + }); |
| 118 | + |
| 119 | + it('badges the draft-only row in the Studio layout too', async () => { |
| 120 | + clientImpl = makeClient(freshServer()); |
| 121 | + renderMatrix({ packageId: 'app.a', studio: true }); |
| 122 | + |
| 123 | + await screen.findByText('Invoice'); |
| 124 | + expect(draftMarks(rowOf('Invoice'))).toHaveLength(1); |
| 125 | + expect(draftMarks(rowOf('Account'))).toHaveLength(0); |
| 126 | + }); |
| 127 | + |
| 128 | + it("writes the draft-only object's grants into the set's draft", async () => { |
| 129 | + const server = freshServer(); |
| 130 | + clientImpl = makeClient(server); |
| 131 | + renderMatrix({ packageId: 'app.a' }); |
| 132 | + |
| 133 | + await screen.findByText('Invoice'); |
| 134 | + fireEvent.click(within(rowOf('Invoice')).getByLabelText('a_invoice Read')); |
| 135 | + |
| 136 | + await waitFor(() => expect(server.saved).toHaveLength(1), { timeout: 4000 }); |
| 137 | + expect((server.saved[0] as any).objects.a_invoice).toMatchObject({ allowRead: true }); |
| 138 | + expect(server.savedOpts[0]).toMatchObject({ mode: 'draft', packageId: 'app.a' }); |
| 139 | + }); |
| 140 | + |
| 141 | + it("reads a draft-only object's fields from its draft", async () => { |
| 142 | + clientImpl = makeClient(freshServer()); |
| 143 | + renderMatrix({ packageId: 'app.a' }); |
| 144 | + |
| 145 | + fireEvent.click(await screen.findByRole('button', { name: /a_invoice/ })); |
| 146 | + await screen.findByLabelText('a_invoice.amount readable'); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe('PermissionMatrixEditPage — environment door (objectstack#26840)', () => { |
| 151 | + it('keeps the published list: no preview read, no draft rows, no badge', async () => { |
| 152 | + const server = freshServer(); |
| 153 | + clientImpl = makeClient(server); |
| 154 | + renderMatrix(); |
| 155 | + |
| 156 | + await screen.findByText('Account'); |
| 157 | + expect(screen.queryByText('Invoice')).not.toBeInTheDocument(); |
| 158 | + expect(document.querySelectorAll('[data-draft-marker]')).toHaveLength(0); |
| 159 | + expect(server.listCalls.length).toBeGreaterThan(0); |
| 160 | + expect(server.listCalls.every((c) => !c.preview)).toBe(true); |
| 161 | + }); |
| 162 | +}); |
0 commit comments