Skip to content

Commit

Permalink
add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsojko committed Feb 2, 2024
1 parent c7117cc commit 81101d9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MfaServiceInterface,
GenerateUuid,
CreateDecryptedBackupFile,
SyncBackoffServiceInterface,
} from '@standardnotes/services'
import { VaultLockServiceInterface } from './../VaultLock/VaultLockServiceInterface'
import { HistoryServiceInterface } from './../History/HistoryServiceInterface'
Expand Down Expand Up @@ -100,6 +101,7 @@ export interface ApplicationInterface {
get storage(): StorageServiceInterface
get subscriptions(): SubscriptionManagerInterface
get sync(): SyncServiceInterface
get syncBackoff(): SyncBackoffServiceInterface
get user(): UserServiceInterface
get vaultInvites(): VaultInviteServiceInterface
get vaultLocks(): VaultLockServiceInterface
Expand Down
5 changes: 5 additions & 0 deletions packages/snjs/lib/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
CreateDecryptedBackupFile,
CreateEncryptedBackupFile,
WebSocketsService,
SyncBackoffServiceInterface,
} from '@standardnotes/services'
import {
SNNote,
Expand Down Expand Up @@ -1016,6 +1017,10 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
return this.dependencies.get<SyncServiceInterface>(TYPES.SyncService)
}

public get syncBackoff(): SyncBackoffServiceInterface {
return this.dependencies.get<SyncBackoffServiceInterface>(TYPES.SyncBackoffService)
}

public get user(): UserServiceInterface {
return this.dependencies.get<UserServiceInterface>(TYPES.UserService)
}
Expand Down
8 changes: 8 additions & 0 deletions packages/snjs/mocha/lib/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,14 @@ export class AppContext {
return note
}

async createUnsyncedNote(title = 'foo', text = 'bar') {
const payload = createNotePayload(title, text)
const item = await this.application.mutator.emitItemFromPayload(payload, PayloadEmitSource.LocalChanged)
await this.application.mutator.setItemDirty(item)

return item
}

lockSyncing() {
this.application.sync.lockSyncing()
}
Expand Down
63 changes: 63 additions & 0 deletions packages/snjs/mocha/sync_tests/online.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,69 @@ describe('online syncing', function () {
await secondContext.deinit()
}).timeout(Factory.SixtySecondTimeout)

it('should eventually sync notes that are backed off for a penalty time because of creating too large of a payload', async function () {
const response = await fetch('/mocha/assets/small_file.md')
const buffer = new Uint8Array(await response.arrayBuffer())
const contents = buffer.toString()
const numberOfNotesToExceedThe1MBPayloadLimit = Math.ceil(100_000 / buffer.length) + 1

expect(application.items.getDirtyItems().length).to.equal(0)

for (let i = 0; i < numberOfNotesToExceedThe1MBPayloadLimit; i++) {
await context.createUnsyncedNote(`note ${i}`, contents)
expectedItemCount++
}
await context.sync()

expect(application.items.getDirtyItems().length).to.equal(numberOfNotesToExceedThe1MBPayloadLimit)

while (application.items.getDirtyItems().length > 0) {
application.items.getDirtyItems().length

await context.sync()

await Factory.sleep(1)
}

expect(application.items.getDirtyItems().length).to.equal(0)
}).timeout(Factory.TwentySecondTimeout)

it('should not sync notes that solely create a too large payload and are backed off for a penalty time', async function () {
const response = await fetch('/mocha/assets/small_file.md')
const buffer = new Uint8Array(await response.arrayBuffer())
const contents = buffer.toString()
const numberOfNotesToExceedThe1MBPayloadLimit = Math.ceil(100_000 / buffer.length) + 1

expect(application.items.getDirtyItems().length).to.equal(0)

let hugeContents = ''
for (let i = 0; i < numberOfNotesToExceedThe1MBPayloadLimit; i++) {
hugeContents += contents
}
const hugeNote = await context.createUnsyncedNote(`Huge note that never should be synced`, hugeContents)
expectedItemCount++
const smallNote = await context.createUnsyncedNote(`Small note that should be synced`, 'Small note that should be synced')
expectedItemCount++

await context.sync()

expect(application.items.getDirtyItems().length).to.equal(2)

while (application.items.getDirtyItems().length > 1) {
await Factory.sleep(1)

application.items.getDirtyItems().length

await context.sync()
}

expect(application.syncBackoff.isItemInBackoff(smallNote)).to.equal(false)
expect(application.syncBackoff.isItemInBackoff(hugeNote)).to.equal(true)

const hugeNoteIsNotConsideredForAPotentialCandidateForNextSync = application.syncBackoff.getSmallerSubsetOfItemUuidsInBackoff().length === 0
expect(hugeNoteIsNotConsideredForAPotentialCandidateForNextSync).to.equal(true)
}).timeout(Factory.TwentySecondTimeout)

it('syncing an item should storage it encrypted', async function () {
const note = await Factory.createMappedNote(application)
await application.mutator.setItemDirty(note)
Expand Down

0 comments on commit 81101d9

Please sign in to comment.