chore: vault tests refactors and lint (#2374)
This commit is contained in:
@@ -13,6 +13,7 @@ import { ItemManager } from '@Lib/Services/Items/ItemManager'
|
||||
import { FeaturesService } from '@Lib/Services/Features/FeaturesService'
|
||||
import { SNComponentManager } from './ComponentManager'
|
||||
import { SyncService } from '../Sync/SyncService'
|
||||
import { LoggerInterface } from '@standardnotes/utils'
|
||||
|
||||
describe('featuresService', () => {
|
||||
let items: ItemManagerInterface
|
||||
@@ -23,6 +24,7 @@ describe('featuresService', () => {
|
||||
let prefs: PreferenceServiceInterface
|
||||
let eventBus: InternalEventBusInterface
|
||||
let device: DeviceInterface
|
||||
let logger: LoggerInterface
|
||||
|
||||
const createManager = (environment: Environment, platform: Platform) => {
|
||||
const manager = new SNComponentManager(
|
||||
@@ -35,6 +37,7 @@ describe('featuresService', () => {
|
||||
environment,
|
||||
platform,
|
||||
device,
|
||||
logger,
|
||||
eventBus,
|
||||
)
|
||||
|
||||
@@ -46,6 +49,8 @@ describe('featuresService', () => {
|
||||
addEventListener: jest.fn(),
|
||||
attachEvent: jest.fn(),
|
||||
} as unknown as Window & typeof globalThis
|
||||
logger = {} as jest.Mocked<LoggerInterface>
|
||||
logger.info = jest.fn()
|
||||
|
||||
sync = {} as jest.Mocked<SyncService>
|
||||
sync.sync = jest.fn()
|
||||
|
||||
@@ -29,7 +29,7 @@ import {
|
||||
GetNativeThemes,
|
||||
NativeFeatureIdentifier,
|
||||
} from '@standardnotes/features'
|
||||
import { Copy, removeFromArray, sleep, isNotUndefined } from '@standardnotes/utils'
|
||||
import { Copy, removeFromArray, sleep, isNotUndefined, LoggerInterface } from '@standardnotes/utils'
|
||||
import { ComponentViewer } from '@Lib/Services/ComponentManager/ComponentViewer'
|
||||
import {
|
||||
AbstractService,
|
||||
@@ -98,6 +98,7 @@ export class SNComponentManager
|
||||
private environment: Environment,
|
||||
private platform: Platform,
|
||||
private device: DeviceInterface,
|
||||
private logger: LoggerInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
@@ -177,6 +178,7 @@ export class SNComponentManager
|
||||
alerts: this.alerts,
|
||||
preferences: this.preferences,
|
||||
features: this.features,
|
||||
logger: this.logger,
|
||||
},
|
||||
{
|
||||
url: this.urlForFeature(component) ?? '',
|
||||
@@ -312,7 +314,7 @@ export class SNComponentManager
|
||||
onWindowMessage = (event: MessageEvent): void => {
|
||||
const data = event.data as ComponentMessage
|
||||
if (data.sessionKey) {
|
||||
this.log('Component manager received message', data)
|
||||
this.logger.info('Component manager received message', data)
|
||||
this.componentViewerForSessionKey(data.sessionKey)?.handleMessage(data)
|
||||
}
|
||||
}
|
||||
@@ -369,7 +371,7 @@ export class SNComponentManager
|
||||
}
|
||||
|
||||
public async toggleTheme(uiFeature: UIFeature<ThemeFeatureDescription>): Promise<void> {
|
||||
this.log('Toggling theme', uiFeature.uniqueIdentifier)
|
||||
this.logger.info('Toggling theme', uiFeature.uniqueIdentifier)
|
||||
|
||||
if (this.isThemeActive(uiFeature)) {
|
||||
await this.removeActiveTheme(uiFeature)
|
||||
@@ -449,7 +451,7 @@ export class SNComponentManager
|
||||
}
|
||||
|
||||
public async toggleComponent(component: ComponentInterface): Promise<void> {
|
||||
this.log('Toggling component', component.uuid)
|
||||
this.logger.info('Toggling component', component.uuid)
|
||||
|
||||
if (this.isComponentActive(component)) {
|
||||
await this.removeActiveComponent(component)
|
||||
|
||||
@@ -65,13 +65,13 @@ import {
|
||||
extendArray,
|
||||
Copy,
|
||||
removeFromArray,
|
||||
log,
|
||||
nonSecureRandomIdentifier,
|
||||
UuidGenerator,
|
||||
Uuids,
|
||||
sureSearchArray,
|
||||
isNotUndefined,
|
||||
uniqueArray,
|
||||
LoggerInterface,
|
||||
} from '@standardnotes/utils'
|
||||
import { ContentType, Uuid } from '@standardnotes/domain-core'
|
||||
|
||||
@@ -80,7 +80,6 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
private streamContextItemOriginalMessage?: ComponentMessage
|
||||
private streamItemsOriginalMessage?: ComponentMessage
|
||||
private removeItemObserver: () => void
|
||||
private loggingEnabled = false
|
||||
public identifier = nonSecureRandomIdentifier()
|
||||
private actionObservers: ActionObserver[] = []
|
||||
|
||||
@@ -102,6 +101,7 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
alerts: AlertService
|
||||
preferences: PreferenceServiceInterface
|
||||
features: FeaturesService
|
||||
logger: LoggerInterface
|
||||
},
|
||||
private options: {
|
||||
item: ComponentViewerItem
|
||||
@@ -143,7 +143,7 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
}
|
||||
})
|
||||
|
||||
this.log('Constructor', this)
|
||||
this.services.logger.info('Constructor', this)
|
||||
}
|
||||
|
||||
public getComponentOrFeatureItem(): UIFeature<IframeComponentFeatureDescription> {
|
||||
@@ -163,7 +163,7 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
this.log('Destroying', this)
|
||||
this.services.logger.info('Destroying', this)
|
||||
this.deinit()
|
||||
}
|
||||
|
||||
@@ -347,7 +347,7 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
this.componentUniqueIdentifier.value,
|
||||
requiredContextPermissions,
|
||||
() => {
|
||||
this.log(
|
||||
this.services.logger.info(
|
||||
'Send context item in reply',
|
||||
'component:',
|
||||
this.componentOrFeature,
|
||||
@@ -364,18 +364,12 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
)
|
||||
}
|
||||
|
||||
private log(message: string, ...args: unknown[]): void {
|
||||
if (this.loggingEnabled) {
|
||||
log('ComponentViewer', message, args)
|
||||
}
|
||||
}
|
||||
|
||||
private sendItemsInReply(
|
||||
items: (DecryptedItemInterface | DeletedItemInterface)[],
|
||||
message: ComponentMessage,
|
||||
source?: PayloadEmitSource,
|
||||
): void {
|
||||
this.log('Send items in reply', this.componentOrFeature, items, message)
|
||||
this.services.logger.info('Send items in reply', this.componentOrFeature, items, message)
|
||||
|
||||
const responseData: MessageReplyData = {}
|
||||
|
||||
@@ -453,10 +447,14 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
*/
|
||||
private sendMessage(message: ComponentMessage | MessageReply, essential = true): void {
|
||||
if (!this.window && message.action === ComponentAction.Reply) {
|
||||
this.log('Component has been deallocated in between message send and reply', this.componentOrFeature, message)
|
||||
this.services.logger.info(
|
||||
'Component has been deallocated in between message send and reply',
|
||||
this.componentOrFeature,
|
||||
message,
|
||||
)
|
||||
return
|
||||
}
|
||||
this.log('Send message to component', this.componentOrFeature, 'message: ', message)
|
||||
this.services.logger.info('Send message to component', this.componentOrFeature, 'message: ', message)
|
||||
|
||||
if (!this.window) {
|
||||
if (essential) {
|
||||
@@ -518,7 +516,7 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
throw Error('Attempting to override component viewer window. Create a new component viewer instead.')
|
||||
}
|
||||
|
||||
this.log('setWindow', 'component: ', this.componentOrFeature, 'window: ', window)
|
||||
this.services.logger.info('setWindow', 'component: ', this.componentOrFeature, 'window: ', window)
|
||||
|
||||
this.window = window
|
||||
this.sessionKey = UuidGenerator.GenerateUuid()
|
||||
@@ -537,7 +535,7 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
},
|
||||
})
|
||||
|
||||
this.log('setWindow got new sessionKey', this.sessionKey)
|
||||
this.services.logger.info('setWindow got new sessionKey', this.sessionKey)
|
||||
|
||||
this.postActiveThemes()
|
||||
}
|
||||
@@ -557,9 +555,9 @@ export class ComponentViewer implements ComponentViewerInterface {
|
||||
}
|
||||
|
||||
handleMessage(message: ComponentMessage): void {
|
||||
this.log('Handle message', message, this)
|
||||
this.services.logger.info('Handle message', message, this)
|
||||
if (!this.componentOrFeature) {
|
||||
this.log('Component not defined for message, returning', message)
|
||||
this.services.logger.info('Component not defined for message, returning', message)
|
||||
void this.services.alerts.alert(
|
||||
'A component is trying to communicate with Standard Notes, ' +
|
||||
'but there is an error establishing a bridge. Please restart the app and try again.',
|
||||
|
||||
Reference in New Issue
Block a user