|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The person's Slack section (#34730 step 3): connect → proposal → confirm → |
| 5 | + * linked, cancel, disconnect, and each answer of the Slack link doors drawn as |
| 6 | + * the door gave it. The doors are faked by URL and method with the envelopes |
| 7 | + * `slack-link-routes.ts` writes; `t` echoes the KEY. |
| 8 | + */ |
| 9 | + |
| 10 | +import '@testing-library/jest-dom/vitest'; |
| 11 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 12 | +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 13 | + |
| 14 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 15 | + ...(await importOriginal<Record<string, unknown>>()), |
| 16 | + useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, language: 'en' }), |
| 17 | +})); |
| 18 | + |
| 19 | +import { SlackLinkSection } from './SlackLinkSection'; |
| 20 | +import { SLACK_LINK_URL } from './slackDoors'; |
| 21 | + |
| 22 | +const ACME = { id: 'acc1', workspace: 'Acme', workspaceId: 'T1', status: 'active' }; |
| 23 | +const LINKED = { id: 'l1', userId: 'alice', slackUserId: 'UADA', slackUserName: 'Ada', state: 'confirmed', confirmedAt: '2026-11-20T10:00:00.000Z' }; |
| 24 | +const ok = (data: unknown) => new Response(JSON.stringify({ success: true, data }), { status: 200 }); |
| 25 | +const refused = (status: number, code: string, message: string, userMessage?: string) => |
| 26 | + new Response(JSON.stringify({ success: false, error: { code, message, ...(userMessage ? { userMessage } : {}) } }), { status }); |
| 27 | + |
| 28 | +type Route = (state: { account: unknown; link: unknown }) => Response; |
| 29 | + |
| 30 | +/** A fake of the four person doors over one stored state; `routes` overrides a door by `METHOD path`. */ |
| 31 | +function doors(initial: { account: unknown; link: unknown }, routes: Record<string, Route> = {}) { |
| 32 | + const state = { ...initial }; |
| 33 | + const fetch = vi.fn(async (url: string, init?: RequestInit) => { |
| 34 | + const key = `${init?.method ?? 'GET'} ${url}`; |
| 35 | + if (routes[key]) return routes[key](state); |
| 36 | + switch (key) { |
| 37 | + case `GET ${SLACK_LINK_URL}`: |
| 38 | + return ok(state); |
| 39 | + case `POST ${SLACK_LINK_URL}/proposal`: |
| 40 | + state.link = { state: 'proposed' }; |
| 41 | + return ok({ link: state.link }); |
| 42 | + case `POST ${SLACK_LINK_URL}/confirm`: |
| 43 | + state.link = LINKED; |
| 44 | + return ok({ link: LINKED }); |
| 45 | + case `DELETE ${SLACK_LINK_URL}`: { |
| 46 | + const removed = state.link !== null; |
| 47 | + state.link = null; |
| 48 | + return ok({ removed }); |
| 49 | + } |
| 50 | + } |
| 51 | + return refused(404, 'NOT_FOUND', key); |
| 52 | + }); |
| 53 | + return { fetch, state }; |
| 54 | +} |
| 55 | + |
| 56 | +const calls = (f: ReturnType<typeof vi.fn>) => f.mock.calls.map(([url, init]) => `${(init as RequestInit | undefined)?.method ?? 'GET'} ${url}`); |
| 57 | + |
| 58 | +afterEach(() => { |
| 59 | + cleanup(); |
| 60 | + vi.restoreAllMocks(); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('SlackLinkSection', () => { |
| 64 | + it('connect → the proposal names no one → confirm → linked', async () => { |
| 65 | + const { fetch } = doors({ account: ACME, link: null }); |
| 66 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 67 | + |
| 68 | + fireEvent.click(await screen.findByTestId('slack-link-connect')); |
| 69 | + const proposal = await screen.findByTestId('slack-link-proposal'); |
| 70 | + expect(proposal).toHaveTextContent('«slackLink.proposed»'); |
| 71 | + expect(proposal).not.toHaveTextContent('Ada'); |
| 72 | + |
| 73 | + fireEvent.click(screen.getByTestId('slack-link-confirm')); |
| 74 | + expect(await screen.findByTestId('slack-link-linked')).toHaveTextContent('«slackLink.linked»'); |
| 75 | + expect(calls(fetch)).toEqual([ |
| 76 | + `GET ${SLACK_LINK_URL}`, |
| 77 | + `POST ${SLACK_LINK_URL}/proposal`, |
| 78 | + `GET ${SLACK_LINK_URL}`, |
| 79 | + `POST ${SLACK_LINK_URL}/confirm`, |
| 80 | + `GET ${SLACK_LINK_URL}`, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + it('cancel removes the proposal', async () => { |
| 85 | + const { fetch, state } = doors({ account: ACME, link: { state: 'proposed' } }); |
| 86 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 87 | + |
| 88 | + fireEvent.click(await screen.findByTestId('slack-link-cancel')); |
| 89 | + expect(await screen.findByTestId('slack-link-connect')).toBeInTheDocument(); |
| 90 | + expect(state.link).toBeNull(); |
| 91 | + expect(calls(fetch)).toContain(`DELETE ${SLACK_LINK_URL}`); |
| 92 | + }); |
| 93 | + |
| 94 | + it('disconnect removes the link', async () => { |
| 95 | + const { fetch, state } = doors({ account: ACME, link: LINKED }); |
| 96 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 97 | + |
| 98 | + fireEvent.click(await screen.findByTestId('slack-link-disconnect')); |
| 99 | + expect(await screen.findByTestId('slack-link-connect')).toBeInTheDocument(); |
| 100 | + expect(state.link).toBeNull(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('no install: says so and offers nothing', async () => { |
| 104 | + const { fetch } = doors({ account: null, link: null }); |
| 105 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 106 | + |
| 107 | + expect(await screen.findByTestId('slack-link-not-installed')).toHaveTextContent('«slackLink.notInstalled»'); |
| 108 | + expect(screen.queryByTestId('slack-link-connect')).toBeNull(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('a revoked token: says so and offers no connect', async () => { |
| 112 | + const { fetch } = doors({ account: { ...ACME, status: 'revoked' }, link: null }); |
| 113 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 114 | + |
| 115 | + expect(await screen.findByTestId('slack-link-revoked')).toHaveTextContent('«slackLink.revoked»'); |
| 116 | + expect(screen.queryByTestId('slack-link-connect')).toBeNull(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('a proposal answering link: null (no verified email, or no Slack user for it) names every cause, never one', async () => { |
| 120 | + const { fetch } = doors({ account: ACME, link: null }, { [`POST ${SLACK_LINK_URL}/proposal`]: () => ok({ link: null }) }); |
| 121 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 122 | + |
| 123 | + fireEvent.click(await screen.findByTestId('slack-link-connect')); |
| 124 | + expect(await screen.findByTestId('slack-link-no-match')).toHaveTextContent('«slackLink.noMatch»'); |
| 125 | + expect(screen.getByTestId('slack-link-connect')).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('a stale proposal: the server’s own sentence (its userMessage), and a new proposal can be asked for', async () => { |
| 129 | + const stale = 'Your email is no longer verified, or is not the email this Slack link was proposed for: ask for a new proposal. Nothing was changed.'; |
| 130 | + const { fetch } = doors({ account: ACME, link: { state: 'proposed' } }, { |
| 131 | + [`POST ${SLACK_LINK_URL}/confirm`]: () => refused(409, 'RESOURCE_CONFLICT', 'english', stale), |
| 132 | + }); |
| 133 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 134 | + |
| 135 | + fireEvent.click(await screen.findByTestId('slack-link-confirm')); |
| 136 | + expect(await screen.findByTestId('slack-link-refused')).toHaveTextContent(stale); |
| 137 | + fireEvent.click(await screen.findByTestId('slack-link-propose-again')); |
| 138 | + await waitFor(() => expect(calls(fetch)).toContain(`POST ${SLACK_LINK_URL}/proposal`)); |
| 139 | + }); |
| 140 | + |
| 141 | + it('a replaced or removed proposal: the server’s 404 sentence, and the section reads the door again', async () => { |
| 142 | + const gone = 'You have no proposed Slack link in this organization to confirm: ask for a proposal first. Nothing was changed.'; |
| 143 | + const { fetch } = doors({ account: ACME, link: { state: 'proposed' } }, { |
| 144 | + [`POST ${SLACK_LINK_URL}/confirm`]: (s) => { |
| 145 | + s.link = null; |
| 146 | + return refused(404, 'RESOURCE_NOT_FOUND', gone); |
| 147 | + }, |
| 148 | + }); |
| 149 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 150 | + |
| 151 | + fireEvent.click(await screen.findByTestId('slack-link-confirm')); |
| 152 | + expect(await screen.findByTestId('slack-link-refused')).toHaveTextContent(gone); |
| 153 | + expect(await screen.findByTestId('slack-link-connect')).toBeInTheDocument(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('Slack refusing the token mid-proposal: the server’s 502 sentence, then the revoked install as re-read', async () => { |
| 157 | + const { fetch } = doors({ account: ACME, link: null }, { |
| 158 | + [`POST ${SLACK_LINK_URL}/proposal`]: (s) => { |
| 159 | + s.account = { ...ACME, status: 'revoked' }; |
| 160 | + return refused(502, 'EXTERNAL_SERVICE_ERROR', 'Slack answered token_revoked. Nothing was changed.'); |
| 161 | + }, |
| 162 | + }); |
| 163 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 164 | + |
| 165 | + fireEvent.click(await screen.findByTestId('slack-link-connect')); |
| 166 | + expect(await screen.findByTestId('slack-link-refused')).toHaveTextContent('Slack answered token_revoked. Nothing was changed.'); |
| 167 | + expect(await screen.findByTestId('slack-link-revoked')).toBeInTheDocument(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('no install when the person clicks: the server’s 409 sentence', async () => { |
| 171 | + const message = 'Slack is not installed for this organization: an administrator installs it first. Nothing was changed.'; |
| 172 | + const { fetch } = doors({ account: ACME, link: null }, { |
| 173 | + [`POST ${SLACK_LINK_URL}/proposal`]: (s) => { |
| 174 | + s.account = null; |
| 175 | + return refused(409, 'RESOURCE_CONFLICT', message); |
| 176 | + }, |
| 177 | + }); |
| 178 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 179 | + |
| 180 | + fireEvent.click(await screen.findByTestId('slack-link-connect')); |
| 181 | + expect(await screen.findByTestId('slack-link-refused')).toHaveTextContent(message); |
| 182 | + expect(await screen.findByTestId('slack-link-not-installed')).toBeInTheDocument(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('a failed read says so', async () => { |
| 186 | + const { fetch } = doors({ account: null, link: null }, { [`GET ${SLACK_LINK_URL}`]: () => refused(503, 'SERVICE_UNAVAILABLE', 'no data engine') }); |
| 187 | + render(<SlackLinkSection apiFetch={fetch} />); |
| 188 | + |
| 189 | + expect(await screen.findByTestId('slack-link-load-failed')).toHaveTextContent('«slackLink.loadFailed»'); |
| 190 | + }); |
| 191 | +}); |
0 commit comments