Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import "@testing-library/jest-dom";
import { EmptyState } from "./EmptyState";
 
// Mock the external dependencies
jest.mock("@humansignal/ui", () => ({
  Button: ({ children, onClick, disabled, "data-testid": testId, ...props }: any) => (
    <button onClick={onClick} disabled={disabled} data-testid={testId} {...props}>
      {children}
    </button>
  ),
  Typography: ({ children, className, ...props }: any) => (
    <div className={className} {...props}>
      {children}
    </div>
  ),
  IconExternal: ({ width, height }: any) => <span data-testid="icon-external" width={width} height={height} />,
  Tooltip: ({ children, title }: any) => <div data-tooltip={title}>{children}</div>,
}));
 
jest.mock("@humansignal/icons", () => ({
  IconUpload: () => <span data-testid="icon-upload" />,
  IconLsLabeling: ({ width, height }: any) => <span data-testid="icon-ls-labeling" width={width} height={height} />,
  IconCheck: ({ width, height }: any) => <span data-testid="icon-check" width={width} height={height} />,
  IconSearch: ({ width, height }: any) => <span data-testid="icon-search" width={width} height={height} />,
  IconInbox: ({ width, height }: any) => <span data-testid="icon-inbox" width={width} height={height} />,
  IconCloudProviderS3: ({ width, height, className }: any) => (
    <span data-testid="icon-cloud-provider-s3" width={width} height={height} className={className} />
  ),
  IconCloudProviderGCS: ({ width, height, className }: any) => (
    <span data-testid="icon-cloud-provider-gcs" width={width} height={height} className={className} />
  ),
  IconCloudProviderAzure: ({ width, height, className }: any) => (
    <span data-testid="icon-cloud-provider-azure" width={width} height={height} className={className} />
  ),
  IconCloudProviderRedis: ({ width, height, className }: any) => (
    <span data-testid="icon-cloud-provider-redis" width={width} height={height} className={className} />
  ),
}));
 
jest.mock("../../../../../../editor/src/utils/docs", () => ({
  getDocsUrl: (path: string) => `https://docs.example.com/${path}`,
}));
 
// Mock AuthProvider/useAuth
jest.mock("@humansignal/core/providers/AuthProvider", () => ({
  useAuth: () => ({
    user: { id: 1, username: "testuser" },
    permissions: {
      can: (ability: string) => ability === "can_manage_storage", // Grant storage management permission by default
    },
    isLoading: false,
  }),
  ABILITY: {
    can_manage_storage: "can_manage_storage",
  },
}));
 
// Mock global window.APP_SETTINGS
Object.defineProperty(window, "APP_SETTINGS", {
  value: { whitelabel_is_active: false },
  writable: true,
});
 
