Initial commit
This commit is contained in:
63
packages/blog/tests/articles.feature.test.ts
Normal file
63
packages/blog/tests/articles.feature.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
// Feature-level test: exercises the full slice
|
||||
// use case factory chain → mock repo
|
||||
// Tests the full dependency chain via direct injection (no container rebinding).
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { MockArticlesRepository } from "../src/infrastructure/repositories/articles.repository.mock";
|
||||
import { getArticlesUseCase } from "../src/application/use-cases/get-articles.use-case";
|
||||
import { createArticleUseCase } from "../src/application/use-cases/create-article.use-case";
|
||||
import { getArticleBySlugUseCase } from "../src/application/use-cases/get-article-by-slug.use-case";
|
||||
import { getArticlesController } from "../src/interface-adapters/controllers/get-articles.controller";
|
||||
import { createArticleController } from "../src/interface-adapters/controllers/create-article.controller";
|
||||
import { getArticleBySlugController } from "../src/interface-adapters/controllers/get-article-by-slug.controller";
|
||||
import { ArticleNotFoundError } from "../src/entities/errors/article";
|
||||
|
||||
describe("blog feature: article end-to-end via direct injection", () => {
|
||||
function buildChain() {
|
||||
const repo = new MockArticlesRepository();
|
||||
const createUseCase = createArticleUseCase(repo);
|
||||
const getArticlesUC = getArticlesUseCase(repo);
|
||||
const getBySlugUC = getArticleBySlugUseCase(repo);
|
||||
const createCtrl = createArticleController(createUseCase);
|
||||
const listCtrl = getArticlesController(getArticlesUC);
|
||||
const bySlugCtrl = getArticleBySlugController(getBySlugUC);
|
||||
return { createCtrl, listCtrl, bySlugCtrl };
|
||||
}
|
||||
|
||||
it("creates an article via controller, then fetches it back by slug", async () => {
|
||||
const { createCtrl, bySlugCtrl } = buildChain();
|
||||
|
||||
const created = await createCtrl({
|
||||
title: "The Vertical Refactor",
|
||||
content: { type: "doc", children: [] },
|
||||
authorId: "u1",
|
||||
slug: "vertical-refactor",
|
||||
});
|
||||
expect(created.id).toBeTruthy();
|
||||
expect(created.slug).toBe("vertical-refactor");
|
||||
|
||||
const fetched = await bySlugCtrl({ slug: "vertical-refactor" });
|
||||
expect(fetched.id).toBe(created.id);
|
||||
expect(fetched.title).toBe("The Vertical Refactor");
|
||||
});
|
||||
|
||||
it("listArticles filters by status", async () => {
|
||||
const { createCtrl, listCtrl } = buildChain();
|
||||
|
||||
await createCtrl({
|
||||
title: "Draft One",
|
||||
content: null,
|
||||
authorId: "u1",
|
||||
});
|
||||
const draftOnly = await listCtrl({ status: "draft" });
|
||||
expect(draftOnly).toHaveLength(1);
|
||||
|
||||
const publishedOnly = await listCtrl({ status: "published" });
|
||||
expect(publishedOnly).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("getArticleBySlugController throws ArticleNotFoundError for missing article", async () => {
|
||||
const { bySlugCtrl } = buildChain();
|
||||
await expect(bySlugCtrl({ slug: "missing-slug" })).rejects.toBeInstanceOf(ArticleNotFoundError);
|
||||
});
|
||||
});
|
||||
147
packages/blog/tests/r44-no-double-capture.test.ts
Normal file
147
packages/blog/tests/r44-no-double-capture.test.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
// Verify the full chain (controller → use case → repo) wraps with
|
||||
// withSpan + withCapture and never double-captures the same error.
|
||||
//
|
||||
// Each layer's withCapture catch checks the __sentryReported flag (set by
|
||||
// the inner-most layer that captured first) and skips. End result: one
|
||||
// error → one logger.captureException call, with the inner-most tags.
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
RecordingTracer,
|
||||
RecordingLogger,
|
||||
} from "@repo/core-testing/instrumentation";
|
||||
import { withSpan, withCapture } from "@repo/core-shared/instrumentation";
|
||||
|
||||
import { MockArticlesRepository } from "../src/infrastructure/repositories/articles.repository.mock";
|
||||
import { getArticleBySlugUseCase } from "../src/application/use-cases/get-article-by-slug.use-case";
|
||||
import { getArticleBySlugController } from "../src/interface-adapters/controllers/get-article-by-slug.controller";
|
||||
|
||||
describe("no double-capture across span/capture-wrapped layers", () => {
|
||||
it("an error originated in the repo is captured exactly once with repo tags", async () => {
|
||||
const tracer = new RecordingTracer();
|
||||
const logger = new RecordingLogger();
|
||||
|
||||
// Repo throws on getArticleBySlug for a special slug. We simulate the
|
||||
// throw via a subclass — the production repos call captureException
|
||||
// inside their own try/catch as part of the repo span body.
|
||||
class ThrowingRepo extends MockArticlesRepository {
|
||||
override getArticleBySlug = async (_slug: string) => {
|
||||
const err = new Error("boom from repo");
|
||||
// Mirror what the real repo does: capture with repo tags, mark the
|
||||
// flag (RecordingLogger.captureException does this for us now).
|
||||
logger.captureException(err, {
|
||||
tags: {
|
||||
feature: "blog",
|
||||
repo: "articles",
|
||||
method: "getArticleBySlug",
|
||||
},
|
||||
});
|
||||
throw err;
|
||||
};
|
||||
}
|
||||
|
||||
const repo = new ThrowingRepo(tracer, logger);
|
||||
|
||||
// Wire the use case + controller exactly the way bind-production does.
|
||||
const wrappedUC = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "use-case" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "blog", layer: "use-case", name: "blog.getArticleBySlug" },
|
||||
getArticleBySlugUseCase(repo),
|
||||
),
|
||||
);
|
||||
const wrappedCtrl = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "controller" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "blog", layer: "controller", name: "blog.getArticleBySlug" },
|
||||
getArticleBySlugController(wrappedUC),
|
||||
),
|
||||
);
|
||||
|
||||
await expect(wrappedCtrl({ slug: "anything" })).rejects.toThrow(
|
||||
"boom from repo",
|
||||
);
|
||||
|
||||
// Exactly one capture. Outer wrappers saw the flag and skipped.
|
||||
expect(logger.captures).toHaveLength(1);
|
||||
const only = logger.captures[0];
|
||||
expect(only?.kind).toBe("exception");
|
||||
if (only?.kind !== "exception") return;
|
||||
expect(only.ctx?.tags).toEqual({
|
||||
feature: "blog",
|
||||
repo: "articles",
|
||||
method: "getArticleBySlug",
|
||||
});
|
||||
});
|
||||
|
||||
it("an error originated in the controller (parse failure) is captured once with controller tags", async () => {
|
||||
const tracer = new RecordingTracer();
|
||||
const logger = new RecordingLogger();
|
||||
const repo = new MockArticlesRepository();
|
||||
|
||||
const wrappedUC = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "use-case" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "blog", layer: "use-case", name: "blog.getArticleBySlug" },
|
||||
getArticleBySlugUseCase(repo),
|
||||
),
|
||||
);
|
||||
const wrappedCtrl = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "controller" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "blog", layer: "controller", name: "blog.getArticleBySlug" },
|
||||
getArticleBySlugController(wrappedUC),
|
||||
),
|
||||
);
|
||||
|
||||
// safeParse fails because slug is missing.
|
||||
await expect(wrappedCtrl({})).rejects.toThrow();
|
||||
|
||||
expect(logger.captures).toHaveLength(1);
|
||||
const only = logger.captures[0];
|
||||
expect(only?.kind).toBe("exception");
|
||||
if (only?.kind !== "exception") return;
|
||||
expect(only.ctx?.tags).toEqual({
|
||||
feature: "blog",
|
||||
layer: "controller",
|
||||
name: "blog.getArticleBySlug",
|
||||
});
|
||||
});
|
||||
|
||||
it("a successful call captures nothing", async () => {
|
||||
const tracer = new RecordingTracer();
|
||||
const logger = new RecordingLogger();
|
||||
const repo = new MockArticlesRepository();
|
||||
|
||||
const wrappedUC = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticles", op: "use-case" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "blog", layer: "use-case", name: "blog.getArticles" },
|
||||
async () => [],
|
||||
),
|
||||
);
|
||||
const wrappedCtrl = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticles", op: "controller" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "blog", layer: "controller", name: "blog.getArticles" },
|
||||
async () => wrappedUC(),
|
||||
),
|
||||
);
|
||||
|
||||
await expect(wrappedCtrl()).resolves.toEqual([]);
|
||||
expect(logger.captures).toHaveLength(0);
|
||||
void repo;
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user