Vitest med enhetstester utan databas och databastester mot en egen testdatabas vars namn måste sluta på _test. Rättar tomma MAP_*-värden i .env och säkerhetskopior med å, ä och ö i filnamnet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
200 lines
10 KiB
TypeScript
200 lines
10 KiB
TypeScript
import { readdir } from "node:fs/promises";
|
|
import { beforeEach, describe, expect, it } from "vitest";
|
|
import { UPLOAD_DIR, saveImageBytes } from "@/lib/files";
|
|
import { nextMapCode, nextMarkerCode, nextScheduleCode } from "@/lib/ids";
|
|
import { TRASH_DIR, TRASH_DAYS, emptyBookTrash, purgeExpiredTrash, purgeTrashItem, restoreBook, restoreEntity, trashBook, trashEntity } from "@/lib/trash";
|
|
import { makeImage } from "../helpers/fixtures";
|
|
import { describeDb, filesIn, makeBook, makeEntity, makeRelation, makeUser, prisma, resetAll } from "../helpers/db";
|
|
|
|
describeDb("papperskorg", () => {
|
|
let ownerId: string;
|
|
let bookId: string;
|
|
let anna: { id: string; code: string | null };
|
|
let erik: { id: string };
|
|
let cafe: { id: string };
|
|
let relationId: string;
|
|
let markerId: string;
|
|
let hostedShiftId: string;
|
|
|
|
beforeEach(async () => {
|
|
await resetAll();
|
|
ownerId = (await makeUser()).id;
|
|
bookId = (await makeBook(ownerId)).id;
|
|
const image = await saveImageBytes(await makeImage(80, 60, "png"));
|
|
anna = await makeEntity(bookId, "CHARACTER", "Anna", { imageFile: image, data: { age: 30 } });
|
|
erik = await makeEntity(bookId, "CHARACTER", "Erik");
|
|
cafe = await makeEntity(bookId, "PREMISES", "Café");
|
|
const company = await makeEntity(bookId, "COMPANY", "Firma");
|
|
|
|
// Anna är med i en relation som parti, i en annan som extra deltagare, har egna pass och är arbetsplats åt ett pass
|
|
relationId = (await makeRelation(bookId, anna.id, erik.id, "Vänner", [cafe.id])).id;
|
|
await makeRelation(bookId, erik.id, company.id, "Jobbar på", [anna.id]);
|
|
const code = await nextScheduleCode(bookId);
|
|
await prisma.shift.create({ data: { bookId, personId: anna.id, workplaceId: cafe.id, weekday: 0, startTime: "08:00", endTime: "16:00", scheduleCode: code } });
|
|
hostedShiftId = (await prisma.shift.create({ data: { bookId, personId: erik.id, workplaceId: anna.id, weekday: 1, startTime: "09:00", endTime: "17:00", scheduleCode: code } })).id;
|
|
const map = await prisma.bookMap.create({ data: { bookId, name: "Karta", kind: "GEO", code: await nextMapCode(bookId, "GEO") } });
|
|
markerId = (await prisma.marker.create({ data: { mapId: map.id, entityId: anna.id, label: "Annas hus", code: await nextMarkerCode(bookId), lat: 1, lng: 2 } })).id;
|
|
});
|
|
|
|
describe("poster", () => {
|
|
it("flyttar posten till papperskorgen och tar bort den ur boken", async () => {
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
expect(await prisma.entity.findUnique({ where: { id: anna.id } })).toBeNull();
|
|
const [item] = await prisma.trashItem.findMany();
|
|
expect(item).toMatchObject({ kind: "entity", label: "Anna", detail: "CHARACTER", ownerId, bookId });
|
|
// Annas relationer och egna pass följer med bort
|
|
expect(await prisma.relation.count({ where: { id: relationId } })).toBe(0);
|
|
expect(await prisma.shift.count({ where: { personId: anna.id } })).toBe(0);
|
|
// Det som bara pekade på henne blir kvar utan koppling
|
|
expect((await prisma.shift.findUniqueOrThrow({ where: { id: hostedShiftId } })).workplaceId).toBeNull();
|
|
expect((await prisma.marker.findUniqueOrThrow({ where: { id: markerId } })).entityId).toBeNull();
|
|
});
|
|
|
|
it("behåller bilden medan posten ligger i papperskorgen", async () => {
|
|
const image = (await prisma.entity.findUniqueOrThrow({ where: { id: anna.id } })).imageFile!;
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
expect(await filesIn(UPLOAD_DIR)).toContain(image);
|
|
});
|
|
|
|
it("återställer allt: samma id och ID-nummer, relationer, deltagande, pass, arbetsplats och markering", async () => {
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
const restored = await restoreEntity(ownerId, item.id);
|
|
expect(restored).toEqual({ bookId, entityId: anna.id });
|
|
|
|
const back = await prisma.entity.findUniqueOrThrow({ where: { id: anna.id }, include: { outgoing: { include: { members: true } }, memberOf: true, shifts: true } });
|
|
expect(back).toMatchObject({ name: "Anna", code: anna.code, data: { age: 30 } });
|
|
expect(back.imageFile).toBeTruthy();
|
|
expect(back.outgoing).toHaveLength(1);
|
|
expect(back.outgoing[0].id).toBe(relationId);
|
|
expect(back.outgoing[0].members.map((m) => m.entityId)).toEqual([cafe.id]);
|
|
expect(back.memberOf).toHaveLength(1);
|
|
expect(back.shifts).toHaveLength(1);
|
|
expect(back.shifts[0].scheduleCode).toBe("SCI-0001");
|
|
expect((await prisma.shift.findUniqueOrThrow({ where: { id: hostedShiftId } })).workplaceId).toBe(anna.id);
|
|
expect((await prisma.marker.findUniqueOrThrow({ where: { id: markerId } })).entityId).toBe(anna.id);
|
|
expect(await prisma.trashItem.count()).toBe(0);
|
|
});
|
|
|
|
it("hoppar över relationer vars andra part tagits bort under tiden", async () => {
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
await prisma.entity.delete({ where: { id: erik.id } });
|
|
const [item] = await prisma.trashItem.findMany();
|
|
await restoreEntity(ownerId, item.id);
|
|
expect(await prisma.entity.findUnique({ where: { id: anna.id } })).not.toBeNull();
|
|
expect(await prisma.relation.count({ where: { id: relationId } })).toBe(0);
|
|
});
|
|
|
|
it("ger en ny kod om den gamla hunnit tas av en annan post", async () => {
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
await prisma.entity.update({ where: { id: erik.id }, data: { code: anna.code } });
|
|
const [item] = await prisma.trashItem.findMany();
|
|
await restoreEntity(ownerId, item.id);
|
|
const back = await prisma.entity.findUniqueOrThrow({ where: { id: anna.id } });
|
|
expect(back.code).toMatch(/^KI-\d{4}$/);
|
|
expect(back.code).not.toBe(anna.code);
|
|
});
|
|
|
|
it("kan inte återställas av någon annan, eller när boken är borta", async () => {
|
|
const intruder = await makeUser("Inkräktare");
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
expect(await restoreEntity(intruder.id, item.id)).toBeNull();
|
|
expect(await prisma.entity.count({ where: { id: anna.id } })).toBe(0);
|
|
|
|
await prisma.book.delete({ where: { id: bookId } });
|
|
expect(await restoreEntity(ownerId, item.id)).toBeNull();
|
|
});
|
|
|
|
it("rensar för gott: tar bort bilden och historiken", async () => {
|
|
const image = (await prisma.entity.findUniqueOrThrow({ where: { id: anna.id } })).imageFile!;
|
|
await prisma.entityVersion.create({ data: { bookId, entityId: anna.id, snapshot: { name: "Gammal" } } });
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
|
|
await purgeTrashItem(ownerId, item.id);
|
|
expect(await prisma.trashItem.count()).toBe(0);
|
|
expect(await filesIn(UPLOAD_DIR)).not.toContain(image);
|
|
expect(await prisma.entityVersion.count({ where: { entityId: anna.id } })).toBe(0);
|
|
});
|
|
|
|
it("kan inte rensa någon annans papperskorg", async () => {
|
|
const intruder = await makeUser("Inkräktare");
|
|
await trashEntity(ownerId, bookId, anna.id);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
await purgeTrashItem(intruder.id, item.id);
|
|
expect(await prisma.trashItem.count()).toBe(1);
|
|
});
|
|
|
|
it("tömmer bokens papperskorg men rör inga andra böcker", async () => {
|
|
const otherBook = await makeBook(ownerId, "Annan");
|
|
const stray = await makeEntity(otherBook.id, "CHARACTER", "Kvar");
|
|
await trashEntity(ownerId, bookId, erik.id);
|
|
await trashEntity(ownerId, otherBook.id, stray.id);
|
|
await emptyBookTrash(ownerId, bookId);
|
|
const left = await prisma.trashItem.findMany();
|
|
expect(left.map((i) => i.label)).toEqual(["Kvar"]);
|
|
});
|
|
|
|
it("rensar bara det som legat längre än gränsen", async () => {
|
|
await trashEntity(ownerId, bookId, erik.id);
|
|
await trashEntity(ownerId, bookId, cafe.id);
|
|
const old = await prisma.trashItem.findFirstOrThrow({ where: { label: "Erik" } });
|
|
await prisma.trashItem.update({ where: { id: old.id }, data: { deletedAt: new Date(Date.now() - (TRASH_DAYS + 1) * 86_400_000) } });
|
|
|
|
expect(await purgeExpiredTrash()).toBe(1);
|
|
expect((await prisma.trashItem.findMany()).map((i) => i.label)).toEqual(["Café"]);
|
|
});
|
|
});
|
|
|
|
describe("böcker", () => {
|
|
it("sparar boken som zip med bilderna och tar bort den", async () => {
|
|
expect(await filesIn(UPLOAD_DIR)).toHaveLength(1);
|
|
await trashBook(ownerId, bookId);
|
|
|
|
expect(await prisma.book.count()).toBe(0);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
expect(item).toMatchObject({ kind: "book", label: "Testbok", ownerId });
|
|
expect(await readdir(TRASH_DIR)).toEqual([item.file]);
|
|
// Bilden ligger nu i zip-filen och inte längre bland de uppladdade
|
|
expect(await filesIn(UPLOAD_DIR)).toHaveLength(0);
|
|
});
|
|
|
|
it("återställer boken som en ny bok med allt innehåll och bilderna", async () => {
|
|
await trashBook(ownerId, bookId);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
const newId = await restoreBook(ownerId, item.id);
|
|
|
|
expect(newId).toBeTruthy();
|
|
const book = await prisma.book.findUniqueOrThrow({ where: { id: newId! }, include: { entities: true, relations: true, shifts: true, maps: { include: { markers: true } } } });
|
|
expect(book.title).toBe("Testbok");
|
|
expect(book.entities).toHaveLength(4);
|
|
expect(book.relations).toHaveLength(2);
|
|
expect(book.shifts).toHaveLength(2);
|
|
expect(book.maps[0].markers).toHaveLength(1);
|
|
expect(book.entities.find((e) => e.name === "Anna")?.code).toBe(anna.code);
|
|
expect(await filesIn(UPLOAD_DIR)).toHaveLength(1);
|
|
|
|
// Papperskorgen och zip-filen är borta
|
|
expect(await prisma.trashItem.count()).toBe(0);
|
|
expect(await readdir(TRASH_DIR)).toEqual([]);
|
|
});
|
|
|
|
it("kan inte återställas av någon annan", async () => {
|
|
const intruder = await makeUser("Inkräktare");
|
|
await trashBook(ownerId, bookId);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
expect(await restoreBook(intruder.id, item.id)).toBeNull();
|
|
expect(await prisma.book.count()).toBe(0);
|
|
});
|
|
|
|
it("rensar zip-filen när den tas bort för gott", async () => {
|
|
await trashBook(ownerId, bookId);
|
|
const [item] = await prisma.trashItem.findMany();
|
|
await purgeTrashItem(ownerId, item.id);
|
|
expect(await readdir(TRASH_DIR)).toEqual([]);
|
|
expect(await prisma.trashItem.count()).toBe(0);
|
|
});
|
|
});
|
|
});
|