import { describe, expect, test } from "bun:test"; import { KeePassDatabase, openKeePassDatabase } from "../../src/keepass"; describe("KeePassDatabase", () => { test("starts with an empty root group", async () => { const db = new KeePassDatabase("db.kdbx", { password: "secret" }); expect(await db.listGroups()).toEqual([{ name: "Racine", path: "" }]); expect(await db.listEntries()).toEqual([]); }); test("creates entries in memory", async () => { const db = new KeePassDatabase("db.kdbx", { password: "secret" }); const created = await db.createEntry({ title: "Entry", username: "user", password: "pass", groupPath: "Folder", }); expect(created).toEqual({ title: "Entry", username: "user", password: "pass", url: "", notes: "", groupPath: "Folder", }); expect(await db.listEntries()).toEqual([created]); }); test("creates groups in memory", async () => { const db = new KeePassDatabase("db.kdbx", { password: "secret" }); const created = await db.createGroup({ name: "Folder1" }); expect(created).toEqual({ name: "Folder1", path: "Folder1" }); expect(await db.listGroups()).toEqual([ { name: "Racine", path: "" }, { name: "Folder1", path: "Folder1" }, ]); }); test("findEntries performs partial matching", async () => { const db = new KeePassDatabase("db.kdbx", { password: "secret" }); await db.createEntry({ title: "Mail", username: "alice", password: "pass", url: "https://example.com", groupPath: "Folder1/SubFolder", }); expect(await db.findEntries({ title: "mai" })).toHaveLength(1); expect(await db.findEntries({ username: "ALI" })).toHaveLength(1); expect(await db.findEntries({ url: "example" })).toHaveLength(1); expect(await db.findEntries({ groupPath: "Folder1" })).toHaveLength(1); expect(await db.findEntries({ title: "missing" })).toEqual([]); }); test("save clears the dirty flag without throwing", async () => { const db = new KeePassDatabase("db.kdbx", { password: "secret" }); await db.createEntry({ title: "Entry" }); await expect(db.save()).resolves.toBeUndefined(); }); test("entry and group collections are cloned on read", async () => { const db = new KeePassDatabase("db.kdbx", { password: "secret" }); await db.createEntry({ title: "Entry" }); await db.createGroup({ name: "Folder1" }); const entries = await db.listEntries(); const groups = await db.listGroups(); entries.push({ title: "X", username: "", password: "", url: "", notes: "" }); groups.push({ name: "X", path: "X" }); expect(await db.listEntries()).toHaveLength(1); expect(await db.listGroups()).toHaveLength(2); }); test("openKeePassDatabase returns a KeePassDatabase instance", () => { const db = openKeePassDatabase("db.kdbx", { password: "secret" }); expect(db).toBeInstanceOf(KeePassDatabase); }); });