From 186ae952f599aa29de519cdfff97112ddc210e7a Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Thu, 3 Jul 2025 11:15:00 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85(frontend)=20test=20successful=20conve?= =?UTF-8?q?rsion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stephan Meijer --- .../y-provider/__tests__/convert.test.ts | 90 +++++++++++++++---- src/frontend/servers/y-provider/package.json | 1 - src/frontend/servers/y-provider/src/utils.ts | 4 +- 3 files changed, 75 insertions(+), 20 deletions(-) diff --git a/src/frontend/servers/y-provider/__tests__/convert.test.ts b/src/frontend/servers/y-provider/__tests__/convert.test.ts index 9f99f68d..6894fd16 100644 --- a/src/frontend/servers/y-provider/__tests__/convert.test.ts +++ b/src/frontend/servers/y-provider/__tests__/convert.test.ts @@ -1,7 +1,7 @@ -import { Hocuspocus } from '@hocuspocus/server'; +import { ServerBlockNoteEditor } from '@blocknote/server-util'; import request from 'supertest'; import { describe, expect, test, vi } from 'vitest'; -import { mock } from 'vitest-mock-extended'; +import * as Y from 'yjs'; vi.mock('../src/env', async (importOriginal) => { return { @@ -20,16 +20,9 @@ import { console.error = vi.fn(); -const mockOpts = { - fallbackMockImplementation: () => { - throw new Error('Unexpected call.'); - }, -}; - describe('Server Tests', () => { test('POST /api/convert with incorrect API key should responds with 403', async () => { - const hocuspocus = mock({}, mockOpts); - const app = initApp(hocuspocus); + const app = initApp(); const response = await request(app) .post('/api/convert') @@ -43,8 +36,7 @@ describe('Server Tests', () => { }); test('POST /api/convert with a Bearer token', async () => { - const hocuspocus = mock({}, mockOpts); - const app = initApp(hocuspocus); + const app = initApp(); const response = await request(app) .post('/api/convert') @@ -59,8 +51,7 @@ describe('Server Tests', () => { }); test('POST /api/convert with missing body param content', async () => { - const hocuspocus = mock({}, mockOpts); - const app = initApp(hocuspocus); + const app = initApp(); const response = await request(app) .post('/api/convert') @@ -74,8 +65,7 @@ describe('Server Tests', () => { }); test('POST /api/convert with body param content being an empty string', async () => { - const hocuspocus = mock({}, mockOpts); - const app = initApp(hocuspocus); + const app = initApp(); const response = await request(app) .post('/api/convert') @@ -90,4 +80,72 @@ describe('Server Tests', () => { error: 'Invalid request: missing content', }); }); + + test('POST /api/convert with correct content', async () => { + const app = initApp(); + + const document = [ + '# Example document', + '', + 'Lorem ipsum dolor sit amet.', + '', + ].join('\n'); + + const response = await request(app) + .post('/api/convert') + .set('Origin', origin) + .set('Authorization', apiKey) + .send({ + content: document, + }); + + expect(response.status).toBe(200); + expect(response.body).toStrictEqual({ + content: expect.any(String), + }); + + const editor = ServerBlockNoteEditor.create(); + const doc = new Y.Doc(); + Y.applyUpdate(doc, Buffer.from(response.body.content, 'base64')); + const blocks = editor.yDocToBlocks(doc, 'document-store'); + + expect(blocks).toStrictEqual([ + { + children: [], + content: [ + { + styles: {}, + text: 'Example document', + type: 'text', + }, + ], + id: expect.any(String), + props: { + backgroundColor: 'default', + isToggleable: false, + level: 1, + textAlignment: 'left', + textColor: 'default', + }, + type: 'heading', + }, + { + children: [], + content: [ + { + styles: {}, + text: 'Lorem ipsum dolor sit amet.', + type: 'text', + }, + ], + id: expect.any(String), + props: { + backgroundColor: 'default', + textAlignment: 'left', + textColor: 'default', + }, + type: 'paragraph', + }, + ]); + }); }); diff --git a/src/frontend/servers/y-provider/package.json b/src/frontend/servers/y-provider/package.json index 92f26aa1..97fde1bd 100644 --- a/src/frontend/servers/y-provider/package.json +++ b/src/frontend/servers/y-provider/package.json @@ -40,7 +40,6 @@ "eslint-config-impress": "*", "nodemon": "3.1.10", "supertest": "7.1.1", - "ts-jest": "29.4.0", "ts-node": "10.9.2", "tsc-alias": "1.8.16", "typescript": "*", diff --git a/src/frontend/servers/y-provider/src/utils.ts b/src/frontend/servers/y-provider/src/utils.ts index 18d7671f..e0166736 100644 --- a/src/frontend/servers/y-provider/src/utils.ts +++ b/src/frontend/servers/y-provider/src/utils.ts @@ -1,9 +1,7 @@ import { COLLABORATION_LOGGING } from './env'; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function logger(...args: any[]) { +export function logger(...args: unknown[]) { if (COLLABORATION_LOGGING === 'true') { - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument console.log(new Date().toISOString(), ' --- ', ...args); } }