Skip to content

Commit

Permalink
TAB-484 test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evandor committed Jan 27, 2024
1 parent 76d3d36 commit 61eb4d6
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 417 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"@types/pouchdb": "^6.4.2",
"@types/rangy": "^0.0.38",
"@types/sanitize-html": "^2.9.0",
"@xenova/transformers": "^2.12.1",
"axios": "^1.3.4",
"buffer": "^6.0.3",
"date-fns": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/models/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export class Window {
public id: number,
public browserWindow: chrome.windows.Window,
public title: string | undefined = undefined,
public index: number = 0) {
public index: number = 0,
public hostList: Set<string> = new Set()) {

this.created = new Date().getTime()
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/ChromeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class ChromeApi {
if (process.env.MODE !== 'bex') {
return
}
console.log("building context menu:", chrome, chrome.contextMenus)
console.log("building context menu:", chrome, chrome?.contextMenus)
const tabsStore = useTabsStore()
if (chrome && chrome.contextMenus) {
chrome.contextMenus.removeAll(
Expand Down
2 changes: 1 addition & 1 deletion src/services/TabsetService2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function useTabsetService() {

const init = async (providedDb: PersistenceService,
doNotInitSearchIndex: boolean = false) => {
console.log("initializing tabsetService2", providedDb)
console.log("initializing tabsetService2")
db = providedDb

useTabsStore().clearTabsets()
Expand Down
163 changes: 88 additions & 75 deletions src/stores/windowsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ function getSortedWindows(windowForId: (id: number) => (Window | undefined)) {
return theWindows;
}

function urlToHost(bwTab: chrome.tabs.Tab) {
const url = bwTab.url
if (url) {
try {
const theURL = new URL(url)
return theURL.host
} catch (err) {
return "?"
}
}
return "?";
}

export const useWindowsStore = defineStore('windows', () => {

const {inBexMode} = useUtils()
Expand Down Expand Up @@ -89,93 +102,93 @@ export const useWindowsStore = defineStore('windows', () => {
async function initialize(providedDb: PersistenceService) {
console.log("initializing windowsStore")
storage = providedDb
setup("initialization")
await setup("initialization")
}

function setup(trigger: string = "") {
async function setup(trigger: string = "") {
if (!inBexMode()) {
return
}
console.debug("init chrome windows listeners with trigger", trigger)
chrome.windows.getAll({populate: true}, (windows) => {
const browserWindows: chrome.windows.Window[] = await chrome.windows.getAll({populate: true})

currentWindows.value = windows
console.debug("initializing current windows with", currentWindows.value.length)
currentWindows.value = browserWindows
console.debug("initializing current windows with", currentWindows.value.length)

// adding potentially new windows to storage
const res: Promise<any>[] = windows.flatMap((window: chrome.windows.Window) => {
return storage.addWindow(new Window(window.id || 0, window, undefined))
})
// adding potentially new windows to storage
const res: Promise<any>[] = browserWindows.flatMap((browserWindow: chrome.windows.Window) => {
const hostList = new Set(_.map(browserWindow.tabs, bwTabs => urlToHost(bwTabs)))
return storage.addWindow(new Window(browserWindow.id || 0, browserWindow, undefined, 0, hostList))
})

// setting all (new and older) windows to 'allWindows'
Promise.all(res)
.then(() => {
allWindows.value = new Map()
let index = 0
//const usedIndices: number[] = []

storage.getWindows().then(storedWindows => {
const sortedStoredWindows = _.sortBy(storedWindows, "index")

sortedStoredWindows.forEach(tabsetWindowFromStorage => {

// index handling
const indexFromDb = tabsetWindowFromStorage.index
const indicesDiffer = indexFromDb !== index
let indexToUse = index++

if (indicesDiffer) {
updateWindowIndex(tabsetWindowFromStorage.id, indexToUse)
.then(() => console.log("done with updating window", tabsetWindowFromStorage.id, indexToUse))
.catch((err) => console.error("error when updating window", tabsetWindowFromStorage.id, indexToUse, err))
}
tabsetWindowFromStorage.index = indexToUse
//usedIndices.push(indexToUse)
allWindows.value.set(tabsetWindowFromStorage.id || 0, tabsetWindowFromStorage)

const inCurrentWindows = windows.find(w => w.id === tabsetWindowFromStorage.id) !== undefined

//console.debug(`assigned window #${tabsetWindowFromStorage.id} (name: ${tabsetWindowFromStorage.title}): ${indexFromDb} -> ${tabsetWindowFromStorage.index}, open: ${inCurrentWindows}`)
})
for (const id of allWindows.value.keys()) {
const w = allWindows.value.get(id)
if (w && w.title) {
//console.log("checking", w, )
windowSet.value.add(w.title)
}
}
//console.log("%callWindows assigned", "color:green", allWindows.value, windowSet.value)

chrome.windows.getCurrent({windowTypes: ['normal']}, (window: chrome.windows.Window) => {
currentWindow.value = window
if (currentWindow.value && currentWindow.value.id) {
//console.log("%c******", "color:blue", currentWindow.value.id, windowNameFor(currentWindow.value.id))
currentWindowName.value = windowNameFor(currentWindow.value.id)
}
})

// add context menus for moving to other window
if (chrome && chrome.contextMenus) {
chrome.windows.getCurrent().then(currentWindow => {
for (const window of currentWindows.value) {
//console.log("da!!!",window,useWindowsStore().windowNameFor(window.id))
// TODO this is always the "default" window
if (currentWindow.id !== window.id) {
chrome.contextMenus.create({
id: 'move_to|' + window.id,
parentId: 'move_to_window',
title: '...to window ' + useWindowsStore().windowNameFor(window.id || 0) || window.id?.toString(),
contexts: ['all']
})
}
}
})
}
})
// setting all (new and older) windows to 'allWindows'
await Promise.all(res)
// .then(() => {
allWindows.value = new Map()
let index = 0
//const usedIndices: number[] = []

})
//storage.getWindows().then(storedWindows => {
const storedWindows: Window[] = await storage.getWindows()//.then(storedWindows => {
const sortedStoredWindows = _.sortBy(storedWindows, "index")

sortedStoredWindows.forEach(tabsetWindowFromStorage => {

// index handling
const indexFromDb = tabsetWindowFromStorage.index
const indicesDiffer = indexFromDb !== index
let indexToUse = index++

if (indicesDiffer) {
updateWindowIndex(tabsetWindowFromStorage.id, indexToUse)
.then(() => console.log("done with updating window", tabsetWindowFromStorage.id, indexToUse))
.catch((err) => console.error("error when updating window", tabsetWindowFromStorage.id, indexToUse, err))
}
tabsetWindowFromStorage.index = indexToUse
//usedIndices.push(indexToUse)
allWindows.value.set(tabsetWindowFromStorage.id || 0, tabsetWindowFromStorage)

const inCurrentWindows = browserWindows.find(w => w.id === tabsetWindowFromStorage.id) !== undefined

//console.debug(`assigned window #${tabsetWindowFromStorage.id} (name: ${tabsetWindowFromStorage.title}): ${indexFromDb} -> ${tabsetWindowFromStorage.index}, open: ${inCurrentWindows}`)
})
for (const id of allWindows.value.keys()) {
const w = allWindows.value.get(id)
if (w && w.title) {
//console.log("checking", w, )
windowSet.value.add(w.title)
}
}
//console.log("%callWindows assigned", "color:green", allWindows.value, windowSet.value)

chrome.windows.getCurrent({windowTypes: ['normal']}, (window: chrome.windows.Window) => {
currentWindow.value = window
if (currentWindow.value && currentWindow.value.id) {
//console.log("%c******", "color:blue", currentWindow.value.id, windowNameFor(currentWindow.value.id))
currentWindowName.value = windowNameFor(currentWindow.value.id)
}
})

// add context menus for moving to other window
if (chrome && chrome.contextMenus) {
//chrome.windows.getCurrent().then(currentWindow => {
const currentWindow: chrome.windows.Window = await chrome.windows.getCurrent()//.then(currentWindow => {
for (const window of currentWindows.value) {
//console.log("da!!!",window,useWindowsStore().windowNameFor(window.id))
// TODO this is always the "default" window
if (currentWindow.id !== window.id) {
chrome.contextMenus.create({
id: 'move_to|' + window.id,
parentId: 'move_to_window',
title: '...to window ' + useWindowsStore().windowNameFor(window.id || 0) || window.id?.toString(),
contexts: ['all']
})
}
}
//})
}


}

async function onRemoved(windowId: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import {installQuasarPlugin} from '@quasar/quasar-app-extension-testing-unit-vit
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {createPinia, setActivePinia} from "pinia";
import IndexedDbPersistenceService from "src/services/IndexedDbPersistenceService";
import {AddTabToTabsetCommand} from "src/domain/tabs/AddTabToTabset";
import {CreateTabsetCommand} from "src/domain/tabsets/CreateTabset";
import {useDB} from "src/services/usePersistenceService";
import {useTabsStore} from "stores/tabsStore";
import PersistenceService from "src/services/PersistenceService";
import {useTabsetService} from "src/services/TabsetService2";
import {DeleteChromeGroupCommand} from "src/domain/groups/DeleteChromeGroupCommand";
Expand Down
9 changes: 5 additions & 4 deletions test/vitest/__tests__/stores/windowsStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ describe('WindowsStore', () => {
// https://groups.google.com/a/chromium.org/g/chromium-extensions/c/hssoAlvluW8
const chromeMock = {
windows: {
getAll: vi.fn((callback) => {
getAll: vi.fn( (options, callback) => {
console.log("mocking chrome.windows.getAll")
callback(currentWindows);
//callback(currentWindows);
return Promise.resolve(currentWindows)
}),
getCurrent: vi.fn((options, callback) => {
console.log("mocking chrome.windows.getCurrent")
//console.log("mocking chrome.windows.getCurrent")
callback(window100)
}),
getLastFocused: vi.fn((options, callback) => {
console.log("mocking chrome.windows.getLastFocused")
//console.log("mocking chrome.windows.getLastFocused")
callback(window100)
}),
get: vi.fn((windowId, queryOptions, callback) => {
Expand Down
Loading

0 comments on commit 61eb4d6

Please sign in to comment.