Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
245 lines
5.9 KiB
TypeScript
245 lines
5.9 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,
|
|
Text as YText,
|
|
} from 'yjs';
|
|
|
|
import {
|
|
parsePageDoc,
|
|
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')
|
|
);
|
|
const docSnapshotWithAiEditable = readFileSync(
|
|
path.join(
|
|
import.meta.dirname,
|
|
'./__fixtures__/test-doc-with-ai-editable.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();
|
|
});
|
|
|
|
test('should parse page doc work', () => {
|
|
const doc = new YDoc({
|
|
guid: 'test-doc',
|
|
});
|
|
applyUpdate(doc, docSnapshot);
|
|
|
|
const result = parsePageDoc({
|
|
workspaceId: 'test-space',
|
|
doc,
|
|
buildBlobUrl: id => `blob://${id}`,
|
|
buildDocUrl: id => `doc://${id}`,
|
|
renderDocTitle: id => `Doc Title ${id}`,
|
|
});
|
|
|
|
expect(result).toMatchSnapshot();
|
|
});
|
|
|
|
test('should parse page doc work with ai editable', () => {
|
|
const doc = new YDoc({
|
|
guid: 'test-doc',
|
|
});
|
|
applyUpdate(doc, docSnapshot);
|
|
|
|
const result = parsePageDoc({
|
|
workspaceId: 'test-space',
|
|
doc,
|
|
buildBlobUrl: id => `blob://${id}`,
|
|
buildDocUrl: id => `doc://${id}`,
|
|
renderDocTitle: id => `Doc Title ${id}`,
|
|
aiEditable: true,
|
|
});
|
|
|
|
expect(result.md).toMatchSnapshot();
|
|
});
|
|
|
|
test('should parse page full doc work with ai editable', () => {
|
|
const doc = new YDoc({
|
|
guid: 'test-doc',
|
|
});
|
|
applyUpdate(doc, docSnapshotWithAiEditable);
|
|
|
|
const result = parsePageDoc({
|
|
workspaceId: 'test-space',
|
|
doc,
|
|
buildBlobUrl: id => `blob://${id}`,
|
|
buildDocUrl: id => `doc://${id}`,
|
|
renderDocTitle: id => `Doc Title ${id}`,
|
|
aiEditable: true,
|
|
});
|
|
|
|
expect(result.md).toMatchSnapshot();
|
|
});
|
|
|
|
test('should index references from database rich-text cells', async () => {
|
|
const doc = new YDoc({
|
|
guid: 'db-doc',
|
|
});
|
|
const blocks = doc.getMap('blocks');
|
|
|
|
const pageTitle = new YText();
|
|
pageTitle.insert(0, 'Page');
|
|
const page = new YMap();
|
|
page.set('sys:id', 'page');
|
|
page.set('sys:flavour', 'affine:page');
|
|
page.set('sys:children', YArray.from(['note']));
|
|
page.set('prop:title', pageTitle);
|
|
blocks.set('page', page);
|
|
|
|
const note = new YMap();
|
|
note.set('sys:id', 'note');
|
|
note.set('sys:flavour', 'affine:note');
|
|
note.set('sys:children', YArray.from(['db']));
|
|
note.set('prop:displayMode', 'page');
|
|
blocks.set('note', note);
|
|
|
|
const dbTitle = new YText();
|
|
dbTitle.insert(0, 'Database');
|
|
const db = new YMap();
|
|
db.set('sys:id', 'db');
|
|
db.set('sys:flavour', 'affine:database');
|
|
db.set('sys:children', new YArray());
|
|
db.set('prop:title', dbTitle);
|
|
|
|
const columns = new YArray();
|
|
const column = new YMap();
|
|
column.set('id', 'col1');
|
|
column.set('name', 'Text');
|
|
column.set('type', 'rich-text');
|
|
column.set('data', new YMap());
|
|
columns.push([column]);
|
|
db.set('prop:columns', columns);
|
|
|
|
const cellText = new YText();
|
|
cellText.applyDelta([
|
|
{ insert: 'See ' },
|
|
{
|
|
insert: 'Target',
|
|
attributes: {
|
|
reference: {
|
|
pageId: 'target-doc',
|
|
params: { mode: 'page' },
|
|
},
|
|
},
|
|
},
|
|
]);
|
|
|
|
const cell = new YMap();
|
|
cell.set('columnId', 'col1');
|
|
cell.set('value', cellText);
|
|
const row = new YMap();
|
|
row.set('col1', cell);
|
|
const cells = new YMap();
|
|
cells.set('row1', row);
|
|
db.set('prop:cells', cells);
|
|
|
|
blocks.set('db', db);
|
|
|
|
const result = await readAllBlocksFromDoc({
|
|
ydoc: doc,
|
|
spaceId: 'test-space',
|
|
});
|
|
|
|
const dbBlock = result?.blocks.find(block => block.blockId === 'db');
|
|
expect(dbBlock?.refDocId).toEqual(['target-doc']);
|
|
expect(dbBlock?.ref).toEqual([
|
|
JSON.stringify({ docId: 'target-doc', mode: 'page' }),
|
|
]);
|
|
});
|