import { headers } from 'next/headers' import { redirect } from 'next/navigation' import { getServerSession } from 'next-auth' import DingtalkEntryPage from './page' import DingtalkEntryClient from './DingtalkEntryClient' jest.mock('next/headers', () => ({ headers: jest.fn() })) jest.mock('next/navigation', () => ({ redirect: jest.fn((path: string) => { throw new Error(path) }), })) jest.mock('next-auth', () => ({ getServerSession: jest.fn() })) jest.mock('@/lib/auth', () => ({ authOptions: {} })) const approvalPath = '/task-detail-change-requests/request-1' function setUserAgent(userAgent: string) { jest.mocked(headers).mockReturnValue(new Headers({ 'user-agent': userAgent })) } describe('DingtalkEntryPage', () => { beforeEach(() => { jest.clearAllMocks() setUserAgent('Mozilla/5.0 Chrome/130.0') jest.mocked(getServerSession).mockResolvedValue(null) }) it('takes a signed-out browser to login with the original approval path', async () => { await expect(DingtalkEntryPage({ searchParams: { redirect: approvalPath } })).rejects.toThrow('/login?redirect=') expect(redirect).toHaveBeenCalledWith(`/login?redirect=${encodeURIComponent(approvalPath)}`) }) it('takes a signed-in browser directly to the approval', async () => { jest.mocked(getServerSession).mockResolvedValue({ user: { name: '伯言' }, expires: '2026-11-28' }) await expect(DingtalkEntryPage({ searchParams: { redirect: approvalPath } })).rejects.toThrow(approvalPath) expect(redirect).toHaveBeenCalledWith(approvalPath) }) it('uses the in-app login flow for an approval opened inside DingTalk', async () => { setUserAgent('Mozilla/5.0 DingTalk/8.0') const page = await DingtalkEntryPage({ searchParams: { redirect: approvalPath, corpId: 'ding-corp' } }) expect(page.type).toBe(DingtalkEntryClient) expect(page.props.redirectPath).toBe(approvalPath) expect(getServerSession).not.toHaveBeenCalled() }) it('keeps performance surveys on their existing notification entry', async () => { const surveyPath = '/performance-survey/survey/survey-1' const page = await DingtalkEntryPage({ searchParams: { redirect: surveyPath, corpId: 'ding-corp' } }) expect(page.type).toBe(DingtalkEntryClient) expect(page.props.redirectPath).toBe(surveyPath) expect(redirect).not.toHaveBeenCalled() }) })