test(page-controller): add happy-dom env and basic tests

This commit is contained in:
Simon
2026-06-11 21:10:05 +08:00
parent e65c7c9601
commit 56c09a9ae9
4 changed files with 129 additions and 0 deletions

View File

@@ -42,10 +42,14 @@
"homepage": "https://alibaba.github.io/page-agent/",
"scripts": {
"build": "vite build",
"test": "vitest run",
"prepublishOnly": "node ../../scripts/pre-publish.js",
"postpublish": "node ../../scripts/post-publish.js"
},
"dependencies": {
"ai-motion": "^0.4.8"
},
"devDependencies": {
"happy-dom": "^20.10.2"
}
}

View File

@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { PageController } from './PageController'
describe('PageController', () => {
it('constructs and exposes the current url', async () => {
const controller = new PageController()
expect(controller).toBeInstanceOf(PageController)
expect(await controller.getCurrentUrl()).toBe(window.location.href)
})
describe('executeJavascript', () => {
it('runs a script and returns its result', async () => {
const controller = new PageController()
const result = await controller.executeJavascript('return 1 + 2')
expect(result).toMatchObject({ success: true })
expect(result.message).toContain('3')
})
it('exposes the abort signal to the script scope', async () => {
const controller = new PageController()
const controllerSignal = new AbortController()
controllerSignal.abort()
const result = await controller.executeJavascript(
'return signal.aborted',
controllerSignal.signal
)
expect(result).toMatchObject({ success: true })
expect(result.message).toContain('true')
})
it('reports a syntax error as a failed result', async () => {
const controller = new PageController()
const result = await controller.executeJavascript('return (')
expect(result.success).toBe(false)
expect(result.message).toContain('❌')
})
})
})

View File

@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
name: 'page-controller',
environment: 'happy-dom',
include: ['src/**/*.test.ts'],
silent: 'passed-only',
},
})