Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion hooks/__tests__/use-welcome-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ describe('useWelcome', () => {
expect(MockWebSocketConstructor).toHaveBeenCalledWith('wss://test-url');
});

it('should default isNewSession to true and create WebSocket when omitted', () => {
const { isNewSession, ...propsWithoutIsNewSession } = defaultProps;

renderHook(() => useWelcome(propsWithoutIsNewSession));

expect(MockWebSocketConstructor).toHaveBeenCalledWith('wss://test-url');
});

it('should not create WebSocket when isNewSession is false', () => {
renderHook(() =>
useWelcome({
Expand Down Expand Up @@ -404,7 +412,7 @@ describe('useWelcome', () => {
'Normal closure',
);

// Should have loaded guided prompts
// Should have loaded guided prompts with the real username passed through
expect(mockLoadGuidedPrompts).toHaveBeenCalledWith({
org: 'tenant-1',
sessionId: 'session-123',
Expand All @@ -414,6 +422,91 @@ describe('useWelcome', () => {
vi.useRealTimers();
});

it('should pass through a real username to guided prompts on eos (issue #2148)', async () => {
vi.useFakeTimers();

mockUseMentorSettings.mockReturnValue({
data: {
greetingMethod: 'proactive_prompt',
proactiveResponse: null,
},
});

mockWebSocketInstance.readyState = WebSocket.OPEN;

renderHook(() =>
useWelcome({
...defaultProps,
username: 'jdoe',
}),
);

act(() => {
mockWebSocketInstance.onmessage?.(
new MessageEvent('message', {
data: JSON.stringify({ eos: true }),
}),
);
});

await act(async () => {
vi.advanceTimersByTime(200);
});

expect(mockLoadGuidedPrompts).toHaveBeenCalledWith({
org: 'tenant-1',
sessionId: 'session-123',
userId: 'jdoe',
});

vi.useRealTimers();
});

it('should fall back to "anonymous" when username is empty in embed mode on eos (issue #2148)', async () => {
vi.useFakeTimers();

mockUseMentorSettings.mockReturnValue({
data: {
greetingMethod: 'proactive_prompt',
proactiveResponse: null,
},
});

mockWebSocketInstance.readyState = WebSocket.OPEN;

// Embed mode passes username as an empty string before hydration.
renderHook(() =>
useWelcome({
...defaultProps,
username: '',
}),
);

act(() => {
mockWebSocketInstance.onmessage?.(
new MessageEvent('message', {
data: JSON.stringify({ eos: true }),
}),
);
});

await act(async () => {
vi.advanceTimersByTime(200);
});

// Must never send an empty userId (would produce /users//sessions/ -> 404).
expect(mockLoadGuidedPrompts).toHaveBeenCalledWith({
org: 'tenant-1',
sessionId: 'session-123',
userId: 'anonymous',
});
expect(mockLoadGuidedPrompts).not.toHaveBeenCalledWith(
expect.objectContaining({ userId: '' }),
);

vi.useRealTimers();
});

it('should close WebSocket with explicit code 1000 on unmount cleanup', () => {
mockUseMentorSettings.mockReturnValue({
data: {
Expand Down
5 changes: 3 additions & 2 deletions hooks/use-welcome-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ export default function useWelcome({
if (response?.eos) {
setTimeout(() => {
_endConnection();
const guidedUserId = username || 'anonymous';
loadGuidedPrompts({
org: tenantKey,
sessionId,
// @ts-ignore
userId: username,
// @ts-ignore - userId is a valid runtime path param not yet in the query arg type
userId: guidedUserId,
});
}, 200);
}
Expand Down
Loading