@tuturuuu/ui 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/biome.json +1 -1
  3. package/package.json +75 -73
  4. package/src/components/ui/accordion.tsx +1 -1
  5. package/src/components/ui/breadcrumb.tsx +1 -1
  6. package/src/components/ui/calendar-app/calendar-page-shell.tsx +4 -0
  7. package/src/components/ui/calendar-app/components/calendar-connections-settings-content.tsx +239 -33
  8. package/src/components/ui/calendar-app/components/load-smart-scheduling-tasks.tsx +143 -0
  9. package/src/components/ui/calendar-app/components/priority-view.tsx +10 -3
  10. package/src/components/ui/calendar-app/components/tasks-sidebar.tsx +4 -116
  11. package/src/components/ui/calendar-app/components/use-calendar-connections-manager.ts +67 -2
  12. package/src/components/ui/calendar.tsx +1 -1
  13. package/src/components/ui/carousel.tsx +1 -1
  14. package/src/components/ui/chat/chat-agent-details-external-thread-panel.test.tsx +1 -1
  15. package/src/components/ui/chat/chat-agent-details-external-thread-panel.tsx +1 -1
  16. package/src/components/ui/chat/chat-agent-details-operations-panel.test.tsx +1 -1
  17. package/src/components/ui/chat/chat-agent-details-operations-panel.tsx +1 -1
  18. package/src/components/ui/chat/chat-agent-details-setup-panel.tsx +1 -1
  19. package/src/components/ui/chat/chat-agent-details-sidebar.test.tsx +1 -1
  20. package/src/components/ui/chat/chat-agent-details-sidebar.tsx +2 -2
  21. package/src/components/ui/chat/chat-agent-details-utils.test.ts +1 -1
  22. package/src/components/ui/chat/chat-agent-details-utils.tsx +1 -1
  23. package/src/components/ui/chat/chat-agent-details-zalo-personal-panel.tsx +2 -2
  24. package/src/components/ui/checkbox.tsx +1 -1
  25. package/src/components/ui/color-picker.tsx +1 -1
  26. package/src/components/ui/command.tsx +1 -1
  27. package/src/components/ui/context-menu.tsx +5 -1
  28. package/src/components/ui/currency-input.test.tsx +43 -0
  29. package/src/components/ui/currency-input.tsx +1 -1
  30. package/src/components/ui/custom/__tests__/settings-dialog-shell.test.tsx +3 -0
  31. package/src/components/ui/custom/__tests__/workspace-select-helpers.test.ts +19 -0
  32. package/src/components/ui/custom/combobox.test.tsx +195 -0
  33. package/src/components/ui/custom/combobox.tsx +273 -156
  34. package/src/components/ui/custom/education/modules/youtube/delete-link-button.tsx +5 -13
  35. package/src/components/ui/custom/facebook-mockup/facebook-mockup.tsx +7 -1
  36. package/src/components/ui/custom/facebook-mockup/form.tsx +1 -1
  37. package/src/components/ui/custom/facebook-mockup/image-upload-field.tsx +1 -1
  38. package/src/components/ui/custom/facebook-mockup/preview.tsx +1 -1
  39. package/src/components/ui/custom/settings-dialog-shell.tsx +2 -1
  40. package/src/components/ui/custom/theme-toggle.tsx +1 -1
  41. package/src/components/ui/custom/workspace-access/workspace-access-default-role-card.tsx +60 -35
  42. package/src/components/ui/custom/workspace-access/workspace-access-member-row.tsx +176 -167
  43. package/src/components/ui/custom/workspace-access/workspace-access-members.tsx +16 -10
  44. package/src/components/ui/custom/workspace-access/workspace-access-page-header.tsx +75 -36
  45. package/src/components/ui/custom/workspace-access/workspace-access-page.tsx +39 -42
  46. package/src/components/ui/custom/workspace-access/workspace-access-people-filters.tsx +1 -1
  47. package/src/components/ui/custom/workspace-access/workspace-access-roles.tsx +113 -91
  48. package/src/components/ui/custom/workspace-access/workspace-access-tabs-toolbar.tsx +73 -32
  49. package/src/components/ui/custom/workspace-select.tsx +8 -3
  50. package/src/components/ui/dialog.test.tsx +52 -0
  51. package/src/components/ui/dialog.tsx +6 -2
  52. package/src/components/ui/dropdown-menu.tsx +5 -1
  53. package/src/components/ui/finance/debts/debt-loan-form.tsx +12 -5
  54. package/src/components/ui/finance/debts/debt-loan-summary.tsx +3 -2
  55. package/src/components/ui/finance/debts/debts-page.test.tsx +54 -5
  56. package/src/components/ui/finance/debts/debts-page.tsx +15 -2
  57. package/src/components/ui/finance/invoices/components/subscription-group-selector.tsx +3 -5
  58. package/src/components/ui/finance/invoices/new-invoice-page.test.tsx +25 -5
  59. package/src/components/ui/finance/invoices/new-invoice-page.tsx +7 -2
  60. package/src/components/ui/finance/invoices/standard-invoice.tsx +4 -2
  61. package/src/components/ui/finance/invoices/subscription-invoice.tsx +4 -2
  62. package/src/components/ui/finance/invoices/utils.ts +3 -1
  63. package/src/components/ui/finance/transactions/form-content-dialog.tsx +3 -0
  64. package/src/components/ui/finance/transactions/form-types.ts +3 -0
  65. package/src/components/ui/finance/transactions/form.tsx +2 -0
  66. package/src/components/ui/finance/transactions/infinite-transactions-list.tsx +2 -0
  67. package/src/components/ui/finance/transactions/period-charts/category-breakdown-dialog.tsx +1 -1
  68. package/src/components/ui/finance/transactions/transaction-card.tsx +21 -9
  69. package/src/components/ui/finance/transactions/transaction-edit-dialog.tsx +1 -4
  70. package/src/components/ui/finance/transactions/transactions-create-summary.tsx +3 -0
  71. package/src/components/ui/finance/transactions/transactions-page.tsx +4 -1
  72. package/src/components/ui/finance/wallets/form.test.tsx +51 -3
  73. package/src/components/ui/finance/wallets/form.tsx +15 -4
  74. package/src/components/ui/finance/wallets/walletId/wallet-details-actions.tsx +4 -0
  75. package/src/components/ui/finance/wallets/walletId/wallet-details-page.tsx +4 -2
  76. package/src/components/ui/finance/wallets/wallets-data-table.tsx +1 -0
  77. package/src/components/ui/finance/wallets/wallets-page.tsx +5 -2
  78. package/src/components/ui/input-otp.tsx +1 -1
  79. package/src/components/ui/legacy/calendar/all-day-event-bar.tsx +28 -39
  80. package/src/components/ui/legacy/calendar/calendar-cell.tsx +2 -0
  81. package/src/components/ui/legacy/calendar/calendar-content.tsx +10 -6
  82. package/src/components/ui/legacy/calendar/calendar-header.tsx +23 -3
  83. package/src/components/ui/legacy/calendar/calendar-loading-skeleton.tsx +135 -0
  84. package/src/components/ui/legacy/calendar/calendar-matrix.tsx +175 -237
  85. package/src/components/ui/legacy/calendar/event-card.test.tsx +177 -0
  86. package/src/components/ui/legacy/calendar/event-card.tsx +220 -131
  87. package/src/components/ui/legacy/calendar/event-modal.tsx +17 -17
  88. package/src/components/ui/legacy/calendar/event-provider-display.tsx +69 -0
  89. package/src/components/ui/legacy/calendar/smart-calendar.test.tsx +86 -4
  90. package/src/components/ui/legacy/calendar/smart-calendar.tsx +32 -2
  91. package/src/components/ui/legacy/meet/create-plan-dialog.tsx +19 -10
  92. package/src/components/ui/money-input.test.tsx +64 -0
  93. package/src/components/ui/money-input.tsx +63 -0
  94. package/src/components/ui/navigation-menu.tsx +1 -1
  95. package/src/components/ui/pagination.tsx +1 -1
  96. package/src/components/ui/radio-group.tsx +1 -1
  97. package/src/components/ui/select.tsx +5 -1
  98. package/src/components/ui/sheet.tsx +1 -1
  99. package/src/components/ui/sidebar.tsx +1 -1
  100. package/src/components/ui/storefront/cart-popover.tsx +61 -0
  101. package/src/components/ui/storefront/cart-summary-parts.tsx +290 -0
  102. package/src/components/ui/storefront/cart-summary.tsx +104 -80
  103. package/src/components/ui/storefront/checkout-overlay.tsx +26 -0
  104. package/src/components/ui/storefront/hero-panel.tsx +2 -8
  105. package/src/components/ui/storefront/image-panel.tsx +6 -0
  106. package/src/components/ui/storefront/index.ts +11 -0
  107. package/src/components/ui/storefront/listing-card.tsx +84 -22
  108. package/src/components/ui/storefront/merch-sections.tsx +70 -0
  109. package/src/components/ui/storefront/product-detail.tsx +289 -0
  110. package/src/components/ui/storefront/product-dialog.tsx +72 -0
  111. package/src/components/ui/storefront/storefront-surface.test.tsx +221 -3
  112. package/src/components/ui/storefront/storefront-surface.tsx +288 -153
  113. package/src/components/ui/storefront/types.ts +27 -1
  114. package/src/components/ui/storefront/utils.ts +117 -27
  115. package/src/components/ui/text-editor/__tests__/content-migration.test.ts +32 -0
  116. package/src/components/ui/text-editor/__tests__/extensions.test.ts +123 -0
  117. package/src/components/ui/text-editor/__tests__/image-extension.test.ts +69 -1
  118. package/src/components/ui/text-editor/__tests__/video-extension.test.ts +47 -0
  119. package/src/components/ui/text-editor/background-color-extension.ts +62 -0
  120. package/src/components/ui/text-editor/color-controls.tsx +284 -0
  121. package/src/components/ui/text-editor/content-migration.ts +41 -18
  122. package/src/components/ui/text-editor/editor.tsx +69 -14
  123. package/src/components/ui/text-editor/extensions.ts +9 -3
  124. package/src/components/ui/text-editor/highlight-extension.ts +22 -0
  125. package/src/components/ui/text-editor/image-extension.ts +40 -18
  126. package/src/components/ui/text-editor/tool-bar.tsx +9 -16
  127. package/src/components/ui/text-editor/video-extension.ts +11 -2
  128. package/src/components/ui/toast.tsx +1 -1
  129. package/src/components/ui/tu-do/boards/__tests__/board-share-dialog.test.tsx +270 -0
  130. package/src/components/ui/tu-do/boards/__tests__/workspace-projects-client-page.test.tsx +70 -1
  131. package/src/components/ui/tu-do/boards/board-public-link-section.tsx +231 -0
  132. package/src/components/ui/tu-do/boards/board-share-dialog.tsx +222 -109
  133. package/src/components/ui/tu-do/boards/boardId/board-column-external-retry.test.tsx +127 -0
  134. package/src/components/ui/tu-do/boards/boardId/board-column.tsx +113 -46
  135. package/src/components/ui/tu-do/boards/boardId/kanban/bulk/bulk-mutations-clear-delete.ts +2 -0
  136. package/src/components/ui/tu-do/boards/boardId/kanban/bulk/bulk-mutations-move.ts +5 -0
  137. package/src/components/ui/tu-do/boards/boardId/kanban/bulk/bulk-mutations-updates.ts +3 -0
  138. package/src/components/ui/tu-do/boards/boardId/kanban/data/kanban-deadline-query.ts +50 -2
  139. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/__tests__/column-reorder.test.ts +17 -0
  140. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/column-reorder.ts +4 -1
  141. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/task-drag-cache.ts +51 -9
  142. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/task-drag-order.ts +2 -8
  143. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/task-sort-key.ts +47 -0
  144. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/use-kanban-dnd.test.ts +63 -0
  145. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/use-kanban-dnd.ts +127 -38
  146. package/src/components/ui/tu-do/boards/boardId/kanban/planner/__tests__/kanban-planner-island.test.tsx +380 -0
  147. package/src/components/ui/tu-do/boards/boardId/kanban/planner/kanban-planner-dialog.tsx +204 -0
  148. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-digest-panel.tsx +61 -0
  149. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-item-strip.tsx +54 -0
  150. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-plan-toolbar.tsx +251 -0
  151. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-scope-badge.tsx +27 -0
  152. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-section.tsx +58 -0
  153. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-share-dialog.tsx +238 -0
  154. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-target-controls.tsx +143 -0
  155. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-utils.ts +65 -0
  156. package/src/components/ui/tu-do/boards/boardId/kanban/planner/use-kanban-planner-state.ts +234 -0
  157. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-columns.test.tsx +410 -4
  158. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-columns.tsx +106 -14
  159. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-deadline-panels.tsx +443 -19
  160. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-skeleton.tsx +94 -32
  161. package/src/components/ui/tu-do/boards/boardId/kanban.tsx +213 -106
  162. package/src/components/ui/tu-do/boards/boardId/task-board-server-page.test.tsx +186 -0
  163. package/src/components/ui/tu-do/boards/boardId/task-board-server-page.tsx +59 -2
  164. package/src/components/ui/tu-do/boards/boardId/task-card/measured-task-card.tsx +3 -0
  165. package/src/components/ui/tu-do/boards/boardId/task-card/task-card-comparator.ts +3 -0
  166. package/src/components/ui/tu-do/boards/boardId/task-card/task-card.tsx +191 -28
  167. package/src/components/ui/tu-do/boards/boardId/task-filter.test.tsx +152 -0
  168. package/src/components/ui/tu-do/boards/boardId/task-filter.tsx +555 -545
  169. package/src/components/ui/tu-do/boards/boardId/task-list.tsx +7 -0
  170. package/src/components/ui/tu-do/boards/boardId/timeline/timeline-display.ts +9 -0
  171. package/src/components/ui/tu-do/boards/boardId/timeline/timeline-grid.tsx +8 -16
  172. package/src/components/ui/tu-do/boards/boardId/timeline/timeline-task-row.tsx +5 -25
  173. package/src/components/ui/tu-do/boards/boardId/timeline/timeline-utils.test.ts +36 -1
  174. package/src/components/ui/tu-do/boards/boardId/timeline/timeline-utils.ts +51 -2
  175. package/src/components/ui/tu-do/boards/share-section.tsx +100 -0
  176. package/src/components/ui/tu-do/boards/workspace-projects-client-page.tsx +13 -3
  177. package/src/components/ui/tu-do/drafts/draft-convert-dialog.tsx +10 -12
  178. package/src/components/ui/tu-do/drafts/drafts-page.tsx +33 -16
  179. package/src/components/ui/tu-do/initiatives/task-initiatives-client.tsx +56 -88
  180. package/src/components/ui/tu-do/my-tasks/my-tasks-content.tsx +26 -2
  181. package/src/components/ui/tu-do/my-tasks/use-my-tasks-state.ts +55 -8
  182. package/src/components/ui/tu-do/notes/note-edit-dialog.tsx +1 -4
  183. package/src/components/ui/tu-do/shared/__tests__/board-client.test.tsx +25 -0
  184. package/src/components/ui/tu-do/shared/__tests__/board-header.test.tsx +341 -38
  185. package/src/components/ui/tu-do/shared/__tests__/board-switcher.test.tsx +253 -0
  186. package/src/components/ui/tu-do/shared/__tests__/board-views.test.tsx +237 -3
  187. package/src/components/ui/tu-do/shared/__tests__/task-board-loading-state.test.tsx +17 -0
  188. package/src/components/ui/tu-do/shared/__tests__/task-legacy-route-recovery.test.tsx +16 -0
  189. package/src/components/ui/tu-do/shared/board-client.tsx +2 -7
  190. package/src/components/ui/tu-do/shared/board-config-storage.ts +7 -1
  191. package/src/components/ui/tu-do/shared/board-header.tsx +465 -937
  192. package/src/components/ui/tu-do/shared/board-layout-settings.tsx +165 -136
  193. package/src/components/ui/tu-do/shared/board-switcher.tsx +209 -217
  194. package/src/components/ui/tu-do/shared/board-views.tsx +596 -82
  195. package/src/components/ui/tu-do/shared/cursor-overlay-multi-wrapper.tsx +53 -12
  196. package/src/components/ui/tu-do/shared/list-view.tsx +227 -1
  197. package/src/components/ui/tu-do/shared/recycle-bin-panel.tsx +142 -94
  198. package/src/components/ui/tu-do/shared/special-task-list-pins.ts +51 -0
  199. package/src/components/ui/tu-do/shared/task-board-loading-state.tsx +28 -0
  200. package/src/components/ui/tu-do/shared/task-dialog-presentation.test.ts +53 -0
  201. package/src/components/ui/tu-do/shared/task-dialog-presentation.ts +19 -0
  202. package/src/components/ui/tu-do/shared/task-edit-dialog/components/compact-task-create-popover.test.tsx +57 -0
  203. package/src/components/ui/tu-do/shared/task-edit-dialog/components/compact-task-create-popover.tsx +136 -111
  204. package/src/components/ui/tu-do/shared/task-edit-dialog/components/task-description-editor.tsx +3 -1
  205. package/src/components/ui/tu-do/shared/task-edit-dialog/field-diff-viewer.tsx +3 -2
  206. package/src/components/ui/tu-do/shared/task-edit-dialog/selective-revert-panel.test.tsx +91 -0
  207. package/src/components/ui/tu-do/shared/task-edit-dialog/selective-revert-panel.tsx +123 -78
  208. package/src/components/ui/tu-do/shared/task-edit-dialog/task-activity-section.tsx +7 -1
  209. package/src/components/ui/tu-do/shared/task-edit-dialog/task-snapshot-dialog.tsx +8 -3
  210. package/src/components/ui/tu-do/shared/task-edit-dialog.tsx +44 -15
  211. package/src/components/ui/tu-do/shared/task-legacy-route-recovery.tsx +2 -9
  212. package/src/declarations.d.ts +1 -0
  213. package/src/hooks/__tests__/use-calendar-readonly.test.tsx +322 -2
  214. package/src/hooks/__tests__/use-calendar-sync.test.tsx +446 -0
  215. package/src/hooks/__tests__/useBoardRealtime.test.tsx +2 -2
  216. package/src/hooks/__tests__/useCursorTracking.test.tsx +212 -0
  217. package/src/hooks/use-calendar-sync.tsx +247 -243
  218. package/src/hooks/use-calendar.tsx +323 -138
  219. package/src/hooks/use-task-actions.ts +24 -0
  220. package/src/hooks/use-user-workspace-config.ts +75 -0
  221. package/src/hooks/use-workspace-currency.ts +8 -3
  222. package/src/hooks/useBoardRealtime.ts +6 -3
  223. package/src/hooks/useBoardRealtime.types.ts +11 -0
  224. package/src/hooks/useBoardRealtimeEventHandler.ts +11 -0
  225. package/src/hooks/useCursorTracking.ts +91 -27
  226. package/src/hooks/useTaskUserRealtime.ts +5 -3
