|
| 1 | +import { configureStore } from '@reduxjs/toolkit' |
| 2 | +import { Provider } from 'react-redux' |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { render } from 'vitest-browser-react' |
| 5 | + |
| 6 | +import type { AgentId, SyncPreviewResult } from '@/shared/types' |
| 7 | + |
| 8 | +const mockSyncPreview = vi.fn() |
| 9 | +const mockSyncExecute = vi.fn() |
| 10 | + |
| 11 | +const SCOPED_PREVIEW: SyncPreviewResult = { |
| 12 | + totalSkills: 4, |
| 13 | + totalAgents: 1, |
| 14 | + toCreate: 2, |
| 15 | + alreadySynced: 2, |
| 16 | + conflicts: [], |
| 17 | + forAgent: 'claude-code', |
| 18 | +} |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + mockSyncPreview.mockReset() |
| 22 | + mockSyncPreview.mockResolvedValue(SCOPED_PREVIEW) |
| 23 | + mockSyncExecute.mockReset() |
| 24 | + mockSyncExecute.mockResolvedValue({ details: [] }) |
| 25 | + // Browser mode replaces Electron's preload bridge, so install the sync IPC |
| 26 | + // surface that CleanupAgentDialog reaches through the Redux thunks. |
| 27 | + vi.stubGlobal('electron', { |
| 28 | + sync: { |
| 29 | + preview: mockSyncPreview, |
| 30 | + execute: mockSyncExecute, |
| 31 | + }, |
| 32 | + }) |
| 33 | +}) |
| 34 | + |
| 35 | +afterEach(() => { |
| 36 | + vi.unstubAllGlobals() |
| 37 | +}) |
| 38 | + |
| 39 | +/** |
| 40 | + * Build the smallest Redux store CleanupAgentDialog needs. |
| 41 | + * @returns Store with the ui slice wired for preview target dispatches. |
| 42 | + * @example |
| 43 | + * const store = await createStore() |
| 44 | + * store.dispatch(setCleanupAgentTarget('claude-code')) |
| 45 | + */ |
| 46 | +async function createStore() { |
| 47 | + const { default: uiReducer } = |
| 48 | + await import('@/renderer/src/redux/slices/uiSlice') |
| 49 | + return configureStore({ |
| 50 | + reducer: { ui: uiReducer }, |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Opens the dialog through Redux after the component's first closed render. |
| 56 | + * This mirrors the production path where AgentItem sets cleanupAgentTarget |
| 57 | + * from a context-menu click after CleanupAgentDialog has already mounted. |
| 58 | + * @param agentId - Agent receiving the scoped cleanup preview. |
| 59 | + * @returns The rendered browser screen and backing store. |
| 60 | + * @example |
| 61 | + * const { screen } = await renderClosedThenOpen('claude-code') |
| 62 | + */ |
| 63 | +async function renderClosedThenOpen(agentId: AgentId) { |
| 64 | + const store = await createStore() |
| 65 | + const { CleanupAgentDialog } = await import('./CleanupAgentDialog') |
| 66 | + const { setCleanupAgentTarget } = |
| 67 | + await import('@/renderer/src/redux/slices/uiSlice') |
| 68 | + |
| 69 | + const screen = await render( |
| 70 | + <Provider store={store}> |
| 71 | + <CleanupAgentDialog /> |
| 72 | + </Provider>, |
| 73 | + ) |
| 74 | + |
| 75 | + // The crash video opened the dialog from an already-mounted, closed state. |
| 76 | + store.dispatch(setCleanupAgentTarget(agentId)) |
| 77 | + |
| 78 | + return { screen, store } |
| 79 | +} |
| 80 | + |
| 81 | +describe('CleanupAgentDialog', () => { |
| 82 | + it('stays closed and skips preview when no agent target is active', async () => { |
| 83 | + const store = await createStore() |
| 84 | + const { CleanupAgentDialog } = await import('./CleanupAgentDialog') |
| 85 | + |
| 86 | + const screen = await render( |
| 87 | + <Provider store={store}> |
| 88 | + <CleanupAgentDialog /> |
| 89 | + </Provider>, |
| 90 | + ) |
| 91 | + |
| 92 | + await new Promise((resolve) => setTimeout(resolve, 0)) |
| 93 | + expect(mockSyncPreview).toHaveBeenCalledTimes(0) |
| 94 | + expect(screen.getByText(/Cleanup missing skills/i).query()).toBeNull() |
| 95 | + expect( |
| 96 | + screen.getByRole('button', { name: /Cleanup \d+ skills/ }).query(), |
| 97 | + ).toBeNull() |
| 98 | + }) |
| 99 | + |
| 100 | + it('opens from the closed state without changing the hook order', async () => { |
| 101 | + const { screen } = await renderClosedThenOpen('claude-code') |
| 102 | + |
| 103 | + await expect |
| 104 | + .poll(() => |
| 105 | + mockSyncPreview.mock.calls.some( |
| 106 | + ([options]) => options?.agentId === 'claude-code', |
| 107 | + ), |
| 108 | + ) |
| 109 | + .toBe(true) |
| 110 | + await expect |
| 111 | + .element(screen.getByText(/Cleanup missing skills.*Claude Code/)) |
| 112 | + .toBeInTheDocument() |
| 113 | + await expect.element(screen.getByText('Symlinks to create')).toBeVisible() |
| 114 | + await expect |
| 115 | + .element(screen.getByRole('button', { name: 'Cleanup 2 skills' })) |
| 116 | + .toBeVisible() |
| 117 | + }) |
| 118 | +}) |
0 commit comments