/** * Structural tests for `renderPullRequestSurface`. Stubs `Text` / `Box` per the * `surfaces/status/statusRender.test.ts` pattern. */ import { createElement, type ReactElement } from 'react' import { createLogInkState, type LogInkState } from '../../../workstation/runtime/inkViewModel' import { createLogInkTheme } from '../../chrome/theme' import { createLogInkContextStatus, updateLogInkContextStatus, } from '../../chrome/context' import type { PullRequestInfo, PullRequestOverview } from '../../../git/pullRequestData' import type { LogInkContext, LogInkComponents } from '../../runtime/types' import { renderPullRequestSurface } from './index' type StubProps = Record const Text = ((props: StubProps) => createElement('text', props, props.children as React.ReactNode) ) as unknown as React.ComponentType const Box = ((props: StubProps) => createElement('box', props, props.children as React.ReactNode) ) as unknown as React.ComponentType const components: LogInkComponents = { Box, Text } function makeState(overrides: Partial = {}): LogInkState { return { ...createLogInkState([]), ...overrides } } function makePr(overrides: Partial = {}): PullRequestInfo { return { number: 42, title: 'Add a thing', url: 'https://github.com/o/r/pull/42', state: 'OPEN', isDraft: false, headRefName: 'feature/x', baseRefName: 'main', ...overrides, } as PullRequestInfo } /** Flatten an Ink element tree into the concatenated visible text. */ function treeText(node: unknown): string { if (node == null || node === false) return '' if (typeof node === 'string' || typeof node === 'number') return String(node) if (Array.isArray(node)) return node.map(treeText).join('') const el = node as { props?: { children?: unknown } } return el.props ? treeText(el.props.children) : '' } function render( state: LogInkState, options: { pullRequest?: PullRequestOverview loading?: boolean provider?: LogInkContext['provider'] pullRequestChecks?: LogInkContext['pullRequestChecks'] checksLoading?: boolean } = {} ): ReactElement { const theme = createLogInkTheme({}) const context: LogInkContext = { ...(options.pullRequest ? { pullRequest: options.pullRequest } : {}), ...(options.provider ? { provider: options.provider } : {}), ...(options.pullRequestChecks ? { pullRequestChecks: options.pullRequestChecks } : {}), } let contextStatus = options.loading ? updateLogInkContextStatus(createLogInkContextStatus('idle'), 'pullRequest', 'loading') : createLogInkContextStatus('ready') if (options.checksLoading) { contextStatus = updateLogInkContextStatus(contextStatus, 'pullRequestChecks', 'loading') } return renderPullRequestSurface({ h: createElement, components, state, context, contextStatus, bodyRows: 30, width: 120, theme, }) } describe('renderPullRequestSurface', () => { it('renders an unavailable state when there is no GitHub remote', () => { const tree = render(makeState(), { pullRequest: { available: false, authenticated: false, message: 'No GitHub remote detected.' }, }) expect(tree).toBeDefined() expect(tree.type).toBe(Box) }) it('renders a not-authenticated state', () => { const tree = render(makeState(), { pullRequest: { available: true, authenticated: false, message: 'GitHub CLI is missing or not authenticated.', }, }) expect(tree).toBeDefined() }) it('renders a loading placeholder while the PR hydrates', () => { expect(render(makeState(), { loading: true })).toBeDefined() }) it('renders a populated current pull request', () => { const tree = render(makeState(), { pullRequest: { available: true, authenticated: true, currentBranch: 'feature/x', repository: { owner: 'o', name: 'r' }, currentPullRequest: makePr(), }, }) expect(tree).toBeDefined() }) it('uses Merge-request wording on a GitLab repo (#0.70)', () => { const tree = render(makeState(), { pullRequest: { available: true, authenticated: true, currentPullRequest: makePr(), }, provider: { repository: { provider: 'gitlab', owner: 'g', name: 'p' }, authenticated: true, } as never, }) const text = treeText(tree) expect(text).toContain('Merge request') expect(text).not.toContain('Pull request') }) it('keeps Pull-request wording on a GitHub repo (#0.70)', () => { const tree = render(makeState(), { pullRequest: { available: true, authenticated: true, currentPullRequest: makePr(), }, provider: { repository: { provider: 'github', owner: 'o', name: 'r' }, authenticated: true, } as never, }) const text = treeText(tree) expect(text).toContain('Pull request') expect(text).not.toContain('Merge request') }) it('reflects focus state via border color', () => { const overview: PullRequestOverview = { available: true, authenticated: true, currentPullRequest: makePr(), } const focused = render(makeState({ focus: 'commits' }), { pullRequest: overview }) const blurred = render(makeState({ focus: 'sidebar' }), { pullRequest: overview }) expect((focused.props as StubProps).borderColor).not.toBe( (blurred.props as StubProps).borderColor ) }) it('structural snapshot — unavailable', () => { expect( render(makeState(), { pullRequest: { available: false, authenticated: false, message: 'No GitHub remote detected.' }, }) ).toMatchSnapshot() }) it('structural snapshot — populated', () => { expect( render(makeState(), { pullRequest: { available: true, authenticated: true, currentPullRequest: makePr(), }, }) ).toMatchSnapshot() }) describe('checks section (OSS-1615)', () => { const overview: PullRequestOverview = { available: true, authenticated: true, currentPullRequest: makePr({ statusCheckRollup: [{ name: 'pipeline', conclusion: 'SUCCESS' }], }), } it('prefers the hydrated per-check breakdown over the rollup', () => { const tree = render(makeState(), { pullRequest: overview, pullRequestChecks: { ok: true, checks: [ { name: 'build', conclusion: 'SUCCESS' }, { name: 'lint', conclusion: 'FAILURE' }, { name: 'test', conclusion: 'SUCCESS' }, ], }, }) const text = treeText(tree) expect(text).toContain('build') expect(text).toContain('lint') expect(text).toContain('test') expect(text).not.toContain('pipeline') }) it('falls back to the overview rollup when no hydrated checks are cached', () => { const tree = render(makeState(), { pullRequest: overview }) const text = treeText(tree) expect(text).toContain('pipeline') }) it('shows a loading line while checks are hydrating', () => { const tree = render(makeState(), { pullRequest: overview, checksLoading: true }) expect(treeText(tree)).toContain('loading checks') }) it('shows the unsupported-forge message when the fetch failed', () => { const tree = render(makeState(), { pullRequest: overview, pullRequestChecks: { ok: false, message: 'Checks are not supported for this forge.' }, }) expect(treeText(tree)).toContain('Checks are not supported for this forge.') }) }) })