feat(blog): add bind-dev-seed binder + dev seed

Sets the canonical pattern for all 5 features. Three new files:

- src/__seeds__/dev.ts — buildDevArticles() function returning 3
  realistic articles (welcome, vertical-feature-architecture,
  wip-post) built from articleFactory with id/slug/title/status
  overrides only.
- src/di/bind-dev-seed.ts — bindDevSeedBlog() async function that
  unbinds IArticlesRepository, constructs MockArticlesRepository,
  seeds it via buildDevArticles(), and rebinds via .toConstantValue.
- src/di/bind-dev-seed.test.ts — 3 tests: populated repo, welcome
  article reachable by slug, idempotent (callable twice).

package.json adds the ./di/bind-dev-seed subpath export, parallel to
./di/bind-production.

Tests + use cases continue to construct MockArticlesRepository
directly — they never go through bindDevSeedBlog. The seed only attaches
when called explicitly at app boot.
This commit is contained in:
2026-05-06 19:00:31 +02:00
parent 449a4aedf5
commit e6560bc9cb
4 changed files with 138 additions and 1 deletions

View File

@@ -0,0 +1,71 @@
import "reflect-metadata";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { bindDevSeedBlog } from "@/di/bind-dev-seed";
import { blogContainer } from "@/di/container";
import { BLOG_SYMBOLS } from "@/di/symbols";
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
import type { IArticlesRepository } from "@/application/repositories/articles.repository.interface";
describe("bindDevSeedBlog", () => {
// Each test starts from the default empty-mock binding and tears down
// afterwards so the global blogContainer state stays clean for siblings.
beforeEach(() => {
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
}
blogContainer
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
.to(MockArticlesRepository);
});
afterEach(() => {
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
}
blogContainer
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
.to(MockArticlesRepository);
});
it("populates the repository with the dev articles", async () => {
await bindDevSeedBlog();
const repo = blogContainer.get<IArticlesRepository>(
BLOG_SYMBOLS.IArticlesRepository,
);
const all = await repo.getArticles();
expect(all.length).toBeGreaterThan(0);
});
it("seeds the welcome article reachable by slug", async () => {
await bindDevSeedBlog();
const repo = blogContainer.get<IArticlesRepository>(
BLOG_SYMBOLS.IArticlesRepository,
);
const welcome = await repo.getArticleBySlug("welcome");
expect(welcome).toBeDefined();
expect(welcome?.title).toBe("Welcome to the blog");
expect(welcome?.status).toBe("published");
});
it("is idempotent — calling twice rebuilds a fresh populated repo", async () => {
await bindDevSeedBlog();
const before = blogContainer.get<IArticlesRepository>(
BLOG_SYMBOLS.IArticlesRepository,
);
const beforeCount = (await before.getArticles()).length;
await bindDevSeedBlog();
const after = blogContainer.get<IArticlesRepository>(
BLOG_SYMBOLS.IArticlesRepository,
);
const afterCount = (await after.getArticles()).length;
expect(afterCount).toBe(beforeCount);
// It's a fresh instance — not the previous one.
expect(after).not.toBe(before);
});
});