Spaces:
Running
Running
File size: 8,026 Bytes
21ad36a f3ad26c 21ad36a f3ad26c 21ad36a 897170b 21ad36a b4f163f f3ad26c b4f163f 7142bcd 897170b 7142bcd f3ad26c 7142bcd f3ad26c 21ad36a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it } from 'vitest';
import type { ModelLoadProgress, ModelOption } from '../../lib/contracts';
import { ChatSurface } from './ChatSurface';
const model: ModelOption = {
id: 'bonsai-1.7b',
manifestId: '1_7b',
label: 'Bonsai 1.7B',
architectureLabel: 'Qwen3 dense',
parameters: '1.7B',
runtimeLabel: 'WebGPU · WASM fallback',
footprint: '237 MiB',
availability: 'loaded',
contextLimit: 32_768,
defaultContext: 4_096,
};
const progress: ModelLoadProgress = {
modelId: model.id,
modelLabel: model.label,
stage: 'ready',
stageProgress: 1,
downloadedBytes: 1,
totalBytes: 1,
residentBytes: 1,
nativeStage: null,
shards: [],
};
describe('ChatSurface message actions', () => {
it('puts copy and role-specific regeneration controls under messages', () => {
const html = renderToStaticMarkup(
<ChatSurface
activeModel={model}
loadedModelId={model.id}
messages={[
{ id: 'user', role: 'user', content: 'Write code', timestamp: '10:00' },
{ id: 'assistant', role: 'assistant', content: 'Done', timestamp: '10:01' },
]}
streamState="complete"
modelLoadProgress={progress}
toolsEnabled={false}
runtimeStatus="Ready"
error={null}
shardRetry={null}
theme="light"
onLoadModel={() => undefined}
onDismissError={() => undefined}
onRetryShard={() => undefined}
onSend={() => undefined}
onRegenerate={() => undefined}
onEditUserMessage={() => undefined}
onCancel={() => undefined}
onToolsEnabledChange={() => undefined}
onOpenConversationList={() => undefined}
onOpenSettings={() => undefined}
/>,
);
expect(html.match(/aria-label="Copy message"/g)).toHaveLength(2);
expect(html).toContain('aria-label="Regenerate from this message"');
expect(html).toContain('aria-label="Regenerate this response"');
expect(html).toContain('aria-label="Edit message and regenerate response"');
expect(html).not.toContain('Regenerate last');
expect(html).toContain('aria-label="Show available tools"');
expect(html).toContain('<code>html_artifact</code>');
expect(html).toContain('<code>artifact_update</code>');
expect(html).toContain('<code>bar_chart</code>');
expect(html).toContain('<code>pie_chart</code>');
expect(html).toContain('<code>web_search</code>');
expect(html).toContain('Network tools ask before sending a query.');
});
it('keeps text visible and identifies a structured tool call while streaming', () => {
const html = renderToStaticMarkup(
<ChatSurface
activeModel={model}
loadedModelId={model.id}
messages={[
{
id: 'text',
role: 'assistant',
content: 'Visible streamed text',
timestamp: '10:00',
state: 'streaming',
},
{
id: 'tool',
role: 'assistant',
content: '',
timestamp: '10:01',
state: 'streaming',
toolActivity: { name: 'html_artifact', argumentCharacters: 12_345 },
},
]}
streamState="streaming"
modelLoadProgress={progress}
toolsEnabled
runtimeStatus="Generating"
error={null}
shardRetry={null}
theme="light"
onLoadModel={() => undefined}
onDismissError={() => undefined}
onRetryShard={() => undefined}
onSend={() => undefined}
onRegenerate={() => undefined}
onEditUserMessage={() => undefined}
onCancel={() => undefined}
onToolsEnabledChange={() => undefined}
onOpenConversationList={() => undefined}
onOpenSettings={() => undefined}
/>,
);
expect(html).toContain('Visible streamed text');
expect(html).toContain('Generating html_artifact tool call');
expect(html).toContain('<code>html_artifact</code>');
expect(html).toContain('12,345 chars');
expect(html.match(/stream-cursor/g)).toHaveLength(2);
});
it('renders generated artifacts inline with HTML and Rendered tabs', () => {
const html = renderToStaticMarkup(
<ChatSurface
activeModel={model}
loadedModelId={model.id}
messages={[{
id: 'assistant',
role: 'assistant',
content: 'Here is the app.',
timestamp: '10:01',
artifacts: [{
id: 'artifact-one',
title: 'Counter app',
source: '<main>Counter</main>',
sandboxedSource: '<!doctype html><main>Counter</main>',
runtimeToken: 'artifact-one-runtime',
createdAt: 1,
}],
}]}
streamState="complete"
modelLoadProgress={progress}
toolsEnabled
runtimeStatus="Ready"
error={null}
shardRetry={null}
theme="light"
onLoadModel={() => undefined}
onDismissError={() => undefined}
onRetryShard={() => undefined}
onSend={() => undefined}
onRegenerate={() => undefined}
onEditUserMessage={() => undefined}
onCancel={() => undefined}
onToolsEnabledChange={() => undefined}
onOpenConversationList={() => undefined}
onOpenSettings={() => undefined}
/>,
);
expect(html).toContain('id="artifact-artifact-one"');
expect(html).toContain('role="tab" aria-selected="true"');
expect(html).toContain('>HTML</button>');
expect(html).toContain('>Rendered</button>');
expect(html).toContain('<main>Counter</main>');
expect(html).not.toContain('<iframe');
});
it('keeps text, tool results, artifacts, and later text in chronological order', () => {
const html = renderToStaticMarkup(
<ChatSurface
activeModel={model}
loadedModelId={model.id}
messages={[{
id: 'assistant',
role: 'assistant',
content: 'Before the chart. After the chart.',
timestamp: '10:01',
segments: [
{ id: 'before', kind: 'text', content: 'Before the chart.' },
{ id: 'chart-call', kind: 'tool', toolCallId: 'tool-one' },
{ id: 'after', kind: 'text', content: 'After the chart.' },
],
tools: [{
id: 'tool-one',
name: 'bar_chart',
input: '{"title":"Quarterly sales"}',
output: '{"ok":true,"evaluation":{"status":"passed","errors":[]}}',
state: 'complete',
artifactId: 'artifact-one',
}],
artifacts: [{
id: 'artifact-one',
title: 'Quarterly sales',
source: '<main>Quarterly sales</main>',
sandboxedSource: '<!doctype html><main>Quarterly sales</main>',
runtimeToken: 'artifact-one-runtime',
createdAt: 1,
}],
}]}
streamState="complete"
modelLoadProgress={progress}
toolsEnabled
runtimeStatus="Ready"
error={null}
shardRetry={null}
theme="light"
onLoadModel={() => undefined}
onDismissError={() => undefined}
onRetryShard={() => undefined}
onSend={() => undefined}
onRegenerate={() => undefined}
onEditUserMessage={() => undefined}
onCancel={() => undefined}
onToolsEnabledChange={() => undefined}
onOpenConversationList={() => undefined}
onOpenSettings={() => undefined}
/>,
);
const before = html.indexOf('Before the chart.');
const tool = html.indexOf('<code>bar_chart</code>');
const artifact = html.indexOf('id="artifact-artifact-one"');
const after = html.indexOf('After the chart.');
expect(before).toBeGreaterThan(-1);
expect(tool).toBeGreaterThan(before);
expect(artifact).toBeGreaterThan(tool);
expect(after).toBeGreaterThan(artifact);
});
});
|