|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act, type ReactNode } from 'react' |
| 3 | +import { createRoot, type Root } from 'react-dom/client' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import type { SlackSearchInstallationView } from '@/lib/api/contracts/knowledge/slack' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + context: vi.fn(), |
| 9 | + list: vi.fn(), |
| 10 | + manifest: vi.fn(), |
| 11 | + configure: vi.fn(), |
| 12 | + remove: vi.fn(), |
| 13 | + install: vi.fn(), |
| 14 | + refetch: vi.fn(), |
| 15 | + removeError: null as Error | null, |
| 16 | +})) |
| 17 | +vi.mock('nuqs', () => ({ useQueryState: () => [null, vi.fn()] })) |
| 18 | +vi.mock('@/components/settings/settings-panel', () => ({ |
| 19 | + SettingsPanel: ({ children }: { children: ReactNode }) => children, |
| 20 | +})) |
| 21 | +vi.mock('@/app/o/[organizationId]/providers/organization-provider', () => ({ |
| 22 | + useOrganizationContext: mocks.context, |
| 23 | +})) |
| 24 | +vi.mock('@/hooks/queries/slack-search', () => ({ |
| 25 | + useSlackSearchInstallations: mocks.list, |
| 26 | + useSlackSearchManifest: mocks.manifest, |
| 27 | + useConfigureSlackSearch: () => ({ mutate: mocks.configure, isPending: false }), |
| 28 | + useRemoveSlackSearch: () => ({ |
| 29 | + mutate: mocks.remove, |
| 30 | + isPending: false, |
| 31 | + error: mocks.removeError, |
| 32 | + reset: vi.fn(), |
| 33 | + }), |
| 34 | + useStartSlackSearchOAuth: () => ({ mutate: mocks.install, isPending: false, reset: vi.fn() }), |
| 35 | +})) |
| 36 | + |
| 37 | +import { OrganizationSearchSlack } from '@/app/o/[organizationId]/settings/components/organization-search-slack' |
| 38 | + |
| 39 | +const installation: SlackSearchInstallationView = { |
| 40 | + id: 'installation-1', |
| 41 | + credentialId: 'credential-1', |
| 42 | + appId: 'A1', |
| 43 | + teamId: 'T1', |
| 44 | + teamName: 'Test workspace', |
| 45 | + enabled: true, |
| 46 | + needsValidation: false, |
| 47 | + lastOutcome: null, |
| 48 | + lastEventAt: null, |
| 49 | +} |
| 50 | + |
| 51 | +let root: Root |
| 52 | +let container: HTMLDivElement |
| 53 | +beforeEach(() => { |
| 54 | + vi.clearAllMocks() |
| 55 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 56 | + mocks.context.mockReturnValue({ organization: { id: 'org-1' }, viewer: { isAdmin: true } }) |
| 57 | + mocks.list.mockReturnValue({ data: { installations: [], bots: [] } }) |
| 58 | + mocks.manifest.mockReturnValue({ |
| 59 | + data: { manifest: '{}', existingApp: null, createAppUrl: 'https://api.slack.com/apps' }, |
| 60 | + isPending: false, |
| 61 | + refetch: mocks.refetch, |
| 62 | + }) |
| 63 | + mocks.removeError = null |
| 64 | + container = document.createElement('div') |
| 65 | + document.body.appendChild(container) |
| 66 | + root = createRoot(container) |
| 67 | +}) |
| 68 | +afterEach(async () => { |
| 69 | + await act(async () => root.unmount()) |
| 70 | + container.remove() |
| 71 | + vi.unstubAllGlobals() |
| 72 | +}) |
| 73 | +async function render(installed = false) { |
| 74 | + if (installed) { |
| 75 | + mocks.list.mockReturnValue({ |
| 76 | + data: { |
| 77 | + installations: [installation], |
| 78 | + bots: [{ id: 'credential-1', displayName: 'Sim Search' }], |
| 79 | + }, |
| 80 | + }) |
| 81 | + } |
| 82 | + await act(async () => root.render(<OrganizationSearchSlack />)) |
| 83 | +} |
| 84 | +function button(label: string) { |
| 85 | + const element = Array.from(document.querySelectorAll<HTMLButtonElement>('button')).find( |
| 86 | + (element) => element.textContent?.trim() === label |
| 87 | + ) |
| 88 | + expect(element, label).toBeDefined() |
| 89 | + return element! |
| 90 | +} |
| 91 | +async function click(label: string) { |
| 92 | + await act(async () => button(label).click()) |
| 93 | +} |
| 94 | +async function action(label: string) { |
| 95 | + const trigger = container.querySelector<HTMLButtonElement>('[aria-label="Sim Search actions"]')! |
| 96 | + await act(async () => { |
| 97 | + trigger.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })) |
| 98 | + }) |
| 99 | + const item = Array.from(document.querySelectorAll<HTMLElement>('[role="menuitem"]')).find( |
| 100 | + (item) => item.textContent?.trim() === label |
| 101 | + ) |
| 102 | + expect(item, label).toBeDefined() |
| 103 | + await act(async () => item!.click()) |
| 104 | +} |
| 105 | + |
| 106 | +describe('Slack Search settings and shared wizard', () => { |
| 107 | + it('starts with one setup action and the prefilled manifest, with no name or token form', async () => { |
| 108 | + await render() |
| 109 | + expect(container.querySelectorAll('button')).toHaveLength(1) |
| 110 | + await click('Set up') |
| 111 | + expect(document.querySelector('[role="dialog"]')).toHaveTextContent('App manifest') |
| 112 | + expect(document.querySelectorAll('input')).toHaveLength(0) |
| 113 | + expect(mocks.manifest).toHaveBeenCalledWith('org-1', 'Sim Search') |
| 114 | + expect(mocks.install).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | + |
| 117 | + it('shows setup errors and blocks progression until the manifest loads', async () => { |
| 118 | + mocks.manifest.mockReturnValue({ |
| 119 | + error: new Error('Slack needs a public HTTPS URL to send messages to Sim.'), |
| 120 | + refetch: mocks.refetch, |
| 121 | + isPending: false, |
| 122 | + }) |
| 123 | + await render() |
| 124 | + await click('Set up') |
| 125 | + expect(document.querySelector('[role="alert"]')).toHaveTextContent('public HTTPS') |
| 126 | + expect(button('Continue').disabled).toBe(true) |
| 127 | + await click('Retry') |
| 128 | + expect(mocks.refetch).toHaveBeenCalledOnce() |
| 129 | + expect(mocks.install).not.toHaveBeenCalled() |
| 130 | + }) |
| 131 | + |
| 132 | + it('reconnects the existing app and credential without offering duplicate setup', async () => { |
| 133 | + await render(true) |
| 134 | + expect(container.textContent).not.toContain('Set up') |
| 135 | + expect(container.textContent).toContain('Enabled') |
| 136 | + await action('Reconnect') |
| 137 | + expect(document.querySelector('[role="dialog"]')).toHaveTextContent('Reconnect Slack Search') |
| 138 | + expect(document.querySelector('a[href="https://api.slack.com/apps/A1"]')).not.toBeNull() |
| 139 | + await click('Continue') |
| 140 | + expect(document.querySelector('[role="dialog"]')).toHaveTextContent('Leave fields blank') |
| 141 | + await click('Continue') |
| 142 | + await click('Install in Slack') |
| 143 | + expect(mocks.install).toHaveBeenCalledWith( |
| 144 | + expect.objectContaining({ |
| 145 | + installationId: 'installation-1', |
| 146 | + organizationId: 'org-1', |
| 147 | + name: 'Sim Search', |
| 148 | + }), |
| 149 | + expect.any(Object) |
| 150 | + ) |
| 151 | + expect(mocks.install.mock.calls[0][0]).not.toHaveProperty('clientSecret') |
| 152 | + }) |
| 153 | + |
| 154 | + it('disables the selected connection from the actions menu', async () => { |
| 155 | + await render(true) |
| 156 | + await action('Disable') |
| 157 | + expect(mocks.configure).toHaveBeenCalledExactlyOnceWith({ |
| 158 | + organizationId: 'org-1', |
| 159 | + credentialId: 'credential-1', |
| 160 | + enabled: false, |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + it('requires confirmation to remove and keeps a failed removal visible inside the modal', async () => { |
| 165 | + await render(true) |
| 166 | + await action('Remove from Search') |
| 167 | + expect(mocks.remove).not.toHaveBeenCalled() |
| 168 | + await click('Remove') |
| 169 | + expect(mocks.remove).toHaveBeenCalledWith( |
| 170 | + { organizationId: 'org-1', installationId: 'installation-1' }, |
| 171 | + expect.any(Object) |
| 172 | + ) |
| 173 | + mocks.removeError = new Error('Connection could not be removed') |
| 174 | + await render(true) |
| 175 | + expect(document.querySelector('[role="dialog"] [role="alert"]')).toHaveTextContent( |
| 176 | + 'Connection could not be removed' |
| 177 | + ) |
| 178 | + }) |
| 179 | +}) |
0 commit comments