@ploykit/ui 0.5.0 → 0.6.2

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 (222) hide show
  1. package/dist/AppProviders.js +2 -2
  2. package/dist/components/ConfirmDialog.js +1 -1
  3. package/dist/components/CronInput.js +3 -3
  4. package/dist/components/DataTable.js +4 -4
  5. package/dist/components/FormField.js +1 -1
  6. package/dist/components/ImpersonationBanner.js +1 -1
  7. package/dist/components/NotificationBell.js +1 -1
  8. package/dist/components/OnboardingChecklist.js +3 -3
  9. package/dist/components/Page.js +2 -2
  10. package/dist/components/ReauthDialog.js +4 -4
  11. package/dist/components/SecretModal.js +2 -2
  12. package/dist/components/SettingsCard.js +2 -2
  13. package/dist/components/SiteBanner.js +1 -1
  14. package/dist/components/SiteBannerLayer.js +1 -1
  15. package/dist/components/StatusBadge.js +1 -1
  16. package/dist/components/UserMenu.js +3 -3
  17. package/dist/components/WorkspaceSwitcher.js +7 -7
  18. package/dist/components/ui/alert-dialog.js +2 -2
  19. package/dist/components/ui/avatar.js +1 -1
  20. package/dist/components/ui/badge.js +1 -1
  21. package/dist/components/ui/breadcrumb.js +1 -1
  22. package/dist/components/ui/button.js +1 -1
  23. package/dist/components/ui/card.js +1 -1
  24. package/dist/components/ui/checkbox.js +1 -1
  25. package/dist/components/ui/dialog.js +2 -2
  26. package/dist/components/ui/dropdown-menu.js +1 -1
  27. package/dist/components/ui/empty.js +1 -1
  28. package/dist/components/ui/field.js +3 -3
  29. package/dist/components/ui/input.js +1 -1
  30. package/dist/components/ui/label.js +1 -1
  31. package/dist/components/ui/popover.js +1 -1
  32. package/dist/components/ui/progress.js +1 -1
  33. package/dist/components/ui/select.js +1 -1
  34. package/dist/components/ui/separator.js +1 -1
  35. package/dist/components/ui/sheet.js +2 -2
  36. package/dist/components/ui/sidebar.js +8 -8
  37. package/dist/components/ui/skeleton.js +1 -1
  38. package/dist/components/ui/switch.js +1 -1
  39. package/dist/components/ui/tabs.js +1 -1
  40. package/dist/components/ui/textarea.js +1 -1
  41. package/dist/components/ui/tooltip.js +1 -1
  42. package/dist/index.js +78 -78
  43. package/dist/layouts/AppShell.js +4 -4
  44. package/dist/layouts/SettingsShell.js +4 -4
  45. package/dist/pages/LoginPage.js +5 -5
  46. package/dist/pages/account/NotificationPreferencesPage.js +3 -3
  47. package/dist/pages/account/ProfileSettingsPage.js +9 -9
  48. package/dist/pages/account/SecuritySettingsPage.js +13 -13
  49. package/dist/pages/account/TokensPage.js +12 -12
  50. package/dist/pages/admin/AdminSettingsPage.js +11 -11
  51. package/dist/pages/admin/AdminShell.js +3 -3
  52. package/dist/pages/admin/AuditPage.js +8 -8
  53. package/dist/pages/admin/OrdersPage.js +12 -12
  54. package/dist/pages/admin/OverviewPage.js +13 -13
  55. package/dist/pages/admin/SSOPage.js +6 -6
  56. package/dist/pages/admin/UserDetailPage.js +13 -13
  57. package/dist/pages/admin/UsersPage.js +13 -13
  58. package/dist/pages/admin/WebhookDeliveriesPage.js +9 -9
  59. package/dist/pages/admin/WorkspaceDetailPage.js +14 -14
  60. package/dist/pages/admin/WorkspacesPage.js +9 -9
  61. package/dist/pages/admin/shared.js +1 -1
  62. package/dist/pages/auth/ForgotPasswordPage.js +4 -4
  63. package/dist/pages/auth/InviteAcceptPage.js +6 -6
  64. package/dist/pages/auth/RegisterPage.js +7 -7
  65. package/dist/pages/auth/ResetPasswordPage.js +5 -5
  66. package/dist/pages/auth/VerifyEmailPage.js +3 -3
  67. package/dist/pages/workspace/BillingPage.js +11 -11
  68. package/dist/pages/workspace/BillingSuccessPage.js +2 -2
  69. package/dist/pages/workspace/MembersPage.js +15 -15
  70. package/dist/pages/workspace/RolePermissionsPage.js +9 -9
  71. package/dist/pages/workspace/SchedulesPage.js +12 -12
  72. package/dist/pages/workspace/UsagePage.js +6 -6
  73. package/dist/pages/workspace/WebhooksPage.js +16 -16
  74. package/dist/pages/workspace/WorkspaceAuditPage.js +7 -7
  75. package/dist/pages/workspace/WorkspaceGeneralPage.js +9 -9
  76. package/package.json +6 -5
  77. package/src/AppProviders.tsx +29 -0
  78. package/src/components/ConfirmDialog.tsx +77 -0
  79. package/src/components/CronInput.tsx +195 -0
  80. package/src/components/DataTable.tsx +152 -0
  81. package/src/components/ErrorBoundary.tsx +46 -0
  82. package/src/components/FormField.tsx +56 -0
  83. package/src/components/ImpersonationBanner.tsx +23 -0
  84. package/src/components/NotificationBell.tsx +135 -0
  85. package/src/components/OnboardingChecklist.tsx +114 -0
  86. package/src/components/Page.tsx +67 -0
  87. package/src/components/ReauthDialog.tsx +94 -0
  88. package/src/components/SecretModal.tsx +147 -0
  89. package/src/components/SettingsCard.tsx +39 -0
  90. package/src/components/SiteBanner.tsx +32 -0
  91. package/src/components/SiteBannerLayer.tsx +29 -0
  92. package/src/components/StatusBadge.tsx +12 -0
  93. package/src/components/UserMenu.tsx +103 -0
  94. package/src/components/WorkspaceSwitcher.tsx +159 -0
  95. package/src/components/__tests__/AppProviders.test.tsx +111 -0
  96. package/src/components/__tests__/CronInput.test.tsx +180 -0
  97. package/src/components/__tests__/DataTable.test.tsx +208 -0
  98. package/src/components/__tests__/ErrorBoundary.test.tsx +61 -0
  99. package/src/components/__tests__/ImpersonationBanner.test.tsx +59 -0
  100. package/src/components/__tests__/NotificationBell.test.tsx +207 -0
  101. package/src/components/__tests__/OnboardingChecklist.test.tsx +97 -0
  102. package/src/components/__tests__/SecretModal.test.tsx +112 -0
  103. package/src/components/__tests__/SiteBannerLayer.test.tsx +44 -0
  104. package/src/components/__tests__/UserMenu.test.tsx +98 -0
  105. package/src/components/__tests__/WorkspaceSwitcher.test.tsx +162 -0
  106. package/src/components/__tests__/form.test.tsx +133 -0
  107. package/src/components/__tests__/semantic-tokens.test.ts +41 -0
  108. package/src/components/__tests__/smoke.test.tsx +11 -0
  109. package/src/components/__tests__/toast.test.tsx +27 -0
  110. package/src/components/toast.tsx +23 -0
  111. package/src/components/ui/__tests__/alert-dialog.test.tsx +66 -0
  112. package/src/components/ui/__tests__/avatar.test.tsx +30 -0
  113. package/src/components/ui/__tests__/checkbox.test.tsx +18 -0
  114. package/src/components/ui/__tests__/dropdown-menu.test.tsx +33 -0
  115. package/src/components/ui/__tests__/field.test.tsx +43 -0
  116. package/src/components/ui/__tests__/overlay-size.test.tsx +84 -0
  117. package/src/components/ui/__tests__/popover.test.tsx +32 -0
  118. package/src/components/ui/__tests__/progress.test.tsx +24 -0
  119. package/src/components/ui/__tests__/select-callback.test.tsx +72 -0
  120. package/src/components/ui/__tests__/select.test.tsx +48 -0
  121. package/src/components/ui/__tests__/sidebar.test.tsx +121 -0
  122. package/src/components/ui/__tests__/stock-mirror.test.ts +39 -0
  123. package/src/components/ui/__tests__/subpath.test.tsx +38 -0
  124. package/src/components/ui/__tests__/switch.test.tsx +24 -0
  125. package/src/components/ui/__tests__/tabs.test.tsx +24 -0
  126. package/src/components/ui/__tests__/textarea.test.tsx +41 -0
  127. package/src/components/ui/alert-dialog.tsx +187 -0
  128. package/src/components/ui/avatar.tsx +108 -0
  129. package/src/components/ui/badge.tsx +51 -0
  130. package/src/components/ui/breadcrumb.tsx +124 -0
  131. package/src/components/ui/button.tsx +57 -0
  132. package/src/components/ui/card.tsx +102 -0
  133. package/src/components/ui/checkbox.tsx +28 -0
  134. package/src/components/ui/dialog.tsx +166 -0
  135. package/src/components/ui/dropdown-menu.tsx +265 -0
  136. package/src/components/ui/empty.tsx +103 -0
  137. package/src/components/ui/field.tsx +238 -0
  138. package/src/components/ui/input.tsx +19 -0
  139. package/src/components/ui/label.tsx +17 -0
  140. package/src/components/ui/popover.tsx +89 -0
  141. package/src/components/ui/progress.tsx +82 -0
  142. package/src/components/ui/select.tsx +235 -0
  143. package/src/components/ui/separator.tsx +24 -0
  144. package/src/components/ui/sheet.tsx +146 -0
  145. package/src/components/ui/sidebar.tsx +723 -0
  146. package/src/components/ui/skeleton.tsx +13 -0
  147. package/src/components/ui/switch.tsx +29 -0
  148. package/src/components/ui/tabs.tsx +79 -0
  149. package/src/components/ui/textarea.tsx +26 -0
  150. package/src/components/ui/tooltip.tsx +63 -0
  151. package/src/dev.css +1 -0
  152. package/src/hooks/use-mobile.ts +19 -0
  153. package/src/index.ts +150 -0
  154. package/src/layouts/AppShell.tsx +163 -0
  155. package/src/layouts/AuthShell.tsx +24 -0
  156. package/src/layouts/SettingsShell.tsx +129 -0
  157. package/src/layouts/__tests__/AppShell.test.tsx +163 -0
  158. package/src/layouts/__tests__/SettingsShell.test.tsx +146 -0
  159. package/src/lib/__tests__/api-error.test.ts +163 -0
  160. package/src/lib/__tests__/utils.test.ts +21 -0
  161. package/src/lib/api-error.ts +98 -0
  162. package/src/lib/utils.ts +109 -0
  163. package/src/pages/LandingPage.tsx +88 -0
  164. package/src/pages/LoginPage.tsx +291 -0
  165. package/src/pages/__tests__/LandingPage.test.tsx +58 -0
  166. package/src/pages/__tests__/LoginPage.test.tsx +229 -0
  167. package/src/pages/__tests__/RegisterPage.test.tsx +138 -0
  168. package/src/pages/__tests__/ssr-smoke.test.tsx +99 -0
  169. package/src/pages/account/NotificationPreferencesPage.tsx +138 -0
  170. package/src/pages/account/ProfileSettingsPage.tsx +189 -0
  171. package/src/pages/account/SecuritySettingsPage.tsx +214 -0
  172. package/src/pages/account/TokensPage.tsx +211 -0
  173. package/src/pages/account/__tests__/NotificationPreferencesPage.test.tsx +150 -0
  174. package/src/pages/account/__tests__/ProfileSettingsPage.test.tsx +173 -0
  175. package/src/pages/account/__tests__/SecuritySettingsPage.test.tsx +212 -0
  176. package/src/pages/account/__tests__/TokensPage.test.tsx +180 -0
  177. package/src/pages/admin/AdminSettingsPage.tsx +564 -0
  178. package/src/pages/admin/AdminShell.tsx +199 -0
  179. package/src/pages/admin/AuditPage.tsx +338 -0
  180. package/src/pages/admin/OrdersPage.tsx +642 -0
  181. package/src/pages/admin/OverviewPage.tsx +559 -0
  182. package/src/pages/admin/SSOPage.tsx +166 -0
  183. package/src/pages/admin/UserDetailPage.tsx +641 -0
  184. package/src/pages/admin/UsersPage.tsx +462 -0
  185. package/src/pages/admin/WebhookDeliveriesPage.tsx +318 -0
  186. package/src/pages/admin/WorkspaceDetailPage.tsx +551 -0
  187. package/src/pages/admin/WorkspacesPage.tsx +299 -0
  188. package/src/pages/admin/__tests__/AdminSettingsPage.test.tsx +260 -0
  189. package/src/pages/admin/__tests__/OrdersPageMarkPaid.test.tsx +207 -0
  190. package/src/pages/admin/__tests__/OverviewPageActivity.test.tsx +213 -0
  191. package/src/pages/admin/__tests__/SSOPage.test.tsx +128 -0
  192. package/src/pages/admin/__tests__/UsersPageActions.test.tsx +188 -0
  193. package/src/pages/admin/__tests__/WorkspaceDetailPageTransfer.test.tsx +170 -0
  194. package/src/pages/admin/__tests__/WorkspacesPagePlans.test.tsx +138 -0
  195. package/src/pages/admin/shared.ts +42 -0
  196. package/src/pages/auth/ForgotPasswordPage.tsx +83 -0
  197. package/src/pages/auth/InviteAcceptPage.tsx +105 -0
  198. package/src/pages/auth/RegisterPage.tsx +204 -0
  199. package/src/pages/auth/ResetPasswordPage.tsx +150 -0
  200. package/src/pages/auth/VerifyEmailPage.tsx +76 -0
  201. package/src/pages/auth/__tests__/ForgotPasswordPage.test.tsx +67 -0
  202. package/src/pages/auth/__tests__/InviteAcceptPage.test.tsx +86 -0
  203. package/src/pages/auth/__tests__/ResetPasswordPage.test.tsx +115 -0
  204. package/src/pages/auth/__tests__/VerifyEmailPage.test.tsx +74 -0
  205. package/src/pages/workspace/BillingPage.tsx +361 -0
  206. package/src/pages/workspace/BillingSuccessPage.tsx +115 -0
  207. package/src/pages/workspace/MembersPage.tsx +568 -0
  208. package/src/pages/workspace/RolePermissionsPage.tsx +202 -0
  209. package/src/pages/workspace/SchedulesPage.tsx +387 -0
  210. package/src/pages/workspace/UsagePage.tsx +207 -0
  211. package/src/pages/workspace/WebhooksPage.tsx +506 -0
  212. package/src/pages/workspace/WorkspaceAuditPage.tsx +229 -0
  213. package/src/pages/workspace/WorkspaceGeneralPage.tsx +184 -0
  214. package/src/pages/workspace/__tests__/BillingPage.test.tsx +302 -0
  215. package/src/pages/workspace/__tests__/BillingSuccessPage.test.tsx +134 -0
  216. package/src/pages/workspace/__tests__/MembersPage.test.tsx +347 -0
  217. package/src/pages/workspace/__tests__/RolePermissionsPage.test.tsx +138 -0
  218. package/src/pages/workspace/__tests__/SchedulesPage.test.tsx +239 -0
  219. package/src/pages/workspace/__tests__/UsagePage.test.tsx +223 -0
  220. package/src/pages/workspace/__tests__/WebhooksPage.test.tsx +309 -0
  221. package/src/pages/workspace/__tests__/WorkspaceAuditPage.test.tsx +112 -0
  222. package/src/pages/workspace/__tests__/WorkspaceGeneralPage.test.tsx +164 -0
