feat(features): add bind-dev-seed binders for auth/marketing-pages/navigation/media
Mirrors the canonical blog pattern landed earlier on this branch. Per feature: - src/__seeds__/dev.ts — lazy buildDev<Entities>() function using the feature's existing factory for sensible defaults - src/di/bind-dev-seed.ts — bindDevSeed<Feature>() async function that rebinds the repo symbol(s) to a populated MockXRepository via .toConstantValue - src/di/bind-dev-seed.test.ts — 3+ tests per feature (populates, reachable by id/slug, idempotent) - package.json — adds ./di/bind-dev-seed subpath export Tests + use cases continue to construct mocks directly; the seed never runs from a *.test.ts path. App boot wiring (USE_DEV_SEED env branch) follows in a separate commit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,8 @@
|
||||
"./ui": "./src/ui/index.ts",
|
||||
"./cms": "./src/integrations/cms/index.ts",
|
||||
"./api": "./src/integrations/api/router.ts",
|
||||
"./di/bind-production": "./src/di/bind-production.ts"
|
||||
"./di/bind-production": "./src/di/bind-production.ts",
|
||||
"./di/bind-dev-seed": "./src/di/bind-dev-seed.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --noEmit",
|
||||
|
||||
55
packages/marketing-pages/src/__seeds__/dev.ts
Normal file
55
packages/marketing-pages/src/__seeds__/dev.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { pageFactory } from "../__factories__/page.factory.js";
|
||||
import { siteSettingsFactory } from "../__factories__/site-settings.factory.js";
|
||||
import type { Page } from "../entities/models/page.js";
|
||||
import type { SiteSettings } from "../entities/models/site-settings.js";
|
||||
|
||||
/**
|
||||
* Realistic marketing-pages seed for dev mode + storybook stories.
|
||||
*
|
||||
* Built from `pageFactory` / `siteSettingsFactory` so factory defaults take
|
||||
* care of boring fields and we only override what makes the data look like a
|
||||
* real site.
|
||||
*
|
||||
* Lazily produced so importing this module is side-effect-free — the
|
||||
* factory's sequence counter only advances when a binder calls
|
||||
* `buildDevPages()` / `buildDevSiteSettings()`.
|
||||
*/
|
||||
export function buildDevPages(): Page[] {
|
||||
const SEED_DATE = new Date("2026-01-01T00:00:00.000Z");
|
||||
return [
|
||||
pageFactory.build({
|
||||
id: "home",
|
||||
slug: "home",
|
||||
title: "Home",
|
||||
hero: { heading: "Welcome to our site" },
|
||||
status: "published",
|
||||
publishedAt: SEED_DATE,
|
||||
seo: { title: "Home | My App" },
|
||||
}),
|
||||
pageFactory.build({
|
||||
id: "about",
|
||||
slug: "about",
|
||||
title: "About Us",
|
||||
hero: { heading: "Our story" },
|
||||
status: "published",
|
||||
publishedAt: SEED_DATE,
|
||||
seo: { title: "About | My App" },
|
||||
}),
|
||||
pageFactory.build({
|
||||
id: "pricing",
|
||||
slug: "pricing",
|
||||
title: "Pricing",
|
||||
hero: { heading: "Simple, transparent pricing" },
|
||||
status: "published",
|
||||
publishedAt: SEED_DATE,
|
||||
seo: { title: "Pricing | My App" },
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
export function buildDevSiteSettings(): SiteSettings {
|
||||
return siteSettingsFactory.build({
|
||||
siteName: "My App",
|
||||
siteDescription: "A vertical-feature monorepo template",
|
||||
});
|
||||
}
|
||||
98
packages/marketing-pages/src/di/bind-dev-seed.test.ts
Normal file
98
packages/marketing-pages/src/di/bind-dev-seed.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import "reflect-metadata";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { bindDevSeedMarketingPages } from "@/di/bind-dev-seed";
|
||||
import { marketingPagesContainer } from "@/di/container";
|
||||
import { MARKETING_PAGES_SYMBOLS } from "@/di/symbols";
|
||||
import { MockPagesRepository } from "@/infrastructure/repositories/pages.repository.mock";
|
||||
import { MockSiteSettingsRepository } from "@/infrastructure/repositories/site-settings.repository.mock";
|
||||
import type { IPagesRepository } from "@/application/repositories/pages.repository.interface";
|
||||
import type { ISiteSettingsRepository } from "@/application/repositories/site-settings.repository.interface";
|
||||
|
||||
describe("bindDevSeedMarketingPages", () => {
|
||||
// Each test starts from default mock bindings and tears down afterwards so
|
||||
// the global marketingPagesContainer state stays clean for siblings.
|
||||
beforeEach(() => {
|
||||
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IPagesRepository)) {
|
||||
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IPagesRepository);
|
||||
}
|
||||
marketingPagesContainer
|
||||
.bind<IPagesRepository>(MARKETING_PAGES_SYMBOLS.IPagesRepository)
|
||||
.to(MockPagesRepository);
|
||||
|
||||
if (
|
||||
marketingPagesContainer.isBound(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
)
|
||||
) {
|
||||
marketingPagesContainer.unbind(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
);
|
||||
}
|
||||
marketingPagesContainer
|
||||
.bind<ISiteSettingsRepository>(MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository)
|
||||
.to(MockSiteSettingsRepository);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IPagesRepository)) {
|
||||
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IPagesRepository);
|
||||
}
|
||||
marketingPagesContainer
|
||||
.bind<IPagesRepository>(MARKETING_PAGES_SYMBOLS.IPagesRepository)
|
||||
.to(MockPagesRepository);
|
||||
|
||||
if (
|
||||
marketingPagesContainer.isBound(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
)
|
||||
) {
|
||||
marketingPagesContainer.unbind(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
);
|
||||
}
|
||||
marketingPagesContainer
|
||||
.bind<ISiteSettingsRepository>(MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository)
|
||||
.to(MockSiteSettingsRepository);
|
||||
});
|
||||
|
||||
it("populates the pages repository with the dev pages", async () => {
|
||||
await bindDevSeedMarketingPages();
|
||||
|
||||
const repo = marketingPagesContainer.get<IPagesRepository>(
|
||||
MARKETING_PAGES_SYMBOLS.IPagesRepository,
|
||||
);
|
||||
const pages = await repo.getPages();
|
||||
|
||||
expect(pages.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("seeds site settings with a non-default site name", async () => {
|
||||
await bindDevSeedMarketingPages();
|
||||
|
||||
const settingsRepo = marketingPagesContainer.get<ISiteSettingsRepository>(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
);
|
||||
const settings = await settingsRepo.getSiteSettings();
|
||||
|
||||
expect(settings.siteName).toBe("My App");
|
||||
expect(settings.siteDescription).toBeDefined();
|
||||
});
|
||||
|
||||
it("is idempotent — calling twice rebuilds fresh populated repos", async () => {
|
||||
await bindDevSeedMarketingPages();
|
||||
const before = marketingPagesContainer.get<IPagesRepository>(
|
||||
MARKETING_PAGES_SYMBOLS.IPagesRepository,
|
||||
);
|
||||
const beforeCount = (await before.getPages()).length;
|
||||
|
||||
await bindDevSeedMarketingPages();
|
||||
const after = marketingPagesContainer.get<IPagesRepository>(
|
||||
MARKETING_PAGES_SYMBOLS.IPagesRepository,
|
||||
);
|
||||
const afterCount = (await after.getPages()).length;
|
||||
|
||||
expect(afterCount).toBe(beforeCount);
|
||||
// It's a fresh instance — not the previous one.
|
||||
expect(after).not.toBe(before);
|
||||
});
|
||||
});
|
||||
46
packages/marketing-pages/src/di/bind-dev-seed.ts
Normal file
46
packages/marketing-pages/src/di/bind-dev-seed.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { marketingPagesContainer } from "./container.js";
|
||||
import { MARKETING_PAGES_SYMBOLS } from "./symbols.js";
|
||||
import { MockPagesRepository } from "../infrastructure/repositories/pages.repository.mock.js";
|
||||
import { buildDevPages, buildDevSiteSettings } from "../__seeds__/dev.js";
|
||||
import type { IPagesRepository } from "../application/repositories/pages.repository.interface.js";
|
||||
import type { ISiteSettingsRepository } from "../application/repositories/site-settings.repository.interface.js";
|
||||
|
||||
/**
|
||||
* Replace the default empty mocks with populated ones for dev mode + storybook.
|
||||
*
|
||||
* Call this from app boot when `USE_DEV_SEED=true`, mutually exclusive with
|
||||
* `bindProductionMarketingPages(config)`. Tests must NOT call this — they
|
||||
* construct mocks directly and seed via factories per-test.
|
||||
*
|
||||
* Idempotent: safe to call multiple times; each call rebuilds fresh populated
|
||||
* repos and rebinds both symbols.
|
||||
*/
|
||||
export async function bindDevSeedMarketingPages(): Promise<void> {
|
||||
// Pages repository — MockPagesRepository accepts an initial array.
|
||||
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IPagesRepository)) {
|
||||
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IPagesRepository);
|
||||
}
|
||||
|
||||
marketingPagesContainer
|
||||
.bind<IPagesRepository>(MARKETING_PAGES_SYMBOLS.IPagesRepository)
|
||||
.toConstantValue(new MockPagesRepository(buildDevPages()));
|
||||
|
||||
// Site settings repository — MockSiteSettingsRepository has no mutable
|
||||
// storage so we bind a constant-value implementation directly.
|
||||
if (
|
||||
marketingPagesContainer.isBound(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
)
|
||||
) {
|
||||
marketingPagesContainer.unbind(
|
||||
MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository,
|
||||
);
|
||||
}
|
||||
|
||||
const settings = buildDevSiteSettings();
|
||||
marketingPagesContainer
|
||||
.bind<ISiteSettingsRepository>(MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository)
|
||||
.toConstantValue({
|
||||
getSiteSettings: async () => settings,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user