@@ -0,0 +1,446 @@
1
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2
+ import { act, renderHook, waitFor } from '@testing-library/react';
3
+ import type { CalendarEvent } from '@tuturuuu/types/primitives/calendar-event';
4
+ import type { ReactNode } from 'react';
5
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
6
+ import { CalendarSyncProvider, useCalendarSync } from '../use-calendar-sync';
7
+
8
+ vi.mock('../../components/ui/sonner', () => ({
9
+ toast: {
10
+ error: vi.fn(),
11
+ info: vi.fn(),
12
+ },
13
+ }));
14
+
15
+ const createQueryClient = () =>
16
+ new QueryClient({
17
+ defaultOptions: {
18
+ queries: {
19
+ retry: false,
20
+ },
21
+ },
22
+ });
23
+
24
+ const createEvent = (
25
+ id: string,
26
+ overrides: Partial<CalendarEvent> = {}
27
+ ): CalendarEvent => ({
28
+ id,
29
+ title: 'Planning',
30
+ description: '',
31
+ start_at: '2026-06-22T09:00:00.000Z',
32
+ end_at: '2026-06-22T10:00:00.000Z',
33
+ color: 'BLUE',
34
+ ws_id: 'workspace-1',
35
+ ...overrides,
36
+ });
37
+
38
+ function renderCalendarSync({
39
+ externalEvents,
40
+ }: {
41
+ externalEvents?: CalendarEvent[];
42
+ } = {}) {
43
+ const queryClient = createQueryClient();
44
+
45
+ const wrapper = ({ children }: { children: ReactNode }) => (
46
+ <QueryClientProvider client={queryClient}>
47
+ <CalendarSyncProvider externalEvents={externalEvents} wsId="workspace-1">
48
+ {children}
49
+ </CalendarSyncProvider>
50
+ </QueryClientProvider>
51
+ );
52
+
53
+ return renderHook(() => useCalendarSync(), { wrapper });
54
+ }
55
+
56
+ function mockCalendarFetch(events: CalendarEvent[]) {
57
+ return vi.fn(async (input: RequestInfo | URL) => {
58
+ const url = String(input);
59
+
60
+ if (url.includes('/calendar/habit-events')) {
61
+ return {
62
+ ok: true,
63
+ json: async () => ({
64
+ completedHabitEventIds: [],
65
+ habitEventIds: [],
66
+ }),
67
+ } as Response;
68
+ }
69
+
70
+ if (url.includes('/calendar/events')) {
71
+ return {
72
+ ok: true,
73
+ json: async () => ({
74
+ count: events.length,
75
+ data: events,
76
+ }),
77
+ } as Response;
78
+ }
79
+
80
+ throw new Error(`Unexpected fetch ${url}`);
81
+ });
82
+ }
83
+
84
+ function createWeekDates(startIsoDate: string) {
85
+ return Array.from({ length: 7 }, (_, index) => {
86
+ const date = new Date(`${startIsoDate}T00:00:00.000Z`);
87
+ date.setUTCDate(date.getUTCDate() + index);
88
+ return date;
89
+ });
90
+ }
91
+
92
+ function mockCalendarFetchByWeek(
93
+ eventsByWeekStart: Record<string, CalendarEvent[]>
94
+ ) {
95
+ return vi.fn(async (input: RequestInfo | URL) => {
96
+ const url = new URL(String(input), 'http://localhost');
97
+
98
+ if (url.pathname.includes('/calendar/habit-events')) {
99
+ return {
100
+ ok: true,
101
+ json: async () => ({
102
+ completedHabitEventIds: [],
103
+ habitEventIds: [],
104
+ }),
105
+ } as Response;
106
+ }
107
+
108
+ if (url.pathname.includes('/calendar/events')) {
109
+ const startAt = url.searchParams.get('start_at');
110
+ const startAtTime = startAt ? new Date(startAt).getTime() : NaN;
111
+ const matchingEvents = Object.entries(eventsByWeekStart).find(
112
+ ([weekStart]) => {
113
+ const expectedStart = new Date(
114
+ `${weekStart}T00:00:00.000Z`
115
+ ).getTime();
116
+
117
+ return Math.abs(startAtTime - expectedStart) < 24 * 60 * 60 * 1000;
118
+ }
119
+ )?.[1];
120
+
121
+ return {
122
+ ok: true,
123
+ json: async () => ({
124
+ count: matchingEvents?.length ?? 0,
125
+ data: matchingEvents ?? [],
126
+ }),
127
+ } as Response;
128
+ }
129
+
130
+ throw new Error(`Unexpected fetch ${url}`);
131
+ });
132
+ }
133
+
134
+ describe('CalendarSyncProvider optimistic visible events', () => {
135
+ beforeEach(() => {
136
+ vi.clearAllMocks();
137
+ vi.stubGlobal('fetch', vi.fn());
138
+ });
139
+
140
+ it('keeps last successful events visible while a refetch is pending', async () => {
141
+ const initialEvent = createEvent('event-1');
142
+ let databaseCalls = 0;
143
+ let resolveRefetch:
144
+ | ((response: Pick<Response, 'json' | 'ok'>) => void)
145
+ | undefined;
146
+ const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
147
+ const url = String(input);
148
+
149
+ if (url.includes('/calendar/habit-events')) {
150
+ return {
151
+ ok: true,
152
+ json: async () => ({
153
+ completedHabitEventIds: [],
154
+ habitEventIds: [],
155
+ }),
156
+ } as Response;
157
+ }
158
+
159
+ if (url.includes('/calendar/events')) {
160
+ databaseCalls += 1;
161
+
162
+ if (databaseCalls === 1) {
163
+ return {
164
+ ok: true,
165
+ json: async () => ({
166
+ count: 1,
167
+ data: [initialEvent],
168
+ }),
169
+ } as Response;
170
+ }
171
+
172
+ return new Promise((resolve) => {
173
+ resolveRefetch = resolve;
174
+ });
175
+ }
176
+
177
+ throw new Error(`Unexpected fetch ${url}`);
178
+ });
179
+ vi.stubGlobal('fetch', fetchMock);
180
+
181
+ const { result } = renderCalendarSync();
182
+
183
+ act(() => {
184
+ result.current.setDates([new Date('2026-06-22T00:00:00.000Z')]);
185
+ });
186
+
187
+ await waitFor(() => {
188
+ expect(result.current.events.map((event) => event.id)).toEqual([
189
+ 'event-1',
190
+ ]);
191
+ });
192
+
193
+ act(() => {
194
+ result.current.refresh();
195
+ });
196
+
197
+ await waitFor(() => {
198
+ expect(databaseCalls).toBe(2);
199
+ });
200
+ expect(result.current.events.map((event) => event.id)).toEqual(['event-1']);
201
+
202
+ act(() => {
203
+ resolveRefetch?.({
204
+ ok: true,
205
+ json: async () => ({
206
+ count: 1,
207
+ data: [initialEvent],
208
+ }),
209
+ });
210
+ });
211
+ });
212
+
213
+ it('keeps last successful events visible and exposes error state on fetch failure', async () => {
214
+ const initialEvent = createEvent('event-1');
215
+ const fetchMock = mockCalendarFetch([initialEvent]);
216
+ vi.stubGlobal('fetch', fetchMock);
217
+
218
+ const { result } = renderCalendarSync();
219
+
220
+ act(() => {
221
+ result.current.setDates([new Date('2026-06-22T00:00:00.000Z')]);
222
+ });
223
+
224
+ await waitFor(() => {
225
+ expect(result.current.events).toHaveLength(1);
226
+ });
227
+
228
+ fetchMock.mockImplementation(async (input: RequestInfo | URL) => {
229
+ if (String(input).includes('/calendar/habit-events')) {
230
+ return {
231
+ ok: true,
232
+ json: async () => ({
233
+ completedHabitEventIds: [],
234
+ habitEventIds: [],
235
+ }),
236
+ } as Response;
237
+ }
238
+
239
+ return {
240
+ ok: false,
241
+ json: async () => ({ error: 'Nope' }),
242
+ } as Response;
243
+ });
244
+
245
+ act(() => {
246
+ result.current.refresh();
247
+ });
248
+
249
+ await waitFor(() => {
250
+ expect(result.current.syncStatus.state).toBe('error');
251
+ });
252
+ expect(result.current.events.map((event) => event.id)).toEqual(['event-1']);
253
+ });
254
+
255
+ it('shows the active week events when navigating away and back', async () => {
256
+ const currentWeekEvent = createEvent('current-week-event', {
257
+ start_at: '2026-06-22T09:00:00.000Z',
258
+ end_at: '2026-06-22T10:00:00.000Z',
259
+ });
260
+ const nextWeekEvent = createEvent('next-week-event', {
261
+ start_at: '2026-06-29T09:00:00.000Z',
262
+ end_at: '2026-06-29T10:00:00.000Z',
263
+ });
264
+ const fetchMock = mockCalendarFetchByWeek({
265
+ '2026-06-22': [currentWeekEvent],
266
+ '2026-06-29': [nextWeekEvent],
267
+ });
268
+ vi.stubGlobal('fetch', fetchMock);
269
+
270
+ const { result } = renderCalendarSync();
271
+
272
+ act(() => {
273
+ result.current.setDates(createWeekDates('2026-06-22'));
274
+ });
275
+
276
+ await waitFor(() => {
277
+ expect(result.current.events.map((event) => event.id)).toEqual([
278
+ 'current-week-event',
279
+ ]);
280
+ });
281
+
282
+ act(() => {
283
+ result.current.setDates(createWeekDates('2026-06-29'));
284
+ });
285
+
286
+ await waitFor(() => {
287
+ expect(result.current.events.map((event) => event.id)).toEqual([
288
+ 'next-week-event',
289
+ ]);
290
+ });
291
+
292
+ act(() => {
293
+ result.current.setDates(createWeekDates('2026-06-22'));
294
+ });
295
+
296
+ await waitFor(() => {
297
+ expect(result.current.events.map((event) => event.id)).toEqual([
298
+ 'current-week-event',
299
+ ]);
300
+ });
301
+
302
+ act(() => {
303
+ result.current.setDates(createWeekDates('2026-06-29'));
304
+ });
305
+
306
+ await waitFor(() => {
307
+ expect(result.current.events.map((event) => event.id)).toEqual([
308
+ 'next-week-event',
309
+ ]);
310
+ });
311
+ });
312
+
313
+ it('applies optimistic insert, update, and delete deltas immediately', async () => {
314
+ const baseEvent = createEvent('event-1');
315
+ const { result } = renderCalendarSync({ externalEvents: [baseEvent] });
316
+
317
+ await waitFor(() => {
318
+ expect(result.current.events).toHaveLength(1);
319
+ });
320
+
321
+ act(() => {
322
+ result.current.patchVisibleEvents(
323
+ [
324
+ createEvent('optimistic-1', {
325
+ title: 'Draft',
326
+ }),
327
+ ],
328
+ { status: 'creating' }
329
+ );
330
+ });
331
+
332
+ expect(result.current.events.map((event) => event.id)).toContain(
333
+ 'optimistic-1'
334
+ );
335
+ expect(
336
+ result.current.events.find((event) => event.id === 'optimistic-1')
337
+ ?._optimisticStatus
338
+ ).toBe('creating');
339
+
340
+ act(() => {
341
+ result.current.patchVisibleEvents(
342
+ [
343
+ {
344
+ ...baseEvent,
345
+ title: 'Moved',
346
+ },
347
+ ],
348
+ { status: 'updating' }
349
+ );
350
+ });
351
+
352
+ expect(
353
+ result.current.events.find((event) => event.id === 'event-1')
354
+ ).toMatchObject({
355
+ _optimisticStatus: 'updating',
356
+ title: 'Moved',
357
+ });
358
+
359
+ act(() => {
360
+ result.current.patchVisibleEvents([], { removeIds: ['event-1'] });
361
+ });
362
+
363
+ expect(result.current.events.some((event) => event.id === 'event-1')).toBe(
364
+ false
365
+ );
366
+
367
+ act(() => {
368
+ result.current.patchVisibleEvents([baseEvent]);
369
+ });
370
+
371
+ expect(
372
+ result.current.events.find((event) => event.id === 'event-1')
373
+ ).toMatchObject({
374
+ title: 'Planning',
375
+ });
376
+ });
377
+
378
+ it('reconciles optimistic updates with later server data', async () => {
379
+ let externalEvents = [createEvent('event-1')];
380
+ const queryClient = createQueryClient();
381
+ const wrapper = ({ children }: { children: ReactNode }) => (
382
+ <QueryClientProvider client={queryClient}>
383
+ <CalendarSyncProvider
384
+ externalEvents={externalEvents}
385
+ wsId="workspace-1"
386
+ >
387
+ {children}
388
+ </CalendarSyncProvider>
389
+ </QueryClientProvider>
390
+ );
391
+ const { rerender, result } = renderHook(() => useCalendarSync(), {
392
+ wrapper,
393
+ });
394
+
395
+ await waitFor(() => {
396
+ expect(result.current.events).toHaveLength(1);
397
+ });
398
+
399
+ act(() => {
400
+ result.current.patchVisibleEvents([
401
+ createEvent('event-1', { title: 'Server-confirmed' }),
402
+ ]);
403
+ });
404
+
405
+ expect(
406
+ result.current.events.find((event) => event.id === 'event-1')?.title
407
+ ).toBe('Server-confirmed');
408
+
409
+ externalEvents = [createEvent('event-1', { title: 'Server-confirmed' })];
410
+ rerender();
411
+
412
+ await waitFor(() => {
413
+ expect(
414
+ result.current.events.find((event) => event.id === 'event-1')
415
+ ?._optimisticStatus
416
+ ).toBeUndefined();
417
+ expect(
418
+ result.current.events.find((event) => event.id === 'event-1')?.title
419
+ ).toBe('Server-confirmed');
420
+ });
421
+ });
422
+
423
+ it('keeps identical title and time events as distinct IDs without deleting either', async () => {
424
+ const duplicateA = createEvent('event-a');
425
+ const duplicateB = createEvent('event-b');
426
+ const fetchMock = mockCalendarFetch([duplicateA, duplicateB]);
427
+ vi.stubGlobal('fetch', fetchMock);
428
+
429
+ const { result } = renderCalendarSync();
430
+
431
+ act(() => {
432
+ result.current.setDates([new Date('2026-06-22T00:00:00.000Z')]);
433
+ });
434
+
435
+ await waitFor(() => {
436
+ expect(result.current.events.map((event) => event.id).sort()).toEqual([
437
+ 'event-a',
438
+ 'event-b',
439
+ ]);
440
+ });
441
+ expect(fetchMock).not.toHaveBeenCalledWith(
442
+ expect.stringContaining('/calendar/events/event-b'),
443
+ expect.objectContaining({ method: 'DELETE' })
444
+ );
445
+ });
446
+ });
@@ -248,7 +248,7 @@ describe('useBoardRealtime', () => {
248
248
  expect(mockChannel.subscribe).toHaveBeenCalledTimes(1);
249
249
  });
