@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,229 @@
1
+ import '@testing-library/jest-dom/vitest'
2
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
3
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
4
+ import { LoginPage, loginRedirectTarget } from '../LoginPage'
5
+
6
+
7
+
8
+ const loginMock = vi.hoisted(() => ({ submit: vi.fn(async () => ({ ok: true })) }))
9
+ vi.mock('../../../../hooks/src/hooks/useLogin', () => ({
10
+ useLogin: () => ({
11
+ sendCode: vi.fn(),
12
+ submit: loginMock.submit,
13
+ loading: false,
14
+ error: null,
15
+ }),
16
+ }))
17
+ const mockedPost = vi.hoisted(() => vi.fn())
18
+ vi.mock('../../../../hooks/src/hooks/useApi', () => ({
19
+ useApi: () => ({ post: mockedPost, get: vi.fn(), put: vi.fn(), patch: vi.fn(), delete: vi.fn() }),
20
+ }))
21
+ const mockedRefresh = vi.hoisted(() => vi.fn())
22
+ vi.mock('../../../../hooks/src/hooks/useAuth', () => ({
23
+ useAuth: () => ({ refresh: mockedRefresh, user: null, workspaces: [], loading: false, logout: vi.fn() }),
24
+ }))
25
+
26
+ const locState = vi.hoisted(() => ({ state: null as unknown }))
27
+ vi.mock('react-router', () => ({
28
+ useLocation: () => ({ state: locState.state }),
29
+ }))
30
+
31
+ beforeEach(() => {
32
+ mockedPost.mockReset().mockResolvedValue({ user: { id: 'u1' }, expires_at: '2026-01-01T00:00:00Z' })
33
+ mockedRefresh.mockReset().mockResolvedValue(undefined)
34
+ locState.state = null
35
+ })
36
+
37
+ function renderLogin(props: Parameters<typeof LoginPage>[0]) {
38
+ return render(<LoginPage {...props} />)
39
+ }
40
+
41
+ describe('LoginPage oauthProviders', () => {
42
+ it('传入 providers 渲染第三方登录按钮(href 指向 /auth/oauth/{id}/start)', () => {
43
+ renderLogin({
44
+ oauthProviders: [
45
+ { id: 'github', label: 'GitHub' },
46
+ { id: 'google', label: 'Google' },
47
+ ],
48
+ })
49
+ const gh = screen.getByRole('link', { name: /GitHub/ })
50
+ expect(gh).toHaveAttribute('href', '/auth/oauth/github/start')
51
+ const gg = screen.getByRole('link', { name: /Google/ })
52
+ expect(gg).toHaveAttribute('href', '/auth/oauth/google/start')
53
+ })
54
+
55
+ it('不传 providers 时不渲染第三方登录区块', () => {
56
+ const { container } = renderLogin({})
57
+ expect(container.querySelector('a[href^="/auth/oauth/"]')).toBeNull()
58
+ })
59
+ })
60
+
61
+ describe('LoginPage enableFedLogin', () => {
62
+ it('enableFedLogin 渲染折叠的企业登录表单(原生 GET /auth/fed/start)', () => {
63
+ const { container } = renderLogin({ enableFedLogin: true })
64
+ const form = container.querySelector('form[action="/auth/fed/start"]')
65
+ expect(form).not.toBeNull()
66
+ expect(form).toHaveAttribute('method', 'get')
67
+ const input = form!.querySelector('input[name="workspace"]')
68
+ expect(input).not.toBeNull()
69
+ expect(input).toHaveAttribute('required', '')
70
+ })
71
+
72
+ it('默认不渲染企业登录区块', () => {
73
+ const { container } = renderLogin({})
74
+ expect(container.querySelector('form[action="/auth/fed/start"]')).toBeNull()
75
+ })
76
+
77
+ it('enableFedLogin=false 显式传入也不渲染', () => {
78
+ const { container } = renderLogin({ enableFedLogin: false })
79
+ expect(container.querySelector('form[action="/auth/fed/start"]')).toBeNull()
80
+ })
81
+ })
82
+
83
+ describe('LoginPage 双模式 Tabs', () => {
84
+ it('默认展示验证码登录 Tab,可切到密码登录', () => {
85
+ renderLogin({})
86
+ expect(screen.getByRole('tab', { name: '验证码登录' })).toHaveAttribute('aria-selected', 'true')
87
+ expect(screen.getByRole('tab', { name: '密码登录' })).toBeInTheDocument()
88
+ })
89
+
90
+ it('密码模式提交 → POST /auth/login 携带 email/password 并触发 onSuccess', async () => {
91
+ const onSuccess = vi.fn()
92
+ renderLogin({ onSuccess })
93
+ fireEvent.click(screen.getByRole('tab', { name: '密码登录' }))
94
+
95
+ fireEvent.change(await screen.findByLabelText('邮箱'), { target: { value: 'user@example.com' } })
96
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'secret-password' } })
97
+ fireEvent.click(screen.getByRole('button', { name: '登录' }))
98
+
99
+ await waitFor(() => expect(mockedPost).toHaveBeenCalledTimes(1))
100
+ expect(mockedPost).toHaveBeenCalledWith('/auth/login', {
101
+ email: 'user@example.com',
102
+ password: 'secret-password',
103
+ })
104
+ await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
105
+ })
106
+
107
+ it('密码模式登录失败展示后端错误文案', async () => {
108
+ mockedPost.mockRejectedValueOnce(new Error('邮箱或密码错误'))
109
+ renderLogin({})
110
+ fireEvent.click(screen.getByRole('tab', { name: '密码登录' }))
111
+
112
+ fireEvent.change(await screen.findByLabelText('邮箱'), { target: { value: 'user@example.com' } })
113
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'wrong' } })
114
+ fireEvent.click(screen.getByRole('button', { name: '登录' }))
115
+
116
+ expect(await screen.findByText('邮箱或密码错误')).toBeInTheDocument()
117
+ })
118
+ })
119
+
120
+ describe('LoginPage 验证码发送(P3-26)', () => {
121
+ it('发送成功 → POST /auth/send-code,按钮进入重发倒计时(节流)', async () => {
122
+ renderLogin({})
123
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
124
+ fireEvent.click(screen.getByRole('button', { name: '发送' }))
125
+
126
+ await waitFor(() =>
127
+ expect(mockedPost).toHaveBeenCalledWith('/auth/send-code', { email: 'user@example.com' }),
128
+ )
129
+ expect(await screen.findByRole('button', { name: /重发(60s)/ })).toBeDisabled()
130
+ })
131
+
132
+ it('发送失败 → 展示错误(role=alert),不误置"已发送"形态', async () => {
133
+ mockedPost.mockRejectedValueOnce(new Error('发送过于频繁'))
134
+ renderLogin({})
135
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
136
+ fireEvent.click(screen.getByRole('button', { name: '发送' }))
137
+
138
+ const alert = await screen.findByRole('alert')
139
+ expect(alert).toHaveTextContent('发送过于频繁')
140
+
141
+ expect(screen.getByRole('button', { name: '发送' })).toBeEnabled()
142
+ })
143
+ })
144
+
145
+ describe('LoginPage 认证辅助链接(aiblog-15:未挂载路由须可隐藏)', () => {
146
+ it('默认渲染 忘记密码/创建账号,指向 resetPath/registerPath 默认路由', () => {
147
+ renderLogin({})
148
+ expect(screen.getByRole('link', { name: '忘记密码?' })).toHaveAttribute('href', '/forgot-password')
149
+ expect(screen.getByRole('link', { name: '创建账号' })).toHaveAttribute('href', '/register')
150
+ })
151
+
152
+ it('resetPath/registerPath 传空串时对应链接隐藏(产品未挂载该路由)', () => {
153
+ renderLogin({ resetPath: '', registerPath: '' })
154
+ expect(screen.queryByRole('link', { name: '忘记密码?' })).toBeNull()
155
+ expect(screen.queryByRole('link', { name: '创建账号' })).toBeNull()
156
+ })
157
+
158
+ it('仅隐藏 reset 链接时 register 链接仍渲染', () => {
159
+ renderLogin({ resetPath: '' })
160
+ expect(screen.queryByRole('link', { name: '忘记密码?' })).toBeNull()
161
+ expect(screen.getByRole('link', { name: '创建账号' })).toBeInTheDocument()
162
+ })
163
+
164
+ it('自定义路径覆盖默认路由', () => {
165
+ renderLogin({ resetPath: '/my-reset', registerPath: '/my-register' })
166
+ expect(screen.getByRole('link', { name: '忘记密码?' })).toHaveAttribute('href', '/my-reset')
167
+ expect(screen.getByRole('link', { name: '创建账号' })).toHaveAttribute('href', '/my-register')
168
+ })
169
+ })
170
+
171
+ describe('LoginPage 深链回跳(B-settings-8)', () => {
172
+ it('location.state.from 为站内路径时,登录成功回跳该路径', async () => {
173
+ locState.state = { from: '/admin/settings' }
174
+ const onSuccess = vi.fn()
175
+ renderLogin({ onSuccess })
176
+ fireEvent.click(screen.getByRole('tab', { name: '密码登录' }))
177
+
178
+ fireEvent.change(await screen.findByLabelText('邮箱'), { target: { value: 'user@example.com' } })
179
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'secret-password' } })
180
+ fireEvent.click(screen.getByRole('button', { name: '登录' }))
181
+
182
+ await waitFor(() => expect(mockedRefresh).toHaveBeenCalledTimes(1))
183
+ await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
184
+ })
185
+
186
+ it('loginRedirectTarget:仅站内相对路径可作回跳,站外/协议相对/缺失一律回退 /app', () => {
187
+ expect(loginRedirectTarget('/admin/settings')).toBe('/admin/settings')
188
+ expect(loginRedirectTarget('/app?x=1')).toBe('/app?x=1')
189
+ expect(loginRedirectTarget('//evil.example.com')).toBe('/app')
190
+ expect(loginRedirectTarget('https://evil.example.com')).toBe('/app')
191
+ expect(loginRedirectTarget(undefined)).toBe('/app')
192
+ expect(loginRedirectTarget(null)).toBe('/app')
193
+ expect(loginRedirectTarget('')).toBe('/app')
194
+ expect(loginRedirectTarget(123)).toBe('/app')
195
+ })
196
+ })
197
+
198
+ describe('LoginPage passwordOnly(aifengkong #4:企业内部纯密码形态)', () => {
199
+ it('passwordOnly 时无验证码 Tab/表单,密码表单直出', () => {
200
+ const { container } = renderLogin({ passwordOnly: true })
201
+ expect(screen.queryByRole('tab', { name: '验证码登录' })).toBeNull()
202
+ expect(screen.queryByRole('tab', { name: '密码登录' })).toBeNull()
203
+ expect(screen.getByLabelText('邮箱')).toBeInTheDocument()
204
+ expect(screen.getByLabelText('密码')).toBeInTheDocument()
205
+ expect(screen.queryByRole('button', { name: '发送' })).toBeNull()
206
+ expect(container.querySelector('form')).not.toBeNull()
207
+ })
208
+
209
+ it('passwordOnly 提交走 /auth/login', async () => {
210
+ const onSuccess = vi.fn()
211
+ renderLogin({ passwordOnly: true, onSuccess })
212
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
213
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'secret-password' } })
214
+ fireEvent.click(screen.getByRole('button', { name: '登录' }))
215
+ await waitFor(() =>
216
+ expect(mockedPost).toHaveBeenCalledWith('/auth/login', {
217
+ email: 'user@example.com',
218
+ password: 'secret-password',
219
+ }),
220
+ )
221
+ await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
222
+ })
223
+
224
+ it('默认形态不受影响(双 Tab 仍在)', () => {
225
+ renderLogin({})
226
+ expect(screen.getByRole('tab', { name: '验证码登录' })).toBeInTheDocument()
227
+ expect(screen.getByRole('tab', { name: '密码登录' })).toBeInTheDocument()
228
+ })
229
+ })
@@ -0,0 +1,138 @@
1
+ import '@testing-library/jest-dom/vitest'
2
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
3
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
4
+ import { ApiError } from '@ploykit/client'
5
+ import { RegisterPage } from '../auth/RegisterPage'
6
+
7
+
8
+
9
+ const mockedPost = vi.hoisted(() => vi.fn())
10
+ const mockedRefresh = vi.hoisted(() => vi.fn())
11
+ vi.mock('../../../../hooks/src/hooks/useApi', () => ({
12
+ useApi: () => ({ post: mockedPost, get: vi.fn(), put: vi.fn(), patch: vi.fn(), delete: vi.fn() }),
13
+ }))
14
+ vi.mock('../../../../hooks/src/hooks/useAuth', () => ({
15
+ useAuth: () => ({ refresh: mockedRefresh, user: null, workspaces: [], loading: false, logout: vi.fn() }),
16
+ }))
17
+
18
+
19
+ vi.mock('../../../../hooks/src/hooks/useSiteConfig', () => ({
20
+ useSiteConfig: () => ({ data: { allow_signup: '1' } }),
21
+ }))
22
+
23
+ const STRONG_PW = 'Abcdef1234!'
24
+
25
+ beforeEach(() => {
26
+ mockedPost.mockReset().mockResolvedValue({ user: { id: 'u1' }, expires_at: '2026-01-01T00:00:00Z' })
27
+ mockedRefresh.mockReset().mockResolvedValue(undefined)
28
+ })
29
+
30
+ function fillValidForm() {
31
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
32
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: STRONG_PW } })
33
+ fireEvent.change(screen.getByLabelText('昵称(可选)'), { target: { value: '小明' } })
34
+ }
35
+
36
+ describe('RegisterPage', () => {
37
+ it('渲染表单字段与"已有账号?登录"入口', () => {
38
+ render(<RegisterPage />)
39
+ expect(screen.getByLabelText('邮箱')).toBeInTheDocument()
40
+ expect(screen.getByLabelText('密码')).toBeInTheDocument()
41
+ expect(screen.getByLabelText('昵称(可选)')).toBeInTheDocument()
42
+ expect(screen.getByRole('button', { name: '登录' })).toBeInTheDocument()
43
+ expect(screen.getByRole('button', { name: /创建账号/ })).toBeInTheDocument()
44
+ })
45
+
46
+ it('弱密码(少于三类字符)被 zod 拦截,不发起请求', async () => {
47
+ render(<RegisterPage />)
48
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
49
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'abcdefghijk' } })
50
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
51
+
52
+ expect(await screen.findByText('需包含大写/小写/数字/符号中的至少三类')).toBeInTheDocument()
53
+ expect(mockedPost).not.toHaveBeenCalled()
54
+ })
55
+
56
+ it('有效提交 → POST /auth/register 携带正确 payload 并触发 onSuccess', async () => {
57
+ const onSuccess = vi.fn()
58
+ render(<RegisterPage onSuccess={onSuccess} />)
59
+ fillValidForm()
60
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
61
+
62
+ await waitFor(() => expect(mockedPost).toHaveBeenCalledTimes(1))
63
+ expect(mockedPost).toHaveBeenCalledWith('/auth/register', {
64
+ email: 'user@example.com',
65
+ password: STRONG_PW,
66
+ display_name: '小明',
67
+ })
68
+ await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
69
+ expect(mockedRefresh).toHaveBeenCalledTimes(1)
70
+ })
71
+
72
+ it('P1-2:昵称留空 → 不发 display_name 字段(空串会被服务端 400)', async () => {
73
+ const onSuccess = vi.fn()
74
+ render(<RegisterPage onSuccess={onSuccess} />)
75
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
76
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: STRONG_PW } })
77
+
78
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
79
+
80
+ await waitFor(() => expect(mockedPost).toHaveBeenCalledTimes(1))
81
+ const payload = mockedPost.mock.calls[0][1] as Record<string, unknown>
82
+ expect('display_name' in payload).toBe(false)
83
+ expect(payload.email).toBe('user@example.com')
84
+ expect(payload.password).toBe(STRONG_PW)
85
+ await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
86
+ })
87
+
88
+ it('P1-2:昵称纯空格 → 同样不发 display_name 字段', async () => {
89
+ render(<RegisterPage onSuccess={vi.fn()} />)
90
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
91
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: STRONG_PW } })
92
+ fireEvent.change(screen.getByLabelText('昵称(可选)'), { target: { value: ' ' } })
93
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
94
+
95
+ await waitFor(() => expect(mockedPost).toHaveBeenCalledTimes(1))
96
+ const payload = mockedPost.mock.calls[0][1] as Record<string, unknown>
97
+ expect('display_name' in payload).toBe(false)
98
+ })
99
+
100
+ it('昵称首尾空白被裁剪后再提交', async () => {
101
+ render(<RegisterPage onSuccess={vi.fn()} />)
102
+ fireEvent.change(screen.getByLabelText('邮箱'), { target: { value: 'user@example.com' } })
103
+ fireEvent.change(screen.getByLabelText('密码'), { target: { value: STRONG_PW } })
104
+ fireEvent.change(screen.getByLabelText('昵称(可选)'), { target: { value: ' 小明 ' } })
105
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
106
+
107
+ await waitFor(() => expect(mockedPost).toHaveBeenCalledTimes(1))
108
+ expect(mockedPost).toHaveBeenCalledWith('/auth/register', {
109
+ email: 'user@example.com',
110
+ password: STRONG_PW,
111
+ display_name: '小明',
112
+ })
113
+ })
114
+
115
+ it('注册失败展示后端错误文案且不跳转', async () => {
116
+ mockedPost.mockRejectedValueOnce(new Error('该邮箱已被注册'))
117
+ const onSuccess = vi.fn()
118
+ render(<RegisterPage onSuccess={onSuccess} />)
119
+ fillValidForm()
120
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
121
+
122
+ expect(await screen.findByRole('alert')).toHaveTextContent('该邮箱已被注册')
123
+ expect(onSuccess).not.toHaveBeenCalled()
124
+ })
125
+
126
+ it('RE2-3:重复邮箱(ApiError "email already registered")映射为中文提示,不直出英文', async () => {
127
+ mockedPost.mockRejectedValueOnce(new ApiError(409, 'E_CONFLICT', 'email already registered'))
128
+ const onSuccess = vi.fn()
129
+ render(<RegisterPage onSuccess={onSuccess} />)
130
+ fillValidForm()
131
+ fireEvent.click(screen.getByRole('button', { name: /创建账号/ }))
132
+
133
+ const alert = await screen.findByRole('alert')
134
+ expect(alert).toHaveTextContent('该邮箱已注册,可直接登录或换个邮箱重试')
135
+ expect(alert).not.toHaveTextContent('email already registered')
136
+ expect(onSuccess).not.toHaveBeenCalled()
137
+ })
138
+ })
@@ -0,0 +1,99 @@
1
+
2
+ import { renderToString } from 'react-dom/server'
3
+ import { MemoryRouter } from 'react-router'
4
+ import { describe, expect, it, vi } from 'vitest'
5
+ import { OnboardingChecklist } from '../../components/OnboardingChecklist'
6
+ import { PloykitProvider } from '@ploykit/hooks'
7
+ import { LandingPage } from '../LandingPage'
8
+ import { LoginPage } from '../LoginPage'
9
+
10
+
11
+
12
+ vi.mock('../../../../hooks/src/hooks/useAuth', () => ({
13
+ useAuth: () => ({
14
+ user: { email: 'ssr@example.com', display_name: 'SSR' },
15
+ workspaces: [{ id: 'ws-1', name: 'Acme' }],
16
+ loading: false,
17
+ error: null,
18
+ refresh: vi.fn(async () => {}),
19
+ logout: vi.fn(async () => {}),
20
+ }),
21
+ }))
22
+
23
+
24
+
25
+ const touched: string[] = []
26
+
27
+ function poison(name: string): () => void {
28
+ const desc = Object.getOwnPropertyDescriptor(globalThis, name)
29
+ Object.defineProperty(globalThis, name, {
30
+ configurable: true,
31
+ get() {
32
+ touched.push(name)
33
+ throw new Error(`[SSR 冒烟] 渲染期访问了浏览器 API:${name}`)
34
+ },
35
+ })
36
+ return () => {
37
+ if (desc) Object.defineProperty(globalThis, name, desc)
38
+ else delete (globalThis as Record<string, unknown>)[name]
39
+ }
40
+ }
41
+
42
+ function withPoisonedStorage(render: () => string): string {
43
+ const restore = ['localStorage', 'sessionStorage'].map(poison)
44
+ try {
45
+ return render()
46
+ } finally {
47
+ restore.forEach((r) => r())
48
+ }
49
+ }
50
+
51
+ const landingProps = {
52
+ brand: 'PloykitSmoke',
53
+ tagline: '构建快',
54
+ description: '冒烟落地页',
55
+ features: [{ icon: '⚡', title: '快', desc: '很快' }],
56
+ plans: [{ name: 'Free', price: '¥0', features: ['a'], cta: '注册' }],
57
+ }
58
+
59
+ describe('ui 包 SSR 渲染冒烟(R2:渲染期禁摸浏览器 API)', () => {
60
+ it('LandingPage(MemoryRouter 内):输出非空且不摸存储 API', () => {
61
+ const html = withPoisonedStorage(() =>
62
+ renderToString(
63
+ <MemoryRouter>
64
+ <LandingPage {...landingProps} />
65
+ </MemoryRouter>,
66
+ ),
67
+ )
68
+ expect(html.length).toBeGreaterThan(0)
69
+ expect(html).toContain('PloykitSmoke')
70
+ expect(touched).toEqual([])
71
+ })
72
+
73
+ it('LoginPage(PloykitProvider + MemoryRouter 内):输出非空且不摸存储 API', () => {
74
+ const html = withPoisonedStorage(() =>
75
+ renderToString(
76
+ <PloykitProvider>
77
+ <MemoryRouter>
78
+ <LoginPage />
79
+ </MemoryRouter>
80
+ </PloykitProvider>,
81
+ ),
82
+ )
83
+ expect(html.length).toBeGreaterThan(0)
84
+ expect(html).toContain('登录')
85
+ expect(touched).toEqual([])
86
+ })
87
+
88
+ it('OnboardingChecklist(R2 回归对象):渲染期不读 localStorage,输出非空', () => {
89
+ const html = withPoisonedStorage(() =>
90
+ renderToString(
91
+ <OnboardingChecklist items={[{ key: 'a', label: '冒烟引导项', done: false }]} />,
92
+ ),
93
+ )
94
+
95
+
96
+ expect(html).toContain('冒烟引导项')
97
+ expect(touched).toEqual([])
98
+ })
99
+ })
@@ -0,0 +1,138 @@
1
+
2
+ import { useId } from 'react'
3
+ import { PageHeader, PageLoading, PageError, PageEmpty } from '../../components/Page'
4
+ import { Switch } from '../../components/ui/switch'
5
+ import { toast } from '../../components/toast'
6
+ import { useSEO } from '@ploykit/hooks'
7
+ import {
8
+ useNotificationPrefs, useSetNotificationPref, type NotificationPreference,
9
+ } from '@ploykit/hooks'
10
+
11
+ export interface NotificationTypeMeta {
12
+ type: string
13
+ label: string
14
+ description?: string
15
+ }
16
+
17
+ export interface NotificationPreferencesPageProps {
18
+
19
+ types?: NotificationTypeMeta[]
20
+ title?: string
21
+ description?: string
22
+
23
+ siteName?: string
24
+
25
+ emailChannelNote?: string
26
+ }
27
+
28
+ export function NotificationPreferencesPage({
29
+ types,
30
+ title = '通知偏好',
31
+ description = '选择每种通知的接收渠道;未显示的类型默认全部开启',
32
+ siteName,
33
+ emailChannelNote,
34
+ }: NotificationPreferencesPageProps) {
35
+ const { prefs, loading, error, refetch } = useNotificationPrefs()
36
+ const { setPref } = useSetNotificationPref()
37
+
38
+
39
+ const uid = useId()
40
+
41
+
42
+
43
+ useSEO({ title: siteName ? `${title} · ${siteName}` : title })
44
+
45
+ if (loading) return <div><PageHeader title={title} description={description} /><PageLoading /></div>
46
+ if (error) {
47
+ return (
48
+ <div>
49
+ <PageHeader title={title} description={description} />
50
+ <PageError message={error instanceof Error ? error.message : '偏好加载失败'} onRetry={() => refetch()} />
51
+ </div>
52
+ )
53
+ }
54
+
55
+
56
+ const byType = new Map(prefs.map((p: NotificationPreference) => [p.notification_type, p]))
57
+ const list: NotificationTypeMeta[] =
58
+ types ?? prefs.map((p: NotificationPreference) => ({ type: p.notification_type, label: p.notification_type }))
59
+
60
+ if (list.length === 0) {
61
+ return (
62
+ <div>
63
+ <PageHeader title={title} description={description} />
64
+ <PageEmpty
65
+ label="暂无可配置的通知类型"
66
+ hint="通知类型目录由服务端下发:首次产生通知设置(或后端接入类型目录接口)后,会出现在这里;默认所有通知双渠道开启。"
67
+ />
68
+ </div>
69
+ )
70
+ }
71
+
72
+ const toggle = async (type: string, channel: 'email_enabled' | 'in_app_enabled', next: boolean) => {
73
+ try {
74
+ await setPref(type, { [channel]: next })
75
+ } catch (e) {
76
+ toast.error(e instanceof Error ? e.message : '保存失败')
77
+ }
78
+ }
79
+
80
+ return (
81
+ <div>
82
+ <PageHeader title={title} description={description} />
83
+
84
+ <div className="w-full max-w-4xl space-y-4">
85
+ {/* B2 诚实化:邮件未接线时产品传入的能力说明(单行渲染一次,不随行重复——
86
+ 390 窄屏行内追加会挤压开关组造成溢出) */}
87
+ {emailChannelNote && (
88
+ <p className="text-sm text-muted-foreground">邮件渠道:{emailChannelNote}</p>
89
+ )}
90
+ {list.map(({ type, label, description: desc }) => {
91
+ const row = byType.get(type)
92
+ const inApp = row?.in_app_enabled ?? true
93
+ const email = row?.email_enabled ?? true
94
+
95
+
96
+
97
+
98
+ const inAppInputId = `${uid}-${type}-in-app`
99
+ const emailInputId = `${uid}-${type}-email`
100
+ const inAppLabelId = `${inAppInputId}-label`
101
+ const emailLabelId = `${emailInputId}-label`
102
+ return (
103
+ <div key={type} className="flex flex-row items-center justify-between gap-4 rounded-lg border p-4">
104
+ <div className="min-w-0">
105
+ <p className="text-sm font-medium">{label}</p>
106
+ {desc && <p className="mt-0.5 text-sm text-muted-foreground">{desc}</p>}
107
+ </div>
108
+ <div className="flex shrink-0 items-center gap-5">
109
+ <div className="flex items-center gap-2 text-sm text-muted-foreground">
110
+ <label id={inAppLabelId} htmlFor={inAppInputId} className="cursor-pointer select-none">
111
+ 站内<span className="sr-only">({label})</span>
112
+ </label>
113
+ <Switch
114
+ id={inAppInputId}
115
+ aria-labelledby={inAppLabelId}
116
+ checked={inApp}
117
+ onCheckedChange={(v) => toggle(type, 'in_app_enabled', v)}
118
+ />
119
+ </div>
120
+ <div className="flex items-center gap-2 text-sm text-muted-foreground">
121
+ <label id={emailLabelId} htmlFor={emailInputId} className="cursor-pointer select-none">
122
+ 邮件<span className="sr-only">({label})</span>
123
+ </label>
124
+ <Switch
125
+ id={emailInputId}
126
+ aria-labelledby={emailLabelId}
127
+ checked={email}
128
+ onCheckedChange={(v) => toggle(type, 'email_enabled', v)}
129
+ />
130
+ </div>
131
+ </div>
132
+ </div>
133
+ )
134
+ })}
135
+ </div>
136
+ </div>
137
+ )
138
+ }