refactor: introduce domain events for feed side effects (Track E — point 5)

Light "collect + dispatch" variant: the Feed aggregate records FeedEvents
(FeedCreated, EmailIngested) on the mutations that have consequences, exposed via
pullEvents(). A new application dispatcher (feed-events.applyFeedEvents) maps
those events to their side effects — counters (awaited) plus WebSub pings and
favicon fetches handed to a BackgroundScheduler. This removes the inline,
scattered side effects from the ingest hot path (email-processor) and from
createFeedRecord; the aggregate is now the source of truth for "what happened".

Side effects with no aggregate mutation (rejected email, feed deletion bypassing
the aggregate, bulk admin ops, the cron, unsubscribes-sent) stay imperative by
design — there is no aggregate event for them to ride on.

BackgroundScheduler type moved to infrastructure/worker.ts (shared). CLAUDE.md
updated. 355 tests pass (+4 event tests); tsc --noEmit clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Julien Herr
2026-05-24 13:12:42 +02:00
parent 46af982c40
commit b3d42f6c50
9 changed files with 146 additions and 27 deletions
+9
View File
@@ -1,6 +1,7 @@
import { describe, it, expect } from "vitest";
import { createMockEnv } from "../test/setup";
import { createFeedRecord, editFeed } from "./feed-service";
import { getCounters } from "./stats";
import type { Env } from "../types";
const mkEnv = (overrides: Partial<Env> = {}) =>
@@ -42,6 +43,14 @@ describe("createFeedRecord — TTL policy", () => {
// 1h (server) wins over 9999h (client).
expect(config.expires_at!).toBeLessThan(before + TWO_HOURS);
});
it("bumps the feeds_created counter via the FeedCreated domain event", async () => {
const env = mkEnv();
await createFeedRecord(env, { ...baseInput });
const counters = await getCounters(env.EMAIL_STORAGE);
expect(counters.feeds_created).toBe(1);
expect(counters.last_feed_created_at).toBeDefined();
});
});
describe("editFeed — TTL policy", () => {