From 9f121ac600554b5965a3fac936724dffb324f7b6 Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Fri, 6 Nov 2026 12:38:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(tasks):=20=E7=BC=96=E8=BE=91=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E4=BD=BF=E7=94=A8=E6=89=93=E5=BC=80=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E6=97=B6=E7=9A=84=20revision=20=E5=BF=AB=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/TaskWindow.tsx | 8 +- tests/task-window-edit-revision.spec.tsx | 112 +++++++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 tests/task-window-edit-revision.spec.tsx diff --git a/src/client/TaskWindow.tsx b/src/client/TaskWindow.tsx index a261d13a..a0d6d1e0 100644 --- a/src/client/TaskWindow.tsx +++ b/src/client/TaskWindow.tsx @@ -240,6 +240,10 @@ export function TaskWindow(props: TaskWindowProps): ReactNode { const [busy, setBusy] = useState(false) const [note, setNote] = useState(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(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={} - onClick={() => { setEditing(true) }} + onClick={() => { editRevisionRef.current = task.revision; setEditing(true) }} > {t('teamTaskEdit')} diff --git a/tests/task-window-edit-revision.spec.tsx b/tests/task-window-edit-revision.spec.tsx new file mode 100644 index 00000000..3df71b08 --- /dev/null +++ b/tests/task-window-edit-revision.spec.tsx @@ -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() + 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 { + 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('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, + })) + }) +})