Skip to content

Commit 9dd15c7

Browse files
committed
fix: use queueMicrotask instead of setTimeout in story mocks
Replace setTimeout(..., 100) with queueMicrotask() for message delivery in Storybook mocks. This ensures messages arrive synchronously (in the microtask queue) rather than 100ms later, eliminating timing races with Chromatic's screenshot capture. - All onChat mock callbacks now use queueMicrotask for message delivery - Add chromatic: { delay: 500 } as backup for ActiveWorkspaceWithChat and MarkdownTables stories - Fix import: use storybook/test instead of @storybook/test (Storybook 10)
1 parent aaff93b commit 9dd15c7

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/browser/App.stories.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { createMockORPCClient, type MockORPCClientOptions } from "../../.storybo
1515
async function waitForChatScroll({ canvasElement }: { canvasElement: HTMLElement }) {
1616
const canvas = within(canvasElement);
1717

18-
// Wait for messages to appear (they load via setTimeout in mock)
18+
// Wait for messages to appear (they load via queueMicrotask in mock)
1919
await waitFor(
2020
() => {
2121
const messages = canvas.queryAllByTestId("chat-message");
@@ -319,6 +319,10 @@ export const ManyWorkspaces: Story = {
319319
* Use this story to test sidebar redesigns and ensure all data is visible.
320320
*/
321321
export const ActiveWorkspaceWithChat: Story = {
322+
parameters: {
323+
// Give Chromatic extra time for messages to load and scroll to complete
324+
chromatic: { delay: 500 },
325+
},
322326
render: () => {
323327
const workspaceId = "demo-workspace";
324328

@@ -563,7 +567,7 @@ main
563567
const onChat = (wsId: string, callback: (msg: WorkspaceChatMessage) => void) => {
564568
// Active workspace with complete chat history
565569
if (wsId === workspaceId) {
566-
setTimeout(() => {
570+
queueMicrotask(() => {
567571
// User message
568572
callback({
569573
type: "message",
@@ -978,7 +982,7 @@ main
978982
tokens: 65,
979983
timestamp: STABLE_TIMESTAMP - 140000,
980984
});
981-
}, 100);
985+
});
982986

983987
// Keep sending reasoning deltas to maintain streaming state
984988
// tokens: 0 to avoid flaky token counts in visual tests
@@ -998,7 +1002,7 @@ main
9981002
};
9991003
} else if (wsId === streamingWorkspaceId) {
10001004
// Streaming workspace - show active work in progress
1001-
setTimeout(() => {
1005+
queueMicrotask(() => {
10021006
const now = NOW; // Use stable timestamp
10031007

10041008
// Previous completed message with status_set (MUST be sent BEFORE caught-up)
@@ -1092,7 +1096,7 @@ main
10921096
tokens: 8,
10931097
timestamp: now - 500, // 0.5 seconds ago
10941098
});
1095-
}, 100);
1099+
});
10961100

10971101
// Keep sending deltas to maintain streaming state
10981102
// tokens: 0 to avoid flaky token counts in visual tests
@@ -1111,9 +1115,9 @@ main
11111115
return () => clearInterval(intervalId);
11121116
} else {
11131117
// Other workspaces - send caught-up immediately
1114-
setTimeout(() => {
1118+
queueMicrotask(() => {
11151119
callback({ type: "caught-up" });
1116-
}, 100);
1120+
});
11171121

11181122
return () => {
11191123
// Cleanup
@@ -1138,6 +1142,10 @@ main
11381142
* Shows various table formats without disruptive copy/download actions
11391143
*/
11401144
export const MarkdownTables: Story = {
1145+
parameters: {
1146+
// Give Chromatic extra time for messages to load and scroll to complete
1147+
chromatic: { delay: 500 },
1148+
},
11411149
render: () => {
11421150
const workspaceId = "my-app-feature";
11431151

@@ -1175,7 +1183,7 @@ export const MarkdownTables: Story = {
11751183
);
11761184

11771185
const onChat = (_wsId: string, emit: (msg: WorkspaceChatMessage) => void) => {
1178-
setTimeout(() => {
1186+
queueMicrotask(() => {
11791187
// User message
11801188
emit({
11811189
type: "message",
@@ -1267,7 +1275,7 @@ These tables should render cleanly without any disruptive copy or download actio
12671275

12681276
// Mark as caught up
12691277
emit({ type: "caught-up" });
1270-
}, 100);
1278+
});
12711279
};
12721280

12731281
return <AppWithMocks projects={projects} workspaces={workspaces} onChat={onChat} />;

0 commit comments

Comments
 (0)