|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#38302 — the Data pillar's own AI line over the object canvas. |
| 5 | + * Its 「放弃 AI 的改动」 drops the object's draft: for an object with a |
| 6 | + * published or code version that goes back to it; for an object the AI |
| 7 | + * created and nobody published there is none, and the discard deletes it. |
| 8 | + * The button's title and the confirm asked over an unsaved edit say which, |
| 9 | + * from the pillar's own reading of the object's layers (`hasBaseline`: an |
| 10 | + * effective or code version). Keys are named, never copy. |
| 11 | + * |
| 12 | + * The harness is the one of `DataPillar.aiDraftDiscard-36511.test.tsx`, with |
| 13 | + * the layers read able to serve an object that holds no layer. |
| 14 | + */ |
| 15 | + |
| 16 | +import '@testing-library/jest-dom/vitest'; |
| 17 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 18 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 19 | +import { MemoryRouter } from 'react-router-dom'; |
| 20 | + |
| 21 | +const PACKAGE = 'com.example.showcase'; |
| 22 | +const live = { |
| 23 | + name: 'showcase_task', |
| 24 | + label: 'Task', |
| 25 | + fields: { |
| 26 | + priority: { name: 'priority', label: 'Priority', type: 'text' }, |
| 27 | + }, |
| 28 | +}; |
| 29 | +/** The AI's version: one field added. */ |
| 30 | +const aiVersion = { |
| 31 | + ...live, |
| 32 | + fields: { ...live.fields, renewal_count: { name: 'renewal_count', label: 'Renewals', type: 'number' } }, |
| 33 | +}; |
| 34 | + |
| 35 | +const minutesAgo = (n: number) => new Date(Date.now() - n * 60_000 - 5_000).toISOString(); |
| 36 | + |
| 37 | +/** What the next draft read serves: `null` for no draft. */ |
| 38 | +let draftEnvelope: Record<string, unknown> | null = null; |
| 39 | +/** Whether the object has a version beyond its draft. */ |
| 40 | +let published = true; |
| 41 | + |
| 42 | +const mockClient = { |
| 43 | + save: vi.fn(async () => ({})), |
| 44 | + publish: vi.fn(async () => ({})), |
| 45 | + reset: vi.fn(async () => { |
| 46 | + draftEnvelope = null; |
| 47 | + return {}; |
| 48 | + }), |
| 49 | + list: vi.fn(async (type: string) => (type === 'object' ? [{ name: 'showcase_task', label: 'Task' }] : [])), |
| 50 | + listDrafts: vi.fn(async () => []), |
| 51 | + // objectstack#38302 — `published` false serves an object with no layer |
| 52 | + // beyond its draft: no published row and no code baseline. |
| 53 | + layered: vi.fn(async () => |
| 54 | + published |
| 55 | + ? { effective: live, code: live, draft: draftEnvelope?.item ?? null } |
| 56 | + : { effective: null, code: null, overlay: null, draft: draftEnvelope?.item ?? null }, |
| 57 | + ), |
| 58 | + getDraft: vi.fn(async () => draftEnvelope), |
| 59 | + get: vi.fn(async () => null), |
| 60 | + withPreviewDrafts() { |
| 61 | + return this; |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 66 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 67 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 68 | +}); |
| 69 | + |
| 70 | +vi.mock('./packages-io', async (importOriginal) => { |
| 71 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 72 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 73 | +}); |
| 74 | + |
| 75 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 76 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 77 | + return { ...mod, useAdapter: () => dataSource }; |
| 78 | +}); |
| 79 | + |
| 80 | +import '@objectstack/lint'; |
| 81 | +import '@object-ui/plugin-detail'; |
| 82 | +import { DataPillar } from './StudioDesignSurface'; |
| 83 | +import { createEmptyDataSource, failOnAbsorbedFetchError } from './__tests__/emptyDataSource'; |
| 84 | +import { addFieldFromFormToolbar } from './__tests__/dataPillarAddField'; |
| 85 | +import { StudioSaveHarness } from './__tests__/studioSaveHarness'; |
| 86 | +import { t } from '../metadata-admin/i18n'; |
| 87 | + |
| 88 | +const discardDrafts = vi.fn(async () => ({ success: true, failedCount: 0, failed: [], discarded: [] })); |
| 89 | +const dataSource = Object.assign(createEmptyDataSource(), { |
| 90 | + getClient: () => ({ packages: { discardDrafts } }), |
| 91 | +}); |
| 92 | +failOnAbsorbedFetchError(); |
| 93 | + |
| 94 | +const L = (key: string) => t(key, 'en-US'); |
| 95 | + |
| 96 | +const confirmSpy = vi.fn((_message?: string) => true); |
| 97 | +beforeEach(() => { |
| 98 | + draftEnvelope = null; |
| 99 | + published = true; |
| 100 | + confirmSpy.mockReset(); |
| 101 | + confirmSpy.mockReturnValue(true); |
| 102 | + window.confirm = confirmSpy as unknown as typeof window.confirm; |
| 103 | +}); |
| 104 | +afterEach(() => { |
| 105 | + cleanup(); |
| 106 | + vi.clearAllMocks(); |
| 107 | +}); |
| 108 | + |
| 109 | +function serveDraft(by: string) { |
| 110 | + draftEnvelope = { |
| 111 | + type: 'object', |
| 112 | + name: 'showcase_task', |
| 113 | + item: { ...aiVersion, _draft: true }, |
| 114 | + version: 'v1', |
| 115 | + draftChanges: [{ path: ['fields', 'renewal_count'], by, actor: 'usr_1', at: minutesAgo(2) }], |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +function renderPillar(onDraftSaved = vi.fn()): { onDraftSaved: ReturnType<typeof vi.fn> } { |
| 120 | + render( |
| 121 | + <MemoryRouter initialEntries={[`/studio/${PACKAGE}/data`]}> |
| 122 | + <StudioSaveHarness locale="en-US"> |
| 123 | + <DataPillar objectName="showcase_task" packageId={PACKAGE} onDraftSaved={onDraftSaved} /> |
| 124 | + </StudioSaveHarness> |
| 125 | + </MemoryRouter>, |
| 126 | + ); |
| 127 | + return { onDraftSaved }; |
| 128 | +} |
| 129 | + |
| 130 | +async function openForm(): Promise<void> { |
| 131 | + fireEvent.click(await screen.findByRole('button', { name: L('engine.studio.data.tab.form') })); |
| 132 | + await screen.findByTestId('form-toolbar'); |
| 133 | +} |
| 134 | + |
| 135 | +const LIVE_TITLE = 'engine.studio.aiDraft.discardTitle'; |
| 136 | +const DELETE_TITLE = 'engine.studio.aiDraft.discardTitleUnpublished'; |
| 137 | +const LIVE_CONFIRM = 'engine.studio.aiDraft.discardConfirmUnsaved'; |
| 138 | +const DELETE_CONFIRM = 'engine.studio.aiDraft.discardConfirmUnsavedUnpublished'; |
| 139 | + |
| 140 | +describe('the Data pillar AI line on an object never published (objectstack#38302)', () => { |
| 141 | + it('an object the AI created and nobody published: the discard title says it deletes it', async () => { |
| 142 | + published = false; |
| 143 | + serveDraft('copilot'); |
| 144 | + renderPillar(); |
| 145 | + const line = await screen.findByTestId('studio-copilot-changes'); |
| 146 | + await waitFor(() => expect(within(line).getByTestId('studio-ai-draft-discard')).toHaveAttribute('title', L(DELETE_TITLE))); |
| 147 | + }); |
| 148 | + |
| 149 | + it('…and over an unsaved edit the confirm says so too; declined, nothing is sent', async () => { |
| 150 | + published = false; |
| 151 | + serveDraft('copilot'); |
| 152 | + renderPillar(); |
| 153 | + await openForm(); |
| 154 | + await addFieldFromFormToolbar(undefined, { label: 'Due' }); |
| 155 | + await screen.findByTestId('studio-save'); |
| 156 | + confirmSpy.mockReturnValueOnce(false); |
| 157 | + fireEvent.click(screen.getByTestId('studio-ai-draft-discard')); |
| 158 | + expect(confirmSpy).toHaveBeenCalledWith(L(DELETE_CONFIRM)); |
| 159 | + expect(mockClient.reset).not.toHaveBeenCalled(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('CONTROL: an object with a published version keeps the live-version wording on the title and the confirm', async () => { |
| 163 | + serveDraft('copilot'); |
| 164 | + renderPillar(); |
| 165 | + const line = await screen.findByTestId('studio-copilot-changes'); |
| 166 | + await waitFor(() => expect(within(line).getByTestId('studio-ai-draft-discard')).toHaveAttribute('title', L(LIVE_TITLE))); |
| 167 | + await openForm(); |
| 168 | + await addFieldFromFormToolbar(undefined, { label: 'Due' }); |
| 169 | + await screen.findByTestId('studio-save'); |
| 170 | + confirmSpy.mockReturnValueOnce(false); |
| 171 | + fireEvent.click(screen.getByTestId('studio-ai-draft-discard')); |
| 172 | + expect(confirmSpy).toHaveBeenCalledWith(L(LIVE_CONFIRM)); |
| 173 | + }); |
| 174 | +}); |
0 commit comments