From 0f2ae24433f73caf6debf8501df1507750ddcb92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=9E=97=E9=93=96?= Date: Mon, 26 Oct 2026 16:41:52 +0800 Subject: [PATCH] feat(organization): add monthly salary templates and Excel export --- DESIGN.md | 1 + PRODUCT.md | 2 +- app/workspaces/[section]/[item]/page.tsx | 6 +- .../PerformanceSalaryDashboard.tsx | 74 +++++++---- .../PerformanceSalaryDetails.test.tsx | 44 +++++++ .../PerformanceSalaryDetails.tsx | 42 ++++++ .../PerformanceSalaryToolbar.tsx | 70 ++++++++++ ...kspaceItemPage.performance-salary.test.tsx | 35 +++++ docs/personnel-salary-base.md | 10 ++ .../performance-salary-export.test.ts | 61 +++++++++ .../performance-salary-templates.test.ts | 51 +++++++ lib/performance-salary-export.ts | 83 ++++++++++++ lib/performance-salary-roles.ts | 12 ++ lib/performance-salary-templates.ts | 124 ++++++++++++++++++ 14 files changed, 583 insertions(+), 32 deletions(-) create mode 100644 components/department-dashboard/PerformanceSalaryDetails.test.tsx create mode 100644 components/department-dashboard/PerformanceSalaryDetails.tsx create mode 100644 components/department-dashboard/PerformanceSalaryToolbar.tsx create mode 100644 lib/__tests__/performance-salary-export.test.ts create mode 100644 lib/__tests__/performance-salary-templates.test.ts create mode 100644 lib/performance-salary-export.ts create mode 100644 lib/performance-salary-roles.ts create mode 100644 lib/performance-salary-templates.ts diff --git a/DESIGN.md b/DESIGN.md index 8e31d61c..2c85f3f3 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -154,6 +154,7 @@ components: - 自动化工作流在 Department Shell 内使用 232px 二级业务栏与同源真实操作面,移动端改为顶部两列菜单。 - 组织架构复用真实组织树与人员明细,提供“品牌中心 / 全公司”范围切换,沿用原管理员权限。 - 权限管理提供“权限组 / 人员权限”两个视图,侧栏只有一个入口。 +- 绩效薪资使用 232px 二级栏,在“薪资明细”分组下固定展开 AI全案策划岗、摄影中心岗、空间视觉岗、管理岗,后台配置以分隔线区分为独立入口。岗位选中态与 URL、右侧标题同步;手机岗位入口为两列,后台配置独立一行,各项最小高度 44px。四岗按确认的 Excel 结构展示只读空模板,月份选择后查询,当前月份与岗位保存在 URL;四岗整套模板可一次导出。分区表头左对齐,字段表头换行、首列固定,桌面和手机均在表格内部横向滚动;不带入真实工资或预设计算公式。 - 素材资产库按权威组织归属选择外壳:品牌中心成员保留 Department Shell,外部普通用户只显示 64px 中性顶栏、素材工作面与退出入口。 - 进行中项目明细采用可键盘聚焦的横向滚动数据表:桌面固定“审批单标题”首列并保留 14 项业务字段,移动端转换为同字段的语义化定义列表;节点时间在数值下方标注对应流程节点名称。 - 按时交付明细使用紧凑的三项流程筛选条;当前项用浅蓝底和蓝字表达,未选项悬停为灰底,切换时同步更新公式、数量和横向明细表。 diff --git a/PRODUCT.md b/PRODUCT.md index 12125f31..5edf2f32 100644 --- a/PRODUCT.md +++ b/PRODUCT.md @@ -25,7 +25,7 @@ web - “自动化工作流”作为“资产与AI自动化”下的业务入口,集中链接现有的绩效考核调研与作品过稿看板。 - “组织架构”统一品牌中心与全公司范围,默认品牌中心,复用真实组织树、人员明细和原管理员权限。 - “绩效薪资”沿用 `/workspaces/organization/performance-review`,仅启用中的超级管理员及明确授权的无邪、清云、伯言、元泽账号可查看;指定账号按固定用户 ID 校验,普通系统/部门管理员不自动获得权限。导航默认隐藏,服务端页面独立核验当前账号权限。 -- “绩效薪资”页内左侧栏提供“薪资明细”和“后台配置”,默认薪资明细,`?view=configuration` 切换配置;窄屏改为顶部两列菜单。“后台配置”提供人员薪资基础部分:姓名、花名、部门、自由文本品牌、基本工资、职级工资、绩效工资,每人一条长期快照,不回写或随 HR 同步覆盖。仅当前启用的超级管理员可增改、软删除,四个指定账号只读。金额精确到分、操作留审计,版本冲突保留草稿并核对最新值;薪资明细仍未接入月度核薪。 +- “绩效薪资”页内左侧栏在“薪资明细”下固定展开 AI全案策划岗、摄影中心岗、空间视觉岗、管理岗四个入口,分别由 `role=ai-planning/photography/spatial-visual/management` 定位,默认 AI全案策划岗;“后台配置”保持独立入口,`?view=configuration` 切换配置。窄屏岗位入口改为顶部两列菜单,后台配置独立一行。四岗按确认的 Excel 结构提供只读空模板、月份查询和四岗整套 Excel 导出;`month=YYYY-MM` 默认为上海当前月并随岗位切换保留。尚未接入人员、月度核薪、绩效公式或人工编辑。“后台配置”提供人员薪资基础部分:姓名、花名、部门、自由文本品牌、基本工资、职级工资、绩效工资,每人一条长期快照,不回写或随 HR 同步覆盖。仅当前启用的超级管理员可增改、软删除,四个指定账号只读。金额精确到分、操作留审计,版本冲突保留草稿并核对最新值。 - 品牌中心工作台访问范围以权威人员绑定和 HR 组织路径为准;品牌中心以外的普通用户登录后只进入 `/assets/material-library`,不显示品牌中心侧栏、返回入口,也不能直接访问 `/` 或 `/workspaces/*`。系统管理员保留维护通行能力。 - “权限管理”统一权限组与人员权限视图;组织数据归组织管理,不再保留数据口径的独立入口。旧链接跳转至对应的新视图。 - “进行中项目”只同步两个已确认的钉钉流程类型,并只统计北京时间 2026 年 4 月 1 日起发起且尚未完结的流程;总览和所有下钻维度共用这一口径。 diff --git a/app/workspaces/[section]/[item]/page.tsx b/app/workspaces/[section]/[item]/page.tsx index 755ed49e..a927e65f 100644 --- a/app/workspaces/[section]/[item]/page.tsx +++ b/app/workspaces/[section]/[item]/page.tsx @@ -4,6 +4,8 @@ import { notFound, redirect } from 'next/navigation' import { Suspense } from 'react' import { authOptions } from '@/lib/auth' import { canViewPerformanceSalary } from '@/lib/performance-salary-access' +import { resolveSalaryRole } from '@/lib/performance-salary-roles' +import { resolveSalaryMonth } from '@/lib/performance-salary-templates' import AutomationWorkflowsDashboard from '@/components/department-dashboard/AutomationWorkflowsDashboard' import PermissionsDashboard from '@/components/department-dashboard/PermissionsDashboard' import BrandHealthDashboard from '@/components/department-dashboard/BrandHealthDashboard' @@ -29,7 +31,7 @@ type WorkspaceItemPageProps = { section: string item: string } - searchParams?: { view?: string; scope?: string } + searchParams?: { view?: string; scope?: string; role?: string | string[]; month?: string | string[] } } function AutomationWorkflowsFallback() { @@ -142,7 +144,7 @@ export default async function WorkspaceItemPage({ params, searchParams }: Worksp if (!session?.user) redirect('/login') if (session.user.canAccessBrandCenter !== true) redirect('/assets/material-library') if (!(await canViewPerformanceSalary(session.user.id))) redirect('/workspaces/dashboard/core-metrics') - return + return } if (workspace.section.key === 'assets' && workspace.item.key === 'automation-workflows') { diff --git a/components/department-dashboard/PerformanceSalaryDashboard.tsx b/components/department-dashboard/PerformanceSalaryDashboard.tsx index 4bbfb5d7..036ea4e6 100644 --- a/components/department-dashboard/PerformanceSalaryDashboard.tsx +++ b/components/department-dashboard/PerformanceSalaryDashboard.tsx @@ -1,48 +1,64 @@ import Link from 'next/link' import { ListChecks, Settings2 } from 'lucide-react' +import { SALARY_ROLES, type SalaryRole } from '@/lib/performance-salary-roles' +import { currentSalaryMonth, salaryDetailsHref, SALARY_PATH } from '@/lib/performance-salary-templates' import DepartmentShell from './DepartmentShell' import PersonnelSalaryPanel from './PersonnelSalaryPanel' +import PerformanceSalaryDetails from './PerformanceSalaryDetails' -// Operate: inherit the existing split workbench; two URL-backed views, no sample salary data. -const VIEWS = [ - { key: 'details', title: '薪资明细', href: '/workspaces/organization/performance-review', icon: ListChecks }, - { key: 'configuration', title: '后台配置', href: '/workspaces/organization/performance-review?view=configuration', icon: Settings2 }, -] as const +const navigationItemClass = (active: boolean) => `flex min-h-11 min-w-0 items-center gap-3 rounded-lg px-3 py-3 text-sm font-semibold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${active ? 'bg-[#e6f4ff] text-[#0050cb]' : 'text-[#4f6077] hover:bg-[#eef2f6] hover:text-[#101828]'}` -export default function PerformanceSalaryDashboard({ view = 'details' }: { view?: 'details' | 'configuration' }) { - const activeView = VIEWS.find((item) => item.key === view)! +// Operate: URL-backed role navigation within the existing salary workbench. +export default function PerformanceSalaryDashboard({ view = 'details', role = 'ai-planning', month = currentSalaryMonth() }: { view?: 'details' | 'configuration'; role?: SalaryRole; month?: string }) { + const activeRole = SALARY_ROLES.find((item) => item.key === role)! + const title = view === 'configuration' ? '后台配置' : `${activeRole.title} · 薪资明细` return (
-

{activeView.title}

+

{title}

- {view === 'configuration' ? :
-

暂无薪资明细

-

薪资数据尚未接入。

-
} + {view === 'configuration' ? : }
diff --git a/components/department-dashboard/PerformanceSalaryDetails.test.tsx b/components/department-dashboard/PerformanceSalaryDetails.test.tsx new file mode 100644 index 00000000..fe30b04a --- /dev/null +++ b/components/department-dashboard/PerformanceSalaryDetails.test.tsx @@ -0,0 +1,44 @@ +import { fireEvent, render, screen, waitFor, within } from '@testing-library/react' +import PerformanceSalaryDetails from './PerformanceSalaryDetails' +import { saveSalaryTemplateWorkbook } from '@/lib/performance-salary-export' + +const push = jest.fn() +jest.mock('next/navigation', () => ({ useRouter: () => ({ push }) })) +jest.mock('@/lib/performance-salary-export', () => ({ + saveSalaryTemplateWorkbook: jest.fn(), +})) + +beforeEach(() => { jest.clearAllMocks() }) + +test('月份切换推入URL,前进后退的新props同步,表格为五行空模板', () => { + const { rerender } = render() + expect(within(screen.getAllByRole('row')[1]).getAllByRole('columnheader')).toHaveLength(25) + expect(screen.getAllByRole('row').slice(2)).toHaveLength(5) + expect(screen.getAllByRole('cell').every(cell => cell.textContent === '')).toBe(true) + fireEvent.change(screen.getByLabelText('薪资月份'), { target: { value: '2027-01' } }) + fireEvent.click(screen.getByRole('button', { name: '查询' })) + expect(push).toHaveBeenCalledWith('/workspaces/organization/performance-review?role=ai-planning&month=2027-01', { scroll: false }) + rerender() + expect(screen.getByLabelText('薪资月份')).toHaveValue('2027-01') + expect(within(screen.getAllByRole('row')[1]).getAllByRole('columnheader')).toHaveLength(23) + expect(within(screen.getByRole('table')).getByText('2027年1月绩效得分')).toBeInTheDocument() + rerender() + expect(screen.getByLabelText('薪资月份')).toHaveValue('2026-09') +}) + +test('导出失败可重试,保存时防止重复点击,月份不受影响', async () => { + const save = saveSalaryTemplateWorkbook as jest.Mock + let finish!: () => void + save.mockRejectedValueOnce(new Error('下载不可用')).mockImplementationOnce(() => new Promise(resolve => { finish = resolve })) + render() + fireEvent.click(screen.getByRole('button', { name: '导出四岗模板' })) + expect(await screen.findByRole('alert')).toHaveTextContent('下载不可用') + expect(screen.getByLabelText('薪资月份')).toHaveValue('2026-09') + fireEvent.click(screen.getByRole('button', { name: '导出四岗模板' })) + await waitFor(() => expect(save).toHaveBeenCalledTimes(2)) + expect(screen.getByRole('button', { name: '正在生成…' })).toBeDisabled() + fireEvent.click(screen.getByRole('button', { name: '正在生成…' })) + expect(save).toHaveBeenCalledTimes(2) + finish() + expect(await screen.findByRole('status')).toHaveTextContent('四岗模板已生成') +}) diff --git a/components/department-dashboard/PerformanceSalaryDetails.tsx b/components/department-dashboard/PerformanceSalaryDetails.tsx new file mode 100644 index 00000000..dd0e0515 --- /dev/null +++ b/components/department-dashboard/PerformanceSalaryDetails.tsx @@ -0,0 +1,42 @@ +import type { SalaryRole } from '@/lib/performance-salary-roles' +import { getSalaryTemplate, SALARY_TEMPLATE_BLANK_ROWS } from '@/lib/performance-salary-templates' +import PerformanceSalaryToolbar from './PerformanceSalaryToolbar' + +export default function PerformanceSalaryDetails({ role, month }: { role: SalaryRole; month: string }) { + const template = getSalaryTemplate(role, month) + const tableWidth = template.columns.reduce((sum, column) => sum + column.width, 0) + return ( + <> + +
+

左右滚动查看完整字段,首列保持固定。

+
+ + + {template.columns.map(column => )} + + {template.grouped && ( + {template.groups.map((group, index) => ( + + ))} + )} + {template.columns.map((column, index) => ( + + ))} + + {Array.from({ length: SALARY_TEMPLATE_BLANK_ROWS }, (_, row) => ( + {template.columns.map((column, index) => ( + + ))} +
{month} {template.title}薪资明细空白模板,5行待配置
+ {group.label} +
{column.label}
+ ))}
+
+
+ + ) +} diff --git a/components/department-dashboard/PerformanceSalaryToolbar.tsx b/components/department-dashboard/PerformanceSalaryToolbar.tsx new file mode 100644 index 00000000..9d682caf --- /dev/null +++ b/components/department-dashboard/PerformanceSalaryToolbar.tsx @@ -0,0 +1,70 @@ +'use client' + +import { useEffect, useRef, useState, useTransition } from 'react' +import { useRouter } from 'next/navigation' +import { Download } from 'lucide-react' +import type { SalaryRole } from '@/lib/performance-salary-roles' +import { isSalaryMonth, salaryDetailsHref, SALARY_PATH } from '@/lib/performance-salary-templates' +import { saveSalaryTemplateWorkbook } from '@/lib/performance-salary-export' + +export default function PerformanceSalaryToolbar({ role, month }: { role: SalaryRole; month: string }) { + const router = useRouter() + const [draft, setDraft] = useState(month) + const [pending, startTransition] = useTransition() + const [exporting, setExporting] = useState(false) + const exportLock = useRef(false) + const [feedback, setFeedback] = useState<{ error: boolean; text: string } | null>(null) + useEffect(() => { setDraft(month); setFeedback(null) }, [month, role]) + + async function exportTemplates() { + if (exportLock.current) return + exportLock.current = true + setExporting(true) + setFeedback(null) + try { + await saveSalaryTemplateWorkbook(month) + setFeedback({ error: false, text: '四岗模板已生成,请在下载中查看。' }) + } catch (error) { + setFeedback({ error: true, text: `${error instanceof Error ? error.message : '模板导出失败。'} 请点击导出重试。` }) + } finally { + exportLock.current = false + setExporting(false) + } + } + + return ( +
+
+
{ + event.preventDefault() + const value = new FormData(event.currentTarget).get('month') + if (isSalaryMonth(value) && value !== month) { + setDraft(value) + startTransition(() => router.push(salaryDetailsHref(role, value), { scroll: false })) + } + }}> + + + +
+ +
+

{pending ? '正在切换月份…' : '当前为空白模板,人员数据与计算规则待配置。'}

+ {feedback &&

{feedback.text}

} +
+ ) +} diff --git a/components/department-dashboard/WorkspaceItemPage.performance-salary.test.tsx b/components/department-dashboard/WorkspaceItemPage.performance-salary.test.tsx index 1f79e323..4588986f 100644 --- a/components/department-dashboard/WorkspaceItemPage.performance-salary.test.tsx +++ b/components/department-dashboard/WorkspaceItemPage.performance-salary.test.tsx @@ -57,6 +57,7 @@ test.each([ const page = await WorkspaceItemPage(props) expect(page.type).toBe(PerformanceSalaryDashboard) expect(page.props.view).toBe('details') + expect(page.props.role).toBe('ai-planning') expect(generateMetadata(props).title).toBe('绩效薪资 | 组织管理 | 品牌中心部门看板') }) @@ -77,6 +78,40 @@ test('后台配置链接同样经过薪资权限门禁', async () => { await expect(WorkspaceItemPage({ ...props, searchParams: { view: 'configuration' } })).rejects.toThrow('redirect:/workspaces/dashboard/core-metrics') }) +test.each(['ai-planning', 'photography', 'spatial-visual', 'management'])('岗位直达链接仍拦截旧超管会话:%s', async role => { + getSession.mockResolvedValue({ user: { id: 'ordinary', isSuperAdmin: true, canAccessBrandCenter: true } }) + findUser.mockResolvedValue({ id: 'ordinary', isSuperAdmin: false, isActive: true, status: 'active', archivedAt: null }) + await expect(WorkspaceItemPage({ ...props, searchParams: { role } })).rejects.toThrow('redirect:/workspaces/dashboard/core-metrics') +}) + +test.each([ + ['ai-planning', 'ai-planning'], + ['photography', 'photography'], + ['spatial-visual', 'spatial-visual'], + ['management', 'management'], + ['unknown', 'ai-planning'], + [['management', 'photography'], 'ai-planning'], +])('授权访问解析岗位参数:%j', async (role, expectedRole) => { + getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } }) + findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null }) + const page = await WorkspaceItemPage({ ...props, searchParams: { role } }) + expect(page.props.role).toBe(expectedRole) +}) + +test('岗位参数不覆盖后台配置视图', async () => { + getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } }) + findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null }) + const page = await WorkspaceItemPage({ ...props, searchParams: { view: 'configuration', role: 'management' } }) + expect(page.props.view).toBe('configuration') +}) + +test.each(['2027-01', '2026-09'])('已授权岗位直达链接解析薪资月份:%s', async month => { + getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } }) + findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null }) + const page = await WorkspaceItemPage({ ...props, searchParams: { role: 'management', month } }) + expect(page.props.month).toBe(month) +}) + test('其他组织管理板块不受薪资专属权限限制', async () => { await WorkspaceItemPage({ params: { section: 'organization', item: 'team-functions' } }) expect(getSession).not.toHaveBeenCalled() diff --git a/docs/personnel-salary-base.md b/docs/personnel-salary-base.md index 412722f8..ad0357f9 100644 --- a/docs/personnel-salary-base.md +++ b/docs/personnel-salary-base.md @@ -2,6 +2,16 @@ 本次仅完成长期基础档案;不计算月度薪资、提成或实际发薪。入口:`/workspaces/organization/performance-review?view=configuration`。 +## 岗位薪资明细框架 + +“薪资明细”下固定展开 AI全案策划岗、摄影中心岗、空间视觉岗、管理岗,分别使用 `role=ai-planning`、`role=photography`、`role=spatial-visual`、`role=management`。缺少、无效或重复岗位参数默认首岗;`view=configuration` 优先显示后台配置。桌面二级栏纵向展开,手机两列排列,后台配置独立一行。 + +四岗按确认的源表结构展示只读模板,分别为 25、16、21、23 列与五行空白内容。AI 去掉保底提成、百分比、实际保底;空间视觉去掉百分比、实际绩效;管理岗将考核和薪资合为一个人员行,去掉保护提成,绩效得分和绩效系数分别保留。模板权重文字沿用原表,空间视觉分区 5% 与单项 6% 仅展示,不代表已确认计算规则。 + +月份使用 `month=YYYY-MM`,缺少、无效或重复参数回退上海时区当前月。选择月份后点击查询,岗位与配置导航保留月份,跨年比较列自动更新;浏览器刷新和前进后退按 URL 恢复。数据来源、可人工修改字段和计算规则待后续配置,不读取基础档案填充模板,不新增业务接口、数据库或人员岗位绑定。 + +“导出四岗模板”从同一模板配置生成 `绩效薪资模板_YYYY-MM.xlsx`,包含四个工作表,保持分区、字段顺序、月份、五行空白、冻结表头和首列。只有模板标题和表头文字,不含人员、实际薪资、公式、外链或隐藏页。沿用薪资查看权限;导出动态加载 ExcelJS,使用浏览器标准下载,不依赖宿主可能未提供的文件系统保存窗口,取消浏览器保存不改变月份,生成失败可重试。 + ## 数据与权限 - 七个快照字段:姓名、花名、部门、品牌、基本工资、职级工资、绩效工资。花名册只供选择在职人员和关联稳定 ID,不回写 HR,也不随 HR 更新覆盖配置。 diff --git a/lib/__tests__/performance-salary-export.test.ts b/lib/__tests__/performance-salary-export.test.ts new file mode 100644 index 00000000..5a0df691 --- /dev/null +++ b/lib/__tests__/performance-salary-export.test.ts @@ -0,0 +1,61 @@ +import ExcelJS from 'exceljs' +import JSZip from 'jszip' +import { buildSalaryTemplateWorkbook, salaryExportFileName, saveSalaryTemplateWorkbook } from '@/lib/performance-salary-export' + +test('导出后重新读取:四岗模板字段、空行、冻结及月份均正确且无隐私数据或公式', async () => { + const workbook = await buildSalaryTemplateWorkbook('2027-01') + const buffer = await workbook.xlsx.writeBuffer() + const reloaded = new ExcelJS.Workbook() + await reloaded.xlsx.load(buffer) + expect(reloaded.worksheets.map(sheet => sheet.name)).toEqual(['AI全案策划岗', '摄影中心岗', '空间视觉岗', '管理岗']) + const expected = [ + { columns: 25, headerRow: 3, lastHeader: '备注', firstMerge: 'A2:G2' }, + { columns: 16, headerRow: 2, lastHeader: '备注', firstMerge: 'A1:P1' }, + { columns: 21, headerRow: 3, lastHeader: '备注', firstMerge: 'A2:F2' }, + { columns: 23, headerRow: 3, lastHeader: '2026年12月薪资', firstMerge: 'A2:C2' }, + ] + reloaded.worksheets.forEach((sheet, index) => { + const target = expected[index] + expect(sheet.state).toBe('visible') + expect(sheet.columnCount).toBe(target.columns) + expect(sheet.rowCount).toBe(target.headerRow + 5) + expect(sheet.getCell(1, 1).value).toBe(`2027-01 ${sheet.name}薪资明细`) + expect(sheet.getCell(target.headerRow, target.columns).value).toBe(target.lastHeader) + expect(sheet.model.merges).toContain(target.firstMerge) + expect(sheet.views[0]).toEqual(expect.objectContaining({ state: 'frozen', xSplit: 1, ySplit: target.headerRow })) + for (let row = target.headerRow + 1; row <= sheet.rowCount; row += 1) { + for (let col = 1; col <= target.columns; col += 1) expect(sheet.getCell(row, col).value).toBeNull() + } + }) + expect(reloaded.worksheets[1].getCell('N2').value).toBe('2026年11月合计薪资') + const zip = await JSZip.loadAsync(buffer) + expect(Object.keys(zip.files).some(path => /externalLinks|hidden/i.test(path))).toBe(false) + for (const path of Object.keys(zip.files).filter(path => /^xl\/worksheets\/sheet\d+\.xml$/.test(path))) { + const xml = await zip.file(path)!.async('string') + expect(xml).not.toMatch(/]/) + } +}) + +test('校验导出月份与文件名', async () => { + expect(salaryExportFileName('2026-09')).toBe('绩效薪资模板_2026-09.xlsx') + await expect(buildSalaryTemplateWorkbook('../../secret')).rejects.toThrow('有效月份') +}) + +test('标准下载文件并回收对象URL', async () => { + const create = jest.fn(() => 'blob:salary-template') + const revoke = jest.fn() + Object.defineProperty(URL, 'createObjectURL', { configurable: true, value: create }) + Object.defineProperty(URL, 'revokeObjectURL', { configurable: true, value: revoke }) + const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function(this: HTMLAnchorElement) { + expect(this.download).toBe('绩效薪资模板_2026-09.xlsx') + }) + const timer = jest.spyOn(window, 'setTimeout') + await saveSalaryTemplateWorkbook('2026-09') + expect(create).toHaveBeenCalledWith(expect.any(Blob)) + expect(click).toHaveBeenCalledTimes(1) + const cleanup = timer.mock.calls.find(call => call[1] === 1000)?.[0] as (() => void) + cleanup() + expect(revoke).toHaveBeenCalledWith('blob:salary-template') + click.mockRestore() + timer.mockRestore() +}) diff --git a/lib/__tests__/performance-salary-templates.test.ts b/lib/__tests__/performance-salary-templates.test.ts new file mode 100644 index 00000000..1635b931 --- /dev/null +++ b/lib/__tests__/performance-salary-templates.test.ts @@ -0,0 +1,51 @@ +import { currentSalaryMonth, getSalaryTemplate, resolveSalaryMonth, salaryDetailsHref, salaryMonthLabel } from '@/lib/performance-salary-templates' + +describe('薪资模板月份与字段', () => { + test('上海时区跨月及非法查询参数', () => { + const now = new Date('2026-12-31T16:00:00Z') + expect(currentSalaryMonth(now)).toBe('2027-01') + for (const value of [undefined, '', '2026-13', '2026-1', '0000-01', ['2026-09'], '2026-09