fix(tasks): 编辑保存使用打开编辑时的 revision 快照
This commit is contained in:
1 parent
9278ecf1d9
commit
9f121ac600
2 files changed
+118
-2
No files matched your search
@@ -240,6 +240,10 @@ export function TaskWindow(props: TaskWindowProps): ReactNode {
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [note, setNote] = useState<string | undefined>(undefined)
|
||||
const [armedDelete, setArmedDelete] = useState(false)
|
||||
/** Revision the draft was opened against: the projection keeps delivering
|
||||
* new revisions while the window stays open, and saving with the live one
|
||||
* would silently overwrite a teammate's write. */
|
||||
const editRevisionRef = useRef<number | undefined>(task?.revision)
|
||||
|
||||
// Re-seed the draft when the caller swaps the subject task: the projection
|
||||
// hands us fresh revisions, and unsaved text must not leak across tasks.
|
||||
@@ -309,7 +313,7 @@ export function TaskWindow(props: TaskWindowProps): ReactNode {
|
||||
? api.teamsTaskCreate(rootId, { subject: nextSubject, description: description.trim() })
|
||||
: api.teamsTaskUpdate(rootId, {
|
||||
taskId: task.id,
|
||||
expectedRevision: task.revision,
|
||||
expectedRevision: editRevisionRef.current ?? task.revision,
|
||||
action: 'edit',
|
||||
subject: nextSubject,
|
||||
description: description.trim(),
|
||||
@@ -378,7 +382,7 @@ export function TaskWindow(props: TaskWindowProps): ReactNode {
|
||||
variant="outline"
|
||||
size="sm"
|
||||
icon={<IconEditOutlineRegular size={12} />}
|
||||
onClick={() => { setEditing(true) }}
|
||||
onClick={() => { editRevisionRef.current = task.revision; setEditing(true) }}
|
||||
>
|
||||
{t('teamTaskEdit')}
|
||||
</Button>
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* The task window's edit save is a CAS write: `expectedRevision` must be the
|
||||
* revision the draft was OPENED against, not the live one the projection keeps
|
||||
* pushing while the window stays open. Saving with the live revision makes the
|
||||
* host's conflict check a no-op and silently overwrites a teammate's write.
|
||||
*/
|
||||
// @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 { Context, 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 } }
|
||||
})
|
||||
|
||||
function taskView(revision: number, subject: string): SidebarTeamTaskView {
|
||||
return {
|
||||
id: 't1',
|
||||
revision,
|
||||
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 projection does
|
||||
* when a teammate's write lands during an open edit. */
|
||||
async function render(task: SidebarTeamTaskView): Promise<void> {
|
||||
await act(async () => {
|
||||
root.render(createElement(TaskWindow, {
|
||||
rootId: 'root-1',
|
||||
task,
|
||||
members: [],
|
||||
ctx: {} as Context,
|
||||
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
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
act(() => { root.unmount() })
|
||||
container.remove()
|
||||
document.body.innerHTML = ''
|
||||
teamsTaskUpdate.mockClear()
|
||||
})
|
||||
|
||||
describe('TaskWindow edit revision snapshot', () => {
|
||||
beforeEach(() => {
|
||||
container = document.createElement('div')
|
||||
document.body.append(container)
|
||||
root = createRoot(container)
|
||||
})
|
||||
|
||||
it('saves against the revision present when editing opened, not the live one', async () => {
|
||||
await render(taskView(1, 'First'))
|
||||
act(() => { button('Edit').click() })
|
||||
// A teammate's write lands while the draft is open: the projection hands
|
||||
// over revision 2, but the draft still describes revision 1.
|
||||
await render(taskView(2, 'Changed elsewhere'))
|
||||
await act(async () => { button('Save').click() })
|
||||
|
||||
expect(teamsTaskUpdate).toHaveBeenCalledWith('root-1', expect.objectContaining({
|
||||
taskId: 't1',
|
||||
action: 'edit',
|
||||
expectedRevision: 1,
|
||||
}))
|
||||
})
|
||||
|
||||
it('re-bases the snapshot when editing is reopened', async () => {
|
||||
await render(taskView(1, 'First'))
|
||||
act(() => { button('Edit').click() })
|
||||
act(() => { button('Cancel').click() })
|
||||
await render(taskView(3, 'Changed elsewhere'))
|
||||
act(() => { button('Edit').click() })
|
||||
await act(async () => { button('Save').click() })
|
||||
|
||||
expect(teamsTaskUpdate).toHaveBeenCalledWith('root-1', expect.objectContaining({
|
||||
expectedRevision: 3,
|
||||
}))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user