@app-vitals/agent-admin 0.0.1 → 0.1.1

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 (325) hide show
  1. package/Dockerfile +43 -0
  2. package/README.md +72 -2
  3. package/e2e/agents-page.e2e.ts +338 -0
  4. package/e2e/chat-degraded.e2e.ts +153 -0
  5. package/e2e/chat-page.e2e.ts +169 -0
  6. package/e2e/chat-progress.e2e.ts +206 -0
  7. package/e2e/chat-thread-page.e2e.ts +444 -0
  8. package/e2e/login-page.e2e.ts +148 -0
  9. package/e2e/test-server.ts +427 -0
  10. package/openapi.json +6098 -0
  11. package/openapi.platform.json +5553 -0
  12. package/package.json +62 -4
  13. package/playwright.config.ts +25 -0
  14. package/prisma/client/browser.ts +134 -0
  15. package/prisma/client/client.ts +158 -0
  16. package/prisma/client/commonInputTypes.ts +578 -0
  17. package/prisma/client/enums.ts +15 -0
  18. package/prisma/client/internal/class.ts +424 -0
  19. package/prisma/client/internal/prismaNamespace.ts +2898 -0
  20. package/prisma/client/internal/prismaNamespaceBrowser.ts +476 -0
  21. package/prisma/client/models/Account.ts +1774 -0
  22. package/prisma/client/models/AccountInvite.ts +1386 -0
  23. package/prisma/client/models/AccountMember.ts +1351 -0
  24. package/prisma/client/models/Agent.ts +4554 -0
  25. package/prisma/client/models/AgentAsk.ts +1373 -0
  26. package/prisma/client/models/AgentChatTokenUsageDailyByModel.ts +1638 -0
  27. package/prisma/client/models/AgentCronJob.ts +2416 -0
  28. package/prisma/client/models/AgentCronRun.ts +2858 -0
  29. package/prisma/client/models/AgentCronRunModelBreakdown.ts +1532 -0
  30. package/prisma/client/models/AgentCronRunSkillUsage.ts +1642 -0
  31. package/prisma/client/models/AgentEnv.ts +1421 -0
  32. package/prisma/client/models/AgentGitHubInstallationsSnapshot.ts +1289 -0
  33. package/prisma/client/models/AgentMember.ts +1316 -0
  34. package/prisma/client/models/AgentPhaseMethodology.ts +1355 -0
  35. package/prisma/client/models/AgentPlugin.ts +1421 -0
  36. package/prisma/client/models/AgentToken.ts +1380 -0
  37. package/prisma/client/models/AgentTool.ts +1351 -0
  38. package/prisma/client/models/AgentWorkQueueSnapshot.ts +1297 -0
  39. package/prisma/client/models/ChatThreadWatch.ts +1183 -0
  40. package/prisma/client/models/PushSubscription.ts +1233 -0
  41. package/prisma/client/models/SessionAlertState.ts +1187 -0
  42. package/prisma/client/models/SessionFollow.ts +1187 -0
  43. package/prisma/client/models/UserNotificationPrefs.ts +1221 -0
  44. package/prisma/client/models.ts +34 -0
  45. package/prisma/migrations/20260608000000_init/migration.sql +112 -0
  46. package/prisma/migrations/20260616000000_agent_members/migration.sql +18 -0
  47. package/prisma/migrations/20260624000000_add_agent_self_hosted/migration.sql +2 -0
  48. package/prisma/migrations/20260624120000_add_agent_repos/migration.sql +3 -0
  49. package/prisma/migrations/20260625000000_add_agent_cron_run/migration.sql +26 -0
  50. package/prisma/migrations/20260626000000_add_cron_run_token_fields/migration.sql +10 -0
  51. package/prisma/migrations/20260627000000_add_agent_chat_token_usage_daily/migration.sql +18 -0
  52. package/prisma/migrations/20260627010000_add_agent_chat_token_usage_fk/migration.sql +2 -0
  53. package/prisma/migrations/20260701000000_add_chat_threads_messages/migration.sql +37 -0
  54. package/prisma/migrations/20260701010000_drop_chat_threads_messages/migration.sql +17 -0
  55. package/prisma/migrations/20260701020000_add_cron_run_model_breakdown/migration.sql +23 -0
  56. package/prisma/migrations/20260701020000_chat_token_by_model/migration.sql +25 -0
  57. package/prisma/migrations/20260701030000_add_secret_flag_to_agent_env/migration.sql +2 -0
  58. package/prisma/migrations/20260701030000_drop_cron_run_legacy_columns/migration.sql +5 -0
  59. package/prisma/migrations/20260706000000_consolidate_cron_run_token_accounting/migration.sql +54 -0
  60. package/prisma/migrations/20260709000000_add_phase_to_agent_cron_run/migration.sql +8 -0
  61. package/prisma/migrations/20260717000000_add_item_tracking_to_agent_cron_run/migration.sql +10 -0
  62. package/prisma/migrations/20260717010000_add_agent_work_queue_snapshot/migration.sql +20 -0
  63. package/prisma/migrations/20260718000000_add_cron_phase_columns/migration.sql +25 -0
  64. package/prisma/migrations/20260718100000_drop_agent_cron_run_phase_column/migration.sql +8 -0
  65. package/prisma/migrations/20260724000000_add_agent_type_name/migration.sql +3 -0
  66. package/prisma/migrations/20260725232433_add_agent_author_allowlist/migration.sql +2 -0
  67. package/prisma/migrations/20260727120000_add_not_null_to_agent_author_allowlist/migration.sql +3 -0
  68. package/prisma/migrations/20260731000000_add_session_id_to_agent_cron_run/migration.sql +2 -0
  69. package/prisma/migrations/20260821000000_add_agent_restrict_slack_to_members/migration.sql +3 -0
  70. package/prisma/migrations/20260901000000_add_push_subscription_and_chat_thread_watch/migration.sql +40 -0
  71. package/prisma/migrations/20260903000000_add_agent_patch_author_allowlist/migration.sql +2 -0
  72. package/prisma/migrations/20260903120000_add_agent_review_author_allowlist/migration.sql +18 -0
  73. package/prisma/migrations/20260903150000_drop_agent_author_allowlist/migration.sql +14 -0
  74. package/prisma/migrations/20260905000000_add_started_at_index_to_agent_cron_run/migration.sql +11 -0
  75. package/prisma/migrations/20260906000000_add_session_follow_notification_prefs_alert_state/migration.sql +47 -0
  76. package/prisma/migrations/20260911000000_backfill_auto_follow_since/migration.sql +16 -0
  77. package/prisma/migrations/20260923115814_add_last_heartbeat_at_to_agent_cron_run/migration.sql +2 -0
  78. package/prisma/migrations/20260924120000_add_trial_expiry_fields_to_agent/migration.sql +6 -0
  79. package/prisma/migrations/20260929000000_add_agent_phase_methodology/migration.sql +21 -0
  80. package/prisma/migrations/20260929120000_add_agent_policy_fields/migration.sql +15 -0
  81. package/prisma/migrations/20261005000000_add_agent_github_installations_snapshot/migration.sql +21 -0
  82. package/prisma/migrations/20261008000000_add_cron_run_telemetry/migration.sql +50 -0
  83. package/prisma/migrations/20261008010000_add_account_models/migration.sql +48 -0
  84. package/prisma/migrations/20261008020000_add_cron_lockdown_disabled_at/migration.sql +3 -0
  85. package/prisma/migrations/20261009000000_chat_token_usage_bigint/migration.sql +7 -0
  86. package/prisma/migrations/20261010000000_add_cron_run_command_variant/migration.sql +2 -0
  87. package/prisma/migrations/20261019000000_add_agent_anthropic_identity/migration.sql +9 -0
  88. package/prisma/migrations/20261025000000_add_agent_ask/migration.sql +23 -0
  89. package/prisma/migrations/migration_lock.toml +3 -0
  90. package/prisma/schema/coding.prisma +85 -0
  91. package/prisma/schema/schema.prisma +506 -0
  92. package/prisma.config.ts +34 -0
  93. package/pwa-assets/icons/apple-touch-icon.png +0 -0
  94. package/pwa-assets/icons/favicon-32.png +0 -0
  95. package/pwa-assets/icons/icon-192.png +0 -0
  96. package/pwa-assets/icons/icon-512.png +0 -0
  97. package/pwa-assets/icons/icon-maskable-192.png +0 -0
  98. package/pwa-assets/icons/icon-maskable-512.png +0 -0
  99. package/src/account-agent-create.ts +42 -0
  100. package/src/account-agent-create.unit.test.ts +28 -0
  101. package/src/account-created-notifier.ts +71 -0
  102. package/src/account-created-notifier.unit.test.ts +70 -0
  103. package/src/account-email.ts +9 -0
  104. package/src/account-email.unit.test.ts +21 -0
  105. package/src/account-invites.ts +70 -0
  106. package/src/account-lifecycle.integration.test.ts +329 -0
  107. package/src/account-lifecycle.ts +206 -0
  108. package/src/account-lifecycle.unit.test.ts +108 -0
  109. package/src/account-members.ts +128 -0
  110. package/src/account-onboarding.integration.test.ts +101 -0
  111. package/src/account-onboarding.ts +95 -0
  112. package/src/account-trial-expiry-sweeper.ts +85 -0
  113. package/src/account-trial-warning-sweeper.integration.test.ts +228 -0
  114. package/src/account-trial-warning-sweeper.ts +216 -0
  115. package/src/accounts-api.integration.test.ts +196 -0
  116. package/src/accounts-api.smoke.test.ts +292 -0
  117. package/src/accounts-api.ts +289 -0
  118. package/src/accounts-api.unit.test.ts +93 -0
  119. package/src/accounts.integration.test.ts +178 -0
  120. package/src/accounts.ts +208 -0
  121. package/src/admin-boot-config.ts +574 -0
  122. package/src/admin-boot-config.unit.test.ts +28 -0
  123. package/src/admin-boot.boot-order.unit.test.ts +91 -0
  124. package/src/admin-boot.ts +690 -0
  125. package/src/admin-dev-login.smoke.test.ts +317 -0
  126. package/src/admin-extension.ts +432 -0
  127. package/src/admin-extension.unit.test.ts +532 -0
  128. package/src/admin-local-agent.smoke.test.ts +1643 -0
  129. package/src/admin-spec.smoke.test.ts +96 -0
  130. package/src/admin-spec.ts +186 -0
  131. package/src/admin-ui-account-nav.ts +20 -0
  132. package/src/admin-ui-account-pages.ts +131 -0
  133. package/src/admin-ui-account-pages.unit.test.ts +112 -0
  134. package/src/admin-ui-account.smoke.test.ts +286 -0
  135. package/src/admin-ui-account.ts +185 -0
  136. package/src/admin-ui-accounts-pages.ts +202 -0
  137. package/src/admin-ui-accounts-pages.unit.test.ts +138 -0
  138. package/src/admin-ui-accounts.smoke.test.ts +434 -0
  139. package/src/admin-ui-accounts.ts +266 -0
  140. package/src/admin-ui-layout.ts +60 -0
  141. package/src/admin-ui-layout.unit.test.ts +140 -0
  142. package/src/admin-ui-pages.ts +3489 -0
  143. package/src/admin-ui-pages.unit.test.ts +4355 -0
  144. package/src/admin-ui-push-test.smoke.test.ts +125 -0
  145. package/src/admin-ui-push-test.ts +102 -0
  146. package/src/admin-ui-styles.ts +656 -0
  147. package/src/admin-ui-styles.unit.test.ts +103 -0
  148. package/src/admin-ui.fixtures.ts +457 -0
  149. package/src/admin-ui.smoke.test.ts +7227 -0
  150. package/src/admin-ui.ts +3824 -0
  151. package/src/agent-account-assignment.ts +49 -0
  152. package/src/agent-account-assignment.unit.test.ts +99 -0
  153. package/src/agent-anthropic-identity.integration.test.ts +99 -0
  154. package/src/agent-anthropic-identity.ts +78 -0
  155. package/src/agent-anthropic-identity.unit.test.ts +77 -0
  156. package/src/agent-ask-config.ts +59 -0
  157. package/src/agent-ask-config.unit.test.ts +70 -0
  158. package/src/agent-ask.integration.test.ts +175 -0
  159. package/src/agent-ask.smoke.test.ts +457 -0
  160. package/src/agent-ask.ts +378 -0
  161. package/src/agent-chat-tokens.integration.test.ts +420 -0
  162. package/src/agent-chat-tokens.smoke.test.ts +551 -0
  163. package/src/agent-chat-tokens.ts +259 -0
  164. package/src/agent-cron-jobs.integration.test.ts +582 -0
  165. package/src/agent-cron-jobs.ts +615 -0
  166. package/src/agent-cron-jobs.unit.test.ts +420 -0
  167. package/src/agent-cron-run-stats.integration.test.ts +1551 -0
  168. package/src/agent-cron-run-stats.smoke.test.ts +953 -0
  169. package/src/agent-cron-run-stats.ts +752 -0
  170. package/src/agent-cron-runs.integration.test.ts +1491 -0
  171. package/src/agent-cron-runs.ts +489 -0
  172. package/src/agent-deletion-checklist.ts +128 -0
  173. package/src/agent-deletion-checklist.unit.test.ts +201 -0
  174. package/src/agent-deletion-per-agent.integration.test.ts +323 -0
  175. package/src/agent-deletion.integration.test.ts +323 -0
  176. package/src/agent-deletion.ts +329 -0
  177. package/src/agent-deletion.unit.test.ts +412 -0
  178. package/src/agent-envs.integration.test.ts +323 -0
  179. package/src/agent-envs.ts +244 -0
  180. package/src/agent-github-installations.integration.test.ts +115 -0
  181. package/src/agent-github-installations.ts +52 -0
  182. package/src/agent-manifest.ts +685 -0
  183. package/src/agent-manifest.unit.test.ts +870 -0
  184. package/src/agent-members.ts +69 -0
  185. package/src/agent-members.unit.test.ts +250 -0
  186. package/src/agent-patch-author-allowlist.integration.test.ts +218 -0
  187. package/src/agent-plugins.integration.test.ts +157 -0
  188. package/src/agent-plugins.ts +95 -0
  189. package/src/agent-plugins.unit.test.ts +43 -0
  190. package/src/agent-provisioner-per-agent.integration.test.ts +949 -0
  191. package/src/agent-provisioner.integration.test.ts +1245 -0
  192. package/src/agent-provisioner.ts +1650 -0
  193. package/src/agent-provisioner.unit.test.ts +1852 -0
  194. package/src/agent-tokens.integration.test.ts +150 -0
  195. package/src/agent-tokens.ts +112 -0
  196. package/src/agent-tools.integration.test.ts +129 -0
  197. package/src/agent-tools.ts +111 -0
  198. package/src/agents-api.integration.test.ts +705 -0
  199. package/src/agents-api.sentry.smoke.test.ts +478 -0
  200. package/src/agents-api.smoke.test.ts +5986 -0
  201. package/src/agents-api.ts +2826 -0
  202. package/src/agents.integration.test.ts +512 -0
  203. package/src/agents.ts +1097 -0
  204. package/src/agents.unit.test.ts +1736 -0
  205. package/src/anthropic-credential-guard.ts +44 -0
  206. package/src/anthropic-credential-guard.unit.test.ts +88 -0
  207. package/src/anthropic-federated-token-provider.integration.test.ts +248 -0
  208. package/src/anthropic-federated-token-provider.ts +249 -0
  209. package/src/anthropic-identity-client.fixtures.ts +237 -0
  210. package/src/anthropic-identity-client.integration.test.ts +342 -0
  211. package/src/anthropic-identity-client.ts +473 -0
  212. package/src/anthropic-identity-fixture-spec.integration.test.ts +169 -0
  213. package/src/anthropic-identity-name.integration.test.ts +83 -0
  214. package/src/anthropic-identity-name.ts +163 -0
  215. package/src/anthropic-identity-name.unit.test.ts +345 -0
  216. package/src/anthropic-openapi-contract.fixtures.ts +192 -0
  217. package/src/api-auth.ts +383 -0
  218. package/src/api-auth.unit.test.ts +837 -0
  219. package/src/api.smoke.test.ts +1225 -0
  220. package/src/api.ts +376 -0
  221. package/src/attachment-validation.ts +124 -0
  222. package/src/attachment-validation.unit.test.ts +218 -0
  223. package/src/caller-scope.ts +131 -0
  224. package/src/caller-scope.unit.test.ts +116 -0
  225. package/src/chat-credential-config.ts +22 -0
  226. package/src/chat-credentials-logging.unit.test.ts +92 -0
  227. package/src/chat-markers.ts +114 -0
  228. package/src/chat-markers.unit.test.ts +116 -0
  229. package/src/chat-service-provisioning-client.integration.test.ts +412 -0
  230. package/src/chat-service-provisioning-client.ts +221 -0
  231. package/src/chat.smoke.test.ts +1875 -0
  232. package/src/clock.ts +47 -0
  233. package/src/coding-task-store-provisioning-client.integration.test.ts +372 -0
  234. package/src/coding-task-store-provisioning-client.ts +156 -0
  235. package/src/crypto.ts +83 -0
  236. package/src/dev-auth-guard.ts +29 -0
  237. package/src/dev-auth-guard.unit.test.ts +51 -0
  238. package/src/errors.ts +77 -0
  239. package/src/errors.unit.test.ts +52 -0
  240. package/src/fixtures/anthropic-identity-recorded-fixtures.json +850 -0
  241. package/src/fixtures/anthropic-openapi/SDK_VERSION +1 -0
  242. package/src/fixtures/anthropic-openapi/mock-spec.json.gz +0 -0
  243. package/src/fixtures/chat-service-provisioning-cassette.json +156 -0
  244. package/src/fixtures/github-app-manifest-cassette.json +44 -0
  245. package/src/fixtures/google-auth-cassette.json +36 -0
  246. package/src/fixtures/http-chat-client-cassette.json +226 -0
  247. package/src/fixtures/k8s-cassette.json +470 -0
  248. package/src/fixtures/okta-auth-cassette.json +36 -0
  249. package/src/fixtures/slack-provision-cassette.json +12 -0
  250. package/src/fixtures/slack-provisioning-http-cassette.json +98 -0
  251. package/src/fixtures/task-store-provisioning-cassette.json +85 -0
  252. package/src/github-app-provisioning-client.integration.test.ts +149 -0
  253. package/src/github-app-provisioning-client.ts +161 -0
  254. package/src/github-app-provisioning-client.unit.test.ts +114 -0
  255. package/src/github-provisioning-service.ts +569 -0
  256. package/src/github-provisioning-service.unit.test.ts +123 -0
  257. package/src/google-auth-client.integration.test.ts +195 -0
  258. package/src/google-auth-client.ts +121 -0
  259. package/src/http-chat-client.integration.test.ts +554 -0
  260. package/src/http-chat-client.ts +533 -0
  261. package/src/http-chat-client.unit.test.ts +119 -0
  262. package/src/import-boundary.unit.test.ts +240 -0
  263. package/src/kubernetes-client.integration.test.ts +697 -0
  264. package/src/kubernetes-client.ts +1027 -0
  265. package/src/kubernetes-client.unit.test.ts +726 -0
  266. package/src/main.smoke.test.ts +91 -0
  267. package/src/main.ts +21 -0
  268. package/src/main.unit.test.ts +801 -0
  269. package/src/okta-auth-client.integration.test.ts +263 -0
  270. package/src/okta-auth-client.ts +152 -0
  271. package/src/openapi-schemas.ts +1310 -0
  272. package/src/openapi-schemas.unit.test.ts +348 -0
  273. package/src/per-agent-identity.fixtures.ts +305 -0
  274. package/src/platform-doc-route.ts +40 -0
  275. package/src/prisma-client.integration.test.ts +57 -0
  276. package/src/prisma-client.ts +187 -0
  277. package/src/prisma-client.unit.test.ts +178 -0
  278. package/src/prisma-tx.ts +23 -0
  279. package/src/push-content.ts +177 -0
  280. package/src/push-content.unit.test.ts +213 -0
  281. package/src/push-crypto.ts +135 -0
  282. package/src/push-crypto.unit.test.ts +66 -0
  283. package/src/push-sender.ts +169 -0
  284. package/src/push-sender.unit.test.ts +107 -0
  285. package/src/push-service.ts +332 -0
  286. package/src/push-service.unit.test.ts +679 -0
  287. package/src/push-toggle.ts +135 -0
  288. package/src/push-toggle.unit.test.ts +78 -0
  289. package/src/pwa-https-gating.smoke.test.ts +229 -0
  290. package/src/pwa-icons.unit.test.ts +41 -0
  291. package/src/pwa-routes.smoke.test.ts +276 -0
  292. package/src/pwa.ts +380 -0
  293. package/src/pwa.unit.test.ts +321 -0
  294. package/src/schema.unit.test.ts +122 -0
  295. package/src/self-serve-config.ts +52 -0
  296. package/src/self-serve-config.unit.test.ts +79 -0
  297. package/src/server.smoke.test.ts +273 -0
  298. package/src/server.ts +50 -0
  299. package/src/service-integration.ts +67 -0
  300. package/src/service-integration.unit.test.ts +129 -0
  301. package/src/slack-provisioning-client.integration.test.ts +308 -0
  302. package/src/slack-provisioning-client.ts +411 -0
  303. package/src/slack-provisioning-client.unit.test.ts +80 -0
  304. package/src/slack-provisioning-service.integration.test.ts +637 -0
  305. package/src/slack-provisioning-service.ts +451 -0
  306. package/src/slack-provisioning.integration.test.ts +395 -0
  307. package/src/specialty-manifest-loader.fixture.integration.test.ts +63 -0
  308. package/src/specialty-manifest-loader.ts +255 -0
  309. package/src/specialty-manifest-loader.unit.test.ts +301 -0
  310. package/src/specialty-registry.schema.unit.test.ts +35 -0
  311. package/src/specialty-registry.ts +236 -0
  312. package/src/specialty-registry.unit.test.ts +272 -0
  313. package/src/startup-policy-reconcile.ts +77 -0
  314. package/src/startup-policy-reconcile.unit.test.ts +251 -0
  315. package/src/theme-tokens.json +12 -0
  316. package/src/token-crypto.ts +60 -0
  317. package/src/token-crypto.unit.test.ts +182 -0
  318. package/src/trial-expiry-sweeper.integration.test.ts +318 -0
  319. package/src/trial-expiry-sweeper.ts +132 -0
  320. package/src/trial-expiry-sweeper.unit.test.ts +391 -0
  321. package/src/trial-expiry-warning-sweeper.integration.test.ts +468 -0
  322. package/src/trial-expiry-warning-sweeper.ts +447 -0
  323. package/src/trial-expiry-warning-sweeper.unit.test.ts +148 -0
  324. package/src/voice-contract.unit.test.ts +102 -0
  325. package/tsconfig.json +9 -0
