Inbjudningslänkar, adressuppslag för kartregistret och närvaromärke
Böcker kan delas med en inbjudningslänk (roll, giltighetstid, en eller flera personer, bara en hash sparas). Inloggningen går tillbaka till inbjudan. Kartregistrets markeringar kan få position genom uppslag mot karttjänsten, ett i sekunden. Redigeringsformulär och listor visar vem som har något öppet för redigering. Migrering 0009. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1fd1ce78cb
commit
5c58f70bdd
34 files changed
+1610
-30
No files matched your search
@@ -0,0 +1,174 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { describeDb, makeBook, makeEntity, makeEvent, makeRelation, makeUser, prisma, resetAll } from "../helpers/db";
|
||||
|
||||
const login = vi.hoisted(() => ({ userId: "" as string }));
|
||||
vi.mock("@/auth", () => ({ auth: async () => (login.userId ? { user: { id: login.userId } } : null) }));
|
||||
vi.mock("next/cache", () => ({ revalidatePath: () => undefined }));
|
||||
vi.mock("next/navigation", () => ({
|
||||
redirect: (to: string) => {
|
||||
throw new Error(`REDIRECT:${to}`);
|
||||
},
|
||||
notFound: () => {
|
||||
throw new Error("NOT_FOUND");
|
||||
},
|
||||
}));
|
||||
|
||||
import { heartbeat, leave } from "@/actions/presence";
|
||||
import { PRESENCE_TTL_MS, editingNow, leavePresence, parsePresenceKind, touchPresence, whoIsEditing } from "@/lib/presence";
|
||||
|
||||
describeDb("vem som har något öppet", () => {
|
||||
let owner: { id: string };
|
||||
let editor: { id: string };
|
||||
let viewer: { id: string };
|
||||
let bookId: string;
|
||||
let anna: { id: string };
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetAll();
|
||||
owner = await makeUser("Ägare");
|
||||
editor = await makeUser("Kollega", "kollega@example.com");
|
||||
viewer = await makeUser("Läsare");
|
||||
bookId = (await makeBook(owner.id)).id;
|
||||
await prisma.bookMember.createMany({
|
||||
data: [
|
||||
{ bookId, userId: editor.id, role: "EDITOR" },
|
||||
{ bookId, userId: viewer.id, role: "VIEWER" },
|
||||
],
|
||||
});
|
||||
anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
||||
login.userId = owner.id;
|
||||
});
|
||||
|
||||
it("parsePresenceKind godtar bara kända typer", () => {
|
||||
for (const kind of ["entity", "relation", "event", "template", "book"]) expect(parsePresenceKind(kind)).toBe(kind);
|
||||
expect(parsePresenceKind("marker")).toBeNull();
|
||||
expect(parsePresenceKind(undefined)).toBeNull();
|
||||
});
|
||||
|
||||
describe("biblioteket", () => {
|
||||
it("registrerar och visar andra, men inte en själv", async () => {
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
await touchPresence(bookId, owner.id, "entity", anna.id);
|
||||
expect(await whoIsEditing(bookId, "entity", anna.id, owner.id)).toEqual(["Kollega"]);
|
||||
expect((await whoIsEditing(bookId, "entity", anna.id)).sort()).toEqual(["Kollega", "Ägare"]);
|
||||
expect(await whoIsEditing(bookId, "entity", "annan-post", owner.id)).toEqual([]);
|
||||
expect(await whoIsEditing(bookId, "relation", anna.id, owner.id)).toEqual([]);
|
||||
});
|
||||
|
||||
it("en rad per person och sak, som uppdateras", async () => {
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
const first = await prisma.presence.findFirstOrThrow();
|
||||
await new Promise((r) => setTimeout(r, 15));
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
expect(await prisma.presence.count()).toBe(1);
|
||||
expect((await prisma.presence.findFirstOrThrow()).seenAt.getTime()).toBeGreaterThan(first.seenAt.getTime());
|
||||
});
|
||||
|
||||
it("den som inte hörts av på en stund räknas inte längre, och gamla rader städas bort", async () => {
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
await prisma.presence.updateMany({ data: { seenAt: new Date(Date.now() - PRESENCE_TTL_MS - 1000) } });
|
||||
expect(await whoIsEditing(bookId, "entity", anna.id, owner.id)).toEqual([]);
|
||||
expect(await prisma.presence.count()).toBe(1);
|
||||
await prisma.presence.updateMany({ data: { seenAt: new Date(Date.now() - 11 * 60_000) } });
|
||||
await touchPresence(bookId, owner.id, "entity", anna.id);
|
||||
expect(await prisma.presence.count()).toBe(1); // bara den nya finns kvar
|
||||
});
|
||||
|
||||
it("lämna tar bort närvaron", async () => {
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
await leavePresence(editor.id, "entity", anna.id);
|
||||
expect(await whoIsEditing(bookId, "entity", anna.id, owner.id)).toEqual([]);
|
||||
});
|
||||
|
||||
it("visar e-postens första del när namn saknas, och samma person bara en gång", async () => {
|
||||
const nameless = await makeUser("X", "nameless@example.com");
|
||||
await prisma.user.update({ where: { id: nameless.id }, data: { name: null } });
|
||||
await touchPresence(bookId, nameless.id, "entity", anna.id);
|
||||
expect(await whoIsEditing(bookId, "entity", anna.id, owner.id)).toEqual(["nameless"]);
|
||||
});
|
||||
|
||||
it("editingNow ger poster med namn för märken i listor", async () => {
|
||||
const bo = await makeEntity(bookId, "CHARACTER", "Bo");
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
await touchPresence(bookId, viewer.id, "entity", anna.id);
|
||||
await touchPresence(bookId, editor.id, "entity", bo.id);
|
||||
await touchPresence(bookId, owner.id, "entity", bo.id);
|
||||
await touchPresence(bookId, editor.id, "relation", "r1");
|
||||
const map = await editingNow(bookId, "entity", owner.id);
|
||||
expect([...map.keys()].sort()).toEqual([anna.id, bo.id].sort());
|
||||
expect(map.get(anna.id)?.sort()).toEqual(["Kollega", "Läsare"]);
|
||||
expect(map.get(bo.id)).toEqual(["Kollega"]);
|
||||
});
|
||||
|
||||
it("hör till boken: försvinner med boken, och syns inte i en annan bok", async () => {
|
||||
const otherBook = await makeBook(owner.id, "Annan");
|
||||
await touchPresence(bookId, editor.id, "entity", anna.id);
|
||||
expect(await whoIsEditing(otherBook.id, "entity", anna.id)).toEqual([]);
|
||||
await prisma.book.delete({ where: { id: bookId } });
|
||||
expect(await prisma.presence.count()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("åtgärden heartbeat", () => {
|
||||
it("registrerar och svarar med vilka andra som har samma sak öppen", async () => {
|
||||
login.userId = editor.id;
|
||||
expect(await heartbeat(bookId, "entity", anna.id)).toEqual([]);
|
||||
login.userId = owner.id;
|
||||
expect(await heartbeat(bookId, "entity", anna.id)).toEqual(["Kollega"]);
|
||||
login.userId = editor.id;
|
||||
expect(await heartbeat(bookId, "entity", anna.id)).toEqual(["Ägare"]);
|
||||
});
|
||||
|
||||
it("fungerar för relationer, händelser, mallar och bokinställningar", async () => {
|
||||
const bo = await makeEntity(bookId, "CHARACTER", "Bo");
|
||||
const relation = await makeRelation(bookId, anna.id, bo.id);
|
||||
const event = await makeEvent(bookId, "Fest", "2000");
|
||||
login.userId = editor.id;
|
||||
await heartbeat(bookId, "relation", relation.id);
|
||||
await heartbeat(bookId, "event", event.id);
|
||||
await heartbeat(bookId, "template", "CHARACTER");
|
||||
await heartbeat(bookId, "book", bookId);
|
||||
expect((await prisma.presence.findMany()).map((p) => p.kind).sort()).toEqual(["book", "event", "relation", "template"]);
|
||||
});
|
||||
|
||||
it("avvisar okända typer, saker som inte finns och saker i en annan bok", async () => {
|
||||
const otherBook = await makeBook(owner.id, "Annan");
|
||||
const foreign = await makeEntity(otherBook.id, "CHARACTER", "Främling");
|
||||
login.userId = editor.id;
|
||||
expect(await heartbeat(bookId, "marker", anna.id)).toEqual([]);
|
||||
expect(await heartbeat(bookId, "entity", "finns-inte")).toEqual([]);
|
||||
expect(await heartbeat(bookId, "entity", foreign.id)).toEqual([]);
|
||||
expect(await heartbeat(bookId, "template", "INTE_EN_TYP")).toEqual([]);
|
||||
expect(await heartbeat(bookId, "book", otherBook.id)).toEqual([]);
|
||||
expect(await prisma.presence.count()).toBe(0);
|
||||
});
|
||||
|
||||
it("kräver redaktör: läsare och obehöriga får 404 och registreras inte", async () => {
|
||||
const stranger = await makeUser("Obehörig");
|
||||
for (const user of [viewer, stranger]) {
|
||||
login.userId = user.id;
|
||||
await expect(heartbeat(bookId, "entity", anna.id)).rejects.toThrow("NOT_FOUND");
|
||||
await expect(leave(bookId, "entity", anna.id)).rejects.toThrow("NOT_FOUND");
|
||||
}
|
||||
expect(await prisma.presence.count()).toBe(0);
|
||||
});
|
||||
|
||||
it("har en gräns för hur ofta en användare kan anmäla sig: över gränsen registreras inget mer", async () => {
|
||||
login.userId = editor.id;
|
||||
for (let i = 0; i < 60; i++) await heartbeat(bookId, "entity", anna.id);
|
||||
await prisma.presence.deleteMany();
|
||||
// Det 61:a anropet inom en minut ignoreras
|
||||
expect(await heartbeat(bookId, "entity", anna.id)).toEqual([]);
|
||||
expect(await prisma.presence.count()).toBe(0);
|
||||
});
|
||||
|
||||
it("lämna tar bort ens egen närvaro men inte andras", async () => {
|
||||
login.userId = editor.id;
|
||||
await heartbeat(bookId, "entity", anna.id);
|
||||
login.userId = owner.id;
|
||||
await heartbeat(bookId, "entity", anna.id);
|
||||
await leave(bookId, "entity", anna.id);
|
||||
expect((await prisma.presence.findMany()).map((p) => p.userId)).toEqual([editor.id]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user