Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/core/use-cases/article/article-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,22 @@ export function validateCoverImageKey(

return key;
}

/**
* Normalizes a tag used as a *read* filter.
*
* Tags are stored lowercased by {@link normalizeTags}, so a filter that is not
* lowercased matches nothing. Unlike the write path this never throws: a
* malformed filter is a filter that matches no article, not a bad request, and
* a blank one is no filter at all.
*
* @param tag - The raw tag from the query string
* @returns The lowercased tag, or undefined when nothing was supplied
*/
export function normalizeTagFilter(tag?: string): string | undefined {
if (!tag) return undefined;

const normalized = tag.trim().toLowerCase();

return normalized.length > 0 ? normalized : undefined;
}
18 changes: 15 additions & 3 deletions src/core/use-cases/article/get-articles/get-articles.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { IFollowRepository } from "@core/ports/repositories/follow.reposito
import type { IUserRepository } from "@core/ports/repositories/user.repository";
import type { CachePort } from "@core/ports/services/cache.port";
import { UnauthorizedError } from "@core/errors";
import { normalizeTagFilter } from "../article-input";
import type { GetArticlesUseCaseInput } from "./get-articles-usecase.input";
import type { GetArticlesUseCaseOutput } from "./get-articles-usecase.output";

Expand Down Expand Up @@ -95,14 +96,21 @@ export class GetArticlesUseCase {
const page = input.page ?? 1;
const limit = input.limit ?? DEFAULT_LIMIT;
const followedOnly = input.followedOnly ?? false;
const tag = normalizeTagFilter(input.tag);

if (followedOnly && !input.currentUserId) {
throw new UnauthorizedError(
"Authentication is required to use the followedOnly filter.",
);
}

const cacheKey = this.buildCacheKey(input, page, limit, followedOnly);
const cacheKey = this.buildCacheKey(
input,
page,
limit,
followedOnly,
tag,
);
const cached = await this.cacheService.get(cacheKey);

if (cached) {
Expand Down Expand Up @@ -136,7 +144,7 @@ export class GetArticlesUseCase {
const result = await this.articleRepository.findAll({
page,
limit,
tag: input.tag,
tag,
authorId,
categories: input.categories,
followingIds,
Expand Down Expand Up @@ -167,15 +175,19 @@ export class GetArticlesUseCase {
* @param page - Resolved page number
* @param limit - Resolved page size
* @param followedOnly - Resolved followed-authors flag
* @param normalizedTag - The tag filter as the repository will see it
* @returns The cache key
*/
private buildCacheKey(
input: GetArticlesUseCaseInput,
page: number,
limit: number,
followedOnly: boolean,
normalizedTag?: string,
): string {
const tag = input.tag ?? "ALL";
// The normalized tag, not the raw one: "NodeJS" and "nodejs" select the
// same articles, so they must not occupy two cache entries.
const tag = normalizedTag ?? "ALL";
const author = input.authorUsername ?? "ALL";
const categories =
input.categories && input.categories.length > 0
Expand Down
23 changes: 20 additions & 3 deletions tests/unit/core/use-cases/article/article-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect } from "vitest";
import {
normalizeBody,
normalizeTags,
normalizeTagFilter,
normalizeTitle,
validateCoverImageKey,
} from "@core/use-cases/article/article-input";
Expand Down Expand Up @@ -87,9 +88,9 @@ describe("article input rules", () => {
});

it("should reject more than five distinct tags", () => {
expect(() =>
normalizeTags(["a", "b", "c", "d", "e", "f"]),
).toThrow(BadRequestError);
expect(() => normalizeTags(["a", "b", "c", "d", "e", "f"])).toThrow(
BadRequestError,
);
});

it("should skip blank entries rather than reject them", () => {
Expand Down Expand Up @@ -164,4 +165,20 @@ describe("article input rules", () => {
);
});
});

describe("normalizeTagFilter()", () => {
it("should lowercase and trim the filter", () => {
expect(normalizeTagFilter(" NodeJS ")).toBe("nodejs");
});

it("should treat a missing or blank filter as no filter", () => {
expect(normalizeTagFilter()).toBeUndefined();
expect(normalizeTagFilter("")).toBeUndefined();
expect(normalizeTagFilter(" ")).toBeUndefined();
});

it("should not throw on a malformed filter", () => {
expect(normalizeTagFilter("Not A Tag!")).toBe("not a tag!");
});
});
});
27 changes: 27 additions & 0 deletions tests/unit/core/use-cases/article/get-articles.usecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ describe("GetArticlesUseCase", () => {
expect(key.startsWith("articles:list:")).toBe(true);
});

it("should lowercase the tag filter before it reaches the repository", async () => {
await useCase.execute({ tag: " NodeJS " });

expect(articleRepository.findAll).toHaveBeenCalledWith(
expect.objectContaining({ tag: "nodejs" }),
);
});

it("should drop a blank tag filter instead of matching the empty tag", async () => {
await useCase.execute({ tag: " " });

expect(articleRepository.findAll).toHaveBeenCalledWith(
expect.objectContaining({ tag: undefined }),
);
});

it("should reuse one key for tags differing only in case", async () => {
await useCase.execute({ tag: "NodeJS" });
const first = vi.mocked(cacheService.get).mock.calls[0][0];

vi.mocked(cacheService.get).mockClear();
await useCase.execute({ tag: "nodejs" });
const second = vi.mocked(cacheService.get).mock.calls[0][0];

expect(first).toBe(second);
});

it("should order categories so the same filter reuses one key", async () => {
await useCase.execute({
categories: [PostCategory.FRONTEND, PostCategory.BACKEND],
Expand Down