82 lines
3.1 KiB
TypeScript
82 lines
3.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ reads: [] as unknown[], rows: new Map<unknown, unknown[]>() }));
|
|
vi.mock("server-only", () => ({}));
|
|
vi.mock("@/lib/media/storage", () => ({ publicMediaUrl: (key: string) => `/media/${key}` }));
|
|
vi.mock("@/db", () => ({
|
|
getDb: () => ({
|
|
select: () => {
|
|
let table: unknown;
|
|
const query = {
|
|
from(value: unknown) { table = value; return query; },
|
|
innerJoin() { return query; },
|
|
leftJoin() { return query; },
|
|
where() { return query; },
|
|
orderBy() { return query; },
|
|
limit() { return query; },
|
|
then(resolve: (rows: unknown[]) => unknown) {
|
|
mocks.reads.push(table);
|
|
return Promise.resolve(mocks.rows.get(table) ?? []).then(resolve);
|
|
},
|
|
};
|
|
return query;
|
|
},
|
|
}),
|
|
}));
|
|
|
|
import {
|
|
artifactConditions, constellationConditions, extraSectionRows,
|
|
guides, guideSections, teamConditions, weaponConditions,
|
|
catalogMaterials, guidePosterBleeding,
|
|
} from "@/db/schema";
|
|
import { getAdminGuideContent } from "./queries";
|
|
|
|
beforeEach(() => {
|
|
mocks.reads.length = 0;
|
|
mocks.rows.clear();
|
|
mocks.rows.set(guides, [{
|
|
guide: { id: "guide", slug: "test", posterArtMediaId: null },
|
|
cover: { objectKey: "cover", status: "ready" },
|
|
character: { key: "test", imageKey: "character", constellations: [], talents: [], ascension: [], leveling: [] },
|
|
}]);
|
|
mocks.rows.set(guideSections, [
|
|
{ id: "extra-a", slug: "extra-a", kind: "extra" },
|
|
{ id: "extra-b", slug: "extra-b", kind: "extra" },
|
|
]);
|
|
});
|
|
|
|
describe("admin section reads", () => {
|
|
it.each([
|
|
["weapon", weaponConditions],
|
|
["artifact", artifactConditions],
|
|
["constellations", constellationConditions],
|
|
["team", teamConditions],
|
|
])("loads only the %s section's conditions", async (section, expectedTable) => {
|
|
const result = await getAdminGuideContent("test", section);
|
|
expect(result).not.toBeNull();
|
|
expect(mocks.reads).toContain(expectedTable);
|
|
for (const table of [weaponConditions, artifactConditions, constellationConditions, teamConditions]) {
|
|
if (table !== expectedTable) expect(mocks.reads).not.toContain(table);
|
|
}
|
|
expect(mocks.reads).not.toContain(extraSectionRows);
|
|
expect(mocks.reads).not.toContain(catalogMaterials);
|
|
expect(mocks.reads).not.toContain(guidePosterBleeding);
|
|
});
|
|
|
|
it("loads just the selected extra section", async () => {
|
|
const result = await getAdminGuideContent("test", "extra-b");
|
|
expect(result?.extras.map((extra) => extra.id)).toEqual(["extra-b"]);
|
|
expect(mocks.reads).toContain(extraSectionRows);
|
|
expect(mocks.reads).not.toContain(weaponConditions);
|
|
expect(mocks.reads).not.toContain(teamConditions);
|
|
});
|
|
|
|
it("preserves full-guide reads for existing preview and validation callers", async () => {
|
|
const result = await getAdminGuideContent("test");
|
|
for (const table of [weaponConditions, artifactConditions, constellationConditions, teamConditions, extraSectionRows]) {
|
|
expect(mocks.reads).toContain(table);
|
|
}
|
|
expect(result?.extras).toHaveLength(2);
|
|
});
|
|
});
|