✅(convert) improve tests with stricter tests and less ipsum
Use real example data to run convert handler tests.
This commit is contained in:
committed by
Manuel Raynaud
parent
ede0a77665
commit
80a62bcbc1
@@ -18,6 +18,9 @@ import {
|
|||||||
COLLABORATION_SERVER_ORIGIN as origin,
|
COLLABORATION_SERVER_ORIGIN as origin,
|
||||||
} from '../src/env';
|
} from '../src/env';
|
||||||
|
|
||||||
|
const expectedMarkdown = '# Example document\n\nLorem ipsum dolor sit amet.';
|
||||||
|
const expectedHTML =
|
||||||
|
'<h1>Example document</h1><p>Lorem ipsum dolor sit amet.</p>';
|
||||||
const expectedBlocks = [
|
const expectedBlocks = [
|
||||||
{
|
{
|
||||||
children: [],
|
children: [],
|
||||||
@@ -151,20 +154,13 @@ describe('Server Tests', () => {
|
|||||||
async (authHeader) => {
|
async (authHeader) => {
|
||||||
const app = initApp();
|
const app = initApp();
|
||||||
|
|
||||||
const document = [
|
|
||||||
'# Example document',
|
|
||||||
'',
|
|
||||||
'Lorem ipsum dolor sit amet.',
|
|
||||||
'',
|
|
||||||
].join('\n');
|
|
||||||
|
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
.post('/api/convert')
|
.post('/api/convert')
|
||||||
.set('Origin', origin)
|
.set('Origin', origin)
|
||||||
.set('Authorization', authHeader)
|
.set('Authorization', authHeader)
|
||||||
.set('content-type', 'text/markdown')
|
.set('content-type', 'text/markdown')
|
||||||
.set('accept', 'application/vnd.yjs.doc')
|
.set('accept', 'application/vnd.yjs.doc')
|
||||||
.send(document);
|
.send(expectedMarkdown);
|
||||||
|
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.body).toBeInstanceOf(Buffer);
|
expect(response.body).toBeInstanceOf(Buffer);
|
||||||
@@ -181,8 +177,7 @@ describe('Server Tests', () => {
|
|||||||
test('POST /api/convert Yjs to HTML', async () => {
|
test('POST /api/convert Yjs to HTML', async () => {
|
||||||
const app = initApp();
|
const app = initApp();
|
||||||
const editor = ServerBlockNoteEditor.create();
|
const editor = ServerBlockNoteEditor.create();
|
||||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
const blocks = await editor.tryParseMarkdownToBlocks(expectedMarkdown);
|
||||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
|
||||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
@@ -195,16 +190,13 @@ describe('Server Tests', () => {
|
|||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.header['content-type']).toBe('text/html; charset=utf-8');
|
expect(response.header['content-type']).toBe('text/html; charset=utf-8');
|
||||||
expect(typeof response.text).toBe('string');
|
expect(typeof response.text).toBe('string');
|
||||||
expect(response.text).toBe(
|
expect(response.text).toBe(expectedHTML);
|
||||||
'<h1>Test Document</h1><p>This is test content.</p>',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('POST /api/convert Yjs to Markdown', async () => {
|
test('POST /api/convert Yjs to Markdown', async () => {
|
||||||
const app = initApp();
|
const app = initApp();
|
||||||
const editor = ServerBlockNoteEditor.create();
|
const editor = ServerBlockNoteEditor.create();
|
||||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
const blocks = await editor.tryParseMarkdownToBlocks(expectedMarkdown);
|
||||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
|
||||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
@@ -219,14 +211,13 @@ describe('Server Tests', () => {
|
|||||||
'text/markdown; charset=utf-8',
|
'text/markdown; charset=utf-8',
|
||||||
);
|
);
|
||||||
expect(typeof response.text).toBe('string');
|
expect(typeof response.text).toBe('string');
|
||||||
expect(response.text.trim()).toBe(markdownContent);
|
expect(response.text.trim()).toBe(expectedMarkdown);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('POST /api/convert Yjs to JSON', async () => {
|
test('POST /api/convert Yjs to JSON', async () => {
|
||||||
const app = initApp();
|
const app = initApp();
|
||||||
const editor = ServerBlockNoteEditor.create();
|
const editor = ServerBlockNoteEditor.create();
|
||||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
const blocks = await editor.tryParseMarkdownToBlocks(expectedMarkdown);
|
||||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
|
||||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
@@ -241,13 +232,24 @@ describe('Server Tests', () => {
|
|||||||
'application/json; charset=utf-8',
|
'application/json; charset=utf-8',
|
||||||
);
|
);
|
||||||
expect(Array.isArray(response.body)).toBe(true);
|
expect(Array.isArray(response.body)).toBe(true);
|
||||||
expect(response.body.length).toBe(2);
|
expect(response.body).toStrictEqual(expectedBlocks);
|
||||||
expect(response.body[0].type).toBe('heading');
|
});
|
||||||
expect(response.body[1].type).toBe('paragraph');
|
|
||||||
expect(response.body[0].content[0].type).toBe('text');
|
test('POST /api/convert Markdown to JSON', async () => {
|
||||||
expect(response.body[0].content[0].text).toBe('Test Document');
|
const app = initApp();
|
||||||
expect(response.body[1].content[0].type).toBe('text');
|
const response = await request(app)
|
||||||
expect(response.body[1].content[0].text).toBe('This is test content.');
|
.post('/api/convert')
|
||||||
|
.set('origin', origin)
|
||||||
|
.set('authorization', apiKey)
|
||||||
|
.set('content-type', 'text/markdown')
|
||||||
|
.set('accept', 'application/json')
|
||||||
|
.send(expectedMarkdown);
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
expect(response.header['content-type']).toBe(
|
||||||
|
'application/json; charset=utf-8',
|
||||||
|
);
|
||||||
|
expect(Array.isArray(response.body)).toBe(true);
|
||||||
|
expect(response.body).toStrictEqual(expectedBlocks);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('POST /api/convert with invalid Yjs content returns 400', async () => {
|
test('POST /api/convert with invalid Yjs content returns 400', async () => {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
"build": "tsc -p tsconfig.build.json && tsc-alias",
|
"build": "tsc -p tsconfig.build.json && tsc-alias",
|
||||||
"dev": "cross-env COLLABORATION_LOGGING=true && nodemon --config nodemon.json",
|
"dev": "cross-env COLLABORATION_LOGGING=true && nodemon --config nodemon.json",
|
||||||
"start": "node ./dist/start-server.js",
|
"start": "node ./dist/start-server.js",
|
||||||
"lint": "eslint . --ext .ts --fix",
|
"lint": "eslint . --ext .ts",
|
||||||
"test": "vitest --run --disable-console-intercept"
|
"test": "vitest --run --disable-console-intercept"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ export const convertHandler = async (
|
|||||||
|
|
||||||
let blocks: PartialBlock[] | null = null;
|
let blocks: PartialBlock[] | null = null;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// First, convert from the input format to blocks
|
// First, convert from the input format to blocks
|
||||||
// application/x-www-form-urlencoded is interpreted as Markdown for backward compatibility
|
// application/x-www-form-urlencoded is interpreted as Markdown for backward compatibility
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user