62 lines
2.8 KiB
TypeScript
62 lines
2.8 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("server-only", () => ({}));
|
|
|
|
const { closeDb, getDb } = await import("@/db");
|
|
const { artifactProfiles, catalogCharacters, guideSections, guides, media } = await import("@/db/schema");
|
|
const { getPublicGuide, getPublicGuideSummary } = await import("@/lib/guides/queries");
|
|
|
|
const databaseUrl = process.env.DATABASE_INTEGRATION_URL;
|
|
const describeWithDatabase = databaseUrl ? describe : describe.skip;
|
|
|
|
describeWithDatabase("public guide section queries", () => {
|
|
const id = randomUUID();
|
|
const key = `test-${id}`;
|
|
const slug = key;
|
|
const originalDatabaseUrl = process.env.DATABASE_URL;
|
|
const originalPublicUrl = process.env.S3_PUBLIC_URL;
|
|
|
|
beforeAll(async () => {
|
|
process.env.DATABASE_URL = databaseUrl;
|
|
process.env.S3_PUBLIC_URL = "https://media.example.test";
|
|
const db = getDb();
|
|
await db.insert(catalogCharacters).values({ key, name: "Test Character", imageKey: "test.png" });
|
|
await db.insert(media).values({ id, objectKey: `test/${id}.png`, fileName: "test.png", mimeType: "image/png", byteSize: 1, status: "ready" });
|
|
await db.insert(guides).values({ id, characterKey: key, slug, name: "Test Guide", overview: "Overview", coverMediaId: id, isPublic: true });
|
|
await db.insert(guideSections).values([
|
|
{ guideId: id, kind: "weapon", slug: "weapon", title: "Weapon", sortOrder: 1 },
|
|
{ guideId: id, kind: "artifact", slug: "artifact", title: "Artifact", sortOrder: 2 },
|
|
]);
|
|
await db.insert(artifactProfiles).values({ guideId: id, sands: ["ATK%"] });
|
|
});
|
|
|
|
afterAll(async () => {
|
|
const db = getDb();
|
|
await db.delete(guides).where(eq(guides.id, id));
|
|
await db.delete(media).where(eq(media.id, id));
|
|
await db.delete(catalogCharacters).where(eq(catalogCharacters.key, key));
|
|
await closeDb();
|
|
if (originalDatabaseUrl === undefined) delete process.env.DATABASE_URL;
|
|
else process.env.DATABASE_URL = originalDatabaseUrl;
|
|
if (originalPublicUrl === undefined) delete process.env.S3_PUBLIC_URL;
|
|
else process.env.S3_PUBLIC_URL = originalPublicUrl;
|
|
});
|
|
|
|
it("probes populated sections without including empty navigation entries", async () => {
|
|
const summary = await getPublicGuideSummary(slug);
|
|
expect(summary?.sections.map((section) => section.slug)).toEqual(["artifact"]);
|
|
});
|
|
|
|
it("fetches only the selected section's content", async () => {
|
|
const overview = await getPublicGuide(slug, "overview");
|
|
expect(overview?.artifactProfile).toBeNull();
|
|
expect(overview?.availableSections?.map((section) => section.slug)).toEqual(["artifact"]);
|
|
const artifact = await getPublicGuide(slug, "artifact");
|
|
expect(artifact?.artifactProfile?.sands).toEqual(["ATK%"]);
|
|
expect(await getPublicGuide(slug, "weapon")).toBeNull();
|
|
});
|
|
});
|