@@ -0,0 +1,138 @@
1
+ /**
2
+ * admin/src/admin-ui-accounts-pages.unit.test.ts
3
+ * Render tests for the admin Accounts pages + agents-list Account column (SSP-5.2).
4
+ */
5
+
6
+ import { describe, expect, test } from "bun:test";
7
+ import {
8
+ renderAccountDetailPage,
9
+ renderAccountsListPage,
10
+ } from "./admin-ui-accounts-pages.ts";
11
+ import { renderAgentsPage } from "./admin-ui-pages.ts";
12
+
13
+ const d = new Date("2026-01-01T00:00:00Z");
14
+ const account = {
15
+ id: "a1",
16
+ name: "Acme <Co>",
17
+ status: "active",
18
+ maxAgents: 3,
19
+ plan: "pro",
20
+ trialExpiresAt: new Date("2026-03-05T00:00:00Z"),
21
+ trialExpiryWarnedAt: null,
22
+ createdAt: d,
23
+ updatedAt: d,
24
+ agentCount: 2,
25
+ memberCount: 2,
26
+ };
27
+
28
+ describe("renderAccountsListPage", () => {
29
+ test("renders row with owner, counts, status, dates; escapes name", () => {
30
+ const html = renderAccountsListPage({
31
+ userEmail: "a@x.com",
32
+ accounts: [account],
33
+ owners: new Map([["a1", ["o@x.com"]]]),
34
+ });
35
+ expect(html).toContain("Acme &lt;Co&gt;");
36
+ expect(html).not.toContain("Acme <Co>");
37
+ expect(html).toContain("o@x.com");
38
+ expect(html).toContain("2 / 3");
39
+ expect(html).toContain("2026-03-05");
40
+ expect(html).toContain("/admin/accounts/a1");
41
+ });
42
+
43
+ test("empty state", () => {
44
+ const html = renderAccountsListPage({
45
+ userEmail: "a@x.com",
46
+ accounts: [],
47
+ owners: new Map(),
48
+ });
49
+ expect(html).toContain("No accounts.");
50
+ });
51
+ });
52
+
53
+ describe("renderAccountDetailPage", () => {
54
+ const base = {
55
+ userEmail: "a@x.com",
56
+ account,
57
+ agents: [{ id: "g1", name: "Bot" }],
58
+ members: [
59
+ {
60
+ id: "m1",
61
+ accountId: "a1",
62
+ email: "o@x.com",
63
+ role: "owner",
64
+ createdAt: d,
65
+ },
66
+ {
67
+ id: "m2",
68
+ accountId: "a1",
69
+ email: "m@x.com",
70
+ role: "member",
71
+ createdAt: d,
72
+ },
73
+ ],
74
+ invites: [
75
+ {
76
+ id: "i1",
77
+ accountId: "a1",
78
+ email: "new@x.com",
79
+ invitedBy: "o@x.com",
80
+ acceptedAt: null,
81
+ createdAt: d,
82
+ },
83
+ ],
84
+ };
85
+
86
+ test("edit form, suspend button, agents, members, invites", () => {
87
+ const html = renderAccountDetailPage(base);
88
+ expect(html).toContain('action="/admin/accounts/a1/update"');
89
+ expect(html).toContain('name="maxAgents"');
90
+ expect(html).toContain('value="2026-03-05"');
91
+ expect(html).toContain("/admin/accounts/a1/suspend");
92
+ expect(html).not.toContain("/admin/accounts/a1/reactivate");
93
+ expect(html).toContain("Bot");
94
+ expect(html).toContain("/admin/accounts/a1/members/demote");
95
+ expect(html).toContain("/admin/accounts/a1/members/promote");
96
+ expect(html).toContain("/admin/accounts/a1/members/add");
97
+ expect(html).toContain("new@x.com");
98
+ });
99
+
100
+ test("suspended shows reactivate; error is shown", () => {
101
+ const html = renderAccountDetailPage({
102
+ ...base,
103
+ account: { ...account, status: "suspended" },
104
+ error: "Boom <b>",
105
+ });
106
+ expect(html).toContain("/admin/accounts/a1/reactivate");
107
+ expect(html).not.toContain("/admin/accounts/a1/suspend");
108
+ expect(html).toContain("Boom &lt;b&gt;");
109
+ });
110
+ });
111
+
112
+ describe("renderAgentsPage Account column", () => {
113
+ const agents = [
114
+ { id: "g1", name: "Bot", slackId: null, createdAt: d, accountId: "a1" },
115
+ ];
116
+ const accountNames = new Map([["a1", "Acme"]]);
117
+
118
+ test("shown for admins when accountNames supplied", () => {
119
+ const html = renderAgentsPage(agents, "a@x.com", true, "UTC", {
120
+ accountNames,
121
+ });
122
+ expect(html).toContain("<th>Account</th>");
123
+ expect(html).toContain("Acme");
124
+ });
125
+
126
+ test("never shown for non-admins even if accountNames passed", () => {
127
+ const html = renderAgentsPage(agents, "u@x.com", false, "UTC", {
128
+ accountNames,
129
+ });
130
+ expect(html).not.toContain("<th>Account</th>");
131
+ expect(html).not.toContain("Acme");
132
+ });
133
+
134
+ test("absent without accountNames (flag off)", () => {
135
+ const html = renderAgentsPage(agents, "a@x.com", true, "UTC");
136
+ expect(html).not.toContain("<th>Account</th>");
137
+ });
138
+ });
@@ -0,0 +1,434 @@
1
+ /**
2
+ * admin/src/admin-ui-accounts.smoke.test.ts
3
+ * Smoke tests for /admin/accounts routes (SSP-5.2): admin vs non-admin on
4
+ * every route, flag-off 404, persisted edits. In-memory service doubles.
5
+ */
6
+
7
+ import { describe, expect, it } from "bun:test";
8
+ import type { MiddlewareHandler } from "hono";
9
+ import { Hono } from "hono";
10
+ import {
11
+ AccountMemberNotFoundError,
12
+ LastOwnerError,
13
+ } from "./account-members.ts";
14
+ import type { AdminUIEnv } from "./admin-ui.ts";
15
+ import {
16
+ type AdminAccountsRouteDeps,
17
+ registerAdminAccountsRoutes,
18
+ } from "./admin-ui-accounts.ts";
19
+ import { UnprocessableEntityError } from "./errors.ts";
20
+
21
+ const d = new Date("2026-01-01T00:00:00Z");
22
+
23
+ function setup(opts: { enabled?: boolean } = {}) {
24
+ const account: Record<string, unknown> = {
25
+ id: "a1",
26
+ name: "Acme",
27
+ status: "active",
28
+ maxAgents: 2,
29
+ plan: null,
30
+ trialExpiresAt: null,
31
+ trialExpiryWarnedAt: null,
32
+ createdAt: d,
33
+ updatedAt: d,
34
+ };
35
+ let members = [
36
+ {
37
+ id: "m1",
38
+ accountId: "a1",
39
+ email: "owner@x.com",
40
+ role: "owner",
41
+ createdAt: d,
42
+ },
43
+ {
44
+ id: "m2",
45
+ accountId: "a1",
46
+ email: "member@x.com",
47
+ role: "member",
48
+ createdAt: d,
49
+ },
50
+ ];
51
+ const agentAccounts: Record<string, string | null> = {
52
+ g1: "a1",
53
+ free: null,
54
+ };
55
+ const reconciled: Array<{
56
+ id: string;
57
+ slug?: string;
58
+ accountId?: string | null;
59
+ }> = [];
60
+ const withCounts = () =>
61
+ ({ ...account, agentCount: 1, memberCount: members.length }) as never;
62
+ const lifecycleCalls: Array<{ id: string; data: unknown }> = [];
63
+ const owners = () => members.filter((m) => m.role === "owner").length;
64
+
65
+ const deps: AdminAccountsRouteDeps = {
66
+ enabled: opts.enabled ?? true,
67
+ requireAuth: (async (c, next) => {
68
+ c.set("userEmail", "admin@x.com");
69
+ c.set("isAdmin", c.req.header("x-test-admin") === "1");
70
+ await next();
71
+ }) as MiddlewareHandler<AdminUIEnv>,
72
+ html: (content, o) =>
73
+ new Response(content, {
74
+ status: o?.status ?? 200,
75
+ headers: { "Content-Type": "text/html" },
76
+ }),
77
+ accounts: {
78
+ async listWithCounts() {
79
+ return [withCounts()];
80
+ },
81
+ async getWithCounts(id) {
82
+ return id === "a1" ? withCounts() : null;
83
+ },
84
+ async listAgents() {
85
+ return [{ id: "g1", name: "Bot" }];
86
+ },
87
+ },
88
+ lifecycle: {
89
+ async update(id, data) {
90
+ lifecycleCalls.push({ id, data });
91
+ if (data.status === "active" && account.status === "trial_expired") {
92
+ throw new UnprocessableEntityError(
93
+ "Clear or extend trialExpiresAt before reactivating.",
94
+ );
95
+ }
96
+ Object.assign(account, data);
97
+ return account as never;
98
+ },
99
+ },
100
+ members: {
101
+ async listByAccount() {
102
+ return members;
103
+ },
104
+ async getByEmail(email) {
105
+ return members.find((m) => m.email === email) ?? null;
106
+ },
107
+ async add(_id, email, role = "member") {
108
+ const m = {
109
+ id: `m${members.length + 1}`,
110
+ accountId: "a1",
111
+ email,
112
+ role,
113
+ createdAt: d,
114
+ };
115
+ members.push(m);
116
+ return m as never;
117
+ },
118
+ async remove(_id, email) {
119
+ const m = members.find((x) => x.email === email);
120
+ if (!m) throw new AccountMemberNotFoundError("a1", email);
121
+ if (m.role === "owner" && owners() <= 1) throw new LastOwnerError("a1");
122
+ members = members.filter((x) => x !== m);
123
+ },
124
+ async promote(_id, email) {
125
+ const m = members.find((x) => x.email === email);
126
+ if (!m) throw new AccountMemberNotFoundError("a1", email);
127
+ m.role = "owner";
128
+ return m as never;
129
+ },
130
+ async demote(_id, email) {
131
+ const m = members.find((x) => x.email === email);
132
+ if (!m) throw new AccountMemberNotFoundError("a1", email);
133
+ if (m.role === "owner" && owners() <= 1) throw new LastOwnerError("a1");
134
+ m.role = "member";
135
+ return m as never;
136
+ },
137
+ },
138
+ invites: {
139
+ async listPending() {
140
+ return [];
141
+ },
142
+ },
143
+ agents: {
144
+ async getDetail(id) {
145
+ return id in agentAccounts
146
+ ? ({
147
+ id,
148
+ name: id,
149
+ selfHosted: false,
150
+ accountId: agentAccounts[id],
151
+ } as never)
152
+ : null;
153
+ },
154
+ async updateFields(id, input) {
155
+ agentAccounts[id] = input.accountId ?? null;
156
+ return {
157
+ id,
158
+ name: id,
159
+ selfHosted: false,
160
+ accountId: agentAccounts[id],
161
+ } as never;
162
+ },
163
+ },
164
+ provisioner: {
165
+ async reconcile(agents) {
166
+ reconciled.push(...agents);
167
+ return {
168
+ recreated: [],
169
+ migrated: [],
170
+ noOwner: [],
171
+ dryRun: false,
172
+ orphans: [],
173
+ failed: [],
174
+ updated: [],
175
+ };
176
+ },
177
+ },
178
+ };
179
+ const app = new Hono<AdminUIEnv>();
180
+ registerAdminAccountsRoutes(app, deps);
181
+ return {
182
+ app,
183
+ account,
184
+ members: () => members,
185
+ agentAccounts,
186
+ reconciled,
187
+ lifecycleCalls,
188
+ };
189
+ }
190
+
191
+ const ADMIN = { "x-test-admin": "1" };
192
+ function post(
193
+ app: Hono<AdminUIEnv>,
194
+ path: string,
195
+ fields: Record<string, string>,
196
+ admin = true,
197
+ ) {
198
+ return app.request(path, {
199
+ method: "POST",
200
+ headers: admin ? ADMIN : {},
201
+ body: new URLSearchParams(fields),
202
+ });
203
+ }
204
+
205
+ describe("/admin/accounts agent assignment (SSP-5.3)", () => {
206
+ it("assigns an agent, then reconciles it with the new accountId", async () => {
207
+ const { app, agentAccounts, reconciled } = setup();
208
+ const res = await post(app, "/admin/accounts/a1/agents/assign", {
209
+ agentId: "free",
210
+ });
211
+ expect(res.status).toBe(302);
212
+ expect(agentAccounts.free).toBe("a1");
213
+ expect(reconciled).toEqual([{ id: "free", slug: "free", accountId: "a1" }]);
214
+ });
215
+
216
+ it("rejects an unknown agent without changing anything", async () => {
217
+ const { app, reconciled } = setup();
218
+ const res = await post(app, "/admin/accounts/a1/agents/assign", {
219
+ agentId: "nope",
220
+ });
221
+ expect(res.status).toBe(400);
222
+ expect(reconciled).toEqual([]);
223
+ });
224
+
225
+ it("unassigns an agent and reconciles with a null accountId", async () => {
226
+ const { app, agentAccounts, reconciled } = setup();
227
+ const res = await post(app, "/admin/accounts/a1/agents/unassign", {
228
+ agentId: "g1",
229
+ });
230
+ expect(res.status).toBe(302);
231
+ expect(agentAccounts.g1).toBeNull();
232
+ expect(reconciled).toEqual([{ id: "g1", slug: "g1", accountId: null }]);
233
+ });
234
+
235
+ it("refuses to unassign an agent owned by another account", async () => {
236
+ const { app, agentAccounts } = setup();
237
+ const res = await post(app, "/admin/accounts/a1/agents/unassign", {
238
+ agentId: "free",
239
+ });
240
+ expect(res.status).toBe(400);
241
+ expect(agentAccounts.free).toBeNull();
242
+ });
243
+ });
244
+
245
+ describe("/admin/accounts", () => {
246
+ it("lists accounts for admin", async () => {
247
+ const { app } = setup();
248
+ const res = await app.request("/admin/accounts", { headers: ADMIN });
249
+ expect(res.status).toBe(200);
250
+ const body = await res.text();
251
+ expect(body).toContain("Acme");
252
+ expect(body).toContain("owner@x.com");
253
+ });
254
+
255
+ it("403 for non-admin on every route", async () => {
256
+ const { app } = setup();
257
+ expect((await app.request("/admin/accounts")).status).toBe(403);
258
+ expect((await app.request("/admin/accounts/a1")).status).toBe(403);
259
+ for (const p of [
260
+ "update",
261
+ "suspend",
262
+ "reactivate",
263
+ "members/add",
264
+ "members/remove",
265
+ "members/promote",
266
+ "members/demote",
267
+ "agents/assign",
268
+ "agents/unassign",
269
+ ]) {
270
+ const res = await post(
271
+ app,
272
+ `/admin/accounts/a1/${p}`,
273
+ { email: "x@y.com" },
274
+ false,
275
+ );
276
+ expect(res.status).toBe(403);
277
+ }
278
+ });
279
+
280
+ it("404 everywhere when flag is off, even for admin", async () => {
281
+ const { app } = setup({ enabled: false });
282
+ expect(
283
+ (await app.request("/admin/accounts", { headers: ADMIN })).status,
284
+ ).toBe(404);
285
+ expect(
286
+ (await app.request("/admin/accounts/a1", { headers: ADMIN })).status,
287
+ ).toBe(404);
288
+ expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
289
+ 404,
290
+ );
291
+ });
292
+
293
+ it("detail 404 for unknown account", async () => {
294
+ const { app } = setup();
295
+ expect(
296
+ (await app.request("/admin/accounts/nope", { headers: ADMIN })).status,
297
+ ).toBe(404);
298
+ expect((await post(app, "/admin/accounts/nope/suspend", {})).status).toBe(
299
+ 404,
300
+ );
301
+ });
302
+
303
+ it("detail page shows agents and members", async () => {
304
+ const { app } = setup();
305
+ const body = await (
306
+ await app.request("/admin/accounts/a1", { headers: ADMIN })
307
+ ).text();
308
+ expect(body).toContain("Bot");
309
+ expect(body).toContain("member@x.com");
310
+ });
311
+
312
+ it("update persists maxAgents, plan and trialExpiresAt", async () => {
313
+ const { app, account } = setup();
314
+ const res = await post(app, "/admin/accounts/a1/update", {
315
+ name: "Acme 2",
316
+ maxAgents: "7",
317
+ plan: "pro",
318
+ trialExpiresAt: "2026-03-05",
319
+ });
320
+ expect(res.status).toBe(302);
321
+ expect(account.name).toBe("Acme 2");
322
+ expect(account.maxAgents).toBe(7);
323
+ expect(account.plan).toBe("pro");
324
+ expect((account.trialExpiresAt as Date).toISOString().slice(0, 10)).toBe(
325
+ "2026-03-05",
326
+ );
327
+ await post(app, "/admin/accounts/a1/update", {
328
+ name: "Acme 2",
329
+ maxAgents: "7",
330
+ plan: "",
331
+ trialExpiresAt: "",
332
+ });
333
+ expect(account.trialExpiresAt).toBeNull();
334
+ expect(account.plan).toBeNull();
335
+ });
336
+
337
+ it("update rejects invalid maxAgents / date / name with 400", async () => {
338
+ const { app, account } = setup();
339
+ const cases: Array<Record<string, string>> = [
340
+ { name: "A", maxAgents: "-1" },
341
+ { name: "A", maxAgents: "1.5" },
342
+ { name: "A", maxAgents: "" },
343
+ { name: "A", maxAgents: "1", trialExpiresAt: "garbage" },
344
+ { name: "", maxAgents: "1" },
345
+ ];
346
+ for (const f of cases) {
347
+ expect((await post(app, "/admin/accounts/a1/update", f)).status).toBe(
348
+ 400,
349
+ );
350
+ }
351
+ expect(account.maxAgents).toBe(2);
352
+ });
353
+
354
+ it("suspend and reactivate go through the account lifecycle (SSP-8.2)", async () => {
355
+ const { app, lifecycleCalls } = setup();
356
+ await post(app, "/admin/accounts/a1/suspend", {});
357
+ await post(app, "/admin/accounts/a1/reactivate", {});
358
+ expect(lifecycleCalls).toEqual([
359
+ { id: "a1", data: { status: "suspended" } },
360
+ { id: "a1", data: { status: "active" } },
361
+ ]);
362
+ });
363
+
364
+ it("reactivate rejected by the lifecycle renders a 400 with the reason", async () => {
365
+ const { app, account } = setup();
366
+ account.status = "trial_expired";
367
+ const res = await post(app, "/admin/accounts/a1/reactivate", {});
368
+ expect(res.status).toBe(400);
369
+ expect(await res.text()).toContain("Clear or extend trialExpiresAt");
370
+ expect(account.status).toBe("trial_expired");
371
+ });
372
+
373
+ it("suspend and reactivate set status", async () => {
374
+ const { app, account } = setup();
375
+ expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
376
+ 302,
377
+ );
378
+ expect(account.status).toBe("suspended");
379
+ expect((await post(app, "/admin/accounts/a1/reactivate", {})).status).toBe(
380
+ 302,
381
+ );
382
+ expect(account.status).toBe("active");
383
+ });
384
+
385
+ it("adds member and owner; rejects invalid and duplicate emails", async () => {
386
+ const { app, members } = setup();
387
+ expect(
388
+ (
389
+ await post(app, "/admin/accounts/a1/members/add", {
390
+ email: "New@X.com",
391
+ role: "owner",
392
+ })
393
+ ).status,
394
+ ).toBe(302);
395
+ expect(members().find((m) => m.email === "new@x.com")?.role).toBe("owner");
396
+ expect(
397
+ (await post(app, "/admin/accounts/a1/members/add", { email: "bad" }))
398
+ .status,
399
+ ).toBe(400);
400
+ expect(
401
+ (
402
+ await post(app, "/admin/accounts/a1/members/add", {
403
+ email: "member@x.com",
404
+ })
405
+ ).status,
406
+ ).toBe(400);
407
+ });
408
+
409
+ it("promote, demote, remove; last-owner guard returns 400", async () => {
410
+ const { app, members } = setup();
411
+ await post(app, "/admin/accounts/a1/members/promote", {
412
+ email: "member@x.com",
413
+ });
414
+ expect(members().find((m) => m.email === "member@x.com")?.role).toBe(
415
+ "owner",
416
+ );
417
+ await post(app, "/admin/accounts/a1/members/remove", {
418
+ email: "member@x.com",
419
+ });
420
+ expect(members().length).toBe(1);
421
+ const res = await post(app, "/admin/accounts/a1/members/remove", {
422
+ email: "owner@x.com",
423
+ });
424
+ expect(res.status).toBe(400);
425
+ expect(await res.text()).toContain("last owner");
426
+ expect(
427
+ (
428
+ await post(app, "/admin/accounts/a1/members/demote", {
429
+ email: "ghost@x.com",
430
+ })
431
+ ).status,
432
+ ).toBe(404);
433
+ });
434
+ });