Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / lib / items / item-proto.transformer.spec.ts
blob99886cd4f6749f33cfe7fa5beb9f83a85c789746
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>;
11     }
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 = {
21             metadata: {
22                 name: itemName,
23                 note: noteContents,
24                 itemUuid: String(Math.random()),
25             },
26             content: {
27                 content: {
28                     oneofKind: 'note',
29                     note: {},
30                 },
31             },
32             extraFields: [],
33         };
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);
43     });
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 = {
52             metadata: {
53                 name: itemName,
54                 note: '',
55                 itemUuid: String(Math.random()),
56             },
57             content: {
58                 content: {
59                     oneofKind: 'login',
60                     login: {
61                         itemEmail,
62                         itemUsername,
63                         password: itemPassword,
64                         urls: [],
65                         totpUri: '',
66                         passkeys: [],
67                     },
68                 },
69             },
70             extraFields: [],
71         };
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);
83     });
84 });