feat(护栏): 后管补挂载测试与 readSource 逐文件台账,另加一支 CI job(runner 现状见正文)

这面此前 0 个挂载测试(@vue/test-utils 不在 package.json、node_modules 是空目录),
.vite 页面全靠"读源码做正则断言"钉住,改法一旦不是那个字面写法就失明。
装上 @vue/test-utils + vitest + jsdom 后补了 2 文件 7 例挂载测试,靶子选两处只 console
不给用户提示的失败路径(公司目录没取到时原先只剩"tab 条没画出来")。

source-pin 台账用锚点分段口径:实测这棵树 1,704 个 test(/it( 全部行首出现、无循环生成,
而括号配对切块会被正则字面量里的 ) 提前收口、漏 48 处——分母悄悄变小对"只许减"是沉默的,
所以另加一条"与独立出现次数对平"的断言。基线落在每个文件上:只钉"总数+文件数"两个标量时,
A 加一条配 B 减一条就能双双不动地绕过去。

⚠ 这支 CI job 今天是纸面的:Gitea Actions 只列得出 3 次 run,末次停在 2026-08-11 且全是
failure,此后 origin/master 又推进 1,131 笔没有产生任何 run。同一个 workflow 里 backend 的
mvn test、frontend 的 npm test 也一并不会执行——"CI 强制"这一项要成立,得先恢复 runner。
This commit is contained in:
2026-10-08 17:37:00 +08:00
parent cd715d66ac
commit c363683184
14 changed files with 2785 additions and 4 deletions
@@ -0,0 +1,82 @@
/**
* 首批组件挂载用例(四面9分口径 §2 后管「0 组件挂载测试」)。
*
* 靶子是改掉的那处静默 catch:CompanyTabs 的 `tab.initialize().catch(...)`。
* 这三支跑的是真组件、真 pinia store、真 watch ⇒ 判的是运行时行为,不是读源码的形状钉子。
*
* 跑法:`npm run test:mount`(vitest + jsdom,配置见 vitest.config.ts)。
* 放在 tests/mount/ 且 `node --test tests/*.test.ts` 的 glob 不递归 ⇒ 两套 runner 互不干扰。
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { createPinia, setActivePinia, type Pinia } from 'pinia'
import CompanyTabs from '@/layout/CompanyTabs.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
import { COMPANY_STATUS_ACTIVE } from '@/api/companies-dto'
const feedback = vi.hoisted(() => ({ showAdminFeedback: vi.fn(), runBusy: vi.fn() }))
const companiesApi = vi.hoisted(() => ({ fetchCompanies: vi.fn() }))
vi.mock('@/components/admin-feedback-ui', () => feedback)
vi.mock('@/api/companies', () => companiesApi)
vi.mock('vue-router', () => ({
useRoute: () => ({ meta: { menuKey: 'admin_users' } }),
}))
const company = (id: number, name: string) => ({
id,
companyName: name,
status: COMPANY_STATUS_ACTIVE,
remark: null,
createdAt: null,
updatedAt: null,
})
describe('CompanyTabs:公司目录取数失败必须让用户看得见', () => {
let pinia: Pinia
beforeEach(() => {
vi.clearAllMocks()
pinia = createPinia()
setActivePinia(pinia)
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
})
it('GET /companies 抛错时经统一出口给一条 warning,而不是只剩 console.warn', async () => {
companiesApi.fetchCompanies.mockRejectedValue(new Error('503 Service Unavailable'))
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
mount(CompanyTabs, { global: { plugins: [pinia] } })
await flushPromises()
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
const [message, kind] = feedback.showAdminFeedback.mock.calls[0]
expect(message).toContain('公司目录没取到')
expect(message).toContain('503 Service Unavailable')
expect(kind).toBe('warning')
expect(warn).toHaveBeenCalledTimes(1)
warn.mockRestore()
})
it('反向钉:目录取到时一句提示都不许有,而 tab 条要真画出公司名', async () => {
companiesApi.fetchCompanies.mockResolvedValue([company(1, '甲公司'), company(2, '乙公司')])
const wrapper = mount(CompanyTabs, { global: { plugins: [pinia] } })
await flushPromises()
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
expect(wrapper.findAll('button[role="tab"]').map((b) => b.text())).toEqual(['甲公司', '乙公司'])
})
it('非超管根本不发这个请求,也就不会凭空弹一条"取不到目录"', async () => {
useAdminSessionStore().user = { id: 2, username: 'ops', role: 'admin' }
companiesApi.fetchCompanies.mockRejectedValue(new Error('不该被调用'))
mount(CompanyTabs, { global: { plugins: [pinia] } })
await flushPromises()
expect(companiesApi.fetchCompanies).not.toHaveBeenCalled()
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
})
})
@@ -0,0 +1,157 @@
/**
* 首批组件挂载用例之二(四面9分口径 §2 后管「0 组件挂载测试」)。
*
* 靶子是 NotificationBell.vue 的两处静默 catch(未读数轮询、点通知标已读)。
* 这里跑真组件:真 pinia 会话、真 `notification-bell-model` 里的提示闸门、真定时器
* (用 vi 的假时钟推进 60s 轮询),只把两个出口(api 与 admin-feedback-ui)换成可断言的假件
* ⇒ 判的是运行时行为,不是读源码的形状钉子。
*
* ⚠ 面板是 `<Teleport to="body">`:每支用例都必须 unmount 并清空 body,
* 否则上一支留下的那一行 `.bell-item--unread` 会被这一支的 document 查询捡到(实测会串味)。
*
* 跑法:`npm run test:mount`。
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { createPinia, setActivePinia, type Pinia } from 'pinia'
import NotificationBell from '@/components/NotificationBell.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
import { NOTIFICATION_POLL_INTERVAL_MS } from '@/layout/notification-bell-model'
const feedback = vi.hoisted(() => ({ showAdminFeedback: vi.fn(), runBusy: vi.fn() }))
const api = vi.hoisted(() => ({
fetchNotificationSummary: vi.fn(),
fetchNotificationList: vi.fn(),
markNotificationRead: vi.fn(),
markAllNotificationsRead: vi.fn(),
}))
vi.mock('@/components/admin-feedback-ui', () => feedback)
vi.mock('@/api/notifications', () => api)
const notification = (over: Record<string, unknown> = {}) => ({
id: 7,
scene: 'service_down',
level: 'error',
title: '品牌检测服务挂了',
content: '上游 503',
read: false,
readAt: null,
createdAt: new Date().toISOString(),
...over,
})
describe('NotificationBell:两处失败必须可见', () => {
let pinia: Pinia
let wrapper: VueWrapper | null = null
function mountBell() {
wrapper = mount(NotificationBell, { attachTo: document.body, global: { plugins: [pinia] } })
return wrapper
}
/** 只看当前这一棵树里的面板:Teleport 出去的内容挂在 document.body 上。 */
function panelRow(): HTMLElement | null {
return document.body.querySelector('.bell-item') as HTMLElement | null
}
beforeEach(() => {
vi.clearAllMocks()
pinia = createPinia()
setActivePinia(pinia)
useAdminSessionStore().user = { id: 9, username: 'root', role: 'super_admin' }
api.fetchNotificationList.mockResolvedValue({ items: [], total: 0, page: 1, pageSize: 10, unreadCount: 0 })
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 0, latestId: 0 })
api.markNotificationRead.mockResolvedValue(true)
vi.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
document.body.innerHTML = ''
vi.useRealTimers()
})
it('未读数接口挂了:立刻给一条带原因的 warning,而不是红点无声消失', async () => {
api.fetchNotificationSummary.mockRejectedValue(new Error('502 Bad Gateway'))
mountBell()
await flushPromises()
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
expect(feedback.showAdminFeedback.mock.calls[0]).toEqual(['通知未读数刷新失败:502 Bad Gateway', 'warning'])
})
it('轮询闸门:同一场故障连打三轮也只提示一次,恢复后再坏一次要再提示', async () => {
vi.useFakeTimers()
api.fetchNotificationSummary.mockRejectedValue(new Error('500'))
mountBell()
await vi.advanceTimersByTimeAsync(0)
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
await vi.advanceTimersByTimeAsync(NOTIFICATION_POLL_INTERVAL_MS * 3)
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 2, latestId: 11 })
await vi.advanceTimersByTimeAsync(NOTIFICATION_POLL_INTERVAL_MS)
api.fetchNotificationSummary.mockRejectedValue(new Error('503'))
await vi.advanceTimersByTimeAsync(NOTIFICATION_POLL_INTERVAL_MS)
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(2)
expect(feedback.showAdminFeedback.mock.calls[1][0]).toContain('503')
})
it('点通知标已读失败:提示带原因,且这条不许被前端冒充成"已读"', async () => {
api.fetchNotificationList.mockResolvedValue({
items: [notification({ id: 21 })],
total: 1,
page: 1,
pageSize: 10,
unreadCount: 1,
})
api.markNotificationRead.mockRejectedValue(new Error('403 Forbidden'))
const bell = mountBell()
await flushPromises()
await bell.find('button.bell-trigger').trigger('click')
await flushPromises()
const row = panelRow()
if (!row) throw new Error('面板 Teleport 到 body 后没渲染出那一行通知')
row.click()
await flushPromises()
expect(feedback.showAdminFeedback).toHaveBeenCalledWith('这条通知没能标记为已读:403 Forbidden', 'error')
// 前端不许替后端宣布已读:行仍是未读态、徽标还在
expect(panelRow()?.className).toContain('bell-item--unread')
expect(bell.find('.bell-badge').exists()).toBe(true)
})
it('反向钉:标已读成功时一句提示都不许有,行也要真的转为已读', async () => {
api.fetchNotificationList.mockResolvedValue({
items: [notification({ id: 22 })],
total: 1,
page: 1,
pageSize: 10,
unreadCount: 1,
})
api.markNotificationRead.mockResolvedValue(true)
const bell = mountBell()
await flushPromises()
await bell.find('button.bell-trigger').trigger('click')
await flushPromises()
const row = panelRow()
if (!row) throw new Error('面板 Teleport 到 body 后没渲染出那一行通知')
row.click()
await flushPromises()
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
expect(panelRow()?.className).not.toContain('bell-item--unread')
expect(bell.find('.bell-badge').exists()).toBe(false)
})
})