backup.test.ts

1.9 KB · raw

1import { describe, expect, it } from "vitest";23import {4  exportEntriesToPeachJson,5  importEntriesFromPeachJson,6  type PeachExportEnvelope,7} from "./backup";89describe("shared backup", () => {10  const sampleEntries = [11    {12      id: "login-1",13      type: "login" as const,14      title: "GitHub",15      username: "june",16      password: "secret123",17      url: "https://github.com",18      notes: "2fa enabled",19      createdAt: 1,20      modifiedAt: 1,21    },22  ];2324  it("importEntriesFromPeachJson decrypts entries with the correct password", async () => {25    const envelope = await exportEntriesToPeachJson(sampleEntries, "export-pass");2627    await expect(28      importEntriesFromPeachJson(JSON.stringify(envelope), "export-pass")29    ).resolves.toMatchObject([30      {31        type: "login",32        title: "GitHub",33        username: "june",34        password: "secret123",35        url: "https://github.com",36      },37    ]);38  });3940  it("importEntriesFromPeachJson rejects the wrong password", async () => {41    const envelope = await exportEntriesToPeachJson(sampleEntries, "export-pass");4243    await expect(44      importEntriesFromPeachJson(JSON.stringify(envelope), "wrong-pass")45    ).rejects.toThrow("INVALID_EXPORT_PASSWORD");46  });4748  it("importEntriesFromPeachJson rejects malformed JSON", async () => {49    await expect(importEntriesFromPeachJson("{not json", "export-pass")).rejects.toThrow(50      "INVALID_PEACH_EXPORT"51    );52  });5354  it("importEntriesFromPeachJson rejects envelopes with missing required fields", async () => {55    const envelope: Partial<PeachExportEnvelope> = {56      version: 2,57      type: "peach-export",58      kdf: "PBKDF2-SHA256",59      salt: "00",60      nonce: "00",61      ciphertext: "00",62    };6364    await expect(65      importEntriesFromPeachJson(JSON.stringify(envelope), "export-pass")66    ).rejects.toThrow("INVALID_PEACH_EXPORT");67  });68});