Removes the system-action skills that were only ever available in the now -retired Basic/Assistant modes (play_music, open/close_app, open_notepad, create_document, create_powerpoint, control_volume, control_media_playback, take_screenshot, clipboard, search_file, set_reminder, lock_screen, empty_recycle_bin, show_notification, print_file, create_folder, create_file, delete_file, fill_browser_field) along with their windows/lib helpers (appRegistry, fileSearch, locations, sendVirtualKey) and the fillField browser-window helper. Keeps the read-only, side-effect -free transversal skills (search_web, navigate_browser and friends, check_system_memory/storage/battery/internet). Drops the now-unused docx/pptxgenjs dependencies and the stale coachStore/meetingStore entries from the coverage exclude list. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
361 lines
13 KiB
TypeScript
361 lines
13 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { EventEmitter } from 'node:events'
|
|
|
|
// jsdom n'implemente pas innerText (pas de mise en page reelle) : le code de production (destine a
|
|
// un vrai Chromium via executeJavaScript) s'appuie dessus. On le fait correspondre a textContent
|
|
// pour ce test, suffisant pour du texte simple sans mise en forme complexe.
|
|
Object.defineProperty(HTMLElement.prototype, 'innerText', {
|
|
get(): string {
|
|
return (this as HTMLElement).textContent ?? ''
|
|
},
|
|
set(value: string) {
|
|
;(this as HTMLElement).textContent = value
|
|
},
|
|
configurable: true
|
|
})
|
|
|
|
// jsdom n'implemente pas scrollIntoView (pas de mise en page reelle) non plus.
|
|
Element.prototype.scrollIntoView = vi.fn()
|
|
|
|
// jsdom n'implemente pas non plus l'API legacy window.find() (recherche de texte dans la page) :
|
|
// implementation minimaliste suffisante pour le test (recherche globale, premiere occurrence,
|
|
// selectionne le noeud texte correspondant).
|
|
;(window as unknown as { find: (text: string) => boolean }).find = (text: string): boolean => {
|
|
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT)
|
|
let node: Node | null
|
|
while ((node = walker.nextNode())) {
|
|
const index = (node.textContent ?? '').indexOf(text)
|
|
if (index >= 0) {
|
|
const range = document.createRange()
|
|
range.setStart(node, index)
|
|
range.setEnd(node, index + text.length)
|
|
const selection = window.getSelection()
|
|
selection?.removeAllRanges()
|
|
selection?.addRange(range)
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// executeJavaScript recoit le code sous forme de chaine ( `(${fn.toString()})(args)` ) : l'evaluer
|
|
// reellement ici (environnement jsdom) execute le vrai code de manipulation DOM contre le vrai
|
|
// `document` de jsdom, ce qui donne a la fois une vraie couverture ET un vrai test de comportement,
|
|
// plutot que de mocker la valeur de retour a la main pour chaque fonction.
|
|
class FakeWebContents extends EventEmitter {
|
|
executeJavaScript = vi.fn(async (code: string): Promise<unknown> => eval(code) as unknown)
|
|
getTitle = vi.fn(() => 'Titre de la page')
|
|
navigationHistory = { canGoBack: vi.fn(() => true), goBack: vi.fn() }
|
|
}
|
|
|
|
class FakeBrowserWindow extends EventEmitter {
|
|
webContents = new FakeWebContents()
|
|
show = vi.fn()
|
|
maximize = vi.fn()
|
|
loadURL = vi.fn()
|
|
destroyed = false
|
|
isDestroyed(): boolean {
|
|
return this.destroyed
|
|
}
|
|
}
|
|
|
|
let lastWindow: FakeBrowserWindow | null = null
|
|
function createBrowserWindow(): FakeBrowserWindow {
|
|
const window = new FakeBrowserWindow()
|
|
lastWindow = window
|
|
return window
|
|
}
|
|
|
|
vi.mock('electron', () => ({ BrowserWindow: vi.fn(createBrowserWindow) }))
|
|
|
|
const {
|
|
navigateTo,
|
|
goBack,
|
|
readPageText,
|
|
clickElementByText,
|
|
collectTranslatableElements,
|
|
applyTranslations,
|
|
highlightText,
|
|
dismissCookieBannerInPage,
|
|
clickByTextInPage,
|
|
collectInPage,
|
|
applyInPage,
|
|
highlightInPage
|
|
} = await import('../../src/main/browser/browserWindow')
|
|
|
|
function finishLoad(): void {
|
|
lastWindow?.webContents.emit('did-finish-load')
|
|
}
|
|
|
|
describe('navigateTo()', () => {
|
|
beforeEach(() => {
|
|
lastWindow?.emit('closed')
|
|
vi.clearAllMocks()
|
|
lastWindow = null
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
it('should create the window, load the URL and return the page title', async () => {
|
|
const promise = navigateTo('https://example.com')
|
|
finishLoad()
|
|
const title = await promise
|
|
expect(title).toBe('Titre de la page')
|
|
expect(lastWindow?.show).toHaveBeenCalled()
|
|
expect(lastWindow?.loadURL).toHaveBeenCalledWith('https://example.com')
|
|
})
|
|
|
|
it('should reject when the page fails to load', async () => {
|
|
const promise = navigateTo('https://example.com')
|
|
lastWindow?.webContents.emit('did-fail-load', {}, 404, 'Not Found')
|
|
await expect(promise).rejects.toThrow(/Echec de chargement/)
|
|
})
|
|
|
|
it('should dismiss a cookie banner by clicking the matching button', async () => {
|
|
document.body.innerHTML = '<button>Tout accepter</button><p>Contenu reel</p>'
|
|
const promise = navigateTo('https://example.com')
|
|
finishLoad()
|
|
await promise
|
|
expect(document.querySelector('button')?.textContent).toBe('Tout accepter')
|
|
})
|
|
|
|
it('should not click anything when no cookie banner phrase matches', async () => {
|
|
document.body.innerHTML = '<button>Parametres avances</button>'
|
|
const clicked = vi.fn()
|
|
document.querySelector('button')?.addEventListener('click', clicked)
|
|
const promise = navigateTo('https://example.com')
|
|
finishLoad()
|
|
await promise
|
|
expect(clicked).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should not fail navigation when the cookie-dismiss script injection itself fails', async () => {
|
|
const promise = navigateTo('https://example.com')
|
|
lastWindow!.webContents.executeJavaScript = vi.fn(async () => {
|
|
throw new Error('injection blocked')
|
|
})
|
|
finishLoad()
|
|
await expect(promise).resolves.toBe('Titre de la page')
|
|
})
|
|
})
|
|
|
|
describe('goBack()', () => {
|
|
beforeEach(() => {
|
|
lastWindow?.emit('closed')
|
|
vi.clearAllMocks()
|
|
lastWindow = null
|
|
})
|
|
|
|
it('should throw when no browser window is open', async () => {
|
|
await expect(goBack()).rejects.toThrow(/Aucun navigateur ouvert/)
|
|
})
|
|
|
|
it('should return false when there is no previous page', async () => {
|
|
const navPromise = navigateTo('https://example.com')
|
|
finishLoad()
|
|
await navPromise
|
|
lastWindow!.webContents.navigationHistory.canGoBack.mockReturnValue(false)
|
|
expect(await goBack()).toBe(false)
|
|
})
|
|
|
|
it('should go back and wait for the page to finish loading', async () => {
|
|
const navPromise = navigateTo('https://example.com')
|
|
finishLoad()
|
|
await navPromise
|
|
const backPromise = goBack()
|
|
finishLoad()
|
|
expect(await backPromise).toBe(true)
|
|
expect(lastWindow?.webContents.navigationHistory.goBack).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('page interaction functions (require an open window)', () => {
|
|
beforeEach(async () => {
|
|
lastWindow?.emit('closed')
|
|
vi.clearAllMocks()
|
|
lastWindow = null
|
|
document.body.innerHTML = ''
|
|
const navPromise = navigateTo('https://example.com')
|
|
finishLoad()
|
|
await navPromise
|
|
})
|
|
|
|
it('readPageText() should throw when no browser window is open', async () => {
|
|
lastWindow?.emit('closed')
|
|
await expect(readPageText()).rejects.toThrow(/Aucun navigateur ouvert/)
|
|
})
|
|
|
|
it('readPageText() should return the trimmed body text', async () => {
|
|
document.body.innerHTML = '<p>Bonjour le monde</p>'
|
|
const text = await readPageText()
|
|
expect(text).toContain('Bonjour le monde')
|
|
})
|
|
|
|
it('clickElementByText() should click a visible button matching the text and return true', async () => {
|
|
document.body.innerHTML = '<button>Connexion</button>'
|
|
const clicked = vi.fn()
|
|
document.querySelector('button')?.addEventListener('click', clicked)
|
|
const result = await clickElementByText('Connexion')
|
|
expect(result).toBe(true)
|
|
expect(clicked).toHaveBeenCalled()
|
|
})
|
|
|
|
it('clickElementByText() should return false when nothing matches', async () => {
|
|
document.body.innerHTML = '<button>Autre chose</button>'
|
|
expect(await clickElementByText('Introuvable')).toBe(false)
|
|
})
|
|
|
|
it('clickElementByText() should fall back to a partial (includes) match', async () => {
|
|
document.body.innerHTML = '<a href="#">Voir tous les articles</a>'
|
|
expect(await clickElementByText('articles')).toBe(true)
|
|
})
|
|
|
|
it('clickElementByText() should match a submit input by its value attribute', async () => {
|
|
document.body.innerHTML = '<input type="submit" value="Envoyer" />'
|
|
expect(await clickElementByText('Envoyer')).toBe(true)
|
|
})
|
|
|
|
it('collectTranslatableElements() should collect visible leaf text nodes', async () => {
|
|
document.body.innerHTML = '<p>Un paragraphe</p><div><span>Imbrique</span></div>'
|
|
const elements = await collectTranslatableElements()
|
|
expect(elements.length).toBeGreaterThan(0)
|
|
expect(elements.some((e) => e.text === 'Un paragraphe')).toBe(true)
|
|
})
|
|
|
|
it('applyTranslations() should replace the text of tagged elements', async () => {
|
|
document.body.innerHTML = '<p>Original</p>'
|
|
const [element] = await collectTranslatableElements()
|
|
await applyTranslations([{ id: element!.id, text: 'Traduit' }])
|
|
expect(document.querySelector('p')?.textContent).toBe('Traduit')
|
|
})
|
|
|
|
it('highlightText() should return false when the text is not found on the page', async () => {
|
|
document.body.innerHTML = '<p>Contenu quelconque</p>'
|
|
expect(await highlightText('texte introuvable')).toBe(false)
|
|
})
|
|
|
|
it('highlightText() should return false for an empty search string', async () => {
|
|
expect(await highlightText(' ')).toBe(false)
|
|
})
|
|
|
|
it('highlightText() should wrap the matching text in a <mark> element', async () => {
|
|
document.body.innerHTML = '<p>Le prix est de 42 euros aujourd-hui</p>'
|
|
const found = await highlightText('42 euros')
|
|
expect(found).toBe(true)
|
|
expect(document.querySelector('mark')?.textContent).toBe('42 euros')
|
|
})
|
|
|
|
it('collectTranslatableElements() should skip elements with empty text', async () => {
|
|
document.body.innerHTML = '<p> </p><p>Texte reel</p>'
|
|
const elements = await collectTranslatableElements()
|
|
expect(elements.map((e) => e.text)).toEqual(['Texte reel'])
|
|
})
|
|
})
|
|
|
|
// Appels directs des fonctions injectees dans la page (exportees pour etre testables) : memes
|
|
// fonctions que celles envoyees a executeJavaScript via .toString(), executees ici directement
|
|
// contre le vrai `document` de jsdom pour une couverture de branches complete.
|
|
describe('dismissCookieBannerInPage()', () => {
|
|
beforeEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
it('should click the first matching phrase and return it', () => {
|
|
document.body.innerHTML = '<button>Tout accepter</button>'
|
|
expect(dismissCookieBannerInPage(['tout accepter'])).toBe('tout accepter')
|
|
})
|
|
|
|
it('should return null when no phrase matches any element', () => {
|
|
document.body.innerHTML = '<button>Parametres</button>'
|
|
expect(dismissCookieBannerInPage(['tout accepter', 'accepter'])).toBeNull()
|
|
})
|
|
|
|
it('should match a [role="button"] or an anchor, not only <button>', () => {
|
|
document.body.innerHTML = '<a role="button">J\'accepte</a>'
|
|
expect(dismissCookieBannerInPage(["j'accepte"])).toBe("j'accepte")
|
|
})
|
|
})
|
|
|
|
describe('clickByTextInPage()', () => {
|
|
beforeEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
it('should return false when there are no candidate elements at all', () => {
|
|
expect(clickByTextInPage('rien')).toBe(false)
|
|
})
|
|
|
|
it('should return false when candidates exist but none match', () => {
|
|
document.body.innerHTML = '<button>Autre chose</button>'
|
|
expect(clickByTextInPage('Introuvable')).toBe(false)
|
|
})
|
|
|
|
it('should prefer an exact match over a partial one when both exist', () => {
|
|
document.body.innerHTML = '<button>Suivant</button><button>Suivant encore</button>'
|
|
expect(clickByTextInPage('Suivant')).toBe(true)
|
|
expect((document.querySelectorAll('button')[0] as HTMLElement).onclick).toBeNull()
|
|
})
|
|
|
|
it('should match an input[type="button"] by its value attribute', () => {
|
|
document.body.innerHTML = '<input type="button" value="Annuler" />'
|
|
expect(clickByTextInPage('Annuler')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('collectInPage()', () => {
|
|
beforeEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
it('should respect the given limit', () => {
|
|
document.body.innerHTML = '<p>Un</p><p>Deux</p><p>Trois</p>'
|
|
expect(collectInPage(2)).toHaveLength(2)
|
|
})
|
|
|
|
it('should skip a non-leaf element containing nested block content', () => {
|
|
document.body.innerHTML = '<div><p>Paragraphe interne</p></div>'
|
|
const results = collectInPage(150)
|
|
expect(results.some((r) => r.text === 'Paragraphe interne')).toBe(true)
|
|
})
|
|
|
|
it('should return an empty array when the page has no matching elements', () => {
|
|
document.body.innerHTML = '<svg></svg>'
|
|
expect(collectInPage(150)).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('applyInPage()', () => {
|
|
it('should do nothing for an id that does not exist on the page', () => {
|
|
document.body.innerHTML = '<p>Original</p>'
|
|
expect(() => applyInPage([{ id: 'inexistant', text: 'Nouveau' }])).not.toThrow()
|
|
expect(document.querySelector('p')?.textContent).toBe('Original')
|
|
})
|
|
})
|
|
|
|
describe('highlightInPage()', () => {
|
|
beforeEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
it('should return false for an empty target string', () => {
|
|
expect(highlightInPage('')).toBe(false)
|
|
})
|
|
|
|
it('should return false when window.find reports no match', () => {
|
|
document.body.innerHTML = '<p>Rien ici</p>'
|
|
expect(highlightInPage('absent')).toBe(false)
|
|
})
|
|
|
|
it('should wrap the matching text node in a <mark> and scroll it into view', () => {
|
|
document.body.innerHTML = '<p>Le prix est de 42 euros aujourd-hui</p>'
|
|
expect(highlightInPage('42 euros')).toBe(true)
|
|
expect(document.querySelector('mark')?.textContent).toBe('42 euros')
|
|
})
|
|
|
|
it('should return false when a match is found but the selection has no range', () => {
|
|
const getSelectionSpy = vi.spyOn(window, 'getSelection').mockReturnValue(null)
|
|
document.body.innerHTML = '<p>Un texte a trouver ici</p>'
|
|
expect(highlightInPage('trouver')).toBe(false)
|
|
getSelectionSpy.mockRestore()
|
|
})
|
|
})
|