Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { describe, expect, it } from 'vitest';
import {
buildMediaPlayerWebViewCommand,
buildMediaPlayerWebViewHtml,
parseMediaPlayerWebViewMessage,
} from '@/session/mediaPlayerWebViewHtml';
import { createMediaPlayerWebViewLifecycle } from '@/session/mediaPlayerWebViewLifecycle';
describe('mediaPlayerWebView', () => {
it('reloads only when backgrounding interrupted an unfinished load', () => {
const lifecycle = createMediaPlayerWebViewLifecycle();
lifecycle.onBackground();
lifecycle.onLoadEnd();
expect(lifecycle.consumeReloadOnActive()).toBe(true);
expect(lifecycle.consumeReloadOnActive()).toBe(false);
lifecycle.onLoadEnd();
lifecycle.onBackground();
expect(lifecycle.consumeReloadOnActive()).toBe(false);
lifecycle.onLoadStart();
lifecycle.onBackground();
expect(lifecycle.consumeReloadOnActive()).toBe(true);
});
it('pauses when its page becomes inactive and never auto-resumes on return', () => {
const lifecycle = createMediaPlayerWebViewLifecycle();
// 翻到本页 / 重复可见:无动作 —— 不代用户播放。
expect(lifecycle.onVisibilityChange(true)).toBe(false);
// 滑到相邻页 / 屏被压栈:暂停一次。
expect(lifecycle.onVisibilityChange(false)).toBe(true);
// 持续失活不重复发暂停。
expect(lifecycle.onVisibilityChange(false)).toBe(false);
// 回到本页不自动续播(播放权交还用户),下次失活再暂停。
expect(lifecycle.onVisibilityChange(true)).toBe(false);
expect(lifecycle.onVisibilityChange(false)).toBe(true);
});
it('builds a video player document with controls and source metadata', () => {
const html = buildMediaPlayerWebViewHtml({
kind: 'video',
mimeType: 'video/mp4',
title: 'demo.mp4',
url: 'https://oss.example/demo.mp4?signature=1',
});
expect(html).toContain('<video controls playsinline');
expect(html).toContain('src="https://oss.example/demo.mp4?signature=1"');
expect(html).toContain('type="video/mp4"');
expect(html).toContain("type: 'xdt-media-player/status'");
expect(html).toContain("parsed.type !== 'xdt-media-player/command'");
expect(html).toContain("window.addEventListener('message', handleCommand)");
expect(html).toContain("document.addEventListener('message', handleCommand)");
expect(html).toContain("media.addEventListener('play'");
expect(html).toContain("media.addEventListener('timeupdate'");
expect(html).toContain('if (!media.paused) media.pause()');
expect(html).not.toContain('<audio');
});
it('builds structured player commands for native lifecycle events', () => {
expect(JSON.parse(buildMediaPlayerWebViewCommand('pause'))).toEqual({
type: 'xdt-media-player/command',
command: 'pause',
});
expect(JSON.parse(buildMediaPlayerWebViewCommand('reset'))).toEqual({
type: 'xdt-media-player/command',
command: 'reset',
});
});
it('builds an audio player document and escapes dynamic values', () => {
const html = buildMediaPlayerWebViewHtml({
kind: 'audio',
mimeType: 'audio/mpeg',
title: '"bad" <script>',
url: 'https://oss.example/a.mp3?x="><script>alert(1)</script>',
});
expect(html).toContain('<audio controls');
expect(html).toContain('"bad" <script>');
expect(html).toContain('https://oss.example/a.mp3?x="><script>alert(1)</script>');
expect(html).not.toContain('"><script>');
});
it('parses only structured player status messages from the WebView', () => {
expect(parseMediaPlayerWebViewMessage(JSON.stringify({
type: 'xdt-media-player/status',
state: 'playing',
currentTime: 12.6,
duration: 120.2,
}))).toEqual({
type: 'xdt-media-player/status',
state: 'playing',
currentTime: 12.6,
duration: 120.2,
error: undefined,
});
expect(parseMediaPlayerWebViewMessage(JSON.stringify({
type: 'xdt-media-player/status',
state: 'error',
currentTime: -1,
duration: Number.POSITIVE_INFINITY,
error: 'decode failed',
}))).toEqual({
type: 'xdt-media-player/status',
state: 'error',
currentTime: undefined,
duration: undefined,
error: 'decode failed',
});
expect(parseMediaPlayerWebViewMessage('not json')).toBeNull();
expect(parseMediaPlayerWebViewMessage(JSON.stringify({ type: 'other', state: 'playing' }))).toBeNull();
expect(parseMediaPlayerWebViewMessage(JSON.stringify({ type: 'xdt-media-player/status', state: 'seeking' }))).toBeNull();
});
});