1 import type { ItemType, SafeProtobufItem } from '@proton/pass/types';
3 import { decodeItemContent, encodeItemContent } from './item-proto.transformer';
5 function checkAndCast<T extends ItemType>(input: SafeProtobufItem, expectedType: T): SafeProtobufItem<T> {
6 const { content } = input.content;
7 const type = content.oneofKind;
9 if (type === expectedType) {
10 return input as SafeProtobufItem<any>;
13 throw new Error(`oneofKind did not match [input:${type}] [expected:${expectedType}]`);
16 describe('ItemContentTransformer', () => {
17 it('should be able to encode and decode a Note', () => {
18 const itemName = 'Item' + Math.random();
19 const noteContents = 'Contents' + Math.random();
20 const sourceItem: SafeProtobufItem = {
24 itemUuid: String(Math.random()),
35 const encoded = encodeItemContent(sourceItem);
36 expect(encoded.length).toBeGreaterThan(0);
38 const decoded = decodeItemContent(encoded);
39 expect(decoded.metadata.name).toStrictEqual(itemName);
41 const note = checkAndCast(decoded, 'note');
42 expect(note.metadata.note).toStrictEqual(noteContents);
45 it('should be able to encode and decode a Login', () => {
46 const itemName = 'Item' + Math.random();
47 const itemEmail = 'Email' + Math.random();
48 const itemUsername = 'Username' + Math.random();
49 const itemPassword = 'Password' + Math.random();
51 const sourceItem: SafeProtobufItem = {
55 itemUuid: String(Math.random()),
63 password: itemPassword,
73 const encoded = encodeItemContent(sourceItem);
74 expect(encoded.length).toBeGreaterThan(0);
76 const decoded = decodeItemContent(encoded);
77 expect(decoded.metadata.name).toStrictEqual(itemName);
79 const login = checkAndCast(decoded, 'login');
80 expect(login.content.content.login.itemEmail).toStrictEqual(itemEmail);
81 expect(login.content.content.login.itemUsername).toStrictEqual(itemUsername);
82 expect(login.content.content.login.password).toStrictEqual(itemPassword);