|
| 1 | +/** |
| 2 | + * Test Helper Utilities for Widget Generation |
| 3 | + * |
| 4 | + * These functions generate HTML and JavaScript content for testing purposes. |
| 5 | + * They are not part of the core UIResource creation flow. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { UIResourceDefinition } from '../../src/server/types/resource.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * Generate HTML content for a widget (utility function for tests) |
| 12 | + * |
| 13 | + * @param definition - Base UI resource definition |
| 14 | + * @param props - Widget properties to inject |
| 15 | + * @returns Generated HTML string |
| 16 | + */ |
| 17 | +export function generateWidgetHtml( |
| 18 | + definition: Pick<UIResourceDefinition, 'name' | 'title' | 'description' | 'size'>, |
| 19 | + props?: Record<string, any> |
| 20 | +): string { |
| 21 | + const [width = '100%', height = '400px'] = definition.size || [] |
| 22 | + const propsJson = props ? JSON.stringify(props) : '{}' |
| 23 | + |
| 24 | + return `<!DOCTYPE html> |
| 25 | +<html> |
| 26 | +<head> |
| 27 | + <meta charset="UTF-8"> |
| 28 | + <title>${definition.title || definition.name}</title> |
| 29 | + <style> |
| 30 | + body { |
| 31 | + margin: 0; |
| 32 | + padding: 20px; |
| 33 | + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
| 34 | + } |
| 35 | + .widget-container { |
| 36 | + width: ${width}; |
| 37 | + height: ${height}; |
| 38 | + border: 1px solid #e0e0e0; |
| 39 | + border-radius: 8px; |
| 40 | + overflow: auto; |
| 41 | + padding: 20px; |
| 42 | + background: white; |
| 43 | + } |
| 44 | + .widget-title { |
| 45 | + font-size: 1.5em; |
| 46 | + font-weight: 600; |
| 47 | + margin-bottom: 10px; |
| 48 | + } |
| 49 | + .widget-description { |
| 50 | + color: #666; |
| 51 | + margin-bottom: 20px; |
| 52 | + } |
| 53 | + </style> |
| 54 | +</head> |
| 55 | +<body> |
| 56 | + <div class="widget-container"> |
| 57 | + <div class="widget-title">${definition.title || definition.name}</div> |
| 58 | + ${definition.description ? `<div class="widget-description">${definition.description}</div>` : ''} |
| 59 | + <div id="widget-root"></div> |
| 60 | + </div> |
| 61 | + <script> |
| 62 | + // Widget props passed from server |
| 63 | + window.__WIDGET_PROPS__ = ${propsJson}; |
| 64 | +
|
| 65 | + // Placeholder for widget initialization |
| 66 | + console.log('Widget ${definition.name} loaded with props:', window.__WIDGET_PROPS__); |
| 67 | +
|
| 68 | + // Communication with parent window |
| 69 | + window.addEventListener('message', (event) => { |
| 70 | + console.log('Received message:', event.data); |
| 71 | + }); |
| 72 | +
|
| 73 | + // Example tool call |
| 74 | + function callTool(toolName, params) { |
| 75 | + window.parent.postMessage({ |
| 76 | + type: 'tool', |
| 77 | + payload: { toolName, params } |
| 78 | + }, '*'); |
| 79 | + } |
| 80 | + </script> |
| 81 | +</body> |
| 82 | +</html>` |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Generate a Remote DOM script for a widget (utility function for tests) |
| 87 | + * |
| 88 | + * @param definition - Base UI resource definition |
| 89 | + * @param props - Widget properties to inject |
| 90 | + * @returns Generated JavaScript string |
| 91 | + */ |
| 92 | +export function generateRemoteDomScript( |
| 93 | + definition: Pick<UIResourceDefinition, 'name' | 'title' | 'description'>, |
| 94 | + props?: Record<string, any> |
| 95 | +): string { |
| 96 | + return ` |
| 97 | +// Remote DOM script for ${definition.name} |
| 98 | +const container = document.createElement('div'); |
| 99 | +container.style.padding = '20px'; |
| 100 | +
|
| 101 | +// Create title |
| 102 | +const title = document.createElement('h2'); |
| 103 | +title.textContent = '${definition.title || definition.name}'; |
| 104 | +container.appendChild(title); |
| 105 | +
|
| 106 | +${definition.description ? ` |
| 107 | +// Add description |
| 108 | +const description = document.createElement('p'); |
| 109 | +description.textContent = '${definition.description}'; |
| 110 | +description.style.color = '#666'; |
| 111 | +container.appendChild(description); |
| 112 | +` : ''} |
| 113 | +
|
| 114 | +// Widget props |
| 115 | +const props = ${JSON.stringify(props || {})}; |
| 116 | +
|
| 117 | +// Create interactive button |
| 118 | +const button = document.createElement('ui-button'); |
| 119 | +button.setAttribute('label', 'Interact with ${definition.name}'); |
| 120 | +button.addEventListener('press', () => { |
| 121 | + window.parent.postMessage({ |
| 122 | + type: 'tool', |
| 123 | + payload: { |
| 124 | + toolName: 'ui_${definition.name}', |
| 125 | + params: props |
| 126 | + } |
| 127 | + }, '*'); |
| 128 | +}); |
| 129 | +container.appendChild(button); |
| 130 | +
|
| 131 | +// Add custom widget logic here |
| 132 | +console.log('Remote DOM widget ${definition.name} initialized with props:', props); |
| 133 | +
|
| 134 | +// Append to root |
| 135 | +root.appendChild(container);` |
| 136 | +} |
| 137 | + |
0 commit comments