tests: vaults-2 (#2368)

This commit is contained in:
Mo
2023-07-26 04:55:58 -05:00
committed by GitHub
parent 6ad5d028af
commit 7222ca7fd0
47 changed files with 900 additions and 310 deletions

View File

@@ -14,14 +14,17 @@ export interface StorageServiceInterface {
getAllKeys(mode?: StorageValueModes): string[]
getValue<T>(key: string, mode?: StorageValueModes, defaultValue?: T): T
canDecryptWithKey(key: RootKeyInterface): Promise<boolean>
savePayload(payload: PayloadInterface): Promise<void>
savePayloads(decryptedPayloads: PayloadInterface[]): Promise<void>
setValue<T>(key: string, value: T, mode?: StorageValueModes): void
removeValue(key: string, mode?: StorageValueModes): Promise<void>
setPersistencePolicy(persistencePolicy: StoragePersistencePolicies): Promise<void>
clearAllData(): Promise<void>
getRawPayloads(uuids: string[]): Promise<FullyFormedTransferPayload[]>
savePayload(payload: PayloadInterface): Promise<void>
savePayloads(decryptedPayloads: PayloadInterface[]): Promise<void>
deletePayloads(payloads: FullyFormedPayloadInterface[]): Promise<void>
deletePayloadsWithUuids(uuids: string[]): Promise<void>
clearAllPayloads(): Promise<void>
isEphemeralSession(): boolean
}

View File

@@ -0,0 +1,48 @@
import { RemoveItemsFromMemory } from './RemoveItemsFromMemory'
import { ItemManagerInterface } from '../../Item/ItemManagerInterface'
import { StorageServiceInterface } from '../StorageServiceInterface'
import { PayloadManagerInterface } from '../../Payloads/PayloadManagerInterface'
import { PayloadEmitSource, DecryptedItemInterface } from '@standardnotes/models'
import { Uuids } from '@standardnotes/utils'
describe('RemoveItemsFromMemory', () => {
let storage: StorageServiceInterface
let items: ItemManagerInterface
let payloads: PayloadManagerInterface
let removeItemsFromMemory: RemoveItemsFromMemory
beforeEach(() => {
storage = {
getRawPayloads: jest.fn().mockImplementation(() => Promise.resolve([])),
} as unknown as StorageServiceInterface
items = {
removeItemsFromMemory: jest.fn(),
} as unknown as ItemManagerInterface
payloads = {
emitPayloads: jest.fn().mockImplementation(() => Promise.resolve()),
} as unknown as PayloadManagerInterface
removeItemsFromMemory = new RemoveItemsFromMemory(storage, items, payloads)
})
it('should execute removeItemsFromMemory use case correctly', async () => {
const testItems: DecryptedItemInterface[] = [
<DecryptedItemInterface>{
uuid: 'uuid1',
content_type: 'type1',
},
<DecryptedItemInterface>{
uuid: 'uuid2',
content_type: 'type2',
},
]
await removeItemsFromMemory.execute(testItems)
expect(items.removeItemsFromMemory).toHaveBeenCalledWith(testItems)
expect(storage.getRawPayloads).toHaveBeenCalledWith(Uuids(testItems))
expect(payloads.emitPayloads).toHaveBeenCalledWith([], PayloadEmitSource.LocalDatabaseLoaded)
})
})

View File

@@ -0,0 +1,26 @@
import { ItemManagerInterface } from '../../Item/ItemManagerInterface'
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
import { StorageServiceInterface } from '../StorageServiceInterface'
import { CreatePayload, DecryptedItemInterface, PayloadEmitSource, PayloadSource } from '@standardnotes/models'
import { Uuids } from '@standardnotes/utils'
import { PayloadManagerInterface } from '../../Payloads/PayloadManagerInterface'
export class RemoveItemsFromMemory implements UseCaseInterface<void> {
constructor(
private storage: StorageServiceInterface,
private items: ItemManagerInterface,
private payloads: PayloadManagerInterface,
) {}
async execute(items: DecryptedItemInterface[]): Promise<Result<void>> {
this.items.removeItemsFromMemory(items)
const rawPayloads = await this.storage.getRawPayloads(Uuids(items))
const encryptedPayloads = rawPayloads.map((payload) => CreatePayload(payload, PayloadSource.LocalDatabaseLoaded))
await this.payloads.emitPayloads(encryptedPayloads, PayloadEmitSource.LocalDatabaseLoaded)
return Result.ok()
}
}