Skip to content

Commit

Permalink
more work to get the window object work
Browse files Browse the repository at this point in the history
  • Loading branch information
blackforestboi committed May 21, 2024
1 parent 191ba21 commit c2f6e1f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 142 deletions.
181 changes: 90 additions & 91 deletions src/backup-restore/background/procedures/restore/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BlobPolyfill from 'node-blob'
import atobPolyfill from 'atob'
// import * as sinon from 'sinon'
import expect from 'expect'
import { BackupBackend, ObjectChange } from '../../backend'
import { BackupRestoreProcedure } from '.'
Expand Down Expand Up @@ -197,103 +196,103 @@ describe('BackupRestoreProcedure', () => {

it.skip('should not restore empty objects in place of Blobs', async () => {
return
const favCreateChange: ObjectChange = {
collection: 'favIcons',
operation: 'create',
objectPk: 'test.com',
object: { favIcon: {}, hostname: 'test.com' },
timestamp: 0,
}
const pageCreateChange: ObjectChange = {
collection: 'pages',
operation: 'create',
objectPk: 'test.com/route',
object: {
url: 'test.com/route',
screenshot: {},
hostname: 'test.com',
},
timestamp: 0,
}
const pageUpdateChange: ObjectChange = {
collection: 'pages',
operation: 'update',
objectPk: 'test.com/route',
object: {
url: 'test.com/route',
screenshot: {},
hostname: 'test.com',
},
timestamp: 0,
}
const pageDeleteChange: ObjectChange = {
collection: 'pages',
operation: 'delete',
objectPk: 'test.com/route',
timestamp: 0,
}
// const favCreateChange: ObjectChange = {
// collection: 'favIcons',
// operation: 'create',
// objectPk: 'test.com',
// object: { favIcon: {}, hostname: 'test.com' },
// timestamp: 0,
// }
// const pageCreateChange: ObjectChange = {
// collection: 'pages',
// operation: 'create',
// objectPk: 'test.com/route',
// object: {
// url: 'test.com/route',
// screenshot: {},
// hostname: 'test.com',
// },
// timestamp: 0,
// }
// const pageUpdateChange: ObjectChange = {
// collection: 'pages',
// operation: 'update',
// objectPk: 'test.com/route',
// object: {
// url: 'test.com/route',
// screenshot: {},
// hostname: 'test.com',
// },
// timestamp: 0,
// }
// const pageDeleteChange: ObjectChange = {
// collection: 'pages',
// operation: 'delete',
// objectPk: 'test.com/route',
// timestamp: 0,
// }

const createObject = sinon.fake()
const updateOneObject = sinon.fake()
const updateObjects = sinon.fake()
const deleteOneObject = sinon.fake()
const deleteObjects = sinon.fake()
// const createObject = sinon.fake()
// const updateOneObject = sinon.fake()
// const updateObjects = sinon.fake()
// const deleteOneObject = sinon.fake()
// const deleteObjects = sinon.fake()

const restoreProcedure = new BackupRestoreProcedure({
backend: null,
storageManager: {
collection: () => ({
createObject,
deleteOneObject,
deleteObjects,
updateOneObject,
updateObjects,
}),
registry: {
collections: {
pages: { pkIndex: 'test' },
favIcons: { pkIndex: 'test' },
},
},
} as any,
logErrors: false,
storage: null,
})
restoreProcedure._getBlobClass = () =>
typeof Blob !== 'undefined' ? Blob : BlobPolyfill
restoreProcedure._getAtobFunction = () =>
typeof atob !== 'undefined' ? atob : atobPolyfill
// const restoreProcedure = new BackupRestoreProcedure({
// backend: null,
// storageManager: {
// collection: () => ({
// createObject,
// deleteOneObject,
// deleteObjects,
// updateOneObject,
// updateObjects,
// }),
// registry: {
// collections: {
// pages: { pkIndex: 'test' },
// favIcons: { pkIndex: 'test' },
// },
// },
// } as any,
// logErrors: false,
// storage: null,
// })
// restoreProcedure._getBlobClass = () =>
// typeof Blob !== 'undefined' ? Blob : BlobPolyfill
// restoreProcedure._getAtobFunction = () =>
// typeof atob !== 'undefined' ? atob : atobPolyfill

const {
screenshot,
...pageWithoutScreenshotKey
} = pageCreateChange.object
// const {
// screenshot,
// ...pageWithoutScreenshotKey
// } = pageCreateChange.object

expect(createObject.callCount).toBe(0)
await restoreProcedure._writeChange(favCreateChange)
expect(createObject.callCount).toBe(0)
// expect(createObject.callCount).toBe(0)
// await restoreProcedure._writeChange(favCreateChange)
// expect(createObject.callCount).toBe(0)

await restoreProcedure._writeChange(pageCreateChange)
expect(createObject.lastCall.calledWith(pageWithoutScreenshotKey)).toBe(
true,
)
// await restoreProcedure._writeChange(pageCreateChange)
// expect(createObject.lastCall.calledWith(pageWithoutScreenshotKey)).toBe(
// true,
// )

await restoreProcedure._writeChange(pageUpdateChange)
expect(
updateObjects.lastCall.calledWith(
{
test: pageUpdateChange.objectPk,
},
pageWithoutScreenshotKey,
),
).toBe(true)
// await restoreProcedure._writeChange(pageUpdateChange)
// expect(
// updateObjects.lastCall.calledWith(
// {
// test: pageUpdateChange.objectPk,
// },
// pageWithoutScreenshotKey,
// ),
// ).toBe(true)

await restoreProcedure._writeChange(pageDeleteChange)
expect(
deleteObjects.lastCall.calledWith({
test: pageDeleteChange.objectPk,
}),
).toBe(true)
// await restoreProcedure._writeChange(pageDeleteChange)
// expect(
// deleteObjects.lastCall.calledWith({
// test: pageDeleteChange.objectPk,
// }),
// ).toBe(true)
})

it.skip('should correctly restore screenshot blobs', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/content-scripts/content_script/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import {
} from '@worldbrain/memex-common/lib/subscriptions/constants'
import type { RemoteSearchInterface } from 'src/search/background/types'
import * as anchoring from '@worldbrain/memex-common/lib/annotations'
import { getWindow } from 'src/util/get-Window'

// Content Scripts are separate bundles of javascript code that can be loaded
// on demand by the browser, as needed. This main function manages the initialisation
Expand Down Expand Up @@ -1093,6 +1094,7 @@ export async function main(
imageSupport: runInBackground(),
pkmSyncBG: runInBackground(),
getRootElement: null,
windowAPI: getWindow(),
})
components.sidebar?.resolve()
},
Expand Down
2 changes: 1 addition & 1 deletion src/sidebar/annotations-sidebar/containers/logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const setupLogicHelper = async ({
backgroundModules.summarizeBG.remoteFunctions,
) as any,
browserAPIs: device.browserAPIs,
windowAPI: device.windowAPI,
windowAPI: window,
customListsBG: backgroundModules.customLists.remoteFunctions,
contentSharingBG: backgroundModules.contentSharing.remoteFunctions,
contentSharingByTabsBG: insertBackgroundFunctionTab(
Expand Down
97 changes: 49 additions & 48 deletions src/tab-management/background/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,62 @@ describe('activity logger background tests', () => {
it(
'should be able to track existing tabs',
async ({ device }) => {
const { browserAPIs } = device
const { tabManagement } = device.backgroundModules
return
// const { browserAPIs } = device
// const { tabManagement } = device.backgroundModules

const mockTabs = [
{ id: 0, url: 'https://test.com', status: 'complete' },
{ id: 1, url: 'chrome://extensions', status: 'loading' },
{ id: 2, url: 'https://test.com/1', status: 'complete' },
{ id: 3, url: 'https://test.com/2', status: 'loading' },
{ id: 4, url: 'https://test.com/3', status: 'complete' },
{ id: 5, url: 'https://worldbrain.io', status: 'complete' },
] as Tabs.Tab[]
// const mockTabs = [
// { id: 0, url: 'https://test.com', status: 'complete' },
// { id: 1, url: 'chrome://extensions', status: 'loading' },
// { id: 2, url: 'https://test.com/1', status: 'complete' },
// { id: 3, url: 'https://test.com/2', status: 'loading' },
// { id: 4, url: 'https://test.com/3', status: 'complete' },
// { id: 5, url: 'https://worldbrain.io', status: 'complete' },
// ] as Tabs.Tab[]

const createScriptCallObj = (tabId: number, file: string) => ({
id: tabId,
script: { file, runAt: 'document_idle' },
})
// const createScriptCallObj = (tabId: number, file: string) => ({
// id: tabId,
// script: { file, runAt: 'document_idle' },
// })

// Mock out tabs API, so it sets something we can check
browserAPIs.tabs.query = async () => mockTabs
// // Mock out tabs API, so it sets something we can check
// browserAPIs.tabs.query = async () => mockTabs

const executeScriptsCalls: Array<{
id: number
script: { file: string }
}> = []
browserAPIs.tabs.executeScript = (async (tabId, script) => {
executeScriptsCalls.push({ id: tabId!, script })
return []
}) as any
await tabManagement.trackExistingTabs()
// const executeScriptsCalls: Array<{
// id: number
// script: { file: string }
// }> = []
// browserAPIs.tabs.executeScript = (async (tabId, script) => {
// executeScriptsCalls.push({ id: tabId!, script })
// return []
// }) as any
// await tabManagement.trackExistingTabs()

expect([...tabManagement.tabManager._tabs.entries()]).toEqual(
mockTabs.map((tab) => [
tab.id,
expect.objectContaining({
id: tab.id,
url: tab.url,
isLoaded: tab.status === 'complete',
}),
]),
)
// expect([...tabManagement.tabManager._tabs.entries()]).toEqual(
// mockTabs.map((tab) => [
// tab.id,
// expect.objectContaining({
// id: tab.id,
// url: tab.url,
// isLoaded: tab.status === 'complete',
// }),
// ]),
// )

const expectedCalls = []
mockTabs.forEach((tab) => {
if (tab.id === 1) {
return
}
// const expectedCalls = []
// mockTabs.forEach((tab) => {
// if (tab.id === 1) {
// return
// }

expectedCalls.push(
createScriptCallObj(tab.id, '/lib/browser-polyfill.js'),
createScriptCallObj(tab.id, '/content_script.js'),
)
})
expect(executeScriptsCalls).toEqual(
expect.arrayContaining(expectedCalls),
)
// expectedCalls.push(
// createScriptCallObj(tab.id, '/lib/browser-polyfill.js'),
// createScriptCallObj(tab.id, '/content_script.js'),
// )
// })
// expect(executeScriptsCalls).toEqual(
// expect.arrayContaining(expectedCalls),
// )
},
{ shouldSkip: true },
)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/background-integration-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export function registerBackgroundIntegrationTest(
test: BackgroundIntegrationTest,
options: BackgroundIntegrationTestSetupOpts = {},
) {
it.skip(test.description + ' - single device', async () => {
it(test.description + ' - single device', async () => {
await runBackgroundIntegrationTest(test, {
...options,
...(test.customTestOpts ?? {}),
Expand Down
1 change: 0 additions & 1 deletion src/tests/integration-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export interface BackgroundIntegrationTestSetup {
persistentStorageManager: StorageManager
backgroundModules: BackgroundModules
browserAPIs: Browser
windowAPI: Window
services: Services
browserLocalStorage: MemoryBrowserStorage
storageChangeDetector: StorageChangeDetector
Expand Down

0 comments on commit c2f6e1f

Please sign in to comment.