import {beforeAll, expect, test} from 'bun:test' import {misc, PwdBox} from '..' let k1!: string let k2!: string let message!: Uint8Array beforeAll(async () => { k1 = 'abc' k2 = 'def' message = misc.payload_fromString('Salut ! ça va ?') }) test('base case', async () => { const box = await PwdBox.encrypt(message, k1) expect(box).not.toBeNull() const unboxed = await box!.decrypt(k1) expect(unboxed).toEqual(message) }) test("Different key can't decrypt", async () => { const box = await PwdBox.encrypt(message, k1) expect(box).not.toBeNull() const unboxed = await box!.decrypt(k2) expect(unboxed).toBeNull() }) test("Encryption works with context", async () => { const c1 = 'context1' const box = await PwdBox.encrypt(message, k1, c1) expect(box).not.toBeNull() const unboxed = await box!.decrypt(k1, c1) expect(unboxed).not.toBeNull() }) test("Encryption doesn't work with different context", async () => { const c1 = 'context1' const c2 = 'context2' const box = await PwdBox.encrypt(message, k1, c1) expect(box).not.toBeNull() const unboxed = await box!.decrypt(k1, c2) expect(unboxed).toBeNull() }) test('String serialization', async () => { const box = await PwdBox.encrypt(message, k1) expect(box).not.toBeNull() const ser = box!.toString() const de = PwdBox.fromString(ser) expect(de).not.toBeNull() expect(de).toEqual(box) const unboxed = await de!.decrypt(k1) expect(unboxed).not.toBeNull() expect(unboxed).toEqual(message) }) test('Byte serialization', async () => { const box = await PwdBox.encrypt(message, k1) expect(box).not.toBeNull() const ser = box!.toBytes() const de = PwdBox.fromBytes(ser) expect(de).not.toBeNull() expect(de).toEqual(box) const unboxed = await de!.decrypt(k1) expect(unboxed).not.toBeNull() expect(unboxed).toEqual(message) })