Files
AFFiNE-Mirror/packages/common/reader/__tests__/reader.spec.ts
fengmk2 39cb1afedb fix(server): limit rootDoc snapshot size (#12625)
close CLOUD-225

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **New Features**
  - Added support for reading document blocks without requiring a workspace or root document snapshot.
- **Bug Fixes**
  - Improved handling of large workspace snapshots by skipping them when they exceed 10MB.
- **Tests**
  - Introduced new test cases to cover scenarios where root or workspace snapshots are absent.
  - Expanded snapshot tests for document block reading.
- **Refactor**
  - Updated several function signatures to make root and workspace snapshot parameters optional for greater flexibility.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-29 05:32:31 +00:00

103 lines
2.6 KiB
TypeScript

import { readFileSync } from 'node:fs';
import path from 'node:path';
import { expect, test } from 'vitest';
import { applyUpdate, Array as YArray, Doc as YDoc, Map as YMap } from 'yjs';
import {
readAllBlocksFromDoc,
readAllDocIdsFromRootDoc,
readAllDocsFromRootDoc,
} from '../src';
const rootDocSnapshot = readFileSync(
path.join(import.meta.dirname, './__fixtures__/test-root-doc.snapshot.bin')
);
const docSnapshot = readFileSync(
path.join(import.meta.dirname, './__fixtures__/test-doc.snapshot.bin')
);
test('should read doc blocks work', async () => {
const rootDoc = new YDoc({
guid: 'test-root-doc',
});
applyUpdate(rootDoc, rootDocSnapshot);
const doc1 = new YDoc({
guid: 'test-doc',
});
applyUpdate(doc1, docSnapshot);
const result = await readAllBlocksFromDoc({
ydoc: doc1,
rootYDoc: rootDoc,
spaceId: 'test-space',
});
expect(result).toMatchSnapshot();
});
test('should read doc blocks work without root doc', async () => {
const doc = new YDoc({
guid: 'test-doc',
});
applyUpdate(doc, docSnapshot);
const result = await readAllBlocksFromDoc({
ydoc: doc,
spaceId: 'test-space',
});
expect(result).toMatchSnapshot();
});
test('should get all docs from root doc work', async () => {
const rootDoc = new YDoc({
guid: 'test-root-doc',
});
rootDoc.getMap('meta').set(
'pages',
YArray.from([
new YMap([
['id', 'test-doc-1'],
['title', 'Test Doc 1'],
]),
new YMap([
['id', 'test-doc-2'],
['title', 'Test Doc 2'],
]),
new YMap([
['id', 'test-doc-3'],
['title', 'Test Doc 3'],
['trash', true],
]),
new YMap([['id', 'test-doc-4']]),
])
);
const docs = readAllDocsFromRootDoc(rootDoc);
expect(Array.from(docs.entries())).toMatchSnapshot();
// include trash
const docsWithTrash = readAllDocsFromRootDoc(rootDoc, {
includeTrash: true,
});
expect(Array.from(docsWithTrash.entries())).toMatchSnapshot();
});
test('should read all docs from root doc snapshot work', async () => {
const rootDoc = new YDoc({
guid: 'test-root-doc',
});
applyUpdate(rootDoc, rootDocSnapshot);
const docsWithTrash = readAllDocsFromRootDoc(rootDoc, {
includeTrash: true,
});
expect(Array.from(docsWithTrash.entries())).toMatchSnapshot();
});
test('should read all doc ids from root doc snapshot work', async () => {
const rootDoc = new YDoc({
guid: 'test-root-doc',
});
applyUpdate(rootDoc, rootDocSnapshot);
const docIds = readAllDocIdsFromRootDoc(rootDoc);
expect(docIds).toMatchSnapshot();
});