mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-21 06:13:48 +00:00
refactor(domain): purify the Feed aggregate (Track D — points 1, 4, 6b)
Remove the infrastructure Env leak and ambient time from the domain core, and model the sender policy as a value object. - Point 1: Feed.create/edit no longer receive Env. The application layer resolves the effective lifetime (parsing FEED_TTL_HOURS and applying the server override) via feed-service.resolveTtlHours and hands the domain a plain ttlHours. resolveExpiresAt(ttlHours, now) is now pure. - Point 4: introduce a Clock port (systemClock default), injected at create/reconstitute. The aggregate uses clock.now() instead of Date.now(). The isExpired edge helper keeps its Date.now() default for routes. - Point 6b: extract SenderPolicy value object built once from the lists (decide(senders)) instead of re-parsing per sender; applySenderPolicy is now a thin wrapper over it. Coverage moved with the logic: the FEED_TTL_HOURS override is now pinned by feed-service.test.ts; aggregate tests use an injected fixed clock. 351 tests pass; tsc --noEmit clean. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+11
-20
@@ -5,35 +5,26 @@ import {
|
||||
applySenderPolicy,
|
||||
trimToByteBudget,
|
||||
} from "./feed";
|
||||
import type { Env, FeedMetadata, EmailMetadata } from "../types";
|
||||
|
||||
const env = (overrides: Partial<Env> = {}): Env =>
|
||||
({ FEED_TTL_HOURS: undefined, ...overrides }) as Env;
|
||||
import type { FeedMetadata, EmailMetadata } from "../types";
|
||||
|
||||
describe("resolveExpiresAt", () => {
|
||||
it("returns undefined when no lifetime applies", () => {
|
||||
expect(resolveExpiresAt(env())).toBeUndefined();
|
||||
expect(resolveExpiresAt(env(), 0)).toBeUndefined();
|
||||
expect(resolveExpiresAt(env(), -5)).toBeUndefined();
|
||||
const NOW = 1_000_000;
|
||||
|
||||
it("returns undefined when no positive lifetime applies", () => {
|
||||
expect(resolveExpiresAt(undefined, NOW)).toBeUndefined();
|
||||
expect(resolveExpiresAt(0, NOW)).toBeUndefined();
|
||||
expect(resolveExpiresAt(-5, NOW)).toBeUndefined();
|
||||
expect(resolveExpiresAt(NaN, NOW)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("computes expiry from a supplied lifetime", () => {
|
||||
const before = Date.now();
|
||||
const result = resolveExpiresAt(env(), 2)!;
|
||||
expect(result).toBeGreaterThanOrEqual(before + 2 * 3_600_000);
|
||||
});
|
||||
|
||||
it("lets a server-side FEED_TTL_HOURS override the client value", () => {
|
||||
const before = Date.now();
|
||||
const result = resolveExpiresAt(env({ FEED_TTL_HOURS: "1" }), 999)!;
|
||||
// Uses 1h (server), not 999h (client).
|
||||
expect(result).toBeLessThan(before + 2 * 3_600_000);
|
||||
it("computes expiry from a supplied lifetime relative to now", () => {
|
||||
expect(resolveExpiresAt(2, NOW)).toBe(NOW + 2 * 3_600_000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isExpired", () => {
|
||||
it("is false when no expiry is set", () => {
|
||||
expect(isExpired({ expires_at: undefined })).toBe(false);
|
||||
expect(isExpired({ expires_at: undefined }, 1000)).toBe(false);
|
||||
});
|
||||
|
||||
it("is true at or past the expiry instant", () => {
|
||||
|
||||
Reference in New Issue
Block a user