From 213c1f87fddec5988561b6de741fbbafc77d065f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alves?= Date: Tue, 4 Aug 2026 20:09:36 +0100 Subject: [PATCH] test(writer-pipeline): featured-image regression guard for revista-imprensa (BIT-28134) Adds unit coverage for resolveFeaturedImageForDraft() so the mandatory featured-image ERRSIG cluster (BIT-27615 / BIT-26192 / BIT-22288) cannot silently regress: ordinary drafts still throw when no image resolves, revista-imprensa leads (by sourceKind or URL) resolve to null instead, and non-revista URLs are not over-matched. Also covers upsertItem preserving sourceKind across updates that omit it. Paperclip-Issue: BIT-28134 Paperclip-Run: d45309d9-128d-4266-93db-9afe285c4486 --- docs/WRITER_PIPELINE_ANDEBOL.md | 31 ++++++ writer-pipeline/src/store-extra.test.ts | 9 ++ writer-pipeline/src/wp-publish.test.ts | 127 ++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 writer-pipeline/src/wp-publish.test.ts diff --git a/docs/WRITER_PIPELINE_ANDEBOL.md b/docs/WRITER_PIPELINE_ANDEBOL.md index 816101f..7bb9764 100644 --- a/docs/WRITER_PIPELINE_ANDEBOL.md +++ b/docs/WRITER_PIPELINE_ANDEBOL.md @@ -31,6 +31,37 @@ See [`docs/ANDEBOL_EDITORIAL_BRIEF.md`](ANDEBOL_EDITORIAL_BRIEF.md) for detailed - **WP publish:** if credentials are missing, the cycle completes with a clear error string and **no** draft creation. - **Featured image (BIT-22288 / BIT-22292 / BIT-24388):** drafts require a rights-cleared image. The pipeline uses RSS enclosure / media:content / media:thumbnail / `og:image` / `twitter:image` first. If none are present, it can **list a WebDAV collection** (authenticated Basic auth) and pick the best-matching filename against the headline, then upload to WP with caption/alt. Configure with `WRITER_WEBDAV_*` env vars (see `deploy/systemd/writer-andebol.env.example`). When WebDAV is **not** enabled, the archive is never contacted (only RSS/HTML hints apply). If WebDAV is enabled but PROPFIND/GET fails, draft creation for that item fails with an explicit error (no silent fallback to a random web image) — operational fallback is to disable `WRITER_WEBDAV_ENABLED` or restore connectivity before retrying. **Note:** FPA RSS feed uses `` for featured images (BIT-24388), which is now properly extracted and attached to WordPress posts. +## Regression tests — mandatory featured image (BIT-28134) + +`resolveFeaturedImageForDraft()` in `writer-pipeline/src/wp-publish.ts` has a unit-test regression +guard (`writer-pipeline/src/wp-publish.test.ts`) against the mandatory-featured-image ERRSIG +cluster (BIT-27615 / BIT-26192 / BIT-22288) triggered by revista-imprensa / archive drafts. It +mocks `fetch` (no live network) and asserts, per draft class: + +- **Ordinary drafts** (not a revista-imprensa lead) still **throw** `Mandatory featured image` + when no image source resolves — required drafts must never ship without a featured image + (BIT-22288). +- **Revista-imprensa leads**, synthetic `/revista-imprensa//` pages that are not real + article pages, resolve to **`null`** instead of throwing — whether detected via `sourceKind` + (BIT-26192) or via the URL path when `sourceKind` is missing/stripped by a later store upsert + (BIT-27615). +- A **non-revista URL** with `sourceKind` missing still throws — the URL heuristic must not + over-match plain article URLs. + +`writer-pipeline/src/store-extra.test.ts` also covers `upsertItem` preserving `sourceKind` across +an update that omits it (the other half of the BIT-27615 fix). + +Run via the existing `npm test` script (builds then runs `node --test`): + +```bash +cd writer-pipeline +npm test +``` + +To confirm the guard actually catches the regression, revert the BIT-27615 URL-detection fallback +in `resolveFeaturedImageForDraft` (drop the `revistaPublisherHome != null` clause) and re-run +`npm test` — the `BIT-27615 regression guard` test fails; restore the fix and it passes again. + ## Commands ```bash diff --git a/writer-pipeline/src/store-extra.test.ts b/writer-pipeline/src/store-extra.test.ts index 716de58..d064cf2 100644 --- a/writer-pipeline/src/store-extra.test.ts +++ b/writer-pipeline/src/store-extra.test.ts @@ -126,6 +126,15 @@ test('upsertItem prefers new featured image fields when provided', () => { assert.equal(s2.items[0].featuredImageCredit, 'New credit'); }); +test('upsertItem preserves sourceKind when a later upsert omits it (BIT-27615)', () => { + const store = emptyStore(); + const item = baseItem({ sourceKind: 'revista_imprensa' }); + const { store: s1 } = upsertItem(store, item); + const { store: s2 } = upsertItem(s1, { ...item, sourceKind: undefined, title: 'Refreshed title' }); + assert.equal(s2.items[0].sourceKind, 'revista_imprensa'); + assert.equal(s2.items[0].title, 'Refreshed title'); +}); + // ── shortlist ────────────────────────────────────────────────────────────── test('shortlist returns newest first, limited to n', () => { diff --git a/writer-pipeline/src/wp-publish.test.ts b/writer-pipeline/src/wp-publish.test.ts new file mode 100644 index 0000000..2953d7e --- /dev/null +++ b/writer-pipeline/src/wp-publish.test.ts @@ -0,0 +1,127 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { resolveFeaturedImageForDraft } from './wp-publish.js'; +import type { AggregatedItem } from './types.js'; + +/** + * Regression guard for the mandatory-featured-image ERRSIG cluster (BIT-27615 / BIT-26192 / + * BIT-22288). Two behaviours must both hold: + * - Ordinary drafts (not a revista-imprensa lead) still throw "Mandatory featured image" + * when no image can be resolved — this is the guarantee drafts must not ship without one. + * - Revista-imprensa leads — synthetic /revista-imprensa// pages that are not real + * article pages — must resolve to null instead of throwing, whether detected via + * sourceKind or (BIT-27615) via the URL path when sourceKind is missing/stripped by a + * later store upsert. + */ + +const WEBDAV_KEYS = [ + 'WRITER_WEBDAV_ENABLED', + 'WRITER_WEBDAV_BASE_URL', + 'WRITER_WEBDAV_USER', + 'WRITER_WEBDAV_PASSWORD', + 'WRITER_WEBDAV_DEFAULT_CREDIT', +] as const; + +function snapshotEnv(keys: readonly string[]): Record { + const o: Record = {}; + for (const k of keys) o[k] = process.env[k]; + return o; +} + +function restoreEnv(snap: Record): void { + for (const k of Object.keys(snap)) { + const v = snap[k]; + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } +} + +function baseItem(overrides: Partial = {}): AggregatedItem { + return { + id: 'abc123', + site: 'andebol.net', + sourceFeedId: 'feed1', + url: 'https://example.com/article', + title: 'Test item', + summary: 'Summary.', + publishedAt: new Date().toISOString(), + ingestedAt: new Date().toISOString(), + ...overrides, + }; +} + +/** Stub fetch with an empty page: no og:image, no tags — every HTML probe comes back empty. */ +function stubEmptyPageFetch(): typeof fetch { + return (async () => + new Response('', { + status: 200, + headers: { 'content-type': 'text/html' }, + })) as unknown as typeof fetch; +} + +test('mandatory featured image guard: non-revista draft with no image source throws (BIT-22288)', async () => { + const envSnap = snapshotEnv(WEBDAV_KEYS); + const originalFetch = globalThis.fetch; + for (const k of WEBDAV_KEYS) delete process.env[k]; + globalThis.fetch = stubEmptyPageFetch(); + try { + const item = baseItem({ url: 'https://example.com/normal-article', sourceKind: 'rss' }); + await assert.rejects(() => resolveFeaturedImageForDraft(item), /Mandatory featured image/); + } finally { + globalThis.fetch = originalFetch; + restoreEnv(envSnap); + } +}); + +test('revista_imprensa lead (sourceKind set) without an image resolves to null, does not throw (BIT-26192)', async () => { + const envSnap = snapshotEnv(WEBDAV_KEYS); + const originalFetch = globalThis.fetch; + for (const k of WEBDAV_KEYS) delete process.env[k]; + globalThis.fetch = stubEmptyPageFetch(); + try { + const item = baseItem({ + url: 'https://www.record.pt/revista-imprensa/18-06-2026/3', + sourceKind: 'revista_imprensa', + }); + const result = await resolveFeaturedImageForDraft(item); + assert.equal(result, null); + } finally { + globalThis.fetch = originalFetch; + restoreEnv(envSnap); + } +}); + +test('revista lead detected by URL when sourceKind is missing resolves to null, does not throw (BIT-27615 regression guard)', async () => { + const envSnap = snapshotEnv(WEBDAV_KEYS); + const originalFetch = globalThis.fetch; + for (const k of WEBDAV_KEYS) delete process.env[k]; + globalThis.fetch = stubEmptyPageFetch(); + try { + const item = baseItem({ + url: 'https://www.abola.pt/revista-imprensa/18-06-2026/5', + sourceKind: undefined, + }); + const result = await resolveFeaturedImageForDraft(item); + assert.equal(result, null); + } finally { + globalThis.fetch = originalFetch; + restoreEnv(envSnap); + } +}); + +test('non-revista URL with sourceKind missing still throws (URL heuristic does not over-match)', async () => { + const envSnap = snapshotEnv(WEBDAV_KEYS); + const originalFetch = globalThis.fetch; + for (const k of WEBDAV_KEYS) delete process.env[k]; + globalThis.fetch = stubEmptyPageFetch(); + try { + const item = baseItem({ + url: 'https://www.record.pt/modalidades/andebol/detalhe/some-article', + sourceKind: undefined, + }); + await assert.rejects(() => resolveFeaturedImageForDraft(item), /Mandatory featured image/); + } finally { + globalThis.fetch = originalFetch; + restoreEnv(envSnap); + } +});