refactor: application dependency management (#2363)

This commit is contained in:
Mo
2023-07-23 15:54:31 -05:00
committed by GitHub
parent e698b1c990
commit a77535456c
299 changed files with 7415 additions and 4890 deletions

View File

@@ -24,8 +24,8 @@ describe('singletons', function () {
return new DecryptedPayload(params)
}
function findOrCreatePrefsSingleton(application) {
return application.singletonManager.findOrCreateContentTypeSingleton(ContentType.TYPES.UserPrefs, FillItemContent({}))
function findOrCreatePrefsSingleton(context) {
return context.singletons.findOrCreateContentTypeSingleton(ContentType.TYPES.UserPrefs, FillItemContent({}))
}
beforeEach(async function () {
@@ -49,7 +49,7 @@ describe('singletons', function () {
}
this.signOut = async () => {
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
this.application = await this.context.signout()
}
this.signIn = async () => {
@@ -78,10 +78,10 @@ describe('singletons', function () {
})
afterEach(async function () {
expect(this.application.syncService.isOutOfSync()).to.equal(false)
expect(this.application.itemManager.items.length).to.equal(this.expectedItemCount)
expect(this.application.sync.isOutOfSync()).to.equal(false)
expect(this.application.items.items.length).to.equal(this.expectedItemCount)
const rawPayloads = await this.application.diskStorageService.getAllRawPayloads()
const rawPayloads = await this.application.storage.getAllRawPayloads()
expect(rawPayloads.length).to.equal(this.expectedItemCount)
await Factory.safeDeinit(this.application)
@@ -100,8 +100,8 @@ describe('singletons', function () {
PayloadEmitSource.LocalChanged,
)
await this.application.mutator.setItemsDirty(items)
await this.application.syncService.sync(syncOptions)
expect(this.application.itemManager.items.length).to.equal(this.expectedItemCount)
await this.application.sync.sync(syncOptions)
expect(this.application.items.items.length).to.equal(this.expectedItemCount)
})
it('duplicate components should auto-resolve to 1', async function () {
@@ -121,30 +121,30 @@ describe('singletons', function () {
await this.application.sync.sync(syncOptions)
expect(this.application.itemManager.itemsMatchingPredicate(ContentType.TYPES.Component, this.extPred).length).to.equal(1)
expect(this.application.items.itemsMatchingPredicate(ContentType.TYPES.Component, this.extPred).length).to.equal(1)
})
it('resolves via find or create', async function () {
/* Set to never synced as singleton manager will attempt to sync before resolving */
this.application.syncService.ut_clearLastSyncDate()
this.application.syncService.ut_setDatabaseLoaded(false)
this.application.sync.ut_clearLastSyncDate()
this.application.sync.ut_setDatabaseLoaded(false)
const contentType = ContentType.TYPES.UserPrefs
const predicate = new Predicate('content_type', '=', contentType)
/* Start a sync right after we await singleton resolve below */
setTimeout(() => {
this.application.syncService.ut_setDatabaseLoaded(true)
this.application.sync.ut_setDatabaseLoaded(true)
this.application.sync.sync({
/* Simulate the first sync occuring as that is handled specially by sync service */
mode: SyncMode.DownloadFirst,
})
})
const userPreferences = await this.application.singletonManager.findOrCreateContentTypeSingleton(contentType, {})
const userPreferences = await this.context.singletons.findOrCreateContentTypeSingleton(contentType, {})
expect(userPreferences).to.be.ok
const refreshedUserPrefs = this.application.items.findItem(userPreferences.uuid)
expect(refreshedUserPrefs).to.be.ok
await this.application.sync.sync(syncOptions)
expect(this.application.itemManager.itemsMatchingPredicate(contentType, predicate).length).to.equal(1)
expect(this.application.items.itemsMatchingPredicate(contentType, predicate).length).to.equal(1)
})
it('resolves registered predicate with signing in/out', async function () {
@@ -187,7 +187,7 @@ describe('singletons', function () {
let didCompleteRelevantSync = false
let beginCheckingResponse = false
this.application.syncService.addEventObserver(async (eventName, data) => {
this.application.sync.addEventObserver(async (eventName, data) => {
if (eventName === SyncEvent.DownloadFirstSyncCompleted) {
beginCheckingResponse = true
}
@@ -202,7 +202,7 @@ describe('singletons', function () {
expect(matching).to.not.be.ok
}
})
await this.application.syncService.sync({ mode: SyncMode.DownloadFirst })
await this.application.sync.sync({ mode: SyncMode.DownloadFirst })
expect(didCompleteRelevantSync).to.equal(true)
}).timeout(10000)
@@ -210,12 +210,14 @@ describe('singletons', function () {
await this.registerUser()
/** Create prefs */
const ogPrefs = await findOrCreatePrefsSingleton(this.application)
const ogPrefs = await findOrCreatePrefsSingleton(this.context)
await this.application.sync.sync(syncOptions)
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
this.application = await this.context.signout()
/** Create another instance while signed out */
await findOrCreatePrefsSingleton(this.application)
await findOrCreatePrefsSingleton(this.context)
await Factory.loginToApplication({
application: this.application,
email: this.email,
@@ -223,36 +225,36 @@ describe('singletons', function () {
})
/** After signing in, the instance retrieved from the server should be the one kept */
const latestPrefs = await findOrCreatePrefsSingleton(this.application)
const latestPrefs = await findOrCreatePrefsSingleton(this.context)
expect(latestPrefs.uuid).to.equal(ogPrefs.uuid)
const allPrefs = this.application.itemManager.getItems(ogPrefs.content_type)
const allPrefs = this.application.items.getItems(ogPrefs.content_type)
expect(allPrefs.length).to.equal(1)
})
it('resolving singleton before first sync, then signing in, should result in correct number of instances', async function () {
await this.registerUser()
/** Create prefs and associate them with account */
const ogPrefs = await findOrCreatePrefsSingleton(this.application)
const ogPrefs = await findOrCreatePrefsSingleton(this.context)
await this.application.sync.sync(syncOptions)
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
this.application = await this.context.signout()
/** Create another instance while signed out */
await findOrCreatePrefsSingleton(this.application)
await findOrCreatePrefsSingleton(this.context)
await Factory.loginToApplication({
application: this.application,
email: this.email,
password: this.password,
})
/** After signing in, the instance retrieved from the server should be the one kept */
const latestPrefs = await findOrCreatePrefsSingleton(this.application)
const latestPrefs = await findOrCreatePrefsSingleton(this.context)
expect(latestPrefs.uuid).to.equal(ogPrefs.uuid)
const allPrefs = this.application.itemManager.getItems(ogPrefs.content_type)
const allPrefs = this.application.items.getItems(ogPrefs.content_type)
expect(allPrefs.length).to.equal(1)
})
it('if only result is errorDecrypting, create new item', async function () {
const item = this.application.itemManager.items.find((item) => item.content_type === ContentType.TYPES.UserPrefs)
const item = this.application.items.items.find((item) => item.content_type === ContentType.TYPES.UserPrefs)
const erroredPayload = new EncryptedPayload({
...item.payload.ejected(),
@@ -260,16 +262,13 @@ describe('singletons', function () {
errorDecrypting: true,
})
await this.application.payloadManager.emitPayload(erroredPayload)
await this.application.payloads.emitPayload(erroredPayload)
const resolvedItem = await this.application.singletonManager.findOrCreateContentTypeSingleton(
item.content_type,
item.content,
)
const resolvedItem = await this.context.singletons.findOrCreateContentTypeSingleton(item.content_type, item.content)
await this.application.sync.sync({ awaitAll: true })
expect(this.application.itemManager.items.length).to.equal(this.expectedItemCount)
expect(this.application.items.items.length).to.equal(this.expectedItemCount)
expect(resolvedItem.uuid).to.not.equal(item.uuid)
expect(resolvedItem.errorDecrypting).to.not.be.ok
})
@@ -318,34 +317,34 @@ describe('singletons', function () {
errorDecrypting: false,
})
await this.application.payloadManager.emitPayload(notErrored)
await this.application.payloads.emitPayload(notErrored)
/** Item will get decrypted on current tick, so wait one before syncing */
await Factory.sleep(0)
await this.application.syncService.sync(syncOptions)
await this.application.sync.sync(syncOptions)
expect(this.application.itemManager.itemsMatchingPredicate(ContentType.TYPES.Component, this.extPred).length).to.equal(1)
expect(this.application.items.itemsMatchingPredicate(ContentType.TYPES.Component, this.extPred).length).to.equal(1)
})
it('alternating the uuid of a singleton should return correct result', async function () {
const payload = createPrefsPayload()
const item = await this.application.mutator.emitItemFromPayload(payload, PayloadEmitSource.LocalChanged)
await this.application.syncService.sync(syncOptions)
await this.application.sync.sync(syncOptions)
const predicate = new Predicate('content_type', '=', item.content_type)
let resolvedItem = await this.application.singletonManager.findOrCreateContentTypeSingleton(
let resolvedItem = await this.context.singletons.findOrCreateContentTypeSingleton(
payload.content_type,
payload.content,
)
const originalUuid = resolvedItem.uuid
await Factory.alternateUuidForItem(this.application, resolvedItem.uuid)
await this.application.syncService.sync(syncOptions)
const resolvedItem2 = await this.application.singletonManager.findOrCreateContentTypeSingleton(
await this.application.sync.sync(syncOptions)
const resolvedItem2 = await this.context.singletons.findOrCreateContentTypeSingleton(
payload.content_type,
payload.content,
)
resolvedItem = this.application.items.findItem(resolvedItem.uuid)
expect(resolvedItem).to.not.be.ok
expect(resolvedItem2.uuid).to.not.equal(originalUuid)
expect(this.application.itemManager.items.length).to.equal(this.expectedItemCount)
expect(this.application.items.items.length).to.equal(this.expectedItemCount)
})
})