250
250
 
251
- it('should create channel with self: false config', () => {
251
+ it('should create a private channel with self: false config', () => {
252
252
  renderHook(() => useBoardRealtime('board-1', { enabled: true }), {
253
253
  wrapper,
254
254
  });
@@ -258,7 +258,7 @@ describe('useBoardRealtime', () => {
258
258
  )();
259
259
  expect(supabaseInstance.channel).toHaveBeenCalledWith(
260
260
  'board-realtime-board-1',
261
- { config: { broadcast: { self: false } } }
261
+ { config: { broadcast: { self: false }, private: true } }
262
262
  );
263
263
  });
264
264
 
@@ -0,0 +1,212 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+
5
+ import { act, renderHook, waitFor } from '@testing-library/react';
6
+ import { createClient } from '@tuturuuu/supabase/next/client';
7
+ import type { RefObject } from 'react';
8
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
9
+ import { PRIVATE_TASK_REALTIME_CHANNEL_CONFIG } from '../useBoardRealtime.types';
10
+ import { useCursorTracking } from '../useCursorTracking';
11
+
12
+ type BroadcastListener = (message: {
13
+ payload: Record<string, unknown>;
14
+ }) => void;
15
+
16
+ type MockSupabaseClient = {
17
+ channel: ReturnType<typeof vi.fn>;
18
+ removeChannel: ReturnType<typeof vi.fn>;
19
+ };
20
+
21
+ type MockCreateClientFn = {
22
+ (): MockSupabaseClient;
23
+ mockReturnValue: (value: MockSupabaseClient) => void;
24
+ };
25
+
26
+ vi.mock('@tuturuuu/supabase/next/client', () => ({
27
+ createClient: vi.fn(),
28
+ }));
29
+
30
+ vi.mock('@tuturuuu/utils/constants', () => ({
31
+ DEV_MODE: false,
32
+ }));
33
+
34
+ function createContainerRef(): RefObject<HTMLDivElement | null> {
35
+ const element = document.createElement('div');
36
+ element.getBoundingClientRect = () =>
37
+ ({
38
+ bottom: 100,
39
+ height: 100,
40
+ left: 0,
41
+ right: 100,
42
+ top: 0,
43
+ width: 100,
44
+ x: 0,
45
+ y: 0,
46
+ }) as DOMRect;
47
+ document.body.append(element);
48
+ return { current: element };
49
+ }
50
+
51
+ describe('useCursorTracking', () => {
52
+ let broadcastListeners: Map<string, BroadcastListener>;
53
+ let mockChannel: {
54
+ on: ReturnType<typeof vi.fn>;
55
+ send: ReturnType<typeof vi.fn>;
56
+ subscribe: ReturnType<typeof vi.fn>;
57
+ };
58
+ let mockRemoveChannel: ReturnType<typeof vi.fn>;
59
+ let requestAnimationFrameMock: ReturnType<typeof vi.fn>;
60
+ let animationFrameCallbacks: FrameRequestCallback[];
61
+
62
+ beforeEach(() => {
63
+ broadcastListeners = new Map();
64
+ animationFrameCallbacks = [];
65
+
66
+ requestAnimationFrameMock = vi.fn((callback: FrameRequestCallback) => {
67
+ animationFrameCallbacks.push(callback);
68
+ return animationFrameCallbacks.length;
69
+ });
70
+ vi.stubGlobal('requestAnimationFrame', requestAnimationFrameMock);
71
+ vi.stubGlobal('cancelAnimationFrame', vi.fn());
72
+
73
+ mockChannel = {
74
+ on: vi.fn(
75
+ (
76
+ type: string,
77
+ config: { event?: string },
78
+ callback: BroadcastListener
79
+ ) => {
80
+ if (type === 'broadcast' && config.event) {
81
+ broadcastListeners.set(config.event, callback);
82
+ }
83
+ return mockChannel;
84
+ }
85
+ ),
86
+ send: vi.fn(),
87
+ subscribe: vi.fn(() => mockChannel),
88
+ };
89
+ mockRemoveChannel = vi.fn();
90
+
91
+ const mockCreateClient = createClient as unknown as MockCreateClientFn;
92
+ mockCreateClient.mockReturnValue({
93
+ channel: vi.fn(() => mockChannel),
94
+ removeChannel: mockRemoveChannel,
95
+ });
96
+ });
97
+
98
+ afterEach(() => {
99
+ document.body.replaceChildren();
100
+ vi.unstubAllGlobals();
101
+ });
102
+
103
+ it('subscribes to cursor channels with private realtime authorization', () => {
104
+ const containerRef = createContainerRef();
105
+
106
+ renderHook(() =>
107
+ useCursorTracking('board-realtime-board-1', containerRef, {
108
+ display_name: 'Current User',
109
+ id: 'user-current',
110
+ })
111
+ );
112
+
113
+ const supabaseInstance = (createClient as unknown as MockCreateClientFn)();
114
+ expect(supabaseInstance.channel).toHaveBeenCalledWith(
115
+ 'board-realtime-board-1',
116
+ PRIVATE_TASK_REALTIME_CHANNEL_CONFIG
117
+ );
118
+ });
119
+
120
+ it('broadcasts cursor payloads without private profile fields', async () => {
121
+ const containerRef = createContainerRef();
122
+
123
+ renderHook(() =>
124
+ useCursorTracking(
125
+ 'board-realtime-board-1',
126
+ containerRef,
127
+ {
128
+ avatar_url: 'https://example.com/avatar.png',
129
+ display_name: 'Current User',
130
+ email: 'current@example.com',
131
+ id: 'user-current',
132
+ },
133
+ { cursorScope: { boardId: 'board-1', type: 'board' } }
134
+ )
135
+ );
136
+
137
+ act(() => {
138
+ containerRef.current?.dispatchEvent(
139
+ new MouseEvent('mousemove', { clientX: 80, clientY: 40 })
140
+ );
141
+ animationFrameCallbacks.shift()?.(0);
142
+ });
143
+
144
+ await waitFor(() => expect(mockChannel.send).toHaveBeenCalled());
145
+ expect(mockChannel.send).toHaveBeenCalledWith({
146
+ event: 'cursor-move',
147
+ payload: {
148
+ metadata: { cursorScope: { boardId: 'board-1', type: 'board' } },
149
+ user: {
150
+ avatar_url: 'https://example.com/avatar.png',
151
+ display_name: 'Current User',
152
+ id: 'user-current',
153
+ },
154
+ x: expect.any(Number),
155
+ y: expect.any(Number),
156
+ },
157
+ type: 'broadcast',
158
+ });
159
+ });
160
+
161
+ it('accepts only well-formed cursor payloads and strips private fields', () => {
162
+ const containerRef = createContainerRef();
163
+ const { result } = renderHook(() =>
164
+ useCursorTracking('board-realtime-board-1', containerRef, {
165
+ display_name: 'Current User',
166
+ id: 'user-current',
167
+ })
168
+ );
169
+
170
+ const listener = broadcastListeners.get('cursor-move');
171
+ expect(listener).toBeDefined();
172
+
173
+ act(() => {
174
+ listener?.({
175
+ payload: {
176
+ user: { email: 'bad@example.com' },
177
+ x: 12,
178
+ y: 18,
179
+ },
180
+ });
181
+ });
182
+ expect(result.current.cursors.size).toBe(0);
183
+
184
+ act(() => {
185
+ listener?.({
186
+ payload: {
187
+ metadata: { cursorScope: { boardId: 'board-1', type: 'board' } },
188
+ user: {
189
+ avatar_url: 'https://example.com/other.png',
190
+ display_name: 'Other User',
191
+ email: 'other@example.com',
192
+ id: 'user-other',
193
+ },
194
+ x: 12,
195
+ y: 18,
196
+ },
197
+ });
198
+ });
199
+
200
+ expect(result.current.cursors.get('user-other')).toEqual({
201
+ lastUpdatedAt: expect.any(Number),
202
+ metadata: { cursorScope: { boardId: 'board-1', type: 'board' } },
203
+ user: {
204
+ avatar_url: 'https://example.com/other.png',
205
+ display_name: 'Other User',
206
+ id: 'user-other',
207
+ },
208
+ x: 12,
209
+ y: 18,
210
+ });
211
+ });
212
+ });