Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, test, vi } from "vitest";
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
const getAllKnowledgeBaseArticlesMock = vi.fn();
const createKnowledgeBaseArticleMock = vi.fn();
@@ -7,6 +8,7 @@ const getFixedTMock = vi.fn();
const knowledgeBaseArticleCountMock = vi.fn();
const knowledgeBaseCategoryCountMock = vi.fn();
const knowledgeBaseDeleteMock = vi.fn();
const getKnowledgeBaseByIdMock = vi.fn();
const cachifiedMock = vi.fn();
vi.mock("../db/kbArticles.db.server", () => ({
@@ -27,7 +29,7 @@ vi.mock("../db/kbCategories.db.server", () => ({
vi.mock("../db/knowledgeBase.db.server", () => ({
getAllKnowledgeBases: vi.fn(),
getKnowledgeBaseById: vi.fn(),
getKnowledgeBaseById: (...args: unknown[]) => getKnowledgeBaseByIdMock(...args),
getKnowledgeBaseBySlug: vi.fn(),
}));
@@ -69,6 +71,7 @@ describe("KnowledgeBaseServiceServer", () => {
knowledgeBaseArticleCountMock.mockResolvedValue(0);
knowledgeBaseCategoryCountMock.mockResolvedValue(0);
knowledgeBaseDeleteMock.mockResolvedValue({ id: "kb-1" });
getKnowledgeBaseByIdMock.mockResolvedValue(null);
cachifiedMock.mockImplementation(async ({ getFreshValue }: { getFreshValue: () => unknown }) => getFreshValue());
});
@@ -87,10 +90,94 @@ describe("KnowledgeBaseServiceServer", () => {
});
expect(getFixedTMock).toHaveBeenCalledWith("de", "translations");
expect(createKnowledgeBaseArticleMock).toHaveBeenCalledWith(expect.objectContaining({ title: "de:knowledgeBase.adminCategories.untitledNumbered:1", language: "de" }));
expect(createKnowledgeBaseArticleMock).toHaveBeenCalledWith(
expect.objectContaining({ title: "de:knowledgeBase.adminCategories.untitledNumbered:1", language: "de" })
);
expect(created).toMatchObject({ title: "de:knowledgeBase.adminCategories.untitledNumbered:1", language: "de" });
});
test("public article DTOs never expose draft content or author identity", async () => {
const publishedAt = new Date("2026-07-14T00:00:00.000Z");
getAllKnowledgeBaseArticlesMock.mockResolvedValueOnce([
{
id: "article-public",
createdAt: publishedAt,
updatedAt: publishedAt,
slug: "published",
title: "Published",
description: "Public description",
order: 0,
contentDraft: "private draft",
contentPublished: "public content",
contentPublishedAsText: "public content",
contentType: "markdown",
language: "de",
featuredOrder: null,
createdByUser: { firstName: "Private", lastName: "Author", avatar: null },
publishedAt,
sectionId: null,
relatedArticles: [],
seoImage: "",
},
]);
const KnowledgeBaseServiceServer = (await import("./KnowledgeBaseService.server")).default;
const kb = {
id: "kb-public",
slug: "docs",
title: "Docs",
defaultLanguage: "de",
} as never;
const articles = await runWithRlsContext({ publicKnowledgeBaseId: "kb-public" }, () =>
KnowledgeBaseServiceServer.getArticles({
kb,
categoryId: "category-public",
language: "de",
query: undefined,
params: {},
request: new Request("https://example.test/docs"),
})
);
expect(articles).toHaveLength(1);
expect(articles[0]).toMatchObject({
contentDraft: "",
contentPublished: "public content",
createdByUser: null,
});
});
test("public KB lookup re-reads the capability-bound id instead of using the shared slug cache", async () => {
getKnowledgeBaseByIdMock.mockResolvedValueOnce({
id: "kb-public",
createdAt: new Date("2026-07-14T00:00:00.000Z"),
updatedAt: new Date("2026-07-14T00:00:00.000Z"),
basePath: "/docs",
slug: "docs",
title: "Docs",
description: "Public docs",
defaultLanguage: "de",
layout: "docs",
color: 0,
enabled: true,
languages: '["de"]',
links: "[]",
logo: "",
seoImage: "",
_count: { categories: 1, articles: 1, views: 0 },
});
const KnowledgeBaseServiceServer = (await import("./KnowledgeBaseService.server")).default;
await expect(
runWithRlsContext({ publicKnowledgeBaseId: "kb-public" }, () =>
KnowledgeBaseServiceServer.get({ slug: "docs", enabled: true, request: new Request("https://example.test/docs") })
)
).resolves.toMatchObject({ id: "kb-public", slug: "docs", enabled: true });
expect(getKnowledgeBaseByIdMock).toHaveBeenCalledWith("kb-public");
expect(cachifiedMock).not.toHaveBeenCalled();
});
test("del exposes coded delete guard errors for localized route handling", async () => {
const { default: KnowledgeBaseServiceServer, getKnowledgeBaseActionErrorKey } = await import("./KnowledgeBaseService.server");
knowledgeBaseArticleCountMock.mockResolvedValueOnce(1);
@@ -24,6 +24,8 @@ import { htmlToText } from "html-to-text";
import { remixI18Next } from "~/locale/i18next.server";
import { cachified } from "~/utils/cache.server";
import { getBaseURL } from "~/utils/url.server";
import { getRlsExecutionContext } from "@hivegrid/db/rls-context.server";
import { mapPublicKnowledgeBasesWithRlsContext } from "../services/publicKnowledgeBaseRls.server";
export type KnowledgeBaseServiceErrorCode =
| "cannot-delete-with-articles"
@@ -65,18 +67,27 @@ export function getKnowledgeBasePublicErrorKey(error: unknown): string {
return "knowledgeBase.public.errors.unavailable";
}
async function getAll({ enabled, request }: { enabled?: boolean; request: Request }): Promise<KnowledgeBaseDto[]> {
const items = await getAllKnowledgeBases({ enabled });
// cachified expects milliseconds. Keep this short until KB mutations publish
// cross-replica invalidations for the web L1 cache.
const PUBLIC_KNOWLEDGE_BASE_CACHE_TTL_MS = 60_000;
async function getAll({ enabled, request, publicOnly = false }: { enabled?: boolean; request: Request; publicOnly?: boolean }): Promise<KnowledgeBaseDto[]> {
const items = publicOnly
? await mapPublicKnowledgeBasesWithRlsContext((knowledgeBaseId) => getKnowledgeBaseById(knowledgeBaseId))
: await getAllKnowledgeBases({ enabled });
return items.map((item) => kbToDto(item, request));
}
async function get({ slug, enabled, request }: { slug: string; enabled?: boolean; request: Request }): Promise<KnowledgeBaseDto> {
const item = await cachified({
key: `kb:${slug}`,
ttl: 60 * 60 * 24,
getFreshValue: () => getKnowledgeBaseBySlug(slug),
});
if (!item) {
const publicKnowledgeBaseId = getRlsExecutionContext()?.publicKnowledgeBaseId;
const item = publicKnowledgeBaseId
? await getKnowledgeBaseById(publicKnowledgeBaseId)
: await cachified({
key: `kb:${slug}`,
ttl: PUBLIC_KNOWLEDGE_BASE_CACHE_TTL_MS,
getFreshValue: () => getKnowledgeBaseBySlug(slug),
});
if (!item || (publicKnowledgeBaseId && item.slug !== slug)) {
throw new KnowledgeBaseServiceError("not-found");
}
@@ -347,6 +358,7 @@ function articleToDto({
request: Request;
}) {
const baseUrl = getBaseURL(request, { https: true });
const isPublicKnowledgeBaseRequest = getRlsExecutionContext()?.publicKnowledgeBaseId === kb.id;
const item: KbArticleDto = {
id: article.id,
createdAt: article.createdAt,
@@ -355,13 +367,13 @@ function articleToDto({
title: article.title,
description: article.description,
order: article.order,
contentDraft: article.contentDraft,
contentDraft: isPublicKnowledgeBaseRequest ? "" : article.contentDraft,
contentPublished: article.contentPublished,
contentPublishedAsText: article.contentPublishedAsText,
contentType: article.contentType as "wysiwyg" | "markdown",
language: article.language,
featuredOrder: article.featuredOrder,
createdByUser: article.createdByUser,
createdByUser: isPublicKnowledgeBaseRequest ? null : article.createdByUser,
publishedAt: article.publishedAt,
href: KnowledgeBaseUtils.getArticleUrl({ kb, article, params }),
sectionId: article.sectionId,