feat(media): full Clean Architecture scaffold
Media is now a complete vertical-feature package mirroring auth/blog structure: entities (models + errors), application (repositories + use-cases), infrastructure (real Payload-backed + mock siblings), interface-adapters (per-use-case controllers), DI (symbols + module + container + bind-production), integrations/api (mediaRouter), factory, contract suite, and feature integration tests. Wired into: - packages/core-api/src/root.ts (added `media: mediaRouter`) - apps/web-next/src/server/bind-production.ts (calls bindProductionMedia) - tsconfig.base.json (added @repo/media/api and ./di/bind-production aliases) 56 new tests in @repo/media (13 test files); core-api router test updated to assert media. procedures. All 26 turbo tasks green. Refactor log: §2, §4.1, §4.2, §5.1, §6.1 Spec: §6.5 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import { describe } from "vitest";
|
||||
import { MockMediaRepository } from "@/infrastructure/repositories/media.repository.mock";
|
||||
import { mediaRepositoryContract } from "@/__contracts__/media-repository.contract";
|
||||
|
||||
describe("MockMediaRepository", () => {
|
||||
mediaRepositoryContract.run(() => new MockMediaRepository());
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import "reflect-metadata";
|
||||
import { injectable } from "inversify";
|
||||
|
||||
import type { IMediaRepository } from "../../application/repositories/media.repository.interface";
|
||||
import type { Media } from "../../entities/models/media";
|
||||
|
||||
@injectable()
|
||||
export class MockMediaRepository implements IMediaRepository {
|
||||
private _media: Media[] = [];
|
||||
|
||||
/** Test helper — seeds the in-memory store directly. */
|
||||
async _store(media: Media): Promise<void> {
|
||||
this._media.push(media);
|
||||
}
|
||||
|
||||
async getMedia(id: string): Promise<Media | undefined> {
|
||||
return this._media.find((m) => m.id === id);
|
||||
}
|
||||
|
||||
async listMedia(opts?: { limit?: number; offset?: number }): Promise<Media[]> {
|
||||
const offset = opts?.offset ?? 0;
|
||||
const limit = opts?.limit ?? 50;
|
||||
return this._media.slice(offset, offset + limit);
|
||||
}
|
||||
|
||||
async deleteMedia(id: string): Promise<void> {
|
||||
this._media = this._media.filter((m) => m.id !== id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { MediaRepository } from "@/infrastructure/repositories/media.repository";
|
||||
import { mediaRepositoryContract } from "@/__contracts__/media-repository.contract";
|
||||
import { stubPayloadConfig } from "@repo/core-testing/payload/stub-config";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// In-memory Payload stub
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function buildPayloadStub() {
|
||||
const store = new Map<string, Record<string, unknown>>();
|
||||
|
||||
return {
|
||||
find: vi.fn(
|
||||
async ({
|
||||
limit,
|
||||
page,
|
||||
}: {
|
||||
collection: string;
|
||||
limit?: number;
|
||||
page?: number;
|
||||
overrideAccess?: boolean;
|
||||
}) => {
|
||||
let docs = Array.from(store.values());
|
||||
const lim = limit ?? 50;
|
||||
const pg = page ?? 1;
|
||||
const offset = (pg - 1) * lim;
|
||||
docs = docs.slice(offset, offset + lim);
|
||||
return { docs };
|
||||
},
|
||||
),
|
||||
findByID: vi.fn(
|
||||
async ({ id }: { collection: string; id: string; overrideAccess?: boolean }) => {
|
||||
const doc = store.get(String(id));
|
||||
if (!doc) throw new Error(`Not found: ${id}`);
|
||||
return doc;
|
||||
},
|
||||
),
|
||||
delete: vi.fn(
|
||||
async ({ id }: { collection: string; id: string; overrideAccess?: boolean }) => {
|
||||
store.delete(String(id));
|
||||
return { id };
|
||||
},
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
vi.mock("payload", () => ({
|
||||
getPayload: vi.fn(),
|
||||
}));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Contract suite
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("MediaRepository", () => {
|
||||
describe("contract", () => {
|
||||
mediaRepositoryContract.run(async () => {
|
||||
const stub = buildPayloadStub();
|
||||
const { getPayload } = await import("payload");
|
||||
(getPayload as ReturnType<typeof vi.fn>).mockResolvedValue(stub);
|
||||
return new MediaRepository(stubPayloadConfig);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Impl-specific tests: Payload doc → domain mapping
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
describe("getMedia", () => {
|
||||
it("returns undefined when Payload throws (not found)", async () => {
|
||||
const { getPayload } = await import("payload");
|
||||
(getPayload as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
findByID: vi.fn().mockRejectedValue(new Error("Not found")),
|
||||
});
|
||||
|
||||
const repo = new MediaRepository(stubPayloadConfig);
|
||||
const result = await repo.getMedia("missing-id");
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("maps Payload doc fields to domain Media", async () => {
|
||||
const { getPayload } = await import("payload");
|
||||
(getPayload as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
findByID: vi.fn().mockResolvedValue({
|
||||
id: "p-1",
|
||||
alt: "Test image",
|
||||
url: "https://cdn.example.com/test.png",
|
||||
filename: "test.png",
|
||||
mimeType: "image/png",
|
||||
filesize: 2048,
|
||||
width: 800,
|
||||
height: 600,
|
||||
}),
|
||||
});
|
||||
|
||||
const repo = new MediaRepository(stubPayloadConfig);
|
||||
const result = await repo.getMedia("p-1");
|
||||
expect(result?.id).toBe("p-1");
|
||||
expect(result?.alt).toBe("Test image");
|
||||
expect(result?.width).toBe(800);
|
||||
expect(result?.height).toBe(600);
|
||||
});
|
||||
});
|
||||
|
||||
describe("listMedia", () => {
|
||||
it("returns an array of mapped Media docs", async () => {
|
||||
const { getPayload } = await import("payload");
|
||||
(getPayload as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
find: vi.fn().mockResolvedValue({
|
||||
docs: [
|
||||
{
|
||||
id: "m-1",
|
||||
alt: "First",
|
||||
url: "https://cdn.example.com/first.png",
|
||||
filename: "first.png",
|
||||
mimeType: "image/png",
|
||||
filesize: 100,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const repo = new MediaRepository(stubPayloadConfig);
|
||||
const result = await repo.listMedia();
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?.id).toBe("m-1");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,76 @@
|
||||
import "reflect-metadata";
|
||||
import { injectable } from "inversify";
|
||||
import { getPayload } from "payload";
|
||||
import type { SanitizedConfig } from "payload";
|
||||
|
||||
import type { IMediaRepository } from "../../application/repositories/media.repository.interface";
|
||||
import type { Media } from "../../entities/models/media";
|
||||
|
||||
type PayloadMediaDoc = {
|
||||
id: string | number;
|
||||
alt?: string | null;
|
||||
url?: string | null;
|
||||
filename?: string | null;
|
||||
mimeType?: string | null;
|
||||
filesize?: number | null;
|
||||
width?: number | null;
|
||||
height?: number | null;
|
||||
};
|
||||
|
||||
function mapDoc(doc: PayloadMediaDoc): Media {
|
||||
return {
|
||||
id: String(doc.id),
|
||||
alt: doc.alt ?? "",
|
||||
url: doc.url ?? "",
|
||||
filename: doc.filename ?? "",
|
||||
mimeType: doc.mimeType ?? "",
|
||||
filesize: doc.filesize ?? 0,
|
||||
...(doc.width != null && { width: doc.width }),
|
||||
...(doc.height != null && { height: doc.height }),
|
||||
};
|
||||
}
|
||||
|
||||
@injectable()
|
||||
export class MediaRepository implements IMediaRepository {
|
||||
private config: SanitizedConfig;
|
||||
|
||||
constructor(config: SanitizedConfig) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
async getMedia(id: string): Promise<Media | undefined> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
try {
|
||||
const doc = await payload.findByID({
|
||||
collection: "media",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return mapDoc(doc as PayloadMediaDoc);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async listMedia(opts?: { limit?: number; offset?: number }): Promise<Media[]> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const result = await payload.find({
|
||||
collection: "media",
|
||||
limit: opts?.limit ?? 50,
|
||||
page: opts?.offset
|
||||
? Math.floor(opts.offset / (opts.limit ?? 50)) + 1
|
||||
: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return result.docs.map((d) => mapDoc(d as PayloadMediaDoc));
|
||||
}
|
||||
|
||||
async deleteMedia(id: string): Promise<void> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
await payload.delete({
|
||||
collection: "media",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user