Initial commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { KeePassDatabase } from "../../src/keepass";
|
||||
|
||||
const spawnMock = mock(() => {
|
||||
throw new Error("spawn should be mocked per test");
|
||||
});
|
||||
|
||||
mock.module("node:child_process", () => ({
|
||||
spawn: spawnMock,
|
||||
}));
|
||||
|
||||
describe("KeePassDatabase", () => {
|
||||
test("listEntries parses successful bridge response", async () => {
|
||||
spawnMock.mockImplementation(() => {
|
||||
const listeners: Record<string, ((chunk: Buffer | string) => void)[]> = {};
|
||||
const child = {
|
||||
stdin: {
|
||||
write: () => undefined,
|
||||
end: () => {
|
||||
listeners.close?.forEach((cb) => cb(Buffer.from("")));
|
||||
},
|
||||
},
|
||||
stdout: {
|
||||
on: (event: string, cb: (chunk: Buffer | string) => void) => {
|
||||
listeners[event] ??= [];
|
||||
listeners[event].push(cb);
|
||||
if (event === "data") {
|
||||
cb(JSON.stringify({ ok: true, data: [{ title: "Entry" }] }));
|
||||
}
|
||||
},
|
||||
},
|
||||
stderr: { on: () => undefined },
|
||||
on: (event: string, cb: (code?: number | null) => void) => {
|
||||
if (event === "close") queueMicrotask(() => cb(0));
|
||||
},
|
||||
};
|
||||
return child as never;
|
||||
});
|
||||
|
||||
const db = new KeePassDatabase("db.kdbx", { password: "secret" }, "python3", new URL("file:///tmp/bridge.py"));
|
||||
const entries = await db.listEntries();
|
||||
|
||||
expect(entries).toEqual([{ title: "Entry" }]);
|
||||
expect(spawnMock).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("throws on bridge error payload", async () => {
|
||||
spawnMock.mockImplementation(() => {
|
||||
const child = {
|
||||
stdin: { write: () => undefined, end: () => undefined },
|
||||
stdout: { on: (_event: string, cb: (chunk: Buffer | string) => void) => cb('{"ok":false,"error":"boom"}') },
|
||||
stderr: { on: () => undefined },
|
||||
on: (event: string, cb: (code?: number | null) => void) => {
|
||||
if (event === "close") queueMicrotask(() => cb(1));
|
||||
},
|
||||
};
|
||||
return child as never;
|
||||
});
|
||||
|
||||
const db = new KeePassDatabase("db.kdbx", { password: "secret" }, "python3", new URL("file:///tmp/bridge.py"));
|
||||
await expect(db.listEntries()).rejects.toThrow("boom");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user