|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#34725 item 7 — the AI assistant's drafts on the Data canvas: |
| 5 | + * - a line over the canvas, 「AI 刚改了 N 处」, counting the pending draft's |
| 6 | + * `draftChanges` entries an AI tool wrote (copilot or MCP), with 查看 (the |
| 7 | + * pending-changes sheet) and 放弃全部未发布改动 (the package's discard door, |
| 8 | + * `packages.discardDrafts`, after a confirm) — the one undo there is; |
| 9 | + * - the Form canvas's card of each field the assistant changed carries the |
| 10 | + * 草稿 chip; |
| 11 | + * - item 6: the copilot's chip names the open object by its label and the view |
| 12 | + * on screen (`Task · Records`), never its internal name. |
| 13 | + * CONTROL: entries a person wrote in Studio, or no draft, draw no line. |
| 14 | + * |
| 15 | + * The records grid's column marks ride on `GridFieldAuthoring.columnMarks`, |
| 16 | + * pinned in `@object-ui/components` (`data-table-columnMarks-34725`): a |
| 17 | + * field the assistant added is a draft, and the grid selects published fields |
| 18 | + * only (cloud#1652), so a new field has no column until it is published. |
| 19 | + */ |
| 20 | + |
| 21 | +import '@testing-library/jest-dom/vitest'; |
| 22 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 23 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 24 | +import { MemoryRouter } from 'react-router-dom'; |
| 25 | + |
| 26 | +const objectDef = { |
| 27 | + name: 'showcase_task', |
| 28 | + label: 'Task', |
| 29 | + fields: { |
| 30 | + priority: { name: 'priority', label: 'Priority', type: 'text' }, |
| 31 | + renewal_count: { name: 'renewal_count', label: 'Renewals', type: 'number' }, |
| 32 | + }, |
| 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 | + |
| 40 | +const mockClient = { |
| 41 | + save: vi.fn(async () => ({})), |
| 42 | + list: vi.fn(async (type: string) => (type === 'object' ? [{ name: 'showcase_task', label: 'Task' }] : [])), |
| 43 | + listDrafts: vi.fn(async () => []), |
| 44 | + layered: vi.fn(async () => ({ effective: objectDef, code: objectDef, draft: draftEnvelope?.item ?? null })), |
| 45 | + getDraft: vi.fn(async () => draftEnvelope), |
| 46 | + get: vi.fn(async () => null), |
| 47 | + withPreviewDrafts() { |
| 48 | + return this; |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 53 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 54 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 55 | +}); |
| 56 | + |
| 57 | +vi.mock('./packages-io', async (importOriginal) => { |
| 58 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 59 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 60 | +}); |
| 61 | + |
| 62 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 63 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 64 | + return { ...mod, useAdapter: () => dataSource }; |
| 65 | +}); |
| 66 | + |
| 67 | +import '@objectstack/lint'; |
| 68 | +import { DataPillar } from './StudioDesignSurface'; |
| 69 | +import { createEmptyDataSource, failOnAbsorbedFetchError } from './__tests__/emptyDataSource'; |
| 70 | +import { t, tFormat } from '../metadata-admin/i18n'; |
| 71 | + |
| 72 | +const discardDrafts = vi.fn(async () => ({ success: true, failedCount: 0, failed: [], discarded: [] })); |
| 73 | +const dataSource = Object.assign(createEmptyDataSource(), { |
| 74 | + getClient: () => ({ packages: { discardDrafts } }), |
| 75 | +}); |
| 76 | +failOnAbsorbedFetchError(); |
| 77 | + |
| 78 | +beforeEach(() => { |
| 79 | + draftEnvelope = null; |
| 80 | +}); |
| 81 | +afterEach(() => { |
| 82 | + cleanup(); |
| 83 | + vi.clearAllMocks(); |
| 84 | +}); |
| 85 | + |
| 86 | +const L = (key: string) => t(key, 'en-US'); |
| 87 | + |
| 88 | +function serveDraft(draftChanges?: unknown[]) { |
| 89 | + draftEnvelope = { |
| 90 | + type: 'object', |
| 91 | + name: 'showcase_task', |
| 92 | + item: { ...objectDef, _draft: true }, |
| 93 | + version: 'v1', |
| 94 | + ...(draftChanges ? { draftChanges } : {}), |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +function renderPillar(props: Partial<React.ComponentProps<typeof DataPillar>> = {}) { |
| 99 | + return render( |
| 100 | + <MemoryRouter initialEntries={['/studio/com.example.showcase/data']}> |
| 101 | + <DataPillar packageId="com.example.showcase" {...props} /> |
| 102 | + </MemoryRouter>, |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +const AI_TWO = [ |
| 107 | + { path: [], by: 'studio', actor: 'usr_1', at: minutesAgo(30) }, |
| 108 | + { path: ['fields', 'renewal_count'], by: 'copilot', actor: 'usr_1', at: minutesAgo(2) }, |
| 109 | + { path: ['fields', 'priority', 'label'], by: 'mcp', client: 'Claude Desktop', actor: 'usr_1', at: minutesAgo(1) }, |
| 110 | +]; |
| 111 | + |
| 112 | +describe('the AI assistant’s drafts on the Data canvas (objectstack#34725)', () => { |
| 113 | + it('a line counts the AI entries; 查看 opens the review; the AI-changed field cards carry the chip', async () => { |
| 114 | + serveDraft(AI_TWO); |
| 115 | + const onReviewChanges = vi.fn(); |
| 116 | + renderPillar({ onReviewChanges }); |
| 117 | + const line = await screen.findByTestId('studio-copilot-changes'); |
| 118 | + expect(line).toHaveTextContent(tFormat('engine.studio.copilotChanges.line', 'en-US', { count: 2 })); |
| 119 | + fireEvent.click(within(line).getByTestId('studio-copilot-changes-view')); |
| 120 | + expect(onReviewChanges).toHaveBeenCalledTimes(1); |
| 121 | + |
| 122 | + fireEvent.click(screen.getByRole('button', { name: L('engine.studio.data.tab.form') })); |
| 123 | + await waitFor(() => expect(screen.getAllByTestId('studio-form-field-draft-chip')).toHaveLength(2)); |
| 124 | + expect( |
| 125 | + screen.getAllByTestId('studio-form-field-draft-chip').map((chip) => chip.getAttribute('data-draft-of')).sort(), |
| 126 | + ).toEqual(['priority', 'renewal_count']); |
| 127 | + }); |
| 128 | + |
| 129 | + it('one AI entry reads in the singular', async () => { |
| 130 | + serveDraft([AI_TWO[1]]); |
| 131 | + renderPillar(); |
| 132 | + expect(await screen.findByTestId('studio-copilot-changes')).toHaveTextContent( |
| 133 | + tFormat('engine.studio.copilotChanges.lineOne', 'en-US', { count: 1 }), |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('放弃全部未发布改动 discards the package’s drafts after a confirm; a declined confirm sends nothing', async () => { |
| 138 | + serveDraft(AI_TWO); |
| 139 | + // jsdom draws no confirm; the pillar asks the window's. |
| 140 | + const confirm = vi.fn().mockReturnValueOnce(false).mockReturnValueOnce(true); |
| 141 | + const original = window.confirm; |
| 142 | + window.confirm = confirm; |
| 143 | + renderPillar(); |
| 144 | + const discard = await screen.findByTestId('studio-copilot-changes-discard'); |
| 145 | + expect(discard).toHaveTextContent(L('engine.studio.copilotChanges.discardAll')); |
| 146 | + fireEvent.click(discard); |
| 147 | + expect(confirm).toHaveBeenCalledWith(L('engine.studio.copilotChanges.discardConfirm')); |
| 148 | + expect(discardDrafts).not.toHaveBeenCalled(); |
| 149 | + |
| 150 | + const reads = mockClient.getDraft.mock.calls.length; |
| 151 | + draftEnvelope = null; |
| 152 | + fireEvent.click(discard); |
| 153 | + await waitFor(() => expect(discardDrafts).toHaveBeenCalledWith('com.example.showcase')); |
| 154 | + // The open object is read again, and the line goes with the draft. |
| 155 | + await waitFor(() => expect(mockClient.getDraft.mock.calls.length).toBeGreaterThan(reads)); |
| 156 | + await waitFor(() => expect(screen.queryByTestId('studio-copilot-changes')).toBeNull()); |
| 157 | + window.confirm = original; |
| 158 | + }); |
| 159 | + |
| 160 | + it('a discard that fails names each item by its label; the server’s words stay under Details', async () => { |
| 161 | + serveDraft(AI_TWO); |
| 162 | + discardDrafts.mockResolvedValueOnce({ |
| 163 | + success: false, |
| 164 | + failedCount: 2, |
| 165 | + failed: [ |
| 166 | + { type: 'object', name: 'showcase_task', error: 'E_LOCKED: row held by txn 42' }, |
| 167 | + { type: 'view', name: 'showcase_task_grid', error: 'E_GONE' }, |
| 168 | + ], |
| 169 | + discarded: [], |
| 170 | + } as never); |
| 171 | + const original = window.confirm; |
| 172 | + window.confirm = vi.fn(() => true); |
| 173 | + renderPillar(); |
| 174 | + fireEvent.click(await screen.findByTestId('studio-copilot-changes-discard')); |
| 175 | + const message = await screen.findByTestId('studio-refusal-message'); |
| 176 | + expect(message).toHaveTextContent( |
| 177 | + tFormat('engine.studio.copilotChanges.discardFailed', 'en-US', { |
| 178 | + items: ['Task', 'showcase_task_grid'].join(L('engine.studio.copilotChanges.itemSeparator')), |
| 179 | + }), |
| 180 | + ); |
| 181 | + expect(message.textContent).not.toContain('E_LOCKED'); |
| 182 | + expect(message.textContent).not.toContain('object/'); |
| 183 | + expect(screen.getByTestId('studio-refusal-detail')).toHaveTextContent('object/showcase_task: E_LOCKED: row held by txn 42'); |
| 184 | + window.confirm = original; |
| 185 | + }); |
| 186 | + |
| 187 | + it('the copilot’s chip reads the object’s label and the view on screen', async () => { |
| 188 | + const onSurfaceLabelChange = vi.fn(); |
| 189 | + renderPillar({ onSurfaceLabelChange }); |
| 190 | + await waitFor(() => |
| 191 | + expect(onSurfaceLabelChange).toHaveBeenLastCalledWith({ |
| 192 | + type: 'object', |
| 193 | + name: 'showcase_task', |
| 194 | + label: `Task · ${L('engine.studio.data.tab.records')}`, |
| 195 | + }), |
| 196 | + ); |
| 197 | + fireEvent.click(await screen.findByRole('button', { name: L('engine.studio.data.tab.form') })); |
| 198 | + await waitFor(() => |
| 199 | + expect(onSurfaceLabelChange).toHaveBeenLastCalledWith( |
| 200 | + expect.objectContaining({ label: `Task · ${L('engine.studio.data.tab.form')}` }), |
| 201 | + ), |
| 202 | + ); |
| 203 | + }); |
| 204 | + |
| 205 | + it('CONTROL: entries a person wrote in Studio, or no draft at all, draw no line', async () => { |
| 206 | + serveDraft([{ path: ['fields', 'renewal_count'], by: 'studio', actor: 'usr_1', at: minutesAgo(2) }]); |
| 207 | + renderPillar(); |
| 208 | + await screen.findByRole('button', { name: L('engine.studio.data.tab.form') }); |
| 209 | + await waitFor(() => expect(mockClient.getDraft).toHaveBeenCalled()); |
| 210 | + expect(screen.queryByTestId('studio-copilot-changes')).toBeNull(); |
| 211 | + cleanup(); |
| 212 | + |
| 213 | + renderPillar(); |
| 214 | + await screen.findByRole('button', { name: L('engine.studio.data.tab.form') }); |
| 215 | + expect(screen.queryByTestId('studio-copilot-changes')).toBeNull(); |
| 216 | + }); |
| 217 | +}); |
0 commit comments