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 <[email protected]>
This commit is contained in:
1 parent
e324571122
commit
23dd0a0c96
8 files changed
+359
-149
No files matched your search
@@ -0,0 +1,76 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { createMockEnv } from "../test/setup";
|
||||
import { createFeedRecord, editFeed } from "./feed-service";
|
||||
import type { Env } from "../types";
|
||||
|
||||
const mkEnv = (overrides: Partial<Env> = {}) =>
|
||||
({ ...createMockEnv(), ...overrides }) as unknown as Env;
|
||||
|
||||
const baseInput = {
|
||||
title: "N",
|
||||
language: "en",
|
||||
allowedSenders: [],
|
||||
blockedSenders: [],
|
||||
};
|
||||
|
||||
const TWO_HOURS = 2 * 3_600_000;
|
||||
|
||||
// The lifetime policy (parse env, apply the server-side FEED_TTL_HOURS override)
|
||||
// lives here in the application layer; the domain only receives a resolved
|
||||
// ttlHours. These tests pin that policy at the public service boundary.
|
||||
describe("createFeedRecord — TTL policy", () => {
|
||||
it("never expires when neither server nor client lifetime is set", async () => {
|
||||
const { config } = await createFeedRecord(mkEnv(), { ...baseInput });
|
||||
expect(config.expires_at).toBeUndefined();
|
||||
});
|
||||
|
||||
it("uses the client lifetimeHours when there is no server override", async () => {
|
||||
const before = Date.now();
|
||||
const { config } = await createFeedRecord(mkEnv(), {
|
||||
...baseInput,
|
||||
lifetimeHours: 2,
|
||||
});
|
||||
expect(config.expires_at!).toBeGreaterThanOrEqual(before + TWO_HOURS);
|
||||
});
|
||||
|
||||
it("lets a server FEED_TTL_HOURS override a larger client lifetime", async () => {
|
||||
const before = Date.now();
|
||||
const { config } = await createFeedRecord(mkEnv({ FEED_TTL_HOURS: "1" }), {
|
||||
...baseInput,
|
||||
lifetimeHours: 9999,
|
||||
});
|
||||
// 1h (server) wins over 9999h (client).
|
||||
expect(config.expires_at!).toBeLessThan(before + TWO_HOURS);
|
||||
});
|
||||
});
|
||||
|
||||
describe("editFeed — TTL policy", () => {
|
||||
it("recomputes expiry from the server override on edit", async () => {
|
||||
const env = mkEnv({ FEED_TTL_HOURS: "1" });
|
||||
const { feedId } = await createFeedRecord(env, { ...baseInput });
|
||||
|
||||
const before = Date.now();
|
||||
const result = await editFeed(env, feedId, { title: "renamed" });
|
||||
|
||||
expect(result.status).toBe("ok");
|
||||
if (result.status === "ok") {
|
||||
expect(result.config.title).toBe("renamed");
|
||||
expect(result.config.expires_at!).toBeLessThan(before + TWO_HOURS);
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves expiry when neither server TTL nor client lifetime is given", async () => {
|
||||
const env = mkEnv();
|
||||
const { feedId, config } = await createFeedRecord(env, {
|
||||
...baseInput,
|
||||
lifetimeHours: 5,
|
||||
});
|
||||
|
||||
const result = await editFeed(env, feedId, { title: "x" });
|
||||
|
||||
expect(result.status).toBe("ok");
|
||||
if (result.status === "ok") {
|
||||
expect(result.config.expires_at).toBe(config.expires_at);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -18,6 +18,22 @@ import {
|
||||
|
||||
export type { CreateFeedInput, UpdateFeedInput };
|
||||
|
||||
/**
|
||||
* Resolve the effective feed lifetime (hours) from a client request and the
|
||||
* server-side `FEED_TTL_HOURS` override. Parsing the env string and applying the
|
||||
* override is application/config policy — the domain only receives the resolved
|
||||
* number. Returns undefined when the feed should never expire.
|
||||
*/
|
||||
function resolveTtlHours(
|
||||
env: Env,
|
||||
requestedHours?: number,
|
||||
): number | undefined {
|
||||
const hours = env.FEED_TTL_HOURS
|
||||
? parseInt(env.FEED_TTL_HOURS, 10)
|
||||
: (requestedHours ?? NaN);
|
||||
return Number.isFinite(hours) && hours > 0 ? hours : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a feed: write its config + empty metadata, register it in the global
|
||||
* list, and bump the `feeds_created` counter. Returns the new feed id + config.
|
||||
@@ -27,7 +43,9 @@ export async function createFeedRecord(
|
||||
input: CreateFeedInput,
|
||||
): Promise<{ feedId: string; config: FeedConfig }> {
|
||||
const repo = FeedRepository.from(env);
|
||||
const feed = Feed.create(FeedId.generate(), input, env);
|
||||
const feed = Feed.create(FeedId.generate(), input, {
|
||||
ttlHours: resolveTtlHours(env, input.lifetimeHours),
|
||||
});
|
||||
|
||||
await repo.save(feed);
|
||||
await repo.addToList(
|
||||
@@ -88,7 +106,14 @@ export async function editFeed(
|
||||
const feed = await repo.load(FeedId.fromTrusted(feedId));
|
||||
if (!feed) return { status: "not_found" };
|
||||
|
||||
if (feed.edit(input, env).status === "expired") {
|
||||
const recomputeExpiry =
|
||||
Boolean(env.FEED_TTL_HOURS) || input.lifetimeHours !== undefined;
|
||||
if (
|
||||
feed.edit(input, {
|
||||
recomputeExpiry,
|
||||
ttlHours: resolveTtlHours(env, input.lifetimeHours),
|
||||
}).status === "expired"
|
||||
) {
|
||||
return { status: "expired" };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user