test(writer-pipeline): featured-image regression guard for revista-imprensa (BIT-28134) #37
No files matched your search
@@ -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 `<media:content>` 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/<date>/<n>` 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
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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/<date>/<n> 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<string, string | undefined> {
|
||||
const o: Record<string, string | undefined> = {};
|
||||
for (const k of keys) o[k] = process.env[k];
|
||||
return o;
|
||||
}
|
||||
|
||||
function restoreEnv(snap: Record<string, string | undefined>): 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> = {}): 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 <img> tags — every HTML probe comes back empty. */
|
||||
function stubEmptyPageFetch(): typeof fetch {
|
||||
return (async () =>
|
||||
new Response('<html><head></head><body></body></html>', {
|
||||
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);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user