π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useDocumentStore } from '@/store/document';
import { useDocumentLock } from './useDocumentLock';
const editLockCalls = vi.hoisted(() => ({ value: [] as any[] }));
const mockPageState: {
current: Record<string, unknown>;
} = { current: {} };
vi.mock('./store', () => ({
usePageEditorStore: (selector: any) => selector(mockPageState.current),
}));
vi.mock('./usePageLockedByOther', () => ({
usePageLockedByOther: () => false,
}));
vi.mock('@/hooks/usePermission', () => ({
usePermission: () => ({ allowed: true }),
}));
vi.mock('@/features/ResourcePermission/useResourceAccess', () => ({
useResourceAccess: () => ({ canEditResource: true }),
}));
vi.mock('@/features/EditLock', () => ({
useEditLock: (options: any) => {
editLockCalls.value.push(options);
return {
expiresAt: null,
health: 'healthy',
holderId: null,
lockedByOther: false,
ownerId: undefined,
pending: false,
};
},
}));
vi.mock('@/libs/swr', () => ({
mutate: vi.fn(),
}));
vi.mock('@/services/document', () => ({
documentService: {
acquireDocumentLock: vi.fn(),
getDocumentLock: vi.fn().mockResolvedValue({ holderId: null }),
releaseDocumentLock: vi.fn(),
},
}));
const createMockEditor = () =>
({
getDocument: vi.fn(() => null),
setDocument: vi.fn(),
}) as any;
describe('useDocumentLock β lockOwnerId publication into the document store', () => {
beforeEach(() => {
const state = useDocumentStore.getState();
Object.keys(state.documents).forEach((id) => state.closeDocument(id));
mockPageState.current = {
documentId: 'doc-1',
isWorkspacePage: true,
lockExpiresAt: null,
lockOwnerId: undefined,
setLockHealth: vi.fn(),
setLockOwnerId: vi.fn(),
setLockPending: vi.fn(),
setLockState: vi.fn(),
};
});
it('publishes lockOwnerId after the document loads into the store post-mount', () => {
// On first mount the SWR fetch has not landed yet, so the store has no
// document row and the initial `updateDocument` dispatch is dropped by the
// reducer. The hook must re-publish once the document exists β otherwise
// every save goes out with `lockOwnerId: undefined` and the server rejects
// it as a CONFLICT against the user's own lease.
renderHook(() => useDocumentLock());
expect(useDocumentStore.getState().documents['doc-1']).toBeUndefined();
act(() => {
useDocumentStore.getState().initDocumentWithEditor({
content: '# Test',
documentId: 'doc-1',
editor: createMockEditor(),
sourceType: 'page',
});
});
const doc = useDocumentStore.getState().documents['doc-1'];
expect(doc).toBeDefined();
expect(doc.lockOwnerId).toMatch(/^page:doc-1:/);
});
it('publishes lockOwnerId immediately when the document is already in the store', () => {
act(() => {
useDocumentStore.getState().initDocumentWithEditor({
content: '# Test',
documentId: 'doc-1',
editor: createMockEditor(),
sourceType: 'page',
});
});
renderHook(() => useDocumentLock());
expect(useDocumentStore.getState().documents['doc-1'].lockOwnerId).toMatch(/^page:doc-1:/);
});
it('clears lockOwnerId from the document store on unmount', () => {
act(() => {
useDocumentStore.getState().initDocumentWithEditor({
content: '# Test',
documentId: 'doc-1',
editor: createMockEditor(),
sourceType: 'page',
});
});
const { unmount } = renderHook(() => useDocumentLock());
expect(useDocumentStore.getState().documents['doc-1'].lockOwnerId).toBeDefined();
unmount();
expect(useDocumentStore.getState().documents['doc-1'].lockOwnerId).toBeUndefined();
});
});
describe('useDocumentLock β inside a collaboration room', () => {
beforeEach(() => {
editLockCalls.value = [];
mockPageState.current = {
collab: { access: 'edit', pending: 0, status: 'synced' },
documentId: 'doc-1',
isWorkspacePage: true,
lockExpiresAt: null,
lockOwnerId: undefined,
setLockHealth: vi.fn(),
setLockOwnerId: vi.fn(),
setLockPending: vi.fn(),
setLockState: vi.fn(),
};
});
it('takes the lock as the room once this member edits, so editors outside the room are locked out', () => {
mockPageState.current.collab = { access: 'edit', edited: true, pending: 0, status: 'synced' };
renderHook(() => useDocumentLock());
const options = editLockCalls.value.at(-1);
expect(options.enabled).toBe(true);
expect(options.isDirty).toBe(true);
expect(options.ownerId).toMatch(/^room:doc-1:/);
});
it('only watches the lock while this member has not edited', () => {
renderHook(() => useDocumentLock());
const options = editLockCalls.value.at(-1);
expect(options.enabled).toBe(true);
expect(options.isDirty).toBe(false);
});
});