managarten/apps/api/src/modules/articles/import-worker.test.ts
Till JS e8774fc233 test(articles): worker rollup + field-meta + consent-wall + recovery UI (#6,#14)
#6 — Worker test coverage on the deterministic helpers
   Three new bun-test files in apps/api/src/modules/articles/:
     - field-meta.test.ts (6 tests): pins down the legacy-vs-F3 fix
       so it can never regress silently — including the regression
       check from the live-test-found bug (string vs object compare
       across both shapes evaluates correctly).
     - consent-wall.test.ts (8 tests): the heuristic we extracted
       in #4. German + English vocab, wordcount threshold + the
       boundary case, case-insensitivity.
     - import-worker.test.ts (5 tests): countByState rollup. Pins
       down the consent-wall-counts-as-saved semantics so the
       progress bar doesn't off-by-one and allTerminal stays correct.
   Total 19 bun tests, all green.
   countByState + StateCounts exported (test-only access).

#14 — Consent-wall recovery UI in JobDetailView
   Bulk-import items that hit a cookie-wand land as state='consent-wall'
   with the teaser saved. Before this commit there was no UX path to
   "rescue" them other than navigating to the article and re-saving
   manually. Now:
     - Job-level hint banner appears when warningCount > 0,
       explaining the cookie-wand semantics + linking to
       /articles/settings (where the v2 bookmarklet lives).
     - Per-item action group on consent-wall rows: "Teaser ansehen"
       (open existing article) + "Erneut speichern" (deep-link to
       /articles/add?source=bookmarklet&url=… so the bookmarklet's
       postMessage handshake has the URL pre-populated).

Plan: docs/plans/articles-bulk-import.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 01:30:04 +02:00

80 lines
2.2 KiB
TypeScript

import { describe, it, expect } from 'bun:test';
import { countByState } from './import-worker';
import type { ImportItemRow } from './import-projection';
function item(state: ImportItemRow['state'], idx = 0): ImportItemRow {
return {
id: `i-${idx}`,
userId: 'u-1',
spaceId: 'sp-1',
jobId: 'j-1',
idx,
url: `https://example.com/${idx}`,
state,
articleId: null,
warning: null,
error: null,
attempts: 0,
lastAttemptAt: null,
};
}
describe('countByState — worker job-counter rollup', () => {
it('returns zeros for empty input + allTerminal=false', () => {
const c = countByState([]);
expect(c).toEqual({
saved: 0,
duplicate: 0,
error: 0,
consentWall: 0,
cancelled: 0,
allTerminal: false,
});
});
it('counts each terminal state independently', () => {
const c = countByState([
item('saved', 0),
item('saved', 1),
item('duplicate', 2),
item('error', 3),
item('cancelled', 4),
]);
expect(c.saved).toBe(2);
expect(c.duplicate).toBe(1);
expect(c.error).toBe(1);
expect(c.cancelled).toBe(1);
expect(c.allTerminal).toBe(true);
});
it('treats consent-wall as semantically saved (so progress UI advances)', () => {
// One real-saved + two consent-wall = three "saved" from the
// user's perspective, but the warning counter tracks the wall hits.
const c = countByState([item('saved', 0), item('consent-wall', 1), item('consent-wall', 2)]);
expect(c.saved).toBe(3);
expect(c.consentWall).toBe(2);
expect(c.allTerminal).toBe(true);
});
it('does not flag allTerminal when any item is non-terminal', () => {
const states: ImportItemRow['state'][] = ['pending', 'extracting', 'extracted'];
for (const nonTerminal of states) {
const c = countByState([item('saved', 0), item(nonTerminal, 1)]);
expect(c.allTerminal).toBe(false);
}
});
it('preserves the saved + consent-wall sum when both are present', () => {
// Regression check: saved must include consent-wall items so the
// finished-counter UI doesn't off-by-one.
const c = countByState([
item('saved', 0),
item('saved', 1),
item('consent-wall', 2),
item('error', 3),
]);
expect(c.saved).toBe(3); // 2 saved + 1 consent-wall
expect(c.consentWall).toBe(1);
expect(c.error).toBe(1);
});
});