import assert from "node:assert/strict"; import test from "node:test"; import { accountKindLabel, accountState, personRoomsText, writableRooms, parseShareDrag, shareTargets, exposureSummary, isPrivateRoom, roomRightsText, inviteLink, lastManagerIds, parseInviteHash, passwordProblem, roleLabel, } from "./admin-core.js"; test("room roles have a German label and an unknown role is not guessed", () => { assert.equal(roleLabel("reader"), "Leser"); assert.equal(roleLabel("manager"), "Verwalter"); assert.equal(roleLabel("owner"), "Eigener Raum"); assert.equal(roleLabel("admin"), "Unbekannt"); }); test("an account is shown as disabled, open for a password change, or active", () => { assert.equal(accountState({ status: "disabled", must_change_password: true }).key, "disabled"); assert.equal(accountState({ status: "active", must_change_password: true }).key, "pending"); assert.equal(accountState({ status: "active", must_change_password: false }).key, "active"); }); test("password rules match the server", () => { assert.match(passwordProblem("kurz"), /mindestens 10/); assert.match(passwordProblem("a".repeat(73)), /zu lang/); assert.match(passwordProblem("ä".repeat(40)), /zu lang/); assert.match(passwordProblem("lang-genug-123", "anders-lang-123"), /nicht gleich/); assert.match(passwordProblem("gleich-bleibend-1", null, "gleich-bleibend-1"), /unterscheiden/); assert.equal(passwordProblem("lang-genug-123", "lang-genug-123", "alt-passwort-1"), ""); }); test("the invitation token stays in the fragment", () => { const link = inviteLink("https://tsumugu.example", { id: "abc", token: "t".repeat(30) }); assert.equal(link, `https://tsumugu.example/invite/abc#${"t".repeat(30)}`); assert.equal(new URL(link).search, ""); assert.equal(parseInviteHash(`#${"t".repeat(30)}`), "t".repeat(30)); assert.equal(parseInviteHash("#kurz"), ""); assert.equal(parseInviteHash(""), ""); }); test("only a single active manager is protected", () => { const members = [ { subject_id: "a", role: "manager", account_status: "active" }, { subject_id: "b", role: "member", account_status: "active" }, ]; assert.deepEqual([...lastManagerIds(members)], ["a"]); assert.equal(lastManagerIds([...members, { subject_id: "c", role: "manager", account_status: "active" }]).size, 0); assert.equal(lastManagerIds([{ subject_id: "a", role: "manager", account_status: "disabled" }]).size, 0); }); test("a room is private until another member joins", () => { assert.equal(isPrivateRoom({ kind: "organization", private: true }), true); assert.equal(isPrivateRoom({ kind: "organization", private: false }), false); assert.equal(isPrivateRoom({ kind: "personal", private: true }), false); assert.equal(isPrivateRoom(null), false); }); test("the room share is described by what the role may do", () => { assert.equal(roomRightsText({ read: true, edit: true }), "liest und bearbeitet"); assert.equal(roomRightsText({ read: true, edit: false }), "liest"); assert.equal(roomRightsText({ read: false, edit: false }), "kein Zugriff"); assert.equal(roomRightsText(undefined), "kein Zugriff"); }); test("the exposure summary counts only what is shared with the room", () => { assert.deepEqual(exposureSummary({ cases: { room: 2, private: 5 }, matters: { room: 1, private: 0 } }), { cases: 2, matters: 1, total: 3, }); assert.equal(exposureSummary(null).total, 0); }); test("a drag payload is accepted only for the matching kind", () => { const text = JSON.stringify({ kind: "case", id: "abc", title: "Akte" }); assert.equal(parseShareDrag(text, "case").id, "abc"); assert.equal(parseShareDrag(text, "matter"), null); assert.equal(parseShareDrag("kein json", "case"), null); assert.equal(parseShareDrag("", "case"), null); assert.equal(parseShareDrag(JSON.stringify({ kind: "case", id: "" }), "case"), null); }); test("only rooms where the person writes are drop targets", () => { const rooms = [ { id: "p", kind: "personal", role: "owner" }, { id: "a", kind: "organization", role: "manager" }, { id: "b", kind: "organization", role: "member" }, { id: "c", kind: "organization", role: "reader" }, ]; assert.deepEqual(shareTargets(rooms).map((room) => room.id), ["a", "b"]); assert.deepEqual(shareTargets(undefined), []); }); test("account kinds have German labels", () => { assert.equal(accountKindLabel("employee"), "Mitarbeiter"); assert.equal(accountKindLabel("main"), "Hauptnutzer"); assert.equal(accountKindLabel(undefined), "Hauptnutzer"); }); test("an employee has no personal workspace to create entries in", () => { const rooms = [ { id: "p", kind: "personal", role: "owner" }, { id: "a", kind: "organization", role: "member" }, { id: "b", kind: "organization", role: "reader" }, ]; assert.deepEqual(writableRooms({ account: { account_kind: "main" }, rooms }).map((r) => r.id), ["p", "a"]); assert.deepEqual(writableRooms({ account: { account_kind: "employee" }, rooms }).map((r) => r.id), ["a"]); assert.deepEqual(writableRooms(null), []); }); test("the rooms of a person are listed with their roles", () => { assert.equal(personRoomsText([]), "in keinem deiner Räume"); assert.equal(personRoomsText([{ name: "Kanzlei", role: "reader" }, { name: "Büro", role: "member" }]), "Kanzlei (Leser), Büro (Mitglied)"); });