♻️(convert) reuse existing convert yprovider endpoint for content API

reuse convert service instead of renaming it in content
This commit is contained in:
Sylvain Zimmer
2025-07-24 14:08:18 +02:00
committed by Manuel Raynaud
parent 8a8a1460e5
commit ede0a77665
14 changed files with 296 additions and 421 deletions

View File

@@ -121,6 +121,31 @@ describe('Server Tests', () => {
});
});
test('POST /api/convert with unsupported Content-Type returns 415', async () => {
const app = initApp();
const response = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', apiKey)
.set('content-type', 'image/png')
.send('randomdata');
expect(response.status).toBe(415);
expect(response.body).toStrictEqual({ error: 'Unsupported Content-Type' });
});
test('POST /api/convert with unsupported Accept returns 406', async () => {
const app = initApp();
const response = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', apiKey)
.set('content-type', 'text/markdown')
.set('accept', 'image/png')
.send('# Header');
expect(response.status).toBe(406);
expect(response.body).toStrictEqual({ error: 'Unsupported format' });
});
test.each([[apiKey], [`Bearer ${apiKey}`]])(
'POST /api/convert with correct content with Authorization: %s',
async (authHeader) => {
@@ -137,6 +162,8 @@ describe('Server Tests', () => {
.post('/api/convert')
.set('Origin', origin)
.set('Authorization', authHeader)
.set('content-type', 'text/markdown')
.set('accept', 'application/vnd.yjs.doc')
.send(document);
expect(response.status).toBe(200);
@@ -150,4 +177,89 @@ describe('Server Tests', () => {
expect(blocks).toStrictEqual(expectedBlocks);
},
);
test('POST /api/convert Yjs to HTML', async () => {
const app = initApp();
const editor = ServerBlockNoteEditor.create();
const markdownContent = '# Test Document\n\nThis is test content.';
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
const response = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', apiKey)
.set('content-type', 'application/vnd.yjs.doc')
.set('accept', 'text/html')
.send(Buffer.from(yjsUpdate));
expect(response.status).toBe(200);
expect(response.header['content-type']).toBe('text/html; charset=utf-8');
expect(typeof response.text).toBe('string');
expect(response.text).toBe(
'<h1>Test Document</h1><p>This is test content.</p>',
);
});
test('POST /api/convert Yjs to Markdown', async () => {
const app = initApp();
const editor = ServerBlockNoteEditor.create();
const markdownContent = '# Test Document\n\nThis is test content.';
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
const response = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', apiKey)
.set('content-type', 'application/vnd.yjs.doc')
.set('accept', 'text/markdown')
.send(Buffer.from(yjsUpdate));
expect(response.status).toBe(200);
expect(response.header['content-type']).toBe(
'text/markdown; charset=utf-8',
);
expect(typeof response.text).toBe('string');
expect(response.text.trim()).toBe(markdownContent);
});
test('POST /api/convert Yjs to JSON', async () => {
const app = initApp();
const editor = ServerBlockNoteEditor.create();
const markdownContent = '# Test Document\n\nThis is test content.';
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
const response = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', apiKey)
.set('content-type', 'application/vnd.yjs.doc')
.set('accept', 'application/json')
.send(Buffer.from(yjsUpdate));
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.length).toBe(2);
expect(response.body[0].type).toBe('heading');
expect(response.body[1].type).toBe('paragraph');
expect(response.body[0].content[0].type).toBe('text');
expect(response.body[0].content[0].text).toBe('Test Document');
expect(response.body[1].content[0].type).toBe('text');
expect(response.body[1].content[0].text).toBe('This is test content.');
});
test('POST /api/convert with invalid Yjs content returns 400', async () => {
const app = initApp();
const response = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', apiKey)
.set('content-type', 'application/vnd.yjs.doc')
.set('accept', 'application/json')
.send(Buffer.from('notvalidyjs'));
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({ error: 'Invalid Yjs content' });
});
});