From 303084810ce9a25e5118fe8240f98d6030078556 Mon Sep 17 00:00:00 2001 From: Kkkakania <200867803+Kkkakania@users.noreply.github.com> Date: Wed, 17 Jun 2026 20:08:35 +0800 Subject: [PATCH] test: cover insertLink fallback branches --- .../api/test/insert-link-fallbacks.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 recto/apps/workers/api/test/insert-link-fallbacks.test.ts diff --git a/recto/apps/workers/api/test/insert-link-fallbacks.test.ts b/recto/apps/workers/api/test/insert-link-fallbacks.test.ts new file mode 100644 index 0000000..2284f59 --- /dev/null +++ b/recto/apps/workers/api/test/insert-link-fallbacks.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest'; +import { insertLink } from '../src/integrations/wordpress'; + +describe('WordPress insertLink - fallback branches', () => { + it('leaves empty content untouched and reports phraseNotFound', () => { + const result = insertLink('', 'missing phrase', 'https://x/missing'); + + expect(result).toEqual({ + content: '', + alreadyLinked: false, + phraseNotFound: true, + }); + }); + + it('wraps a free phrase even when the content has no paragraph break', () => { + const html = 'Intro sentence with target phrase and no block tags.'; + const result = insertLink(html, 'target phrase', 'https://x/target'); + + expect(result.alreadyLinked).toBe(false); + expect(result.phraseNotFound).toBeFalsy(); + expect(result.via).toBe('wrap'); + expect(result.content).toBe( + 'Intro sentence with target phrase and no block tags.' + ); + }); + + it('does not require a pre-existing recto marker to wrap a matching phrase', () => { + const html = '

A clean paragraph mentions internal link coverage.

'; + const result = insertLink(html, 'internal link coverage', 'https://x/coverage'); + + expect(html).not.toContain('data-recto-link="1"'); + expect(result.content).toBe( + '

A clean paragraph mentions internal link coverage.

' + ); + }); + + it('keeps an unrelated recto marker while adding the requested href', () => { + const html = + '

Existing link plus fresh target phrase.

'; + const result = insertLink(html, 'fresh target phrase', 'https://x/fresh'); + + expect(result.alreadyLinked).toBe(false); + expect(result.phraseNotFound).toBeFalsy(); + expect(result.content).toContain('href="https://x/existing"'); + expect(result.content).toContain( + 'fresh target phrase' + ); + }); +});