forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatTranscript.test.tsx
More file actions
132 lines (118 loc) · 3.39 KB
/
Copy pathChatTranscript.test.tsx
File metadata and controls
132 lines (118 loc) · 3.39 KB
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
import { render } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { ChatTranscript } from '@/components/chat/ChatTranscript';
import type { ClientMessage } from '@/types/conversation';
vi.mock('@tschk/moonshine-next/image', () => ({
default: ({ alt }: React.ComponentProps<'img'>) => <img alt={alt} />,
}));
vi.mock('@/components/chat/ChatMarkdown', () => ({
ChatMarkdown: ({ children }: { children: string }) => <div>{children}</div>,
}));
function message(id: string, text: string): ClientMessage {
return {
id,
sender: 'ai',
text,
created_at: '2026-08-11T00:00:00Z',
type: 'text',
from_external_integration: false,
files: [],
memories: [],
ask_for_nps: false,
};
}
function Scroller({
children,
scrollTop,
}: {
children: React.ReactNode;
scrollTop: number;
}) {
return (
<div
data-testid="scroller"
style={{ overflowY: 'auto', height: 800 }}
ref={(node) => {
if (!node) return;
Object.defineProperty(node, 'scrollHeight', { value: 2000, configurable: true });
Object.defineProperty(node, 'clientHeight', { value: 800, configurable: true });
node.scrollTop = scrollTop;
}}
>
{children}
</div>
);
}
beforeEach(() => {
Element.prototype.scrollIntoView = vi.fn();
});
describe('ChatTranscript live-edge following', () => {
it('places a new exchange even when the scroller is not yet at the bottom', () => {
render(
<Scroller scrollTop={100}>
<ChatTranscript
messages={[message('ai-1', 'Hello')]}
isLoading={false}
isStreaming={false}
streamingText=""
currentThinking=""
/>
</Scroller>,
);
expect(Element.prototype.scrollIntoView).toHaveBeenCalled();
});
it('does not follow a stream once the reader has left the live edge', () => {
const { rerender } = render(
<Scroller scrollTop={100}>
<ChatTranscript
messages={[message('ai-1', 'Hello')]}
isLoading={false}
isStreaming
streamingText=""
currentThinking=""
/>
</Scroller>,
);
const scrollIntoView = Element.prototype.scrollIntoView as ReturnType<typeof vi.fn>;
scrollIntoView.mockClear();
rerender(
<Scroller scrollTop={100}>
<ChatTranscript
messages={[message('ai-1', 'Hello')]}
isLoading={false}
isStreaming
streamingText="Hello there"
currentThinking=""
/>
</Scroller>,
);
expect(scrollIntoView).not.toHaveBeenCalled();
});
it('keeps following while the scroller stays pinned to the live edge', () => {
const { rerender } = render(
<Scroller scrollTop={1200}>
<ChatTranscript
messages={[message('ai-1', 'Hello')]}
isLoading={false}
isStreaming
streamingText=""
currentThinking=""
/>
</Scroller>,
);
const scrollIntoView = Element.prototype.scrollIntoView as ReturnType<typeof vi.fn>;
scrollIntoView.mockClear();
rerender(
<Scroller scrollTop={1200}>
<ChatTranscript
messages={[message('ai-1', 'Hello')]}
isLoading={false}
isStreaming
streamingText="Hello there"
currentThinking=""
/>
</Scroller>,
);
expect(scrollIntoView).toHaveBeenCalled();
});
});