|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#26471 — a draft card's 发布 (N) follows a publish it did not |
| 11 | + * run. The chat banner (and Studio) publish the package, then fire the host's |
| 12 | + * live-metadata pulse; the card re-reads its package's pending count from the |
| 13 | + * same `fetchPendingDraftCount` its first read used, and a count of 0 draws |
| 14 | + * the Published badge instead of the button. |
| 15 | + * |
| 16 | + * Pinned: |
| 17 | + * - 发布 (2) → pulse → the fetch now answers 0 → no 发布, Published shown; |
| 18 | + * - a re-render alone fetches nothing (the count is re-read on a pulse, not |
| 19 | + * per render); |
| 20 | + * - unmount unsubscribes, and a later pulse fetches nothing. |
| 21 | + */ |
| 22 | +import '@testing-library/jest-dom/vitest'; |
| 23 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 24 | +import { act, cleanup, render, screen } from '@testing-library/react'; |
| 25 | +import { ChatbotEnhanced, type ChatbotEnhancedMessage } from '../ChatbotEnhanced'; |
| 26 | +import { detectDraftResult } from '../mapMessages'; |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | + cleanup(); |
| 30 | +}); |
| 31 | + |
| 32 | +const PACKAGE_ID = 'app.crm'; |
| 33 | + |
| 34 | +/** One assistant message whose single tool part drafted a field into `app.crm`. */ |
| 35 | +function draftedMessage(): ChatbotEnhancedMessage { |
| 36 | + const draftReview = detectDraftResult({ |
| 37 | + status: 'drafted', |
| 38 | + type: 'object', |
| 39 | + name: 'crm_account', |
| 40 | + summary: 'Added field Priority', |
| 41 | + changedKeys: ['fields'], |
| 42 | + packageId: PACKAGE_ID, |
| 43 | + }); |
| 44 | + if (!draftReview) throw new Error('fixture is not a drafted envelope'); |
| 45 | + return { |
| 46 | + id: 'a1', |
| 47 | + role: 'assistant', |
| 48 | + content: '', |
| 49 | + toolInvocations: [{ toolCallId: 't1', toolName: 'add_field', state: 'output-available', draftReview }], |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** The host's pulse, shaped like app-shell's `subscribeMetadataRefresh` / `emitMetadataRefresh`. */ |
| 54 | +function makePulse() { |
| 55 | + const listeners = new Set<() => void>(); |
| 56 | + return { |
| 57 | + listeners, |
| 58 | + subscribe: (listener: () => void) => { |
| 59 | + listeners.add(listener); |
| 60 | + return () => { |
| 61 | + listeners.delete(listener); |
| 62 | + }; |
| 63 | + }, |
| 64 | + emit: () => { |
| 65 | + for (const l of listeners) l(); |
| 66 | + }, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +function chat(pulse: ReturnType<typeof makePulse>, fetchPendingDraftCount: (pkg: string) => Promise<number>) { |
| 71 | + return ( |
| 72 | + <ChatbotEnhanced |
| 73 | + messages={[draftedMessage()]} |
| 74 | + onSendMessage={vi.fn()} |
| 75 | + onPublishDrafts={vi.fn()} |
| 76 | + publishDraftsLabel="发布" |
| 77 | + publishedLabel="已发布" |
| 78 | + fetchPendingDraftCount={fetchPendingDraftCount} |
| 79 | + subscribeMetadataRefresh={pulse.subscribe} |
| 80 | + /> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +describe('objectstack#26471 — the draft card re-reads its pending count on the metadata pulse', () => { |
| 85 | + it('发布 (2), then a publish elsewhere fires the pulse and the card stops offering 发布', async () => { |
| 86 | + const pulse = makePulse(); |
| 87 | + let pending = 2; |
| 88 | + const fetchPendingDraftCount = vi.fn(async (_pkg: string) => pending); |
| 89 | + const { rerender } = render(chat(pulse, fetchPendingDraftCount)); |
| 90 | + |
| 91 | + expect(await screen.findByTestId('draft-publish')).toHaveTextContent('发布 (2)'); |
| 92 | + expect(fetchPendingDraftCount).toHaveBeenCalledTimes(1); |
| 93 | + expect(fetchPendingDraftCount).toHaveBeenCalledWith(PACKAGE_ID); |
| 94 | + |
| 95 | + // A re-render with nothing new is not a reason to read again. |
| 96 | + rerender(chat(pulse, fetchPendingDraftCount)); |
| 97 | + expect(fetchPendingDraftCount).toHaveBeenCalledTimes(1); |
| 98 | + |
| 99 | + // The banner published the package and pulsed; the server now has 0 pending. |
| 100 | + pending = 0; |
| 101 | + await act(async () => { |
| 102 | + pulse.emit(); |
| 103 | + }); |
| 104 | + |
| 105 | + expect(fetchPendingDraftCount).toHaveBeenCalledTimes(2); |
| 106 | + expect(fetchPendingDraftCount).toHaveBeenLastCalledWith(PACKAGE_ID); |
| 107 | + expect(screen.queryByTestId('draft-publish')).not.toBeInTheDocument(); |
| 108 | + expect(screen.getByText('已发布')).toBeInTheDocument(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('unmount unsubscribes: a later pulse reads nothing', async () => { |
| 112 | + const pulse = makePulse(); |
| 113 | + const fetchPendingDraftCount = vi.fn(async (_pkg: string) => 2); |
| 114 | + const { unmount } = render(chat(pulse, fetchPendingDraftCount)); |
| 115 | + |
| 116 | + expect(await screen.findByTestId('draft-publish')).toHaveTextContent('发布 (2)'); |
| 117 | + expect(pulse.listeners.size).toBe(1); |
| 118 | + |
| 119 | + unmount(); |
| 120 | + expect(pulse.listeners.size).toBe(0); |
| 121 | + |
| 122 | + pulse.emit(); |
| 123 | + expect(fetchPendingDraftCount).toHaveBeenCalledTimes(1); |
| 124 | + }); |
| 125 | +}); |
0 commit comments