From 87e9181e119d6761f63bebf8e126194a2db8b299 Mon Sep 17 00:00:00 2001 From: Paul Huliganga Date: Tue, 24 Mar 2026 22:02:10 -0400 Subject: [PATCH] test(import): add malformed JSON-LD endpoint case --- TODO.md | 2 +- src/backend/tests/import.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index ec8171d..e3dadd9 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/src/backend/tests/import.test.ts b/src/backend/tests/import.test.ts index e578491..6e14a76 100644 --- a/src/backend/tests/import.test.ts +++ b/src/backend/tests/import.test.ts @@ -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 = ` + + + + + + + `; + + 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,