|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26767 — a share link to any object other than `ai_conversations` |
| 5 | + * renders the shared record read-only, as labelled rows, and never as the raw |
| 6 | + * `JSON.stringify(record)` dump it used to print in a `pre` block. |
| 7 | + * |
| 8 | + * The stub answers `/share-links/:token/resolve` the way both producers do: the |
| 9 | + * enveloped `{ success, data: { record, link, redactFields } }`, with the |
| 10 | + * redacted fields already stripped from `record`. Every visitor is anonymous. |
| 11 | + */ |
| 12 | + |
| 13 | +import '@testing-library/jest-dom/vitest'; |
| 14 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 15 | +import { cleanup, render, screen, within } from '@testing-library/react'; |
| 16 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 17 | +import { formatDate } from '@object-ui/core'; |
| 18 | +import { I18nProvider } from '@object-ui/i18n'; |
| 19 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 20 | +import SharedRecordPage from './SharedRecordPage'; |
| 21 | + |
| 22 | +const TOKEN = 'tok_26767'; |
| 23 | + |
| 24 | +/** An `account`-like record as resolve hands it over, `ssn` already redacted. */ |
| 25 | +const ACCOUNT = { |
| 26 | + id: 'acc_1', |
| 27 | + name: 'Acme Corporation', |
| 28 | + annual_revenue: 1250000, |
| 29 | + is_active: true, |
| 30 | + founded_on: '2026-03-15', |
| 31 | + tags: ['enterprise', 'emea'], |
| 32 | + billing_address: { city: 'Paris', zip: '75001' }, |
| 33 | + parent_id: null, |
| 34 | + created_at: '2026-01-02T03:04:05.000Z', |
| 35 | +}; |
| 36 | + |
| 37 | +const json = (status: number, body: unknown) => |
| 38 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 39 | + |
| 40 | +function shareServer(objectName: string, record: Record<string, unknown>, redactFields: string[]) { |
| 41 | + vi.stubGlobal( |
| 42 | + 'fetch', |
| 43 | + vi.fn(async (input: string | URL | Request) => { |
| 44 | + const raw = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; |
| 45 | + const { pathname } = new URL(raw, 'http://localhost'); |
| 46 | + if (pathname === `/api/v1/share-links/${TOKEN}/resolve`) { |
| 47 | + return json(200, { |
| 48 | + success: true, |
| 49 | + data: { |
| 50 | + record, |
| 51 | + link: { |
| 52 | + id: 'sl_1', |
| 53 | + token: TOKEN, |
| 54 | + object_name: objectName, |
| 55 | + record_id: String(record.id), |
| 56 | + permission: 'view', |
| 57 | + audience: 'anyone', |
| 58 | + }, |
| 59 | + redactFields, |
| 60 | + }, |
| 61 | + }); |
| 62 | + } |
| 63 | + if (pathname === `/api/v1/share-links/${TOKEN}/messages`) return json(200, { success: true, data: [] }); |
| 64 | + return new Response(null, { status: 404 }); |
| 65 | + }), |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +function renderLink(language?: 'zh') { |
| 70 | + const routed = ( |
| 71 | + <MemoryRouter initialEntries={[`/s/${TOKEN}`]}> |
| 72 | + <Routes> |
| 73 | + <Route path="/s/:token" element={<SharedRecordPage />} /> |
| 74 | + </Routes> |
| 75 | + </MemoryRouter> |
| 76 | + ); |
| 77 | + return render( |
| 78 | + language ? ( |
| 79 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 80 | + {routed} |
| 81 | + </I18nProvider> |
| 82 | + ) : ( |
| 83 | + routed |
| 84 | + ), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +/** The value cell beside the row whose label reads `label`. */ |
| 89 | +function valueOf(label: string): string { |
| 90 | + const term = screen.getByText(label, { selector: 'dt' }); |
| 91 | + const value = term.parentElement?.querySelector('dd'); |
| 92 | + expect(value).not.toBeNull(); |
| 93 | + return value!.textContent ?? ''; |
| 94 | +} |
| 95 | + |
| 96 | +afterEach(() => { |
| 97 | + cleanup(); |
| 98 | + vi.unstubAllGlobals(); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('SharedRecordPage — a shared non-chat record renders as read-only rows (objectstack#26767)', () => { |
| 102 | + it('renders each record key as a labelled, formatted row, and no pre block or JSON', async () => { |
| 103 | + shareServer('account', ACCOUNT, ['ssn']); |
| 104 | + const { container } = renderLink(); |
| 105 | + |
| 106 | + expect(await screen.findByText('Shared account')).toBeInTheDocument(); |
| 107 | + |
| 108 | + expect(container.querySelector('pre')).toBeNull(); |
| 109 | + expect(container.textContent).not.toContain('"name"'); |
| 110 | + expect(container.textContent).not.toContain('{'); |
| 111 | + |
| 112 | + expect(valueOf('Name')).toBe('Acme Corporation'); |
| 113 | + expect(valueOf('Annual Revenue')).toBe('1,250,000'); |
| 114 | + expect(valueOf('Is Active')).toBe('Yes'); |
| 115 | + expect(valueOf('Founded On')).toBe(formatDate('2026-03-15', undefined, { locale: 'en' })); |
| 116 | + expect(valueOf('Founded On')).not.toBe('2026-03-15'); |
| 117 | + expect(valueOf('Parent Id')).toBe('—'); |
| 118 | + expect(valueOf('Created At')).not.toContain('T03:04:05'); |
| 119 | + |
| 120 | + // Exactly the keys the record carried: no row is added, none is dropped. |
| 121 | + expect(container.querySelectorAll('dt')).toHaveLength(Object.keys(ACCOUNT).length); |
| 122 | + // System fields are shown, ordered after the business fields. |
| 123 | + const system = screen.getByTestId('shared-record-system-fields'); |
| 124 | + expect(within(system).getByText('Id', { selector: 'dt' })).toBeInTheDocument(); |
| 125 | + expect(within(system).getByText('Created At', { selector: 'dt' })).toBeInTheDocument(); |
| 126 | + expect(within(system).queryByText('Name', { selector: 'dt' })).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('a nested value renders as text, never as [object Object] or JSON', async () => { |
| 130 | + shareServer('account', ACCOUNT, []); |
| 131 | + const { container } = renderLink(); |
| 132 | + |
| 133 | + await screen.findByText('Shared account'); |
| 134 | + expect(valueOf('Tags')).toBe('enterprise, emea'); |
| 135 | + expect(valueOf('Billing Address')).toBe('City: Paris; Zip: 75001'); |
| 136 | + expect(container.textContent).not.toContain('[object Object]'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('a redacted field stays absent, and the hidden-fields line still shows', async () => { |
| 140 | + shareServer('account', ACCOUNT, ['ssn']); |
| 141 | + renderLink(); |
| 142 | + |
| 143 | + await screen.findByText('Shared account'); |
| 144 | + expect(screen.queryByText('Ssn', { selector: 'dt' })).toBeNull(); |
| 145 | + expect(screen.getByText('Some fields are hidden by the owner: ssn')).toBeInTheDocument(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('a record with no fields left says so instead of an empty box', async () => { |
| 149 | + shareServer('account', {}, ['name']); |
| 150 | + const { container } = renderLink(); |
| 151 | + |
| 152 | + await screen.findByText('Shared account'); |
| 153 | + expect(screen.getByText('No data')).toBeInTheDocument(); |
| 154 | + expect(container.querySelector('dt')).toBeNull(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('the ai_conversations branch is unchanged: the transcript, not a field list', async () => { |
| 158 | + shareServer('ai_conversations', { id: 'conv_1', title: 'Quarterly plan' }, []); |
| 159 | + const { container } = renderLink(); |
| 160 | + |
| 161 | + expect(await screen.findByText('Quarterly plan')).toBeInTheDocument(); |
| 162 | + expect(await screen.findByText('This conversation has no messages yet.')).toBeInTheDocument(); |
| 163 | + expect(container.querySelector('dt')).toBeNull(); |
| 164 | + expect(container.querySelector('pre')).toBeNull(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('under zh the value words come from the zh pack', async () => { |
| 168 | + const { zh, en } = builtInLocales; |
| 169 | + // Guard: these are real zh rows, not English copies. |
| 170 | + expect(zh.common.yes).not.toBe(en.common.yes); |
| 171 | + expect(zh.sharedRecord.redactedFields).not.toBe(en.sharedRecord.redactedFields); |
| 172 | + |
| 173 | + shareServer('account', { id: 'acc_1', is_active: true, is_vip: false }, ['ssn']); |
| 174 | + renderLink('zh'); |
| 175 | + |
| 176 | + expect(await screen.findByText('分享的 account')).toBeInTheDocument(); |
| 177 | + expect(valueOf('Is Active')).toBe(zh.common.yes); |
| 178 | + expect(valueOf('Is Vip')).toBe(zh.common.no); |
| 179 | + expect(screen.getByText(zh.sharedRecord.redactedFields.replace('{{fields}}', 'ssn'))).toBeInTheDocument(); |
| 180 | + }); |
| 181 | +}); |
0 commit comments