test(import): add malformed JSON-LD endpoint case

This commit is contained in:
Paul Huliganga 2026-03-24 22:02:10 -04:00
parent 37b17f7284
commit 87e9181e11
2 changed files with 32 additions and 1 deletions

View File

@ -28,7 +28,7 @@ MVP is functionally complete (core app + docs + tests).
- [x] Add backend import endpoint: `POST /api/import/url`
- [x] Implement Schema.org Recipe JSON-LD parser service
- [x] Normalize parsed recipe into internal Recipe draft format
- [ ] Add import endpoint tests (valid recipe page, non-recipe page, malformed JSON-LD)
- [x] Add import endpoint tests (valid recipe page, non-recipe page, malformed JSON-LD)
### Phase 2: Import UI
- [ ] Add “Import from URL” UI page/form in frontend

View File

@ -116,6 +116,37 @@ describe('Import API', () => {
expect(response.body.data.draft_recipe).toBeNull();
});
it('should ignore malformed JSON-LD and return null draft when no valid recipe blocks exist', async () => {
const html = `
<html>
<head>
<script type="application/ld+json">{"@type":"Recipe","name":"Broken JSON"</script>
<script type="application/ld+json">{"@type":"Thing","name":"Still not recipe"}</script>
</head>
</html>
`;
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
status: 200,
headers: new Headers({ 'content-type': 'text/html; charset=utf-8' }),
text: async () => html,
} as Response);
const response = await request(app)
.post('/api/import/url')
.send({ url: 'https://example.com/malformed-jsonld' })
.expect(200);
expect(response.body.success).toBe(true);
expect(response.body.data.json_ld_blocks).toEqual([
'{"@type":"Recipe","name":"Broken JSON"',
'{"@type":"Thing","name":"Still not recipe"}'
]);
expect(response.body.data.draft_recipe).toBeNull();
});
it('should return an error for non-HTML responses', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,