describe("EmptyState Component", () => {
  const defaultProps = {
    canImport: true,
    onOpenSourceStorageModal: jest.fn(),
    onOpenImportModal: jest.fn(),
  };
 
  beforeEach(() => {
    jest.clearAllMocks();
  });
 
  describe("Basic Import Functionality", () => {
    it("should render the default import state when no role is specified", () => {
      render(<EmptyState {...defaultProps} />);
 
      // Check main title and description
      expect(screen.getByText("Import data to get your project started")).toBeInTheDocument();
      expect(screen.getByText("Connect your cloud storage or upload files from your computer")).toBeInTheDocument();
 
      // Check that storage provider icons are present
      expect(screen.getByTestId("dm-storage-provider-icons")).toBeInTheDocument();
 
      // Check that both buttons are present
      expect(screen.getByTestId("dm-connect-source-storage-button")).toBeInTheDocument();
      expect(screen.getByTestId("dm-import-button")).toBeInTheDocument();
 
      // Check accessibility features
      expect(screen.getByTestId("empty-state-label")).toHaveAttribute("aria-labelledby", "dm-empty-title");
      expect(screen.getByTestId("empty-state-label")).toHaveAttribute("aria-describedby", "dm-empty-desc");
    });
 
    it("should render non-interactive state when canImport is false", () => {
      render(<EmptyState {...defaultProps} canImport={false} />);
 
      const label = screen.getByTestId("empty-state-label");
      expect(label).toHaveAttribute("aria-labelledby", "dm-empty-title");
      expect(label).toHaveAttribute("aria-describedby", "dm-empty-desc");
 
      // Import button should not be present when canImport is false
      expect(screen.queryByTestId("dm-import-button")).not.toBeInTheDocument();
      // Connect Storage button should still be present
      expect(screen.getByTestId("dm-connect-source-storage-button")).toBeInTheDocument();
    });
 
    it("should render interactive state when canImport is true", () => {
      render(<EmptyState {...defaultProps} canImport={true} />);
 
      const label = screen.getByTestId("empty-state-label");
      expect(label).toHaveAttribute("aria-labelledby", "dm-empty-title");
      expect(label).toHaveAttribute("aria-describedby", "dm-empty-desc");
 
      // Both buttons should be present
      expect(screen.getByTestId("dm-connect-source-storage-button")).toBeInTheDocument();
      expect(screen.getByTestId("dm-import-button")).toBeInTheDocument();
    });
  });
 
  describe("Button Interactions", () => {
    it("should call onOpenSourceStorageModal when Connect Storage button is clicked", async () => {
      const user = userEvent.setup();
      const mockOpenStorage = jest.fn();
 
      render(<EmptyState {...defaultProps} onOpenSourceStorageModal={mockOpenStorage} />);
 
      const connectButton = screen.getByTestId("dm-connect-source-storage-button");
      await user.click(connectButton);
 
      expect(mockOpenStorage).toHaveBeenCalledTimes(1);
    });
 
    it("should call onOpenImportModal when Import button is clicked", async () => {
      const user = userEvent.setup();
      const mockOpenImport = jest.fn();
 
      render(<EmptyState {...defaultProps} onOpenImportModal={mockOpenImport} />);
 
      const importButton = screen.getByTestId("dm-import-button");
      await user.click(importButton);
 
      expect(mockOpenImport).toHaveBeenCalledTimes(1);
    });
  });
 
  describe("Role-Based Empty States", () => {
    describe("Filter-based Empty State", () => {
      it("should render filter empty state when hasFilters is true", () => {
        render(<EmptyState {...defaultProps} hasFilters={true} onClearFilters={jest.fn()} />);
 
        expect(screen.getByText("No tasks found")).toBeInTheDocument();
        expect(screen.getByText("Try adjusting or clearing the filters to see more results")).toBeInTheDocument();
        expect(screen.getByTestId("dm-clear-filters-button")).toBeInTheDocument();
        expect(screen.getByTestId("icon-search")).toBeInTheDocument();
      });
 
      it("should call onClearFilters when Clear Filters button is clicked", async () => {
        const user = userEvent.setup();
        const mockClearFilters = jest.fn();
 
        render(<EmptyState {...defaultProps} hasFilters={true} onClearFilters={mockClearFilters} />);
 
        const clearButton = screen.getByTestId("dm-clear-filters-button");
        await user.click(clearButton);
 
        expect(mockClearFilters).toHaveBeenCalledTimes(1);
      });
    });
 
    describe("Reviewer Role", () => {
      it("should render reviewer empty state", () => {
        render(<EmptyState {...defaultProps} userRole="REVIEWER" />);
 
        expect(screen.getByText("No tasks available for review or labeling")).toBeInTheDocument();
        expect(screen.getByText("Tasks imported to this project will appear here")).toBeInTheDocument();
        expect(screen.getByTestId("icon-check")).toBeInTheDocument();
      });
    });
 
    describe("Annotator Role", () => {
      it("should render annotator auto-distribution state with Label All Tasks button", () => {
        const mockLabelAllTasks = jest.fn();
        const project = {
          assignment_settings: {
            label_stream_task_distribution: "auto_distribution",
          },
        };
 
        render(
          <EmptyState {...defaultProps} userRole="ANNOTATOR" project={project} onLabelAllTasks={mockLabelAllTasks} />,
        );
 
        expect(screen.getByText("Start labeling tasks")).toBeInTheDocument();
        expect(screen.getByText("Tasks you've labeled will appear here")).toBeInTheDocument();
        expect(screen.getByTestId("dm-label-all-tasks-button")).toBeInTheDocument();
        expect(screen.getByTestId("icon-ls-labeling")).toBeInTheDocument();
      });
 
      it("should call onLabelAllTasks when Label All Tasks button is clicked", async () => {
        const user = userEvent.setup();
        const mockLabelAllTasks = jest.fn();
        const project = {
          assignment_settings: {
            label_stream_task_distribution: "auto_distribution",
          },
        };
 
        render(
          <EmptyState {...defaultProps} userRole="ANNOTATOR" project={project} onLabelAllTasks={mockLabelAllTasks} />,
        );
 
        const labelButton = screen.getByTestId("dm-label-all-tasks-button");
        await user.click(labelButton);
 
        expect(mockLabelAllTasks).toHaveBeenCalledTimes(1);
      });
 
      it("should render annotator manual distribution state without button", () => {
        const project = {
          assignment_settings: {
            label_stream_task_distribution: "assigned_only",
          },
        };
 
        render(<EmptyState {...defaultProps} userRole="ANNOTATOR" project={project} />);
 
        expect(screen.getByText("No tasks available")).toBeInTheDocument();
        expect(screen.getByText("Tasks assigned to you will appear here")).toBeInTheDocument();
        expect(screen.getByTestId("icon-inbox")).toBeInTheDocument();
        expect(screen.queryByTestId("dm-label-all-tasks-button")).not.toBeInTheDocument();
      });
 
      it("should render fallback annotator state for unknown distribution setting", () => {
        const project = {
          assignment_settings: {
            label_stream_task_distribution: "unknown_setting",
          },
        };
 
        render(<EmptyState {...defaultProps} userRole="ANNOTATOR" project={project} />);
 
        expect(screen.getByText("No tasks available")).toBeInTheDocument();
        expect(screen.getByText("Tasks will appear here when they become available")).toBeInTheDocument();
        expect(screen.getByTestId("icon-inbox")).toBeInTheDocument();
      });
    });
  });
 
  describe("Accessibility", () => {
    it("should have proper ARIA attributes", () => {
      render(<EmptyState {...defaultProps} />);
 
      const label = screen.getByTestId("empty-state-label");
      const title = screen.getByText("Import data to get your project started");
      const description = screen.getByText("Connect your cloud storage or upload files from your computer");
 
      expect(label).toHaveAttribute("aria-labelledby", "dm-empty-title");
      expect(label).toHaveAttribute("aria-describedby", "dm-empty-desc");
      expect(title).toHaveAttribute("id", "dm-empty-title");
      expect(description).toHaveAttribute("id", "dm-empty-desc");
    });
 
    it("should render documentation link with proper accessibility", () => {
      render(<EmptyState {...defaultProps} />);
 
      const docLink = screen.getByTestId("dm-docs-data-import-link");
      expect(docLink).toHaveAttribute("href", "https://docs.example.com/guide/tasks");
      expect(docLink).toHaveAttribute("target", "_blank");
      expect(docLink).toHaveAttribute("rel", "noopener noreferrer");
 
      const srText = docLink.querySelector(".sr-only");
      expect(srText).toHaveTextContent("(opens in a new tab)");
    });
  });
 
  describe("Conditional Content", () => {
    it("should hide documentation link when whitelabel is active", () => {
      // Mock whitelabel active
      Object.defineProperty(window, "APP_SETTINGS", {
        value: { whitelabel_is_active: true },
        writable: true,
      });
 
      render(<EmptyState {...defaultProps} />);
 
      expect(screen.queryByTestId("dm-docs-data-import-link")).not.toBeInTheDocument();
 
      // Reset for other tests
      Object.defineProperty(window, "APP_SETTINGS", {
        value: { whitelabel_is_active: false },
        writable: true,
      });
    });
 
    it("should show documentation link when whitelabel is not active", () => {
      render(<EmptyState {...defaultProps} />);
 
      expect(screen.getByTestId("dm-docs-data-import-link")).toBeInTheDocument();
    });
  });
 
  describe("Storage Provider Icons", () => {
    it("should render storage provider icons with proper tooltips", () => {
      render(<EmptyState {...defaultProps} />);
 
      const iconsContainer = screen.getByTestId("dm-storage-provider-icons");
      expect(iconsContainer).toBeInTheDocument();
 
      // Check for aria-labels on storage provider containers
      const s3Container = screen.getByLabelText("Amazon S3");
      const gcsContainer = screen.getByLabelText("Google Cloud Storage");
      const azureContainer = screen.getByLabelText("Azure Blob Storage");
      const redisContainer = screen.getByLabelText("Redis Storage");
 
      expect(s3Container).toBeInTheDocument();
      expect(gcsContainer).toBeInTheDocument();
      expect(azureContainer).toBeInTheDocument();
      expect(redisContainer).toBeInTheDocument();
    });
 
    it("should show storage icons in correct order", () => {
      render(<EmptyState {...defaultProps} />);
 
      const iconsContainer = screen.getByTestId("dm-storage-provider-icons");
      const iconContainers = iconsContainer.querySelectorAll("[aria-label]");
 
      expect(iconContainers).toHaveLength(4);
      expect(iconContainers[0]).toHaveAttribute("aria-label", "Amazon S3");
      expect(iconContainers[1]).toHaveAttribute("aria-label", "Google Cloud Storage");
      expect(iconContainers[2]).toHaveAttribute("aria-label", "Azure Blob Storage");
      expect(iconContainers[3]).toHaveAttribute("aria-label", "Redis Storage");
    });
  });
 
  describe("Button States and Props", () => {
    it("should render buttons with correct text content", () => {
      render(<EmptyState {...defaultProps} />);
 
      expect(screen.getByTestId("dm-connect-source-storage-button")).toHaveTextContent("Connect Cloud Storage");
      expect(screen.getByTestId("dm-import-button")).toHaveTextContent("Import");
    });
 
    it("should render Clear Filters button with correct text", () => {
      render(<EmptyState {...defaultProps} hasFilters={true} onClearFilters={jest.fn()} />);
 
      expect(screen.getByTestId("dm-clear-filters-button")).toHaveTextContent("Clear Filters");
    });
 
    it("should render Label All Tasks button with correct text and state", () => {
      const project = {
        assignment_settings: {
          label_stream_task_distribution: "auto_distribution",
        },
      };
 
      render(<EmptyState {...defaultProps} userRole="ANNOTATOR" project={project} onLabelAllTasks={jest.fn()} />);
 
      const labelButton = screen.getByTestId("dm-label-all-tasks-button");
      expect(labelButton).toHaveTextContent("Label All Tasks");
      expect(labelButton).not.toBeDisabled();
    });
  });
 
  describe("Edge Cases", () => {
    it("should handle missing project object gracefully", () => {
      render(<EmptyState {...defaultProps} userRole="ANNOTATOR" />);
 
      // Should render fallback state
      expect(screen.getByText("No tasks available")).toBeInTheDocument();
      expect(screen.getByText("Tasks will appear here when they become available")).toBeInTheDocument();
    });
 
    it("should handle missing assignment settings gracefully", () => {
      const project = {}; // No assignment_settings
 
      render(<EmptyState {...defaultProps} userRole="ANNOTATOR" project={project} />);
 
      // Should render fallback state
      expect(screen.getByText("No tasks available")).toBeInTheDocument();
      expect(screen.getByText("Tasks will appear here when they become available")).toBeInTheDocument();
    });
  });
});