@@ -0,0 +1,207 @@
1
+
2
+ import '@testing-library/jest-dom/vitest';
3
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
4
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
5
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
6
+
7
+ const mockedGet = vi.hoisted(() => vi.fn());
8
+ const mockedPost = vi.hoisted(() => vi.fn());
9
+ const toastMocks = vi.hoisted(() => ({
10
+ success: vi.fn(),
11
+ error: vi.fn(),
12
+ info: vi.fn(),
13
+ }));
14
+ vi.mock('@ploykit/client', () => ({
15
+ api: {
16
+ get: mockedGet,
17
+ put: vi.fn(),
18
+ post: mockedPost,
19
+ patch: vi.fn(),
20
+ delete: vi.fn(),
21
+ },
22
+ ApiError: class ApiError extends Error {
23
+ status: number;
24
+ code: string;
25
+ constructor(status: number, code: string, message: string) {
26
+ super(message);
27
+ this.status = status;
28
+ this.code = code;
29
+ }
30
+ },
31
+ }));
32
+
33
+
34
+ vi.mock('../../../components/ConfirmDialog', () => ({
35
+ useConfirm: () => async () => true,
36
+ }));
37
+ vi.mock('../../../components/toast', () => ({ toast: toastMocks }));
38
+
39
+ import { MemoryRouter } from 'react-router';
40
+ import { OrdersPage } from '../OrdersPage';
41
+
42
+ const ORDERS = [
43
+ {
44
+
45
+ id: 'ord-manual-1',
46
+ workspace_id: 'ws-1',
47
+ workspace_name: 'Acme',
48
+ user_id: 'u-1',
49
+ plan_code: 'pro',
50
+ interval: 'one_time',
51
+ amount_cents: 12800,
52
+ currency: 'CNY',
53
+ channel: 'manual',
54
+ status: 'pending',
55
+ created_at: '2026-10-07T08:00:00Z',
56
+ },
57
+ {
58
+
59
+ id: 'ord-stripe-2',
60
+ workspace_id: 'ws-2',
61
+ workspace_name: 'Beta',
62
+ user_id: 'u-2',
63
+ plan_code: 'starter',
64
+ interval: 'monthly',
65
+ amount_cents: 9900,
66
+ currency: 'CNY',
67
+ channel: 'stripe',
68
+ status: 'pending',
69
+ created_at: '2026-10-07T09:00:00Z',
70
+ },
71
+ {
72
+
73
+ id: 'ord-paid-3',
74
+ workspace_id: 'ws-3',
75
+ workspace_name: 'Gamma',
76
+ user_id: 'u-3',
77
+ plan_code: 'pro',
78
+ interval: 'one_time',
79
+ amount_cents: 6400,
80
+ currency: 'CNY',
81
+ channel: 'manual',
82
+ status: 'paid',
83
+ created_at: '2026-10-06T08:00:00Z',
84
+ },
85
+ ];
86
+
87
+ function renderPage() {
88
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
89
+ return render(
90
+ <MemoryRouter initialEntries={['/admin/orders']}>
91
+ <QueryClientProvider client={qc}>
92
+ <OrdersPage />
93
+ </QueryClientProvider>
94
+ </MemoryRouter>,
95
+ );
96
+ }
97
+
98
+ beforeEach(() => {
99
+ mockedGet.mockReset().mockImplementation(async (path: string) => {
100
+ if (path.startsWith('/api/admin/orders'))
101
+ return { items: ORDERS, total: ORDERS.length };
102
+ if (path.startsWith('/api/admin/payment-events'))
103
+ return { items: [], total: 0 };
104
+ if (path.startsWith('/api/admin/billing/config')) return { channels: [] };
105
+ throw new Error('unexpected GET ' + path);
106
+ });
107
+ mockedPost.mockReset().mockResolvedValue({ status: 'ok' });
108
+ toastMocks.success.mockClear();
109
+ toastMocks.error.mockClear();
110
+ });
111
+
112
+ describe('OrdersPage 标记已付(批次 2.8)', () => {
113
+ it('仅 pending+manual 行出现"标记已付"(stripe/已付行不出现)', async () => {
114
+ renderPage();
115
+ expect(await screen.findAllByText('Acme')).toBeTruthy();
116
+ expect(
117
+ await waitFor(() => screen.getAllByRole('button', { name: '标记已付' })),
118
+ ).toHaveLength(1);
119
+ });
120
+
121
+ it('确认后 POST mark-paid → 成功 toast + 订单列表失效重拉', async () => {
122
+ renderPage();
123
+ await screen.findAllByText('Acme');
124
+
125
+ fireEvent.click(screen.getByRole('button', { name: '标记已付' }));
126
+ await waitFor(() =>
127
+
128
+ expect(mockedPost).toHaveBeenCalledWith(
129
+ '/api/admin/billing/orders/ord-manual-1/mark-paid',
130
+ undefined,
131
+ ),
132
+ );
133
+ await waitFor(() => expect(toastMocks.success).toHaveBeenCalled());
134
+
135
+ await waitFor(() =>
136
+ expect(
137
+ mockedGet.mock.calls.filter(([p]: [string]) =>
138
+ p.startsWith('/api/admin/orders'),
139
+ ).length,
140
+ ).toBeGreaterThanOrEqual(2),
141
+ );
142
+ });
143
+
144
+ it('409(非 pending 并发窗口)→ toast 透出服务端语义', async () => {
145
+ mockedPost.mockRejectedValueOnce(
146
+ new (class extends Error {
147
+ status = 409;
148
+ code = 'E_CONFLICT';
149
+ })('order is not pending'),
150
+ );
151
+ renderPage();
152
+ await screen.findAllByText('Acme');
153
+
154
+ fireEvent.click(screen.getByRole('button', { name: '标记已付' }));
155
+ await waitFor(() => expect(toastMocks.error).toHaveBeenCalled());
156
+ expect(toastMocks.error.mock.calls[0][0]).toContain('order is not pending');
157
+ });
158
+
159
+ it('P2-16:触发超额出账先过确认链(确认后 POST run-overage)', async () => {
160
+ renderPage();
161
+ await screen.findAllByText('Acme');
162
+
163
+
164
+ fireEvent.click(screen.getByRole('button', { name: '触发超额出账' }));
165
+ await waitFor(() =>
166
+ expect(mockedPost).toHaveBeenCalledWith(
167
+ '/api/admin/billing/run-overage',
168
+ undefined,
169
+ ),
170
+ );
171
+ await waitFor(() => expect(toastMocks.success).toHaveBeenCalled());
172
+ });
173
+
174
+ it('P2-16:触发到期降级先过确认链(确认后 POST run-expiry)', async () => {
175
+ renderPage();
176
+ await screen.findAllByText('Acme');
177
+
178
+ fireEvent.click(screen.getByRole('button', { name: '触发到期降级' }));
179
+ await waitFor(() =>
180
+ expect(mockedPost).toHaveBeenCalledWith(
181
+ '/api/admin/billing/run-expiry',
182
+ undefined,
183
+ ),
184
+ );
185
+ });
186
+
187
+ it('B-orders-3:状态筛选选项为中文标签(refunded=已退款),选中后 query 带 status', async () => {
188
+ renderPage();
189
+ await screen.findAllByText('Acme');
190
+
191
+ const trigger = screen.getByRole('combobox', { name: '按订单状态过滤' });
192
+ fireEvent.click(trigger);
193
+ const option = await screen.findByRole('option', { name: '已退款' });
194
+ fireEvent.pointerDown(option, { button: 0, pointerType: 'mouse' });
195
+ fireEvent.pointerUp(option, { button: 0, pointerType: 'mouse' });
196
+ fireEvent.click(option);
197
+
198
+ await waitFor(() => {
199
+ const ordersCalls = mockedGet.mock.calls
200
+ .map((c) => String(c[0]))
201
+ .filter((p) => p.startsWith('/api/admin/orders'));
202
+ expect(
203
+ ordersCalls.some((p) => p.includes('status=refunded')),
204
+ ).toBe(true);
205
+ });
206
+ });
207
+ });
@@ -0,0 +1,213 @@
1
+
2
+ import '@testing-library/jest-dom/vitest';
3
+ import {
4
+ fireEvent,
5
+ render,
6
+ screen,
7
+ waitFor,
8
+ } from '@testing-library/react';
9
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
10
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
11
+
12
+ const mockedGet = vi.hoisted(() => vi.fn());
13
+ vi.mock('@ploykit/client', () => ({
14
+ api: {
15
+ get: mockedGet,
16
+ put: vi.fn(),
17
+ post: vi.fn(),
18
+ patch: vi.fn(),
19
+ delete: vi.fn(),
20
+ },
21
+ ApiError: class ApiError extends Error {
22
+ status: number;
23
+ code: string;
24
+ constructor(status: number, code: string, message: string) {
25
+ super(message);
26
+ this.status = status;
27
+ this.code = code;
28
+ }
29
+ },
30
+ }));
31
+
32
+
33
+ const paramsState = vi.hoisted(() => ({ params: new URLSearchParams() }));
34
+ vi.mock('react-router', () => ({
35
+ useLocation: () => ({ hash: '' }),
36
+ useSearchParams: () => [
37
+ paramsState.params,
38
+ (p: URLSearchParams) => {
39
+ paramsState.params = p;
40
+ },
41
+ ],
42
+ }));
43
+
44
+ import { OverviewPage } from '../OverviewPage';
45
+
46
+ const STATS = {
47
+ total_users: 10,
48
+ active_users: 8,
49
+ total_workspaces: 4,
50
+ paid_workspaces: 1,
51
+ new_users_7d: 3,
52
+ new_users_30d: 6,
53
+ };
54
+ const SUMMARY = [
55
+ { event_type: 'task_created', count: 42, last_at: '2026-10-08T09:00:00Z' },
56
+ { event_type: 'checkout_started', count: 7, last_at: '2026-10-08T08:00:00Z' },
57
+ ];
58
+ const RECENT = Array.from({ length: 20 }, (_, i) => ({
59
+ id: 100 - i,
60
+ workspace_id:
61
+ i % 2 === 0 ? 'ws-00000000-0000-0000-0000-00000000000a' : undefined,
62
+ user_id: 'u-1',
63
+ event_type: i % 2 === 0 ? 'task_created' : 'checkout_started',
64
+ entity_type: 'task',
65
+ entity_id: `tk-${i}`,
66
+ payload: {},
67
+ created_at: `2026-10-08T09:${String(59 - i).padStart(2, '0')}:00Z`,
68
+ }));
69
+
70
+ function renderPage() {
71
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
72
+ return render(
73
+ <QueryClientProvider client={qc}>
74
+ <OverviewPage />
75
+ </QueryClientProvider>,
76
+ );
77
+ }
78
+
79
+ beforeEach(() => {
80
+ paramsState.params = new URLSearchParams();
81
+ mockedGet.mockReset().mockImplementation(async (path: string) => {
82
+ if (path === '/api/admin/stats') return STATS;
83
+ if (path.startsWith('/api/admin/analytics?')) return SUMMARY;
84
+ if (path.startsWith('/api/admin/analytics/recent')) return RECENT;
85
+ throw new Error('unexpected GET ' + path);
86
+ });
87
+ });
88
+
89
+ describe('OverviewPage 活动流(批次 3.3)', () => {
90
+ it('渲染最近事件卡片:20 条倒序列表(limit=20 默认)', async () => {
91
+ renderPage();
92
+ expect(await screen.findByText('最近事件')).toBeInTheDocument();
93
+
94
+ expect(await screen.findByText('task tk-0')).toBeInTheDocument();
95
+ expect(screen.getByText('task tk-19')).toBeInTheDocument();
96
+ expect(mockedGet).toHaveBeenCalledWith(
97
+ '/api/admin/analytics/recent?limit=20',
98
+ );
99
+ });
100
+
101
+ it('type 过滤:选项来自汇总表类型并集,选中后 query 带 &type= 并重拉', async () => {
102
+ renderPage();
103
+ await screen.findByText('最近事件');
104
+
105
+ const trigger = screen.getByRole('combobox', {
106
+ name: '按事件类型过滤活动流',
107
+ });
108
+ fireEvent.click(trigger);
109
+ const option = await screen.findByRole('option', {
110
+ name: 'checkout_started',
111
+ });
112
+ fireEvent.pointerDown(option, { button: 0, pointerType: 'mouse' });
113
+ fireEvent.pointerUp(option, { button: 0, pointerType: 'mouse' });
114
+ fireEvent.click(option);
115
+
116
+ await waitFor(() =>
117
+ expect(mockedGet).toHaveBeenCalledWith(
118
+ `/api/admin/analytics/recent?limit=20&type=${encodeURIComponent('checkout_started')}`,
119
+ ),
120
+ );
121
+
122
+ expect(paramsState.params.get('type')).toBe('checkout_started');
123
+ });
124
+
125
+ it('B4:切换统计时间范围后,活动流类型过滤不被重置(选项跨范围累积)', async () => {
126
+ renderPage();
127
+ await screen.findByText('最近事件');
128
+
129
+
130
+ const typeTrigger = screen.getByRole('combobox', {
131
+ name: '按事件类型过滤活动流',
132
+ });
133
+ fireEvent.click(typeTrigger);
134
+ const option = await screen.findByRole('option', {
135
+ name: 'checkout_started',
136
+ });
137
+ fireEvent.pointerDown(option, { button: 0, pointerType: 'mouse' });
138
+ fireEvent.pointerUp(option, { button: 0, pointerType: 'mouse' });
139
+ fireEvent.click(option);
140
+ await waitFor(() =>
141
+ expect(mockedGet).toHaveBeenCalledWith(
142
+ `/api/admin/analytics/recent?limit=20&type=${encodeURIComponent('checkout_started')}`,
143
+ ),
144
+ );
145
+
146
+
147
+
148
+ const rangeTrigger = screen.getByRole('combobox', { name: '统计时间范围' });
149
+ fireEvent.click(rangeTrigger);
150
+ const range30 = await screen.findByRole('option', { name: '近 30 天' });
151
+ fireEvent.pointerDown(range30, { button: 0, pointerType: 'mouse' });
152
+ fireEvent.pointerUp(range30, { button: 0, pointerType: 'mouse' });
153
+ fireEvent.click(range30);
154
+
155
+ await waitFor(() =>
156
+ expect(mockedGet).toHaveBeenCalledWith('/api/admin/analytics?days=30',),
157
+ );
158
+
159
+
160
+ await new Promise((r) => setTimeout(r, 50));
161
+ const recentCalls = mockedGet.mock.calls
162
+ .map((c) => String(c[0]))
163
+ .filter((p) => p.startsWith('/api/admin/analytics/recent'));
164
+ expect(recentCalls.length).toBe(2);
165
+ expect(recentCalls[1]).toContain('type=checkout_started');
166
+ expect(
167
+ screen.getByRole('combobox', { name: '按事件类型过滤活动流' }),
168
+ ).toHaveTextContent('checkout_started');
169
+ });
170
+
171
+ it('B8:带 days/type 参数渲染时按 URL 初始化过滤', async () => {
172
+ paramsState.params = new URLSearchParams('days=30&type=task_created');
173
+ renderPage();
174
+ await screen.findByText('最近事件');
175
+ expect(mockedGet).toHaveBeenCalledWith('/api/admin/analytics?days=30');
176
+ await waitFor(() =>
177
+ expect(mockedGet).toHaveBeenCalledWith(
178
+ `/api/admin/analytics/recent?limit=20&type=${encodeURIComponent('task_created')}`,
179
+ ),
180
+ );
181
+ });
182
+
183
+ it('空态:该类型无事件时给出换类型提示', async () => {
184
+ mockedGet.mockImplementation(async (path: string) => {
185
+ if (path === '/api/admin/stats') return STATS;
186
+ if (path.startsWith('/api/admin/analytics?')) return SUMMARY;
187
+ if (path.startsWith('/api/admin/analytics/recent')) return [];
188
+ throw new Error('unexpected GET ' + path);
189
+ });
190
+ const { container } = renderPage();
191
+ await waitFor(() =>
192
+ expect(
193
+ container.querySelector('table') ?? container.textContent,
194
+ ).toBeTruthy(),
195
+ );
196
+
197
+ expect(await screen.findByText('暂无埋点事件')).toBeInTheDocument();
198
+ });
199
+
200
+ it('加载失败 → 错误行内展示 + 重试入口', async () => {
201
+ mockedGet.mockImplementation(async (path: string) => {
202
+ if (path === '/api/admin/stats') return STATS;
203
+ if (path.startsWith('/api/admin/analytics?')) return SUMMARY;
204
+ if (path.startsWith('/api/admin/analytics/recent')) {
205
+ throw new Error('db down');
206
+ }
207
+ throw new Error('unexpected GET ' + path);
208
+ });
209
+ renderPage();
210
+ expect(await screen.findByText(/活动流加载失败/)).toBeInTheDocument();
211
+ expect(screen.getByRole('button', { name: '重试' })).toBeInTheDocument();
212
+ });
213
+ });
@@ -0,0 +1,128 @@
1
+
2
+ import '@testing-library/jest-dom/vitest';
3
+ import { render, screen } from '@testing-library/react';
4
+ import { MemoryRouter } from 'react-router';
5
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
6
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
7
+ import { SSOPage } from '../SSOPage';
8
+
9
+ const mockedGet = vi.hoisted(() => vi.fn());
10
+ vi.mock('@ploykit/client', () => ({
11
+ api: {
12
+ get: mockedGet,
13
+ put: vi.fn(),
14
+ post: vi.fn(),
15
+ patch: vi.fn(),
16
+ delete: vi.fn(),
17
+ },
18
+ ApiError: class ApiError extends Error {
19
+ status: number;
20
+ code: string;
21
+ constructor(status: number, code: string, message: string) {
22
+ super(message);
23
+ this.status = status;
24
+ this.code = code;
25
+ }
26
+ },
27
+ }));
28
+
29
+ function renderPage() {
30
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
31
+ return render(
32
+ <MemoryRouter initialEntries={['/admin/sso']}>
33
+ <QueryClientProvider client={qc}>
34
+ <SSOPage />
35
+ </QueryClientProvider>
36
+ </MemoryRouter>,
37
+ );
38
+ }
39
+
40
+ const ROWS = [
41
+ {
42
+ workspace_id: 'ws-1',
43
+ workspace_slug: 'acme',
44
+ workspace_name: 'Acme',
45
+ issuer_url: 'https://idp.acme.corp',
46
+ client_id: 'acme-client',
47
+ scopes: 'openid email profile',
48
+ secret_sealed: true,
49
+ created_at: '2026-10-01T08:00:00Z',
50
+ updated_at: '2026-10-02T08:00:00Z',
51
+ },
52
+ {
53
+ workspace_id: 'ws-2',
54
+ workspace_slug: 'globex',
55
+ workspace_name: 'Globex',
56
+ issuer_url: 'https://sso.globex.io',
57
+ client_id: 'globex-client',
58
+ scopes: 'openid email',
59
+ secret_sealed: false, // 迁移 016 时代存量明文 → 警告徽标
60
+ created_at: '2026-09-01T08:00:00Z',
61
+ updated_at: '2026-09-01T08:00:00Z',
62
+ },
63
+ ];
64
+
65
+ beforeEach(() => {
66
+ mockedGet.mockReset();
67
+ });
68
+
69
+ describe('SSOPage', () => {
70
+ it('渲染 provider 列表:工作区、Issuer/client_id、密封徽标两态', async () => {
71
+ mockedGet.mockResolvedValue(ROWS);
72
+ renderPage();
73
+
74
+ expect(await screen.findByText('Acme')).toBeInTheDocument();
75
+ expect(screen.getByText('https://idp.acme.corp')).toBeInTheDocument();
76
+ expect(screen.getByText('client_id: acme-client')).toBeInTheDocument();
77
+
78
+ expect(screen.getByText('已加密')).toBeInTheDocument();
79
+ expect(screen.getByText('明文')).toBeInTheDocument();
80
+
81
+ expect(
82
+ screen.getByText(/配置的增删改在各工作区\s+自助面完成/),
83
+ ).toBeInTheDocument();
84
+ expect(mockedGet).toHaveBeenCalledWith('/api/admin/sso');
85
+ });
86
+
87
+ it('B-sso-3:工作区名链接到管理台工作区详情页', async () => {
88
+ mockedGet.mockResolvedValue(ROWS);
89
+ renderPage();
90
+
91
+ const link = await screen.findByRole('link', { name: /Acme/ });
92
+ expect(link).toHaveAttribute('href', '/admin/workspaces/ws-1');
93
+ });
94
+
95
+ it('B-sso-2:明文行常显"需重新保存以加密"指引(不只在 title 悬停)', async () => {
96
+ mockedGet.mockResolvedValue(ROWS);
97
+ renderPage();
98
+
99
+ expect(await screen.findByText('需重新保存以加密')).toBeInTheDocument();
100
+ });
101
+
102
+ it('B-sso-7:配置/更新时间双值常显(updated != created 时显示更新行)', async () => {
103
+ mockedGet.mockResolvedValue(ROWS);
104
+ renderPage();
105
+
106
+ await screen.findByText('Acme');
107
+
108
+ expect(screen.getByText(/更新 /)).toBeInTheDocument();
109
+ expect(screen.getByText('配置 / 更新时间')).toBeInTheDocument();
110
+ });
111
+
112
+ it('空列表 → 空态"无工作区配置联邦 SSO"', async () => {
113
+ mockedGet.mockResolvedValue([]);
114
+ renderPage();
115
+
116
+ expect(await screen.findByText('无工作区配置联邦 SSO')).toBeInTheDocument();
117
+ });
118
+
119
+ it('加载失败 → 错误态可重试(不渲染表格行)', async () => {
120
+ mockedGet.mockRejectedValue(new Error('db down'));
121
+ renderPage();
122
+
123
+ expect(
124
+ await screen.findByRole('button', { name: '重试' }),
125
+ ).toBeInTheDocument();
126
+ expect(screen.queryByText('Acme')).not.toBeInTheDocument();
127
+ });
128
+ });