@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,188 @@
1
+
2
+ import '@testing-library/jest-dom/vitest'
3
+ import { fireEvent, render, screen, waitFor } 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
+
8
+ const mockedGet = vi.hoisted(() => vi.fn())
9
+ const mockedPatch = vi.hoisted(() => vi.fn())
10
+ const mockedPost = vi.hoisted(() => vi.fn())
11
+ const confirmMock = vi.hoisted(() => vi.fn())
12
+ vi.mock('@ploykit/client', () => ({
13
+ api: {
14
+ get: mockedGet,
15
+ put: vi.fn(),
16
+ post: mockedPost,
17
+ patch: mockedPatch,
18
+ delete: vi.fn(),
19
+ },
20
+ ApiError: class ApiError extends Error {
21
+ status: number
22
+ code: string
23
+ constructor(status: number, code: string, message: string) {
24
+ super(message)
25
+ this.status = status
26
+ this.code = code
27
+ }
28
+ },
29
+ }))
30
+ vi.mock('../../../../../hooks/src/hooks/useApi', () => ({
31
+ useApi: () => ({
32
+ get: mockedGet,
33
+ put: vi.fn(),
34
+ post: mockedPost,
35
+ patch: mockedPatch,
36
+ delete: vi.fn(),
37
+ }),
38
+ }))
39
+ vi.mock('../../../../../hooks/src/hooks/useAuth', () => ({
40
+ useAuth: () => ({
41
+ user: { id: 'me-1', email: 'admin@audit.local', is_platform_admin: true },
42
+ loading: false,
43
+ refresh: vi.fn(),
44
+ logout: vi.fn(),
45
+ workspaces: [],
46
+ }),
47
+ }))
48
+ vi.mock('../../../components/ConfirmDialog', () => ({
49
+ useConfirm: () => confirmMock,
50
+ }))
51
+
52
+ import { UsersPage } from '../UsersPage'
53
+
54
+ const ME = {
55
+ id: 'me-1',
56
+ email: 'admin@audit.local',
57
+ display_name: 'Admin',
58
+ status: 'active',
59
+ is_platform_admin: true,
60
+ email_verified: true,
61
+ created_at: '2026-09-01T00:00:00Z',
62
+ }
63
+ const OTHER = {
64
+ id: 'u-9',
65
+ email: 'user@audit.local',
66
+ display_name: 'User',
67
+ status: 'active',
68
+ is_platform_admin: false,
69
+ email_verified: true,
70
+ created_at: '2026-09-02T00:00:00Z',
71
+ }
72
+
73
+ function renderPage(props: Parameters<typeof UsersPage>[0] = {}) {
74
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
75
+ return render(
76
+ <MemoryRouter initialEntries={['/admin/users']}>
77
+ <QueryClientProvider client={qc}>
78
+ <UsersPage {...props} />
79
+ </QueryClientProvider>
80
+ </MemoryRouter>,
81
+ )
82
+ }
83
+
84
+ beforeEach(() => {
85
+ mockedGet.mockReset().mockImplementation(async (path: string) => {
86
+ if (path.startsWith('/api/admin/users')) return { items: [ME, OTHER], total: 2 }
87
+ throw new Error('unexpected GET ' + path)
88
+ })
89
+ mockedPatch.mockReset().mockResolvedValue({})
90
+ mockedPost.mockReset().mockResolvedValue({})
91
+ confirmMock.mockReset().mockResolvedValue(true)
92
+ })
93
+
94
+ describe('UsersPage 危险操作(P2-25)', () => {
95
+ it('B-users-1 回归:行操作菜单可打开且不崩溃(Label 在 Group 内)', async () => {
96
+ renderPage()
97
+ await screen.findAllByText('user@audit.local')
98
+ fireEvent.click(screen.getByRole('button', { name: '操作 user@audit.local' }))
99
+
100
+ expect(await screen.findByRole('menuitem', { name: '用户详情' })).toBeInTheDocument()
101
+ expect(screen.getByRole('menuitem', { name: '封禁账号' })).toBeInTheDocument()
102
+ expect(screen.getByRole('menuitem', { name: '模拟登录' })).toBeInTheDocument()
103
+ })
104
+
105
+ it('封禁:ImpactConfirmation 勾选后 PATCH status=disabled', async () => {
106
+ renderPage()
107
+ await screen.findAllByText('user@audit.local')
108
+ fireEvent.click(screen.getByRole('button', { name: '操作 user@audit.local' }))
109
+ fireEvent.click(await screen.findByRole('menuitem', { name: '封禁账号' }))
110
+
111
+
112
+ const confirmBtn = await screen.findByRole('button', { name: '封禁' })
113
+ expect(confirmBtn).toBeDisabled()
114
+ expect(screen.getByText(/其名下工作区与数据保留/)).toBeInTheDocument()
115
+ fireEvent.click(screen.getByLabelText(/我理解并接受以上影响/))
116
+ fireEvent.click(screen.getByRole('button', { name: '封禁' }))
117
+
118
+ await waitFor(() =>
119
+ expect(mockedPatch).toHaveBeenCalledWith('/api/admin/users/u-9/status', {
120
+ status: 'disabled',
121
+ }),
122
+ )
123
+ })
124
+
125
+ it('授予平台管理员:confirm 确认后 PATCH admin {is_admin:true},文案明示后果', async () => {
126
+ renderPage()
127
+ await screen.findAllByText('user@audit.local')
128
+ fireEvent.click(screen.getByRole('button', { name: '操作 user@audit.local' }))
129
+ fireEvent.click(await screen.findByRole('menuitem', { name: '授予平台管理员' }))
130
+
131
+ await waitFor(() => expect(confirmMock).toHaveBeenCalled())
132
+ expect(confirmMock.mock.calls[0][0].description).toContain('平台管理权限')
133
+ await waitFor(() =>
134
+ expect(mockedPatch).toHaveBeenCalledWith('/api/admin/users/u-9/admin', {
135
+ is_admin: true,
136
+ }),
137
+ )
138
+ })
139
+
140
+ it('取消确认则不提权', async () => {
141
+ confirmMock.mockResolvedValueOnce(false)
142
+ renderPage()
143
+ await screen.findAllByText('user@audit.local')
144
+ fireEvent.click(screen.getByRole('button', { name: '操作 user@audit.local' }))
145
+ fireEvent.click(await screen.findByRole('menuitem', { name: '授予平台管理员' }))
146
+
147
+ await waitFor(() => expect(confirmMock).toHaveBeenCalled())
148
+ expect(mockedPatch).not.toHaveBeenCalled()
149
+ })
150
+
151
+ it('模拟登录:ImpactConfirmation 勾选后 POST impersonate → 跳 impersonatePath', async () => {
152
+ renderPage({ impersonatePath: '/custom-app' })
153
+ await screen.findAllByText('user@audit.local')
154
+ fireEvent.click(screen.getByRole('button', { name: '操作 user@audit.local' }))
155
+ fireEvent.click(await screen.findByRole('menuitem', { name: '模拟登录' }))
156
+
157
+ const confirmBtn = await screen.findByRole('button', { name: '以该用户进入产品' })
158
+ fireEvent.click(screen.getByLabelText(/我理解并接受以上影响/))
159
+ fireEvent.click(confirmBtn)
160
+
161
+ await waitFor(() =>
162
+ expect(mockedPost).toHaveBeenCalledWith('/api/admin/users/u-9/impersonate'),
163
+ )
164
+ // window.location.href 整页跳转在 jsdom 未实现(Not implemented: navigation)——
165
+ // 以 POST 完成即视为链路通;跳转目标由 impersonatePath prop 驱动(P2-15)。
166
+ })
167
+
168
+ it('自己的行不渲染封禁/互模入口(详情可看)', async () => {
169
+ renderPage()
170
+ await screen.findAllByText('admin@audit.local')
171
+ expect(
172
+ screen.queryByRole('button', { name: '操作 admin@audit.local' }),
173
+ ).not.toBeInTheDocument()
174
+ expect(screen.getByRole('button', { name: '详情' })).toBeInTheDocument()
175
+ })
176
+
177
+ it('平台管理员行:模拟登录入口禁用(服务端禁止互模)', async () => {
178
+ mockedGet.mockImplementation(async () => ({
179
+ items: [ME, { ...OTHER, id: 'u-10', email: 'admin2@audit.local', is_platform_admin: true }],
180
+ total: 2,
181
+ }))
182
+ renderPage()
183
+ await screen.findAllByText('admin2@audit.local')
184
+ fireEvent.click(screen.getByRole('button', { name: '操作 admin2@audit.local' }))
185
+ const item = await screen.findByRole('menuitem', { name: '模拟登录' })
186
+ expect(item).toHaveAttribute('aria-disabled', 'true')
187
+ })
188
+ })
@@ -0,0 +1,170 @@
1
+
2
+ import '@testing-library/jest-dom/vitest';
3
+ import {
4
+ fireEvent,
5
+ render,
6
+ screen,
7
+ waitFor,
8
+ within,
9
+ } from '@testing-library/react';
10
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
11
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
12
+
13
+ const mockedGet = vi.hoisted(() => vi.fn());
14
+ const mockedPost = vi.hoisted(() => vi.fn());
15
+ vi.mock('@ploykit/client', () => ({
16
+ api: {
17
+ get: mockedGet,
18
+ put: vi.fn(),
19
+ post: mockedPost,
20
+ patch: vi.fn(),
21
+ delete: vi.fn(),
22
+ },
23
+ ApiError: class ApiError extends Error {
24
+ status: number;
25
+ code: string;
26
+ constructor(status: number, code: string, message: string) {
27
+ super(message);
28
+ this.status = status;
29
+ this.code = code;
30
+ }
31
+ },
32
+ }));
33
+
34
+
35
+ vi.mock('../../../../../hooks/src/hooks/useAuth', () => ({
36
+ useAuth: () => ({
37
+ user: { id: 'op-admin', email: 'op@test.local' },
38
+ loading: false,
39
+ }),
40
+ }));
41
+
42
+ vi.mock('react-router', () => ({
43
+ useParams: () => ({ id: 'ws-1' }),
44
+ useNavigate: () => () => {},
45
+ Link: ({ to, children }: { to: string; children: React.ReactNode }) => (
46
+ <a href={to}>{children}</a>
47
+ ),
48
+ }));
49
+
50
+ import { WorkspaceDetailPage } from '../WorkspaceDetailPage';
51
+ // fail-fast 后 useConfirm 无 Provider 即抛错:此测试直接渲染页面,须包 ConfirmProvider
52
+ import { ConfirmProvider } from '../../../components/ConfirmDialog';
53
+
54
+ const DETAIL = {
55
+ id: 'ws-1',
56
+ slug: 'acme',
57
+ name: 'Acme',
58
+ plan_code: 'free',
59
+ member_count: 3,
60
+ created_at: '2026-09-01T08:00:00Z',
61
+ members: [
62
+ {
63
+ user_id: 'alice',
64
+ email: 'alice@t.co',
65
+ display_name: 'Alice',
66
+ role: 'owner',
67
+ created_at: '2026-09-01T08:00:00Z',
68
+ },
69
+ {
70
+ user_id: 'bob',
71
+ email: 'bob@t.co',
72
+ display_name: 'Bob',
73
+ role: 'member',
74
+ created_at: '2026-09-02T08:00:00Z',
75
+ },
76
+
77
+ {
78
+ user_id: 'op-admin',
79
+ email: 'op@test.local',
80
+ display_name: 'Op',
81
+ role: 'admin',
82
+ created_at: '2026-09-03T08:00:00Z',
83
+ },
84
+ ],
85
+ };
86
+
87
+ function rowOf(email: string): HTMLElement {
88
+ const row = screen.getByText(email).closest('tr');
89
+ if (!row) throw new Error(`row of ${email} not found`);
90
+ return row;
91
+ }
92
+
93
+ function renderPage() {
94
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
95
+ return render(
96
+ <QueryClientProvider client={qc}>
97
+ <ConfirmProvider>
98
+ <WorkspaceDetailPage />
99
+ </ConfirmProvider>
100
+ </QueryClientProvider>,
101
+ );
102
+ }
103
+
104
+ beforeEach(() => {
105
+ mockedGet.mockReset().mockResolvedValue(DETAIL);
106
+ mockedPost.mockReset().mockResolvedValue({ status: 'ok' });
107
+ });
108
+
109
+ describe('WorkspaceDetailPage · 转让所有权', () => {
110
+ it('member/admin 行有"转让所有权",owner 行与自己没有', async () => {
111
+ renderPage();
112
+
113
+ await screen.findByText('alice@t.co');
114
+
115
+ expect(
116
+ within(rowOf('bob@t.co')).getByRole('button', { name: '转让所有权' }),
117
+ ).toBeInTheDocument();
118
+
119
+ expect(
120
+ within(rowOf('op@test.local')).queryByRole('button', {
121
+ name: '转让所有权',
122
+ }),
123
+ ).not.toBeInTheDocument();
124
+
125
+ expect(
126
+ within(rowOf('alice@t.co')).queryByRole('button', { name: '转让所有权' }),
127
+ ).not.toBeInTheDocument();
128
+ });
129
+
130
+ it('ImpactConfirmation 列明降级影响 → 勾选后 POST transfer-ownership', async () => {
131
+ renderPage();
132
+ await screen.findByText('bob@t.co');
133
+
134
+ fireEvent.click(
135
+ within(rowOf('bob@t.co')).getByRole('button', { name: '转让所有权' }),
136
+ );
137
+
138
+
139
+ expect(
140
+ await screen.findByText('转让 Acme 的所有权给 bob@t.co'),
141
+ ).toBeInTheDocument();
142
+ expect(screen.getByText(/alice@t\.co 将降为 member/)).toBeInTheDocument();
143
+ expect(screen.getByText(/始终保有 owner/)).toBeInTheDocument();
144
+
145
+ fireEvent.click(screen.getByLabelText(/我理解并接受以上影响/));
146
+ fireEvent.click(screen.getByRole('button', { name: '确认转让' }));
147
+
148
+ await waitFor(() =>
149
+ expect(mockedPost).toHaveBeenCalledWith(
150
+ '/api/admin/workspaces/ws-1/transfer-ownership',
151
+ {
152
+ user_id: 'bob',
153
+ },
154
+ ),
155
+ );
156
+ });
157
+
158
+ it('不勾选接受影响 → 确认按钮禁用,不发 POST', async () => {
159
+ renderPage();
160
+ await screen.findByText('bob@t.co');
161
+
162
+ fireEvent.click(
163
+ within(rowOf('bob@t.co')).getByRole('button', { name: '转让所有权' }),
164
+ );
165
+ expect(
166
+ await screen.findByRole('button', { name: '确认转让' }),
167
+ ).toBeDisabled();
168
+ expect(mockedPost).not.toHaveBeenCalled();
169
+ });
170
+ });
@@ -0,0 +1,138 @@
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 mockedPatch = vi.hoisted(() => vi.fn());
9
+ vi.mock('@ploykit/client', () => ({
10
+ api: {
11
+ get: mockedGet,
12
+ put: vi.fn(),
13
+ post: vi.fn(),
14
+ patch: mockedPatch,
15
+ delete: vi.fn(),
16
+ },
17
+ ApiError: class ApiError extends Error {
18
+ status: number;
19
+ code: string;
20
+ constructor(status: number, code: string, message: string) {
21
+ super(message);
22
+ this.status = status;
23
+ this.code = code;
24
+ }
25
+ },
26
+ }));
27
+ vi.mock('react-router', () => ({
28
+ Link: ({ to, children }: { to: string; children: React.ReactNode }) => (
29
+ <a href={to}>{children}</a>
30
+ ),
31
+ useSearchParams: () => [
32
+ new URLSearchParams(),
33
+ (_p: URLSearchParams) => {},
34
+ ],
35
+ }));
36
+
37
+ import { WorkspacesPage } from '../WorkspacesPage';
38
+
39
+ const WS = {
40
+ id: 'ws-1',
41
+ name: 'Acme',
42
+ slug: 'acme',
43
+ plan_code: 'free',
44
+ member_count: 2,
45
+ created_at: '2026-09-01T08:00:00Z',
46
+ };
47
+
48
+ function renderPage() {
49
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
50
+ return render(
51
+ <QueryClientProvider client={qc}>
52
+ <WorkspacesPage />
53
+ </QueryClientProvider>,
54
+ );
55
+ }
56
+
57
+ beforeEach(() => {
58
+ mockedGet.mockReset().mockImplementation(async (path: string) => {
59
+ if (path.startsWith('/api/admin/workspaces'))
60
+ return { items: [WS], total: 1 };
61
+ if (path.startsWith('/api/admin/plans')) {
62
+ return {
63
+ items: ['free', 'pro'],
64
+ plans: [
65
+ {
66
+ code: 'free',
67
+ name: 'Free',
68
+ currency: 'CNY',
69
+ trial_days: 0,
70
+ sort_no: 1,
71
+ },
72
+ {
73
+ code: 'pro',
74
+ name: 'Pro',
75
+ currency: 'CNY',
76
+ trial_days: 14,
77
+ sort_no: 2,
78
+ },
79
+ ],
80
+ };
81
+ }
82
+ throw new Error('unexpected GET ' + path);
83
+ });
84
+ mockedPatch.mockReset().mockResolvedValue({});
85
+ });
86
+
87
+ describe('WorkspacesPage 套餐选项(批次 2.5 收尾)', () => {
88
+ it('选项用 plan.name 并标注试用天数(B-workspaces-9):Pro(14 天)带提示,Free 纯名', async () => {
89
+ renderPage();
90
+ expect(await screen.findByText('Acme')).toBeInTheDocument();
91
+
92
+ const trigger = screen.getByRole('combobox', { name: /变更 Acme 套餐/ });
93
+ fireEvent.click(trigger);
94
+ expect(
95
+ await screen.findByRole('option', { name: 'Pro(新订阅享 14 天试用)' }),
96
+ ).toBeInTheDocument();
97
+ expect(screen.getByRole('option', { name: 'Free' })).toBeInTheDocument();
98
+ });
99
+
100
+ it('plans 视图缺失(老后端只有码表)→ 回退纯码表标签', async () => {
101
+ mockedGet.mockImplementation(async (path: string) => {
102
+ if (path.startsWith('/api/admin/workspaces'))
103
+ return { items: [WS], total: 1 };
104
+ if (path.startsWith('/api/admin/plans'))
105
+ return { items: ['free', 'pro'] };
106
+ throw new Error('unexpected GET ' + path);
107
+ });
108
+ renderPage();
109
+ await screen.findByText('Acme');
110
+
111
+ const trigger = screen.getByRole('combobox', { name: /变更 Acme 套餐/ });
112
+ fireEvent.click(trigger);
113
+ expect(
114
+ await screen.findByRole('option', { name: 'pro' }),
115
+ ).toBeInTheDocument();
116
+ });
117
+
118
+ it('选中试用套餐 → PATCH plan {plan_code}', async () => {
119
+ renderPage();
120
+ await screen.findByText('Acme');
121
+
122
+ const trigger = screen.getByRole('combobox', { name: /变更 Acme 套餐/ });
123
+ fireEvent.click(trigger);
124
+ const option = await screen.findByRole('option', {
125
+ name: 'Pro(新订阅享 14 天试用)',
126
+ });
127
+ fireEvent.pointerDown(option, { button: 0, pointerType: 'mouse' });
128
+ fireEvent.pointerUp(option, { button: 0, pointerType: 'mouse' });
129
+ fireEvent.click(option);
130
+
131
+ await waitFor(() =>
132
+ expect(mockedPatch).toHaveBeenCalledWith(
133
+ '/api/admin/workspaces/ws-1/plan',
134
+ { plan_code: 'pro' },
135
+ ),
136
+ );
137
+ });
138
+ });
@@ -0,0 +1,42 @@
1
+
2
+ import { ApiError } from '@ploykit/hooks';
3
+ import { apiErrorMessage } from '../../lib/api-error';
4
+
5
+
6
+ export const ADMIN_PAGE_SIZE = 20;
7
+
8
+
9
+ export function adminError(err: unknown, fallback: string): string {
10
+ if (err instanceof ApiError && err.status === 403) {
11
+ return '需要平台管理员权限(当前账号无权访问管理控制台)';
12
+ }
13
+ return apiErrorMessage(err, fallback);
14
+ }
15
+
16
+
17
+ export interface AdminListEnvelope<T> {
18
+ items: T[];
19
+ total: number;
20
+ }
21
+
22
+
23
+ export function adminListQuery(
24
+ page: number,
25
+ pageSize: number = ADMIN_PAGE_SIZE,
26
+ filters: Record<string, string | undefined> = {},
27
+ ): string {
28
+ const p = new URLSearchParams();
29
+ p.set('page', String(page));
30
+ p.set('page_size', String(pageSize));
31
+ for (const [k, v] of Object.entries(filters)) {
32
+ if (v && v.trim()) p.set(k, v.trim());
33
+ }
34
+ return `?${p.toString()}`;
35
+ }
36
+
37
+
38
+ export function formatCents(cents: number, currency = 'CNY'): string {
39
+ const symbol =
40
+ currency.toUpperCase() === 'CNY' ? '¥' : `${currency.toUpperCase()} `;
41
+ return `${symbol}${(cents / 100).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
42
+ }
@@ -0,0 +1,83 @@
1
+
2
+ import { useState, type FormEvent } from 'react'
3
+ import { AuthShell } from '../../layouts/AuthShell'
4
+ import { FormField } from '../../components/FormField'
5
+ import { Button } from '../../components/ui/button'
6
+ import { Input } from '../../components/ui/input'
7
+ import { usePasswordReset } from '@ploykit/hooks'
8
+
9
+ export interface ForgotPasswordPageProps {
10
+
11
+ title?: string
12
+
13
+ sentHint?: string
14
+
15
+ backPath?: string
16
+ }
17
+
18
+ export function ForgotPasswordPage({
19
+ title = '找回密码',
20
+ sentHint = '如果该邮箱存在,重置链接已发送,请查收邮箱。',
21
+ backPath = '/login',
22
+ }: ForgotPasswordPageProps) {
23
+ const { requestReset } = usePasswordReset()
24
+ const [email, setEmail] = useState('')
25
+ const [loading, setLoading] = useState(false)
26
+ const [sent, setSent] = useState(false)
27
+ const [error, setError] = useState<string | null>(null)
28
+
29
+ const handleSubmit = async (e: FormEvent) => {
30
+ e.preventDefault()
31
+ if (!email) return
32
+ setError(null)
33
+ setLoading(true)
34
+ try {
35
+ await requestReset(email)
36
+ setSent(true)
37
+ } catch (err) {
38
+ setError(err instanceof Error ? err.message : '发送失败,请稍后重试')
39
+ } finally {
40
+ setLoading(false)
41
+ }
42
+ }
43
+
44
+ return (
45
+ <AuthShell>
46
+ <form onSubmit={handleSubmit} className="space-y-4">
47
+ <div className="space-y-1 text-center">
48
+ <h1 className="text-2xl font-bold tracking-tight">{title}</h1>
49
+ </div>
50
+
51
+ {sent ? (
52
+ <p className="text-sm text-muted-foreground" data-testid="sent-hint">
53
+ {sentHint}
54
+ </p>
55
+ ) : (
56
+ <>
57
+ <FormField label="邮箱" error={error ?? undefined} htmlFor="forgot-email">
58
+ <Input
59
+ id="forgot-email"
60
+ type="email"
61
+ value={email}
62
+ onChange={(e) => setEmail(e.target.value)}
63
+ placeholder="you@example.com"
64
+ required
65
+ />
66
+ </FormField>
67
+
68
+ <Button type="submit" className="w-full" disabled={loading || !email}>
69
+ {loading ? '发送中…' : '发送重置链接'}
70
+ </Button>
71
+ </>
72
+ )}
73
+
74
+ <a
75
+ href={backPath}
76
+ className="block text-center text-sm text-primary underline-offset-4 hover:underline"
77
+ >
78
+ 返回登录
79
+ </a>
80
+ </form>
81
+ </AuthShell>
82
+ )
83
+ }