import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
import {
cesiumBrowserToolContracts,
cesiumCoreToolContracts,
} from '../../packages/cesium-mcp-contracts/src/index.js'
const html = readFileSync(new URL('./index.html', import.meta.url), 'utf8')
describe('browser-agent startup order', () => {
it('enables the WebMCP origin trial for remote scanners', () => {
const token = html.match(//)?.[1]
expect(token).toBeTruthy()
const decoded = Buffer.from(token!, 'base64').toString('utf8')
expect(decoded).toContain('https://cesium-browser-agent.pages.dev:443')
expect(decoded).toContain('"feature":"WebMCP"')
})
it('publishes a branded site icon for directories and browser tabs', () => {
expect(html).toContain('')
expect(html).toContain('')
expect(html).toContain('https://cesium-browser-agent.pages.dev/favicon.svg')
})
it('registers WebMCP without starting Cesium during page load', () => {
const bootstrapStart = html.indexOf('async function bootstrap()')
const registration = html.indexOf('await registerPageWebMcpTools()', bootstrapStart)
const bootstrapEnd = html.indexOf('\n}', bootstrapStart)
const bootstrap = html.slice(bootstrapStart, bootstrapEnd)
expect(bootstrapStart).toBeGreaterThan(-1)
expect(registration).toBeGreaterThan(bootstrapStart)
expect(bootstrap).not.toContain('initCesium()')
expect(bootstrap).toContain('armCesiumActivation()')
})
it('does not parser-block startup on the remote Cesium script', () => {
expect(html).not.toContain('')
expect(html).toContain('id="toolModeSelect"')
expect(html).toContain("value=\"auto\"")
expect(html).toContain("value=\"core\"")
expect(html).toContain("value=\"all\"")
expect(html).toContain('CesiumToolRouter.resolveToolSelection')
expect(html).toContain('tools: activeToolSelection.tools')
})
it('retries incomplete tool responses without rendering raw tool markup', () => {
expect(html).toContain('')
expect(html).toContain('CesiumAgentResponse.requestValidChoice')
expect(html).toContain("maxRetries: 1")
expect(html).toContain("err.code === 'INCOMPLETE_TOOL_RESPONSE'")
expect(html).toContain('Do not generate large inline CZML')
expect(html).toContain('不要生成大段内联 CZML')
})
it('rewrites the allowlisted HTTP tiles origin for both execution paths', () => {
expect(html).toContain("'http://jojo1986.cn:8888': 'jojo'")
expect(html).toContain('function prepareCommandParams(action, params)')
expect(html).toContain('CesiumToolRouter.rewriteAssetUrl')
expect(html.match(/prepareCommandParams\(/g)).toHaveLength(3)
})
})