@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,309 @@
1
+ import '@testing-library/jest-dom/vitest'
2
+ import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
3
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
5
+ import { WebhooksPage } from '../WebhooksPage'
6
+
7
+ const apiMocks = vi.hoisted(() => ({
8
+ get: vi.fn(), post: vi.fn(), put: vi.fn(), patch: vi.fn(), delete: vi.fn(),
9
+ }))
10
+ vi.mock('../../../../../hooks/src/hooks/useApi', () => ({ useApi: () => apiMocks }))
11
+
12
+ const wsMock = vi.hoisted(() => ({
13
+ current: {
14
+ id: 'ws-1', name: 'Acme', slug: 'acme', plan_code: 'free', role: 'owner',
15
+ },
16
+ }))
17
+ vi.mock('../../../../../hooks/src/hooks/useWorkspace', () => ({
18
+ useWorkspace: () => ({
19
+ ...wsMock,
20
+ switchTo: vi.fn(),
21
+ clear: vi.fn(),
22
+ }),
23
+ }))
24
+
25
+ const toastMocks = vi.hoisted(() => ({
26
+ success: vi.fn(), error: vi.fn(), info: vi.fn(),
27
+ }))
28
+ vi.mock('../../../components/toast', () => ({ toast: toastMocks }))
29
+
30
+
31
+ const confirmMock = vi.hoisted(() => ({ calls: [] as Array<{ title?: string; description?: string }>, ret: true }))
32
+ vi.mock('../../../components/ConfirmDialog', () => ({
33
+ useConfirm: () => async (opts: { title?: string; description?: string }) => {
34
+ confirmMock.calls.push(opts)
35
+ return confirmMock.ret
36
+ },
37
+ }))
38
+
39
+ function renderPage() {
40
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
41
+ return render(
42
+ <QueryClientProvider client={qc}>
43
+ <WebhooksPage />
44
+ </QueryClientProvider>,
45
+ )
46
+ }
47
+
48
+ const subscription = {
49
+ id: 'sub-1', workspace_id: 'ws-1', event_types: ['task.created'],
50
+ url: 'https://example.com/hook', description: 'CI 触发', is_active: true,
51
+ created_at: '2026-06-01T00:00:00Z',
52
+ }
53
+ const delivery = {
54
+ id: 'dlv-1', subscription_id: 'sub-1', event_id: 'evt-1', event_type: 'task.created',
55
+ status: 'delivered', attempts: 1, last_status_code: 200,
56
+ delivered_at: '2026-06-02T00:00:00Z', created_at: '2026-06-02T00:00:00Z',
57
+ }
58
+
59
+ beforeEach(() => {
60
+ vi.clearAllMocks()
61
+ wsMock.current = {
62
+ id: 'ws-1', name: 'Acme', slug: 'acme', plan_code: 'free', role: 'owner',
63
+ }
64
+ confirmMock.calls.length = 0
65
+ confirmMock.ret = true
66
+ apiMocks.get.mockImplementation(async (path: string) => {
67
+ if (path.endsWith('/subscriptions')) return { items: [subscription] }
68
+ if (path.includes('/deliveries')) return { items: [delivery] }
69
+ if (path.endsWith('/events')) {
70
+ return { items: [{ type: 'task.created', description: '任务创建' }, { type: 'task.deleted', description: '任务删除' }] }
71
+ }
72
+ throw new Error('unexpected GET ' + path)
73
+ })
74
+ apiMocks.post.mockResolvedValue({})
75
+ apiMocks.patch.mockResolvedValue(subscription)
76
+ apiMocks.delete.mockResolvedValue({})
77
+ })
78
+
79
+ describe('WebhooksPage(从 example 提升进框架)', () => {
80
+ it('渲染订阅表(URL/事件 chips/Switch/最近投递)', async () => {
81
+ renderPage()
82
+ expect(await screen.findByText('https://example.com/hook')).toBeInTheDocument()
83
+ expect(screen.getByText('task.created')).toBeInTheDocument()
84
+ expect(screen.getByRole('switch', { name: /暂停订阅/ })).toHaveAttribute('aria-checked', 'true')
85
+ expect(screen.getByText('已投递')).toBeInTheDocument()
86
+ })
87
+
88
+ it('Switch 暂停订阅 → PATCH subscriptions/{id} {is_active:false}', async () => {
89
+ renderPage()
90
+ const sw = await screen.findByRole('switch', { name: /暂停订阅/ })
91
+ fireEvent.click(sw)
92
+ await waitFor(() =>
93
+ expect(apiMocks.patch).toHaveBeenCalledWith(
94
+ '/api/workspaces/ws-1/webhooks/subscriptions/sub-1',
95
+ { is_active: false },
96
+ ),
97
+ )
98
+ })
99
+
100
+ it('行操作菜单:删除(destructive)→ DELETE subscriptions/{id}', async () => {
101
+ renderPage()
102
+ fireEvent.click(await screen.findByRole('button', { name: /example.com\/hook 的操作/ }))
103
+ fireEvent.click(await screen.findByRole('menuitem', { name: '删除' }))
104
+ await waitFor(() =>
105
+ expect(apiMocks.delete).toHaveBeenCalledWith(
106
+ '/api/workspaces/ws-1/webhooks/subscriptions/sub-1',
107
+ ),
108
+ )
109
+ await waitFor(() => expect(toastMocks.success).toHaveBeenCalled())
110
+ })
111
+
112
+ it('创建订阅:提交正确 payload,secret 走 SecretModal 一次性展示', async () => {
113
+ apiMocks.post.mockResolvedValue({ subscription, secret: 'whsec_abc123' })
114
+ renderPage()
115
+ fireEvent.click(await screen.findByRole('button', { name: /创建订阅/ }))
116
+
117
+ fireEvent.change(await screen.findByLabelText('回调 URL'), {
118
+ target: { value: 'https://example.com/hook' },
119
+ })
120
+
121
+ const checkbox = screen.getByRole('checkbox', { name: /task\.created/ })
122
+ fireEvent.click(checkbox)
123
+ fireEvent.click(screen.getByRole('button', { name: '创建订阅' }))
124
+
125
+ await waitFor(() =>
126
+ expect(apiMocks.post).toHaveBeenCalledWith('/api/workspaces/ws-1/webhooks/subscriptions', {
127
+ url: 'https://example.com/hook',
128
+ description: '',
129
+ events: ['task.created'],
130
+ }),
131
+ )
132
+
133
+ await waitFor(() => expect(screen.getByText('whsec_abc123')).toBeInTheDocument())
134
+ })
135
+
136
+ it('投递记录页签:状态 Badge + 重投 POST deliveries/{id}/redeliver', async () => {
137
+ apiMocks.post.mockResolvedValue({ ...delivery, id: 'dlv-2', event_id: 'evt-2' })
138
+ renderPage()
139
+ fireEvent.click(await screen.findByRole('tab', { name: /投递记录/ }))
140
+
141
+ fireEvent.click(await screen.findByRole('button', { name: '重新投递' }))
142
+ await waitFor(() =>
143
+ expect(apiMocks.post).toHaveBeenCalledWith(
144
+ '/api/workspaces/ws-1/webhooks/deliveries/dlv-1/redeliver',
145
+ ),
146
+ )
147
+ await waitFor(() => expect(toastMocks.success).toHaveBeenCalled())
148
+ })
149
+
150
+
151
+
152
+ it('轮换密钥:确认弹窗明示 24h 宽限双签名,POST rotate-secret,新明文走 SecretModal 一次性展示', async () => {
153
+ apiMocks.post.mockImplementation(async (path: string) => {
154
+ if (path.endsWith('/rotate-secret')) return { secret: 'whk_rotated_new' }
155
+ return {}
156
+ })
157
+ renderPage()
158
+ fireEvent.click(await screen.findByRole('button', { name: /example.com\/hook 的操作/ }))
159
+ fireEvent.click(await screen.findByRole('menuitem', { name: '轮换密钥' }))
160
+
161
+
162
+ await waitFor(() => expect(confirmMock.calls.length).toBeGreaterThan(0))
163
+ expect(confirmMock.calls[0].description).toContain('24h 宽限期')
164
+ expect(confirmMock.calls[0].description).toContain('双签名')
165
+
166
+ await waitFor(() =>
167
+ expect(apiMocks.post).toHaveBeenCalledWith(
168
+ '/api/workspaces/ws-1/webhooks/subscriptions/sub-1/rotate-secret',
169
+ ),
170
+ )
171
+ await waitFor(() => expect(toastMocks.success).toHaveBeenCalledWith('密钥已轮换'))
172
+
173
+ expect(await screen.findByText('whk_rotated_new')).toBeInTheDocument()
174
+ expect(screen.getByText('Webhook 签名密钥轮换成功')).toBeInTheDocument()
175
+ })
176
+
177
+ it('P2-5:member 不渲染任何写入口(创建/Switch/行菜单/重投),只读浏览', async () => {
178
+ wsMock.current = { ...wsMock.current, role: 'member' }
179
+ renderPage()
180
+
181
+
182
+ expect(await screen.findByText('已启用')).toBeInTheDocument()
183
+
184
+ expect(screen.queryByRole('button', { name: /创建订阅/ })).not.toBeInTheDocument()
185
+ expect(screen.queryByRole('switch')).not.toBeInTheDocument()
186
+ expect(screen.queryByRole('button', { name: /example.com\/hook 的操作/ })).not.toBeInTheDocument()
187
+
188
+ fireEvent.click(await screen.findByRole('tab', { name: '投递记录' }))
189
+ await waitFor(() => expect(screen.queryByRole('button', { name: '重新投递' })).not.toBeInTheDocument())
190
+ })
191
+
192
+ it('轮换密钥失败 → toast.error,不弹 SecretModal', async () => {
193
+ apiMocks.post.mockRejectedValue(new Error('boom'))
194
+ renderPage()
195
+ fireEvent.click(await screen.findByRole('button', { name: /example.com\/hook 的操作/ }))
196
+ fireEvent.click(await screen.findByRole('menuitem', { name: '轮换密钥' }))
197
+
198
+ await waitFor(() => expect(toastMocks.error).toHaveBeenCalled())
199
+ expect(screen.queryByText(/whk_/)).not.toBeInTheDocument()
200
+ })
201
+
202
+
203
+
204
+ it('B3:点击事件目录 label 文本区可切换选中(恰好一次,不双发)', async () => {
205
+ apiMocks.post.mockResolvedValue({ subscription, secret: 'whsec_abc123' })
206
+ renderPage()
207
+ fireEvent.click(await screen.findByRole('button', { name: /创建订阅/ }))
208
+
209
+ fireEvent.change(await screen.findByLabelText('回调 URL'), {
210
+ target: { value: 'https://example.com/hook' },
211
+ })
212
+
213
+ fireEvent.click(await screen.findByText('任务创建'))
214
+ fireEvent.click(screen.getByRole('button', { name: '创建订阅' }))
215
+
216
+ await waitFor(() =>
217
+ expect(apiMocks.post).toHaveBeenCalledWith('/api/workspaces/ws-1/webhooks/subscriptions', {
218
+ url: 'https://example.com/hook',
219
+ description: '',
220
+
221
+ events: ['task.created'],
222
+ }),
223
+ )
224
+ })
225
+
226
+ it('B3:直接点方块本体仍单次切换(label 接管处理不与 Checkbox 自身双发)', async () => {
227
+ apiMocks.post.mockResolvedValue({ subscription, secret: 'whsec_abc123' })
228
+ renderPage()
229
+ fireEvent.click(await screen.findByRole('button', { name: /创建订阅/ }))
230
+
231
+ fireEvent.change(await screen.findByLabelText('回调 URL'), {
232
+ target: { value: 'https://example.com/hook' },
233
+ })
234
+ fireEvent.click(screen.getByRole('checkbox', { name: /task\.created/ }))
235
+ fireEvent.click(screen.getByRole('checkbox', { name: /task\.deleted/ }))
236
+ fireEvent.click(screen.getByRole('button', { name: '创建订阅' }))
237
+
238
+ await waitFor(() =>
239
+ expect(apiMocks.post).toHaveBeenCalledWith('/api/workspaces/ws-1/webhooks/subscriptions', {
240
+ url: 'https://example.com/hook',
241
+ description: '',
242
+ events: ['task.created', 'task.deleted'],
243
+ }),
244
+ )
245
+ })
246
+
247
+
248
+
249
+ it('B5:投递记录 15s 轮询(页面可见时自动 refetch,进度无需手动刷新)', async () => {
250
+
251
+ vi.useFakeTimers({ shouldAdvanceTime: true })
252
+ try {
253
+ renderPage()
254
+ await screen.findByText('已投递')
255
+ const deliveriesCalls = () =>
256
+ apiMocks.get.mock.calls.filter(([p]: [string]) => p.includes('/deliveries')).length
257
+ const before = deliveriesCalls()
258
+
259
+ await act(async () => {
260
+ await vi.advanceTimersByTimeAsync(15_000)
261
+ })
262
+
263
+ expect(deliveriesCalls()).toBeGreaterThan(before)
264
+ } finally {
265
+ vi.useRealTimers()
266
+ }
267
+ })
268
+
269
+
270
+
271
+ it('B8:订阅已删的投递行标注"已删除订阅",命中行显示归属 URL', async () => {
272
+ apiMocks.get.mockImplementation(async (path: string) => {
273
+ if (path.endsWith('/subscriptions')) return { items: [subscription] }
274
+ if (path.includes('/deliveries')) {
275
+ return {
276
+ items: [
277
+ delivery,
278
+ { ...delivery, id: 'dlv-orphan', subscription_id: 'sub-gone', event_id: 'evt-2' },
279
+ ],
280
+ }
281
+ }
282
+ if (path.endsWith('/events')) {
283
+ return { items: [{ type: 'task.created', description: '任务创建' }] }
284
+ }
285
+ throw new Error('unexpected GET ' + path)
286
+ })
287
+ renderPage()
288
+ fireEvent.click(await screen.findByRole('tab', { name: /投递记录/ }))
289
+
290
+ await screen.findByText('evt-2')
291
+ expect(screen.getByText('已删除订阅')).toBeInTheDocument()
292
+
293
+ expect(screen.getAllByText('https://example.com/hook').length).toBeGreaterThan(0)
294
+ })
295
+
296
+ it('B8:订阅列表未成功加载时不误标"已删除订阅"(显示 —)', async () => {
297
+ apiMocks.get.mockImplementation(async (path: string) => {
298
+ if (path.endsWith('/subscriptions')) throw new Error('subs down')
299
+ if (path.includes('/deliveries')) return { items: [delivery] }
300
+ if (path.endsWith('/events')) return { items: [] }
301
+ throw new Error('unexpected GET ' + path)
302
+ })
303
+ renderPage()
304
+ fireEvent.click(await screen.findByRole('tab', { name: /投递记录/ }))
305
+
306
+ await screen.findByText('evt-1')
307
+ expect(screen.queryByText('已删除订阅')).not.toBeInTheDocument()
308
+ })
309
+ })
@@ -0,0 +1,112 @@
1
+ import '@testing-library/jest-dom/vitest'
2
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
3
+ import { MemoryRouter } from 'react-router'
4
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
5
+ import { WorkspaceAuditPage } from '../WorkspaceAuditPage'
6
+
7
+ const auditMocks = vi.hoisted(() => ({
8
+ useAudit: vi.fn(),
9
+ exportAuditCsv: vi.fn(),
10
+ }))
11
+ vi.mock('../../../../../hooks/src/hooks/useAudit', async (importOriginal) => {
12
+ const orig = await importOriginal<typeof import('../../../hooks/useAudit')>()
13
+ return { ...orig, useAudit: auditMocks.useAudit, exportAuditCsv: auditMocks.exportAuditCsv }
14
+ })
15
+
16
+ const toastMocks = vi.hoisted(() => ({
17
+ success: vi.fn(), error: vi.fn(), info: vi.fn(),
18
+ }))
19
+ vi.mock('../../../components/toast', () => ({ toast: toastMocks }))
20
+
21
+ const items = [
22
+ {
23
+ id: 'ev-1', workspace_id: 'ws-1', actor_type: 'user', actor_id: 'u-1',
24
+ actor_snapshot: { email: 'alice@example.com' },
25
+ action: 'workspace.rename', resource_type: 'workspace',
26
+ resource_id: 'ws-1', metadata: {}, created_at: '2026-10-01T08:00:00Z',
27
+ },
28
+ {
29
+ id: 'ev-2', workspace_id: 'ws-1', actor_type: 'system', actor_id: null,
30
+ actor_snapshot: {}, action: 'billing.plan_changed', resource_type: 'workspace',
31
+ resource_id: 'ws-1', metadata: {}, created_at: '2026-10-02T08:00:00Z',
32
+ },
33
+ ]
34
+
35
+ function stubAudit(overrides: Record<string, unknown> = {}) {
36
+ auditMocks.useAudit.mockReturnValue({
37
+ wsId: 'ws-1', items, total: 2, loading: false, error: null, refetch: vi.fn(),
38
+ ...overrides,
39
+ })
40
+ }
41
+
42
+
43
+ function renderPage() {
44
+ return render(<MemoryRouter><WorkspaceAuditPage /></MemoryRouter>)
45
+ }
46
+
47
+ beforeEach(() => {
48
+ vi.clearAllMocks()
49
+ auditMocks.exportAuditCsv.mockResolvedValue(undefined)
50
+ })
51
+
52
+ describe('WorkspaceAuditPage', () => {
53
+ it('渲染过滤行与审计表(时间/操作者/动作/资源)', () => {
54
+ stubAudit()
55
+ renderPage()
56
+ expect(screen.getByText('审计日志')).toBeInTheDocument()
57
+ expect(screen.getByRole('button', { name: /查询/ })).toBeInTheDocument()
58
+ expect(screen.getByRole('button', { name: /导出 CSV/ })).toBeInTheDocument()
59
+ expect(screen.getByText('alice@example.com')).toBeInTheDocument()
60
+ expect(screen.getByText('workspace.rename')).toBeInTheDocument()
61
+
62
+ expect(
63
+ screen.getByText((_, el) =>
64
+ el?.getAttribute('data-slot') === 'data-table-pagination' &&
65
+ /共\s*2\s*条\s*·\s*第\s*1\/1\s*页/.test(el.textContent ?? ''),
66
+ ),
67
+ ).toBeInTheDocument()
68
+ expect(screen.getByRole('button', { name: '上一页' })).toBeDisabled()
69
+ expect(screen.getByRole('button', { name: '下一页' })).toBeDisabled()
70
+ })
71
+
72
+ it('过滤条件随“查询”提交(日期补 RFC3339 边界),offset 归零', async () => {
73
+ auditMocks.useAudit.mockImplementation((filters: { action: string; from: string; to: string }, _limit: number, offset: number) => ({
74
+ wsId: 'ws-1', items: [], total: 0, loading: false, error: null, refetch: vi.fn(), _filters: filters, _offset: offset,
75
+ }))
76
+ renderPage()
77
+
78
+ fireEvent.change(screen.getByPlaceholderText('如 workspace.rename'), { target: { value: 'workspace.rename' } })
79
+ fireEvent.change(screen.getByLabelText('开始日期'), { target: { value: '2026-10-01' } })
80
+ fireEvent.change(screen.getByLabelText('结束日期'), { target: { value: '2026-10-05' } })
81
+ fireEvent.click(screen.getByRole('button', { name: /查询/ }))
82
+
83
+ await waitFor(() => expect(auditMocks.useAudit).toHaveBeenCalled())
84
+ const lastCall = auditMocks.useAudit.mock.calls.at(-1)
85
+ expect(lastCall?.[0]).toMatchObject({
86
+ action: 'workspace.rename',
87
+ from: '2026-10-01T00:00:00Z',
88
+ to: '2026-10-05T23:59:59Z',
89
+ })
90
+ expect(lastCall?.[2]).toBe(0)
91
+ })
92
+
93
+ it('加载中显示 PageLoading;错误显示重试', () => {
94
+ const { rerender } = renderPage()
95
+ stubAudit({ loading: true, items: [] })
96
+ rerender(<MemoryRouter><WorkspaceAuditPage /></MemoryRouter>)
97
+ expect(screen.getByText('加载审计日志…')).toBeInTheDocument()
98
+
99
+ stubAudit({ loading: false, items: [], error: new Error('boom') })
100
+ rerender(<MemoryRouter><WorkspaceAuditPage /></MemoryRouter>)
101
+ expect(screen.getByText('boom')).toBeInTheDocument()
102
+ expect(screen.getByRole('button', { name: '重试' })).toBeInTheDocument()
103
+ })
104
+
105
+ it('导出 CSV 调用 exportAuditCsv(当前过滤条件)', async () => {
106
+ stubAudit()
107
+ renderPage()
108
+ fireEvent.click(screen.getByRole('button', { name: /导出 CSV/ }))
109
+ await waitFor(() => expect(auditMocks.exportAuditCsv).toHaveBeenCalledTimes(1))
110
+ expect(toastMocks.success).toHaveBeenCalled()
111
+ })
112
+ })
@@ -0,0 +1,164 @@
1
+ import '@testing-library/jest-dom/vitest'
2
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
3
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4
+ import { MemoryRouter, Routes, Route } from 'react-router'
5
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
6
+ import { ApiError } from '@ploykit/client'
7
+ import { WorkspaceGeneralPage } from '../WorkspaceGeneralPage'
8
+
9
+ const apiMocks = vi.hoisted(() => ({
10
+ get: vi.fn(), post: vi.fn(), put: vi.fn(), patch: vi.fn(), delete: vi.fn(),
11
+ }))
12
+ vi.mock('../../../../../hooks/src/hooks/useApi', () => ({ useApi: () => apiMocks }))
13
+
14
+ const wsMocks = vi.hoisted(() => ({ role: 'owner' as string }))
15
+ vi.mock('../../../../../hooks/src/hooks/useWorkspace', () => ({
16
+ useWorkspace: () => ({
17
+ current: { id: 'ws-1', name: 'Acme', slug: 'acme', plan_code: 'free', role: wsMocks.role },
18
+ switchTo: vi.fn(),
19
+ clear: vi.fn(),
20
+ }),
21
+ }))
22
+
23
+ const toastMocks = vi.hoisted(() => ({
24
+ success: vi.fn(), error: vi.fn(), info: vi.fn(),
25
+ }))
26
+ vi.mock('../../../components/toast', () => ({ toast: toastMocks }))
27
+
28
+ vi.mock('../../../components/ConfirmDialog', () => ({
29
+ useConfirm: () => async () => true,
30
+ }))
31
+
32
+ function renderPage() {
33
+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
34
+ return render(
35
+ <QueryClientProvider client={qc}>
36
+ <MemoryRouter initialEntries={['/settings/workspace/general']}>
37
+ <Routes>
38
+ <Route path="/settings/workspace/general" element={<WorkspaceGeneralPage />} />
39
+ <Route path="/app" element={<div>app-page</div>} />
40
+ </Routes>
41
+ </MemoryRouter>
42
+ </QueryClientProvider>,
43
+ )
44
+ }
45
+
46
+ beforeEach(() => {
47
+ vi.clearAllMocks()
48
+ wsMocks.role = 'owner'
49
+ apiMocks.patch.mockResolvedValue({})
50
+ apiMocks.post.mockResolvedValue({})
51
+ apiMocks.delete.mockResolvedValue(undefined)
52
+ })
53
+
54
+ describe('WorkspaceGeneralPage', () => {
55
+ it('渲染名称表单(slug 只读)与危险区', async () => {
56
+ renderPage()
57
+ const nameInput = await screen.findByLabelText('工作区名称')
58
+ expect(nameInput).toHaveValue('Acme')
59
+ expect(screen.getByLabelText('Slug(只读)')).toBeDisabled()
60
+ expect(screen.getByRole('button', { name: '退出工作区' })).toBeInTheDocument()
61
+ expect(screen.getByRole('button', { name: '删除工作区' })).toBeInTheDocument()
62
+ })
63
+
64
+ it('重命名提交 PATCH /api/workspaces/{id} {name}', async () => {
65
+ renderPage()
66
+ const nameInput = await screen.findByLabelText('工作区名称')
67
+ fireEvent.change(nameInput, { target: { value: 'Acme Renamed' } })
68
+ fireEvent.click(screen.getByRole('button', { name: '保存修改' }))
69
+
70
+ await waitFor(() =>
71
+ expect(apiMocks.patch).toHaveBeenCalledWith('/api/workspaces/ws-1', { name: 'Acme Renamed' }),
72
+ )
73
+ await waitFor(() => expect(toastMocks.success).toHaveBeenCalled())
74
+ })
75
+
76
+ it('退出工作区 409(last owner)→ 直显中文可行动文案,不再英文原文+括号补丁(B7)', async () => {
77
+ apiMocks.post.mockRejectedValue(
78
+ new ApiError(409, 'E_CONFLICT', 'last owner cannot leave; transfer or delete the workspace'),
79
+ )
80
+ renderPage()
81
+ fireEvent.click(await screen.findByRole('button', { name: '退出工作区' }))
82
+ await waitFor(() => expect(toastMocks.error).toHaveBeenCalled())
83
+ const text = toastMocks.error.mock.calls[0][0]
84
+ expect(text).toContain('最后一个所有者不能离开工作区')
85
+
86
+ expect(text).not.toContain('last owner')
87
+
88
+ expect(screen.getByRole('button', { name: '保存修改' })).toBeInTheDocument()
89
+ })
90
+
91
+ it('删除工作区:ImpactConfirmation 确认后 DELETE /api/workspaces/{id}', async () => {
92
+ renderPage()
93
+ fireEvent.click(await screen.findByRole('button', { name: '删除工作区' }))
94
+
95
+
96
+ expect(await screen.findByText('所有成员立即失去访问权限')).toBeInTheDocument()
97
+ const accept = screen.getByRole('checkbox')
98
+ fireEvent.click(accept)
99
+ fireEvent.click(screen.getByRole('button', { name: '永久删除' }))
100
+
101
+ await waitFor(() => expect(apiMocks.delete).toHaveBeenCalledWith('/api/workspaces/ws-1'))
102
+ await waitFor(() => expect(screen.getByText('app-page')).toBeInTheDocument())
103
+ })
104
+
105
+ it('BeforeDelete 钩子拦截(409)→ 错误经通用映射转中文(G7.4 后不再直出英文原文)', async () => {
106
+ apiMocks.delete.mockRejectedValue(
107
+ new ApiError(409, 'E_CONFLICT', 'workspace cannot be deleted: open orders exist'),
108
+ )
109
+ renderPage()
110
+ fireEvent.click(await screen.findByRole('button', { name: '删除工作区' }))
111
+ fireEvent.click(screen.getByRole('checkbox'))
112
+ fireEvent.click(screen.getByRole('button', { name: '永久删除' }))
113
+
114
+ await waitFor(() => expect(toastMocks.error).toHaveBeenCalled())
115
+
116
+ expect(toastMocks.error.mock.calls[0][0]).toContain('工作区暂时无法删除')
117
+ expect(toastMocks.error.mock.calls[0][0]).not.toContain('cannot be deleted')
118
+ })
119
+
120
+ it('纯空格名称被拒(B10:trim 后非空),0 网络请求', async () => {
121
+ renderPage()
122
+ const nameInput = await screen.findByLabelText('工作区名称')
123
+ fireEvent.change(nameInput, { target: { value: ' ' } })
124
+ fireEvent.click(screen.getByRole('button', { name: '保存修改' }))
125
+
126
+
127
+ await waitFor(() => expect(screen.getByRole('alert')).toBeInTheDocument())
128
+ expect(screen.getByRole('alert')).toHaveTextContent('请输入工作区名称')
129
+ expect(apiMocks.patch).not.toHaveBeenCalled()
130
+ })
131
+
132
+ it('首尾空白在提交前被 trim(B10:落库名称不带空白)', async () => {
133
+ renderPage()
134
+ const nameInput = await screen.findByLabelText('工作区名称')
135
+ fireEvent.change(nameInput, { target: { value: ' Acme Renamed ' } })
136
+ fireEvent.click(screen.getByRole('button', { name: '保存修改' }))
137
+
138
+ await waitFor(() =>
139
+ expect(apiMocks.patch).toHaveBeenCalledWith('/api/workspaces/ws-1', { name: 'Acme Renamed' }),
140
+ )
141
+ })
142
+ })
143
+
144
+ describe('WorkspaceGeneralPage 角色门控(IA v2 矩阵)', () => {
145
+ it('admin:可改名,不可删除(删除卡隐藏),退出可见', async () => {
146
+ wsMocks.role = 'admin'
147
+ renderPage()
148
+ const nameInput = await screen.findByLabelText('工作区名称')
149
+ expect(nameInput).toBeEnabled()
150
+ expect(screen.getByRole('button', { name: '保存修改' })).toBeInTheDocument()
151
+ expect(screen.queryByRole('button', { name: '删除工作区' })).not.toBeInTheDocument()
152
+ expect(screen.getByRole('button', { name: '退出工作区' })).toBeInTheDocument()
153
+ })
154
+
155
+ it('member:名称只读(无保存按钮),删除卡隐藏,退出仍可见(后端兜底 last-owner)', async () => {
156
+ wsMocks.role = 'member'
157
+ renderPage()
158
+ const nameInput = await screen.findByLabelText('工作区名称')
159
+ expect(nameInput).toBeDisabled()
160
+ expect(screen.queryByRole('button', { name: '保存修改' })).not.toBeInTheDocument()
161
+ expect(screen.queryByRole('button', { name: '删除工作区' })).not.toBeInTheDocument()
162
+ expect(screen.getByRole('button', { name: '退出工作区' })).toBeInTheDocument()
163
+ })
164
+ })