import { describe, expect, it } from "vitest"; import { exportEntriesToPeachJson, importEntriesFromPeachJson, type PeachExportEnvelope, } from "./backup"; describe("shared backup", () => { const sampleEntries = [ { id: "login-1", type: "login" as const, title: "GitHub", username: "june", password: "secret123", url: "https://github.com", notes: "2fa enabled", createdAt: 1, modifiedAt: 1, }, ]; it("importEntriesFromPeachJson decrypts entries with the correct password", async () => { const envelope = await exportEntriesToPeachJson(sampleEntries, "export-pass"); await expect( importEntriesFromPeachJson(JSON.stringify(envelope), "export-pass") ).resolves.toMatchObject([ { type: "login", title: "GitHub", username: "june", password: "secret123", url: "https://github.com", }, ]); }); it("importEntriesFromPeachJson rejects the wrong password", async () => { const envelope = await exportEntriesToPeachJson(sampleEntries, "export-pass"); await expect( importEntriesFromPeachJson(JSON.stringify(envelope), "wrong-pass") ).rejects.toThrow("INVALID_EXPORT_PASSWORD"); }); it("importEntriesFromPeachJson rejects malformed JSON", async () => { await expect(importEntriesFromPeachJson("{not json", "export-pass")).rejects.toThrow( "INVALID_PEACH_EXPORT" ); }); it("importEntriesFromPeachJson rejects envelopes with missing required fields", async () => { const envelope: Partial = { version: 2, type: "peach-export", kdf: "PBKDF2-SHA256", salt: "00", nonce: "00", ciphertext: "00", }; await expect( importEntriesFromPeachJson(JSON.stringify(envelope), "export-pass") ).rejects.toThrow("INVALID_PEACH_EXPORT"); }); });