forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseScreenFrames.test.tsx
More file actions
167 lines (137 loc) · 5.79 KB
/
Copy pathuseScreenFrames.test.tsx
File metadata and controls
167 lines (137 loc) · 5.79 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
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
import { act, renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useScreenFrames } from '@/hooks/useScreenFrames';
import type {
ConversationScreenFrame,
ConversationScreenFrameSet,
} from '@/types/conversation';
vi.mock('@/lib/api', () => ({
getConversationScreenFrames: vi.fn(),
deleteScreenFrame: vi.fn(),
deleteAllScreenFrames: vi.fn(),
patchScreenFrameSharing: vi.fn(),
}));
const api = await import('@/lib/api');
function frame(id: string): ConversationScreenFrame {
return {
id,
captured_at: '2026-08-24T10:00:00Z',
role: 'strip',
rank: 0,
caption: `caption-${id}`,
labels: [],
source_badge: null,
focal_region: null,
width: 1600,
height: 900,
content_url: `https://example.com/${id}.jpg`,
thumbnail_url: `https://example.com/${id}_thumb.jpg`,
url_expires_at: '2026-08-24T11:00:00Z',
ground: { stops: ['#101010', '#202020'], is_neutral: false },
};
}
function frameSet(
overrides: Partial<ConversationScreenFrameSet> = {},
): ConversationScreenFrameSet {
return { revision: 1, banner: null, strip: [frame('a'), frame('b')], ...overrides };
}
async function renderLoaded(conversationId = 'conv-1', initial = frameSet()) {
vi.mocked(api.getConversationScreenFrames).mockResolvedValue(initial);
const view = renderHook(() => useScreenFrames(conversationId));
await waitFor(() => expect(view.result.current.loading).toBe(false));
return view;
}
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(console, 'error').mockImplementation(() => {});
});
describe('useScreenFrames', () => {
it('loads the frame set on mount', async () => {
const { result } = await renderLoaded();
expect(result.current.frameSet?.strip).toHaveLength(2);
expect(result.current.error).toBeNull();
expect(api.getConversationScreenFrames).toHaveBeenCalledWith('conv-1');
});
it('does not fetch when disabled or conversationId is null', async () => {
const { result } = renderHook(() => useScreenFrames(null));
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.frameSet).toBeNull();
expect(api.getConversationScreenFrames).not.toHaveBeenCalled();
});
it('surfaces a load failure instead of hanging in a loading state', async () => {
vi.mocked(api.getConversationScreenFrames).mockRejectedValue(new Error('offline'));
const { result } = renderHook(() => useScreenFrames('conv-1'));
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.error).toBe('offline');
expect(result.current.frameSet).toBeNull();
});
it('replaces local state with the server response after deleting a frame', async () => {
const { result } = await renderLoaded();
const updated = frameSet({ revision: 2, strip: [frame('b')] });
vi.mocked(api.deleteScreenFrame).mockResolvedValue(updated);
let success = false;
await act(async () => {
success = await result.current.deleteFrame('a');
});
expect(success).toBe(true);
expect(api.deleteScreenFrame).toHaveBeenCalledWith('conv-1', 'a');
expect(result.current.frameSet).toEqual(updated);
});
it('surfaces (and does not clear existing state on) a failed frame delete', async () => {
const { result } = await renderLoaded();
vi.mocked(api.deleteScreenFrame).mockRejectedValue(new Error('conflict'));
let success = true;
await act(async () => {
success = await result.current.deleteFrame('a');
});
expect(success).toBe(false);
expect(result.current.error).toBe('conflict');
expect(result.current.frameSet?.strip).toHaveLength(2);
});
it('replaces local state after deleting every frame', async () => {
const { result } = await renderLoaded();
const emptied = frameSet({ revision: 3, banner: null, strip: [] });
vi.mocked(api.deleteAllScreenFrames).mockResolvedValue(emptied);
await act(async () => {
await result.current.deleteAll();
});
expect(api.deleteAllScreenFrames).toHaveBeenCalledWith('conv-1');
expect(result.current.frameSet?.strip).toHaveLength(0);
});
it('patches sharing and replaces local state with the response', async () => {
const { result } = await renderLoaded();
const updated = frameSet({ revision: 4 });
vi.mocked(api.patchScreenFrameSharing).mockResolvedValue(updated);
await act(async () => {
await result.current.setSharingEnabled(false);
});
expect(api.patchScreenFrameSharing).toHaveBeenCalledWith('conv-1', false);
expect(result.current.frameSet).toEqual(updated);
});
it('ignores a stale response for a conversation the caller has moved away from', async () => {
vi.mocked(api.getConversationScreenFrames).mockResolvedValueOnce(frameSet());
const { result, rerender } = renderHook(({ id }) => useScreenFrames(id), {
initialProps: { id: 'conv-1' },
});
await waitFor(() => expect(result.current.loading).toBe(false));
let resolveSecond: ((value: ConversationScreenFrameSet) => void) | undefined;
vi.mocked(api.getConversationScreenFrames).mockReturnValueOnce(
new Promise((resolve) => {
resolveSecond = resolve;
}),
);
rerender({ id: 'conv-2' });
await waitFor(() =>
expect(api.getConversationScreenFrames).toHaveBeenCalledWith('conv-2'),
);
// Navigate away again before the in-flight request for conv-2 resolves.
rerender({ id: 'conv-3' });
vi.mocked(api.getConversationScreenFrames).mockResolvedValueOnce(frameSet());
await act(async () => {
resolveSecond?.(frameSet({ revision: 99, strip: [frame('stale')] }));
await Promise.resolve();
});
// The stale conv-2 response must not have landed once conv-3 is current.
expect(result.current.frameSet?.strip?.map((f) => f.id)).not.toEqual(['stale']);
});
});