Files
apes-sidebar/tests/task-window-delete-arm.spec.tsx
T
unsc9 fe7125ae3b refactor(safety): 复用既有 helper、归位注释——c8d5030 的质量收敛
不改变行为,只收敛上一提交引入的重复与错位:

- FileTree 会话重置改为复用 clearSelection():它自带「选中已空则返回」守卫,未选
  中时不再为一次重置白跑整棵树的 re-render。anchor 仍无条件清——取消最后一行选中
  会留下「空选中 + 非空锚点」,这个状态必须在切换时一并丢掉。依赖数组去掉已不再
  被该 effect 引用的 setSelection。
- fs-operations:isWorkspaceRoot 的 JSDoc 原本紧贴 sameSpelling 上方,结果
  sameSpelling 挂着两段注释,而真正执行安全守卫的函数反而没有文档——已归位,
  让「为什么必须比 dev+ino 而不是比字符串」跟着守卫走。
- 测试:task-window 三份重复挂载收进 beforeEach;git-view-worktree 新用例改用同
  文件已有的 makeRoot()(提到模块级,两个 describe 共用);fs-operations 的
  makeProject() 同样提出模块级,供软链接用例复用。

验证:typecheck 通过;改动文件 lint 干净;相关 6 个 spec 64 通过。其中
fs-operations 两条 symlink 用例在本机 EPERM(无创建软链接权限),stash 对比确认
改动前同样失败,与本次无关。
2026-10-05 11:31:54 +08:00

131 lines
4.2 KiB
TypeScript

/**
* The task window's delete is a TWO-step button armed in component state: the
* first click arms it ("Confirm delete"), the second one deletes. Every task
* surface swaps the subject task through props with no `key`, so React reuses
* the instance — an armed button carried into the next task made that reader's
* FIRST click the delete itself, one click away from removing a task they
* never armed.
*
* What must NOT change: arming still works, and the second click still goes
* through with the visible task's id.
*/
// @vitest-environment jsdom
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { TaskWindow } from '../src/client/TaskWindow.tsx'
import type { SidebarTeamTaskView } from '../src/context-types.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
// vitest follows the OS locale; pin en-US so the button copy is English.
beforeAll(() => {
Object.defineProperty(window.navigator, 'language', { value: 'en-US', configurable: true })
})
const { teamsTaskUpdate } = vi.hoisted(() => ({
teamsTaskUpdate: vi.fn(async () => ({ ok: true })),
}))
vi.mock('../src/client/api.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/client/api.ts')>()
return { ...actual, api: { ...actual.api, teamsTaskUpdate } }
})
/** One board row as the projection hands it over. */
function taskView(id: string, subject: string): SidebarTeamTaskView {
return {
id,
revision: 1,
subject,
description: 'body',
status: 'in_progress',
blockedBy: [],
writeScopes: [],
ready: true,
writeScopeWarnings: [],
}
}
let container: HTMLDivElement
let root: Root
/** Render — or RE-render in place — the one window, as the caller's popover
* state does when another task is opened. */
async function render(task: SidebarTeamTaskView): Promise<void> {
await act(async () => {
root.render(createElement(TaskWindow, {
rootId: 'root-1',
task,
members: [],
onClose: () => {},
}))
})
}
/** One footer button by its copy (the window is body-portaled). */
function button(label: string): HTMLElement {
const found = [...document.querySelectorAll<HTMLElement>('button')]
.find(el => el.textContent?.trim() === label)
if (found === undefined) throw new Error(`button not found: ${label}`)
return found
}
function hasButton(label: string): boolean {
return [...document.querySelectorAll<HTMLElement>('button')]
.some(el => el.textContent?.trim() === label)
}
afterEach(() => {
act(() => { root.unmount() })
container.remove()
document.body.innerHTML = ''
teamsTaskUpdate.mockClear()
})
describe('TaskWindow two-step delete', () => {
beforeEach(() => {
container = document.createElement('div')
document.body.append(container)
root = createRoot(container)
})
it('disarms the delete when the window swaps to another task', async () => {
await render(taskView('t1', 'First'))
act(() => { button('Delete').click() })
expect(hasButton('Confirm delete')).toBe(true)
expect(hasButton('Delete')).toBe(false)
expect(teamsTaskUpdate).not.toHaveBeenCalled()
// Another task opened in the same instance (no key on the caller's side).
await render(taskView('t2', 'Second'))
expect(hasButton('Delete')).toBe(true)
expect(hasButton('Confirm delete')).toBe(false)
})
it('makes the first click on the next task an arm, never the delete', async () => {
await render(taskView('t1', 'First'))
act(() => { button('Delete').click() }) // armed on t1
await render(taskView('t2', 'Second'))
await act(async () => { button('Delete').click() }) // first click on t2
expect(teamsTaskUpdate).not.toHaveBeenCalled()
expect(hasButton('Confirm delete')).toBe(true)
})
it('still deletes the visible task on the second click', async () => {
await render(taskView('t9', 'Only'))
act(() => { button('Delete').click() })
await act(async () => { button('Confirm delete').click() })
expect(teamsTaskUpdate).toHaveBeenCalledWith('root-1', {
taskId: 't9',
expectedRevision: 1,
action: 'delete',
})
})
})