Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

test: add coverage on collaborative editing (#1747)

Himself65926bf49b26b8
diff --git a/packages/workspace/src/affine/__tests__/api.spec.ts b/packages/workspace/src/affine/__tests__/api.spec.ts
index 9b8d38f559..297a37ec2f 100644
--- a/packages/workspace/src/affine/__tests__/api.spec.ts
+++ b/packages/workspace/src/affine/__tests__/api.spec.ts
@@ -7,6 +7,8 @@ import { readFile } from 'node:fs/promises';
import { MessageCode } from '@affine/env/constant';
import { createStatusApis } from '@affine/workspace/affine/api/status';
+import user1 from '@affine-test/fixtures/built-in-user1.json';
+import user2 from '@affine-test/fixtures/built-in-user2.json';
import { assertExists } from '@blocksuite/global/utils';
import { Workspace } from '@blocksuite/store';
import { faker } from '@faker-js/faker';
@@ -87,6 +89,33 @@ async function createWorkspace(
}
describe('api', () => {
+ test('built-in mock user', async () => {
+ const data = await fetch('http://localhost:3000/api/user/token', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ type: 'DebugLoginUser',
+ email: user1.email,
+ password: user1.password,
+ }),
+ }).then(r => r.json());
+ loginResponseSchema.parse(data);
+ const data2 = await fetch('http://localhost:3000/api/user/token', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ type: 'DebugLoginUser',
+ email: user2.email,
+ password: user2.password,
+ }),
+ }).then(r => r.json());
+ loginResponseSchema.parse(data2);
+ });
+
test('failed', async () => {
workspaceApis = createWorkspaceApis('http://localhost:10086/404/');
const listener = vi.fn(
diff --git a/tests/fixtures/built-in-user1.json b/tests/fixtures/built-in-user1.json
new file mode 100644
index 0000000000..94886cbb28
--- /dev/null
+++ b/tests/fixtures/built-in-user1.json
@@ -0,0 +1,5 @@
+{
+ "name": "debug1",
+ "email": "[email protected]",
+ "password": "debug1"
+}
diff --git a/tests/fixtures/built-in-user2.json b/tests/fixtures/built-in-user2.json
new file mode 100644
index 0000000000..8c09b145d8
--- /dev/null
+++ b/tests/fixtures/built-in-user2.json
@@ -0,0 +1,5 @@
+{
+ "name": "debug2",
+ "email": "[email protected]",
+ "password": "debug2"
+}
diff --git a/tests/libs/utils.ts b/tests/libs/utils.ts
index a781f8ca37..59a323816f 100644
--- a/tests/libs/utils.ts
+++ b/tests/libs/utils.ts
@@ -5,6 +5,38 @@ const userA = require('../fixtures/userA.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const userB = require('../fixtures/userB.json');
+// eslint-disable-next-line @typescript-eslint/no-var-requires
+const user1 = require('@affine-test/fixtures/built-in-user1.json');
+// eslint-disable-next-line @typescript-eslint/no-var-requires
+const user2 = require('@affine-test/fixtures/built-in-user2.json');
+
+export async function getBuiltInUser() {
+ return Promise.all([
+ fetch('http://localhost:3000/api/user/token', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ type: 'DebugLoginUser',
+ email: user1.email,
+ password: user1.password,
+ }),
+ }).then(r => r.json()),
+ fetch('http://localhost:3000/api/user/token', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ type: 'DebugLoginUser',
+ email: user2.email,
+ password: user2.password,
+ }),
+ }).then(r => r.json()),
+ ]);
+}
+
export async function createFakeUser() {
try {
const response = await Promise.all([
diff --git a/tests/libs/workspace.ts b/tests/libs/workspace.ts
index 31f504a438..6ab8b1b786 100644
--- a/tests/libs/workspace.ts
+++ b/tests/libs/workspace.ts
@@ -27,7 +27,6 @@ export async function assertCurrentWorkspaceFlavour(
flavour: 'affine' | 'local',
page: Page
) {
- // @ts-expect-error type globalThis.currentWorkspace is not defined in playwright context
const actual = await page.evaluate(() => globalThis.currentWorkspace.flavour);
expect(actual).toBe(flavour);
}
diff --git a/tests/parallels/affine/affine-built-in-workspace.spec.ts b/tests/parallels/affine/affine-built-in-workspace.spec.ts
new file mode 100644
index 0000000000..c1282c268a
--- /dev/null
+++ b/tests/parallels/affine/affine-built-in-workspace.spec.ts
@@ -0,0 +1,40 @@
+import { expect } from '@playwright/test';
+
+import { waitMarkdownImported } from '../../libs/page-logic';
+import { test } from '../../libs/playwright';
+import {
+ clickNewPageButton,
+ clickSideBarCurrentWorkspaceBanner,
+} from '../../libs/sidebar';
+import { getBuiltInUser, loginUser, openHomePage } from '../../libs/utils';
+
+test.describe('affine built in workspace', () => {
+ test('collaborative', async ({ page, context }) => {
+ await openHomePage(page);
+ await waitMarkdownImported(page);
+ const [a, b] = await getBuiltInUser();
+ await loginUser(page, a);
+ await page.reload();
+ await page.waitForTimeout(50);
+ await clickSideBarCurrentWorkspaceBanner(page);
+ await page.getByText('Cloud Workspace').click();
+ const page2 = await context.newPage();
+ await openHomePage(page2);
+ await waitMarkdownImported(page2);
+ await loginUser(page2, b);
+ await page2.reload();
+ await clickSideBarCurrentWorkspaceBanner(page2);
+ await page2.getByText('Joined Workspace').click();
+ await clickNewPageButton(page);
+ const url = page.url();
+ await page2.goto(url);
+ await page.type('.affine-default-page-block-title-container', 'hello', {
+ delay: 50,
+ });
+ await page.waitForTimeout(100);
+ const title = await page
+ .locator('.affine-default-page-block-title-container')
+ .textContent();
+ expect(title.trim()).toBe('hello');
+ });
+});