@nextsparkjs/theme-default 0.1.0-beta.2 → 0.1.0-beta.21

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/package.json +8 -4
  2. package/templates/(public)/page.tsx +1 -1
  3. package/tests/cypress/e2e/_devtools/access.bdd.md +262 -0
  4. package/tests/cypress/e2e/_devtools/access.cy.ts +171 -0
  5. package/tests/cypress/e2e/_devtools/navigation.bdd.md +261 -0
  6. package/tests/cypress/e2e/_devtools/navigation.cy.ts +157 -0
  7. package/tests/cypress/e2e/_devtools/pages.bdd.md +303 -0
  8. package/tests/cypress/e2e/_devtools/pages.cy.ts +184 -0
  9. package/tests/cypress/e2e/_docs/README.md +215 -0
  10. package/tests/cypress/e2e/_docs/tutorials/sector7-superadmin-teams.narration.json +155 -0
  11. package/tests/cypress/e2e/_docs/tutorials/sector7-superadmin.cy.ts +390 -0
  12. package/tests/cypress/e2e/_docs/tutorials/teams-system.doc.cy.ts +349 -0
  13. package/tests/cypress/e2e/_docs/tutorials/teams-system.narration.json +165 -0
  14. package/tests/cypress/e2e/_selectors/auth.cy.ts +306 -0
  15. package/tests/cypress/e2e/_selectors/billing.cy.ts +89 -0
  16. package/tests/cypress/e2e/_selectors/dashboard-mobile.cy.ts +113 -0
  17. package/tests/cypress/e2e/_selectors/dashboard-navigation.cy.ts +89 -0
  18. package/tests/cypress/e2e/_selectors/dashboard-sidebar.cy.ts +60 -0
  19. package/tests/cypress/e2e/_selectors/dashboard-topnav.cy.ts +146 -0
  20. package/tests/cypress/e2e/_selectors/devtools.cy.ts +210 -0
  21. package/tests/cypress/e2e/_selectors/global-search.cy.ts +88 -0
  22. package/tests/cypress/e2e/_selectors/pages-editor.cy.ts +179 -0
  23. package/tests/cypress/e2e/_selectors/posts-editor.cy.ts +282 -0
  24. package/tests/cypress/e2e/_selectors/public.cy.ts +112 -0
  25. package/tests/cypress/e2e/_selectors/settings-api-keys.cy.ts +228 -0
  26. package/tests/cypress/e2e/_selectors/settings-billing.cy.ts +105 -0
  27. package/tests/cypress/e2e/_selectors/settings-layout.cy.ts +119 -0
  28. package/tests/cypress/e2e/_selectors/settings-password.cy.ts +71 -0
  29. package/tests/cypress/e2e/_selectors/settings-profile.cy.ts +82 -0
  30. package/tests/cypress/e2e/_selectors/settings-teams.cy.ts +68 -0
  31. package/tests/cypress/e2e/_selectors/superadmin.cy.ts +185 -0
  32. package/tests/cypress/e2e/_selectors/tasks.cy.ts +242 -0
  33. package/tests/cypress/e2e/_selectors/taxonomies.cy.ts +126 -0
  34. package/tests/cypress/e2e/_selectors/teams.cy.ts +142 -0
  35. package/tests/cypress/e2e/_superadmin/all-teams.bdd.md +261 -0
  36. package/tests/cypress/e2e/_superadmin/all-teams.cy.ts +177 -0
  37. package/tests/cypress/e2e/_superadmin/all-users.bdd.md +406 -0
  38. package/tests/cypress/e2e/_superadmin/all-users.cy.ts +294 -0
  39. package/tests/cypress/e2e/_superadmin/dashboard.bdd.md +235 -0
  40. package/tests/cypress/e2e/_superadmin/dashboard.cy.ts +149 -0
  41. package/tests/cypress/e2e/_superadmin/subscriptions-overview.bdd.md +290 -0
  42. package/tests/cypress/e2e/_superadmin/subscriptions-overview.cy.ts +194 -0
  43. package/tests/cypress/e2e/ai/ai-usage.cy.ts +209 -0
  44. package/tests/cypress/e2e/ai/chat-api.cy.ts +107 -0
  45. package/tests/cypress/e2e/ai/guardrails.cy.ts +332 -0
  46. package/tests/cypress/e2e/api/billing/BillingAPIController.js +319 -0
  47. package/tests/cypress/e2e/api/billing/check-action.cy.ts +326 -0
  48. package/tests/cypress/e2e/api/billing/checkout.cy.ts +358 -0
  49. package/tests/cypress/e2e/api/billing/lifecycle.cy.ts +423 -0
  50. package/tests/cypress/e2e/api/billing/plans/README.md +345 -0
  51. package/tests/cypress/e2e/api/billing/plans/business.cy.ts +412 -0
  52. package/tests/cypress/e2e/api/billing/plans/downgrade.cy.ts +510 -0
  53. package/tests/cypress/e2e/api/billing/plans/fixtures/billing-plans.json +163 -0
  54. package/tests/cypress/e2e/api/billing/plans/free.cy.ts +500 -0
  55. package/tests/cypress/e2e/api/billing/plans/pro.cy.ts +497 -0
  56. package/tests/cypress/e2e/api/billing/plans/starter.cy.ts +342 -0
  57. package/tests/cypress/e2e/api/billing/portal.cy.ts +313 -0
  58. package/tests/cypress/e2e/api/devtools/registries.bdd.md +300 -0
  59. package/tests/cypress/e2e/api/devtools/registries.cy.ts +368 -0
  60. package/tests/cypress/e2e/api/entities/blocks-scope.cy.ts +396 -0
  61. package/tests/cypress/e2e/api/entities/customers-crud.cy.ts +648 -0
  62. package/tests/cypress/e2e/api/entities/customers-metas.cy.ts +839 -0
  63. package/tests/cypress/e2e/api/entities/pages-crud.cy.ts +425 -0
  64. package/tests/cypress/e2e/api/entities/pages-status.cy.ts +335 -0
  65. package/tests/cypress/e2e/api/entities/post-categories-crud.cy.ts +610 -0
  66. package/tests/cypress/e2e/api/entities/posts-crud.cy.ts +709 -0
  67. package/tests/cypress/e2e/api/entities/posts-status.cy.ts +396 -0
  68. package/tests/cypress/e2e/api/entities/tasks-crud.cy.ts +602 -0
  69. package/tests/cypress/e2e/api/entities/tasks-metas.cy.ts +878 -0
  70. package/tests/cypress/e2e/api/entities/users-crud.cy.ts +469 -0
  71. package/tests/cypress/e2e/api/entities/users-metas.cy.ts +913 -0
  72. package/tests/cypress/e2e/api/entities/users-security.cy.ts +375 -0
  73. package/tests/cypress/e2e/api/scheduled-actions/cron-endpoint.bdd.md +375 -0
  74. package/tests/cypress/e2e/api/scheduled-actions/cron-endpoint.cy.ts +346 -0
  75. package/tests/cypress/e2e/api/scheduled-actions/devtools-endpoint.bdd.md +451 -0
  76. package/tests/cypress/e2e/api/scheduled-actions/devtools-endpoint.cy.ts +447 -0
  77. package/tests/cypress/e2e/api/scheduled-actions/scheduling.bdd.md +649 -0
  78. package/tests/cypress/e2e/api/scheduled-actions/scheduling.cy.ts +333 -0
  79. package/tests/cypress/e2e/api/settings/api-keys.crud.cy.ts +923 -0
  80. package/tests/cypress/e2e/uat/auth/app-roles/developer-login.bdd.md +231 -0
  81. package/tests/cypress/e2e/uat/auth/app-roles/developer-login.cy.ts +144 -0
  82. package/tests/cypress/e2e/uat/auth/app-roles/superadmin-login.bdd.md +118 -0
  83. package/tests/cypress/e2e/uat/auth/app-roles/superadmin-login.cy.ts +84 -0
  84. package/tests/cypress/e2e/uat/auth/custom-roles/editor-login.bdd.md +288 -0
  85. package/tests/cypress/e2e/uat/auth/custom-roles/editor-login.cy.ts +188 -0
  86. package/tests/cypress/e2e/uat/auth/login-logout.bdd.md +160 -0
  87. package/tests/cypress/e2e/uat/auth/login-logout.cy.ts +116 -0
  88. package/tests/cypress/e2e/uat/auth/password-reset.bdd.md +289 -0
  89. package/tests/cypress/e2e/uat/auth/password-reset.cy.ts +200 -0
  90. package/tests/cypress/e2e/uat/auth/team-roles/admin-login.bdd.md +225 -0
  91. package/tests/cypress/e2e/uat/auth/team-roles/admin-login.cy.ts +148 -0
  92. package/tests/cypress/e2e/uat/auth/team-roles/member-login.bdd.md +251 -0
  93. package/tests/cypress/e2e/uat/auth/team-roles/member-login.cy.ts +163 -0
  94. package/tests/cypress/e2e/uat/auth/team-roles/owner-login.bdd.md +231 -0
  95. package/tests/cypress/e2e/uat/auth/team-roles/owner-login.cy.ts +141 -0
  96. package/tests/cypress/e2e/uat/billing/extended.bdd.md +273 -0
  97. package/tests/cypress/e2e/uat/billing/extended.cy.ts +209 -0
  98. package/tests/cypress/e2e/uat/billing/feature-gates.bdd.md +407 -0
  99. package/tests/cypress/e2e/uat/billing/feature-gates.cy.ts +307 -0
  100. package/tests/cypress/e2e/uat/billing/page.bdd.md +329 -0
  101. package/tests/cypress/e2e/uat/billing/page.cy.ts +250 -0
  102. package/tests/cypress/e2e/uat/billing/status.bdd.md +190 -0
  103. package/tests/cypress/e2e/uat/billing/status.cy.ts +145 -0
  104. package/tests/cypress/e2e/uat/billing/team-switch.bdd.md +156 -0
  105. package/tests/cypress/e2e/uat/billing/team-switch.cy.ts +122 -0
  106. package/tests/cypress/e2e/uat/billing/usage.bdd.md +218 -0
  107. package/tests/cypress/e2e/uat/billing/usage.cy.ts +176 -0
  108. package/tests/cypress/e2e/uat/blocks/hero.bdd.md +124 -0
  109. package/tests/cypress/e2e/uat/blocks/hero.cy.ts +56 -0
  110. package/tests/cypress/e2e/uat/devtools/api-tester.cy.ts +390 -0
  111. package/tests/cypress/e2e/uat/entities/customers/member.bdd.md +275 -0
  112. package/tests/cypress/e2e/uat/entities/customers/member.cy.ts +122 -0
  113. package/tests/cypress/e2e/uat/entities/customers/owner.bdd.md +243 -0
  114. package/tests/cypress/e2e/uat/entities/customers/owner.cy.ts +165 -0
  115. package/tests/cypress/e2e/uat/entities/pages/block-crud.bdd.md +476 -0
  116. package/tests/cypress/e2e/uat/entities/pages/block-crud.cy.ts +486 -0
  117. package/tests/cypress/e2e/uat/entities/pages/block-editor.bdd.md +460 -0
  118. package/tests/cypress/e2e/uat/entities/pages/block-editor.cy.ts +301 -0
  119. package/tests/cypress/e2e/uat/entities/pages/list.bdd.md +432 -0
  120. package/tests/cypress/e2e/uat/entities/pages/list.cy.ts +273 -0
  121. package/tests/cypress/e2e/uat/entities/pages/public-rendering.bdd.md +696 -0
  122. package/tests/cypress/e2e/uat/entities/pages/public-rendering.cy.ts +340 -0
  123. package/tests/cypress/e2e/uat/entities/posts/categories-api-aware.bdd.md +161 -0
  124. package/tests/cypress/e2e/uat/entities/posts/categories-api-aware.cy.ts +104 -0
  125. package/tests/cypress/e2e/uat/entities/posts/categories.bdd.md +375 -0
  126. package/tests/cypress/e2e/uat/entities/posts/categories.cy.ts +241 -0
  127. package/tests/cypress/e2e/uat/entities/posts/editor.bdd.md +429 -0
  128. package/tests/cypress/e2e/uat/entities/posts/editor.cy.ts +257 -0
  129. package/tests/cypress/e2e/uat/entities/posts/list.bdd.md +340 -0
  130. package/tests/cypress/e2e/uat/entities/posts/list.cy.ts +177 -0
  131. package/tests/cypress/e2e/uat/entities/posts/public.bdd.md +614 -0
  132. package/tests/cypress/e2e/uat/entities/posts/public.cy.ts +249 -0
  133. package/tests/cypress/e2e/uat/entities/tasks/member.bdd.md +222 -0
  134. package/tests/cypress/e2e/uat/entities/tasks/member.cy.ts +165 -0
  135. package/tests/cypress/e2e/uat/entities/tasks/owner.bdd.md +419 -0
  136. package/tests/cypress/e2e/uat/entities/tasks/owner.cy.ts +191 -0
  137. package/tests/cypress/e2e/uat/roles/editor-role.bdd.md +552 -0
  138. package/tests/cypress/e2e/uat/roles/editor-role.cy.ts +210 -0
  139. package/tests/cypress/e2e/uat/roles/member-restrictions.bdd.md +450 -0
  140. package/tests/cypress/e2e/uat/roles/member-restrictions.cy.ts +189 -0
  141. package/tests/cypress/e2e/uat/roles/owner-full-crud.bdd.md +530 -0
  142. package/tests/cypress/e2e/uat/roles/owner-full-crud.cy.ts +247 -0
  143. package/tests/cypress/e2e/uat/scheduled-actions/devtools-ui.bdd.md +736 -0
  144. package/tests/cypress/e2e/uat/scheduled-actions/devtools-ui.cy.ts +740 -0
  145. package/tests/cypress/e2e/uat/teams/roles-matrix.bdd.md +553 -0
  146. package/tests/cypress/e2e/uat/teams/roles-matrix.cy.ts +185 -0
  147. package/tests/cypress/e2e/uat/teams/switcher.bdd.md +1151 -0
  148. package/tests/cypress/e2e/uat/teams/switcher.cy.ts +497 -0
  149. package/tests/cypress/e2e/uat/teams/team-switcher.md +198 -0
  150. package/tests/cypress/fixtures/blocks.json +218 -0
  151. package/tests/cypress/fixtures/entities.json +78 -0
  152. package/tests/cypress/fixtures/page-builder.json +21 -0
  153. package/tests/cypress/src/components/CategoriesPOM.ts +382 -0
  154. package/tests/cypress/src/components/CustomersPOM.ts +439 -0
  155. package/tests/cypress/src/components/DevKeyringPOM.ts +160 -0
  156. package/tests/cypress/src/components/EntityForm.ts +375 -0
  157. package/tests/cypress/src/components/EntityList.ts +389 -0
  158. package/tests/cypress/src/components/PageBuilderPOM.ts +710 -0
  159. package/tests/cypress/src/components/PostEditorPOM.ts +370 -0
  160. package/tests/cypress/src/components/PostsListPOM.ts +223 -0
  161. package/tests/cypress/src/components/PublicPagePOM.ts +447 -0
  162. package/tests/cypress/src/components/PublicPostPOM.ts +146 -0
  163. package/tests/cypress/src/components/TasksPOM.ts +272 -0
  164. package/tests/cypress/src/components/TeamSwitcherPOM.ts +450 -0
  165. package/tests/cypress/src/components/index.ts +21 -0
  166. package/tests/cypress/src/controllers/ApiKeysAPIController.js +178 -0
  167. package/tests/cypress/src/controllers/BaseAPIController.js +317 -0
  168. package/tests/cypress/src/controllers/CustomerAPIController.js +251 -0
  169. package/tests/cypress/src/controllers/PagesAPIController.js +226 -0
  170. package/tests/cypress/src/controllers/PostsAPIController.js +250 -0
  171. package/tests/cypress/src/controllers/TaskAPIController.js +240 -0
  172. package/tests/cypress/src/controllers/UsersAPIController.js +242 -0
  173. package/tests/cypress/src/controllers/index.js +25 -0
  174. package/tests/cypress/src/core/AuthPOM.ts +450 -0
  175. package/tests/cypress/src/core/BasePOM.ts +86 -0
  176. package/tests/cypress/src/core/BlockEditorBasePOM.ts +576 -0
  177. package/tests/cypress/src/core/DashboardEntityPOM.ts +692 -0
  178. package/tests/cypress/src/core/index.ts +14 -0
  179. package/tests/cypress/src/entities/CustomersPOM.ts +172 -0
  180. package/tests/cypress/src/entities/PagesPOM.ts +137 -0
  181. package/tests/cypress/src/entities/PostsPOM.ts +137 -0
  182. package/tests/cypress/src/entities/TasksPOM.ts +176 -0
  183. package/tests/cypress/src/entities/index.ts +14 -0
  184. package/tests/cypress/src/features/BillingPOM.ts +385 -0
  185. package/tests/cypress/src/features/DashboardPOM.ts +245 -0
  186. package/tests/cypress/src/features/DevtoolsPOM.ts +739 -0
  187. package/tests/cypress/src/features/PageBuilderPOM.ts +263 -0
  188. package/tests/cypress/src/features/PostEditorPOM.ts +313 -0
  189. package/tests/cypress/src/features/ScheduledActionsPOM.ts +463 -0
  190. package/tests/cypress/src/features/SettingsPOM.ts +362 -0
  191. package/tests/cypress/src/features/SuperadminPOM.ts +331 -0
  192. package/tests/cypress/src/features/SuperadminTeamRolesPOM.ts +285 -0
  193. package/tests/cypress/src/features/index.ts +28 -0
  194. package/tests/cypress/src/helpers/ApiInterceptor.ts +177 -0
  195. package/tests/cypress/src/index.ts +101 -0
  196. package/tests/cypress/src/pages/dashboard/Dashboard.js +677 -0
  197. package/tests/cypress/src/pages/dashboard/DashboardPage.js +43 -0
  198. package/tests/cypress/src/pages/dashboard/DashboardStats.js +546 -0
  199. package/tests/cypress/src/pages/dashboard/index.js +6 -0
  200. package/tests/cypress/src/pages/index.js +5 -0
  201. package/tests/cypress/src/pages/public/FeaturesPage.js +28 -0
  202. package/tests/cypress/src/pages/public/LandingPage.js +69 -0
  203. package/tests/cypress/src/pages/public/PricingPage.js +33 -0
  204. package/tests/cypress/src/pages/public/index.js +6 -0
  205. package/tests/cypress/src/selectors.ts +46 -0
  206. package/tests/cypress/src/session-helpers.ts +500 -0
  207. package/tests/cypress/support/doc-commands.ts +260 -0
  208. package/tests/cypress.config.ts +150 -0
  209. package/tests/jest/components/post-header.test.tsx +377 -0
  210. package/tests/jest/config/role-config.test.ts +529 -0
  211. package/tests/jest/jest.config.ts +81 -0
  212. package/tests/jest/langchain/COVERAGE.md +372 -0
  213. package/tests/jest/langchain/guardrails.test.ts +465 -0
  214. package/tests/jest/langchain/streaming.test.ts +367 -0
  215. package/tests/jest/langchain/token-tracker.test.ts +455 -0
  216. package/tests/jest/langchain/tracer-callbacks.test.ts +881 -0
  217. package/tests/jest/langchain/tracer.test.ts +823 -0
  218. package/tests/jest/user-roles/role-helpers.test.ts +432 -0
  219. package/tests/jest/validation/categories.test.ts +429 -0
  220. package/tests/jest/validation/posts.test.ts +546 -0
  221. package/tests/tsconfig.json +15 -0
  222. package/LICENSE +0 -21
@@ -0,0 +1,455 @@
1
+ /**
2
+ * Unit Tests - Token Tracker Service
3
+ *
4
+ * Tests token usage tracking and cost calculation:
5
+ * - Cost calculation for different models
6
+ * - Usage tracking with database mocking
7
+ * - Usage statistics retrieval
8
+ * - Daily usage aggregation
9
+ *
10
+ * Focus: Business logic with mocked database calls.
11
+ */
12
+
13
+ import { tokenTracker } from '@/plugins/langchain/lib/token-tracker'
14
+
15
+ // Mock database functions
16
+ jest.mock('@nextsparkjs/core/lib/db', () => ({
17
+ mutateWithRLS: jest.fn(),
18
+ queryWithRLS: jest.fn(),
19
+ }))
20
+
21
+ import { mutateWithRLS, queryWithRLS } from '@nextsparkjs/core/lib/db'
22
+
23
+ const mockMutate = mutateWithRLS as jest.MockedFunction<typeof mutateWithRLS>
24
+ const mockQuery = queryWithRLS as jest.MockedFunction<typeof queryWithRLS>
25
+
26
+ describe('Token Tracker Service', () => {
27
+ beforeEach(() => {
28
+ jest.clearAllMocks()
29
+ })
30
+
31
+ describe('calculateCost', () => {
32
+ describe('known models', () => {
33
+ it('should calculate cost for gpt-4o', () => {
34
+ const usage = { inputTokens: 1000, outputTokens: 500, totalTokens: 1500 }
35
+ const result = tokenTracker.calculateCost('gpt-4o', usage)
36
+
37
+ // gpt-4o: input $5/1M, output $15/1M
38
+ expect(result.inputCost).toBeCloseTo(0.005, 6) // 1000/1M * 5
39
+ expect(result.outputCost).toBeCloseTo(0.0075, 6) // 500/1M * 15
40
+ expect(result.totalCost).toBeCloseTo(0.0125, 6)
41
+ })
42
+
43
+ it('should calculate cost for gpt-4o-mini', () => {
44
+ const usage = { inputTokens: 10000, outputTokens: 5000, totalTokens: 15000 }
45
+ const result = tokenTracker.calculateCost('gpt-4o-mini', usage)
46
+
47
+ // gpt-4o-mini: input $0.15/1M, output $0.60/1M
48
+ expect(result.inputCost).toBeCloseTo(0.0015, 6) // 10000/1M * 0.15
49
+ expect(result.outputCost).toBeCloseTo(0.003, 6) // 5000/1M * 0.60
50
+ expect(result.totalCost).toBeCloseTo(0.0045, 6)
51
+ })
52
+
53
+ it('should calculate cost for gpt-4-turbo', () => {
54
+ const usage = { inputTokens: 1000, outputTokens: 1000, totalTokens: 2000 }
55
+ const result = tokenTracker.calculateCost('gpt-4-turbo', usage)
56
+
57
+ // gpt-4-turbo: input $10/1M, output $30/1M
58
+ expect(result.inputCost).toBeCloseTo(0.01, 6)
59
+ expect(result.outputCost).toBeCloseTo(0.03, 6)
60
+ expect(result.totalCost).toBeCloseTo(0.04, 6)
61
+ })
62
+
63
+ it('should calculate cost for gpt-3.5-turbo', () => {
64
+ const usage = { inputTokens: 1000000, outputTokens: 500000, totalTokens: 1500000 }
65
+ const result = tokenTracker.calculateCost('gpt-3.5-turbo', usage)
66
+
67
+ // gpt-3.5-turbo: input $0.50/1M, output $1.50/1M
68
+ expect(result.inputCost).toBeCloseTo(0.5, 6)
69
+ expect(result.outputCost).toBeCloseTo(0.75, 6)
70
+ expect(result.totalCost).toBeCloseTo(1.25, 6)
71
+ })
72
+
73
+ it('should calculate cost for claude-3-5-sonnet', () => {
74
+ const usage = { inputTokens: 1000, outputTokens: 1000, totalTokens: 2000 }
75
+ const result = tokenTracker.calculateCost('claude-3-5-sonnet', usage)
76
+
77
+ // claude-3-5-sonnet: input $3/1M, output $15/1M
78
+ expect(result.inputCost).toBeCloseTo(0.003, 6)
79
+ expect(result.outputCost).toBeCloseTo(0.015, 6)
80
+ expect(result.totalCost).toBeCloseTo(0.018, 6)
81
+ })
82
+
83
+ it('should calculate cost for claude-3-opus', () => {
84
+ const usage = { inputTokens: 1000, outputTokens: 1000, totalTokens: 2000 }
85
+ const result = tokenTracker.calculateCost('claude-3-opus', usage)
86
+
87
+ // claude-3-opus: input $15/1M, output $75/1M
88
+ expect(result.inputCost).toBeCloseTo(0.015, 6)
89
+ expect(result.outputCost).toBeCloseTo(0.075, 6)
90
+ expect(result.totalCost).toBeCloseTo(0.09, 6)
91
+ })
92
+
93
+ it('should calculate cost for claude-3-haiku', () => {
94
+ const usage = { inputTokens: 10000, outputTokens: 10000, totalTokens: 20000 }
95
+ const result = tokenTracker.calculateCost('claude-3-haiku', usage)
96
+
97
+ // claude-3-haiku: input $0.25/1M, output $1.25/1M
98
+ expect(result.inputCost).toBeCloseTo(0.0025, 6)
99
+ expect(result.outputCost).toBeCloseTo(0.0125, 6)
100
+ expect(result.totalCost).toBeCloseTo(0.015, 6)
101
+ })
102
+ })
103
+
104
+ describe('ollama models (free)', () => {
105
+ it('should return zero cost for ollama models', () => {
106
+ const usage = { inputTokens: 100000, outputTokens: 50000, totalTokens: 150000 }
107
+ const result = tokenTracker.calculateCost('ollama/llama3.2:3b', usage)
108
+
109
+ expect(result.inputCost).toBe(0)
110
+ expect(result.outputCost).toBe(0)
111
+ expect(result.totalCost).toBe(0)
112
+ })
113
+
114
+ it('should match ollama wildcard pattern', () => {
115
+ const models = ['ollama/mistral', 'ollama/codellama', 'ollama/phi3']
116
+ const usage = { inputTokens: 1000, outputTokens: 1000, totalTokens: 2000 }
117
+
118
+ models.forEach(model => {
119
+ const result = tokenTracker.calculateCost(model, usage)
120
+ expect(result.totalCost).toBe(0)
121
+ })
122
+ })
123
+ })
124
+
125
+ describe('unknown models', () => {
126
+ it('should return zero cost for unknown models', () => {
127
+ const usage = { inputTokens: 1000, outputTokens: 1000, totalTokens: 2000 }
128
+ const result = tokenTracker.calculateCost('unknown-model', usage)
129
+
130
+ expect(result.inputCost).toBe(0)
131
+ expect(result.outputCost).toBe(0)
132
+ expect(result.totalCost).toBe(0)
133
+ })
134
+ })
135
+
136
+ describe('edge cases', () => {
137
+ it('should handle zero tokens', () => {
138
+ const usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
139
+ const result = tokenTracker.calculateCost('gpt-4o', usage)
140
+
141
+ expect(result.inputCost).toBe(0)
142
+ expect(result.outputCost).toBe(0)
143
+ expect(result.totalCost).toBe(0)
144
+ })
145
+
146
+ it('should handle very large token counts', () => {
147
+ const usage = { inputTokens: 10000000, outputTokens: 5000000, totalTokens: 15000000 }
148
+ const result = tokenTracker.calculateCost('gpt-4o', usage)
149
+
150
+ // gpt-4o: input $5/1M, output $15/1M
151
+ expect(result.inputCost).toBeCloseTo(50, 2) // 10M/1M * 5
152
+ expect(result.outputCost).toBeCloseTo(75, 2) // 5M/1M * 15
153
+ expect(result.totalCost).toBeCloseTo(125, 2)
154
+ })
155
+
156
+ it('should use custom pricing when provided', () => {
157
+ const usage = { inputTokens: 1000000, outputTokens: 1000000, totalTokens: 2000000 }
158
+ const customPricing = {
159
+ 'custom-model': { input: 1.0, output: 2.0 },
160
+ }
161
+
162
+ const result = tokenTracker.calculateCost('custom-model', usage, customPricing)
163
+
164
+ expect(result.inputCost).toBeCloseTo(1.0, 6)
165
+ expect(result.outputCost).toBeCloseTo(2.0, 6)
166
+ expect(result.totalCost).toBeCloseTo(3.0, 6)
167
+ })
168
+ })
169
+ })
170
+
171
+ describe('trackUsage', () => {
172
+ const context = { userId: 'user-123', teamId: 'team-456' }
173
+
174
+ it('should call mutateWithRLS with correct parameters', async () => {
175
+ mockMutate.mockResolvedValue(undefined)
176
+
177
+ await tokenTracker.trackUsage({
178
+ context,
179
+ sessionId: 'session-789',
180
+ provider: 'openai',
181
+ model: 'gpt-4o',
182
+ usage: { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
183
+ agentName: 'test-agent',
184
+ })
185
+
186
+ expect(mockMutate).toHaveBeenCalledTimes(1)
187
+ expect(mockMutate).toHaveBeenCalledWith(
188
+ expect.stringContaining('INSERT INTO'),
189
+ expect.arrayContaining(['user-123', 'team-456', 'session-789', 'openai', 'gpt-4o']),
190
+ 'user-123'
191
+ )
192
+ })
193
+
194
+ it('should pass null for optional sessionId', async () => {
195
+ mockMutate.mockResolvedValue(undefined)
196
+
197
+ await tokenTracker.trackUsage({
198
+ context,
199
+ provider: 'anthropic',
200
+ model: 'claude-3-5-sonnet',
201
+ usage: { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
202
+ })
203
+
204
+ expect(mockMutate).toHaveBeenCalledWith(
205
+ expect.any(String),
206
+ expect.arrayContaining([null]), // sessionId is null
207
+ 'user-123'
208
+ )
209
+ })
210
+
211
+ it('should handle metadata serialization', async () => {
212
+ mockMutate.mockResolvedValue(undefined)
213
+
214
+ await tokenTracker.trackUsage({
215
+ context,
216
+ provider: 'openai',
217
+ model: 'gpt-4o',
218
+ usage: { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
219
+ metadata: { toolsUsed: ['search', 'calculator'], executionTime: 1500 },
220
+ })
221
+
222
+ expect(mockMutate).toHaveBeenCalledWith(
223
+ expect.any(String),
224
+ expect.arrayContaining([
225
+ expect.stringContaining('toolsUsed'),
226
+ ]),
227
+ 'user-123'
228
+ )
229
+ })
230
+ })
231
+
232
+ describe('getUsage', () => {
233
+ const context = { userId: 'user-123', teamId: 'team-456' }
234
+
235
+ it('should return empty stats when no data exists', async () => {
236
+ mockQuery.mockResolvedValue([])
237
+
238
+ const result = await tokenTracker.getUsage(context, '30d')
239
+
240
+ expect(result.totalTokens).toBe(0)
241
+ expect(result.totalCost).toBe(0)
242
+ expect(result.inputTokens).toBe(0)
243
+ expect(result.outputTokens).toBe(0)
244
+ expect(result.requestCount).toBe(0)
245
+ expect(result.byModel).toEqual({})
246
+ })
247
+
248
+ it('should aggregate usage by model', async () => {
249
+ mockQuery.mockResolvedValue([
250
+ {
251
+ model: 'gpt-4o',
252
+ totalTokens: '1000',
253
+ totalCost: '0.05',
254
+ inputTokens: '600',
255
+ outputTokens: '400',
256
+ requestCount: '5',
257
+ modelTokens: '1000',
258
+ modelCost: '0.05',
259
+ },
260
+ {
261
+ model: 'claude-3-5-sonnet',
262
+ totalTokens: '2000',
263
+ totalCost: '0.03',
264
+ inputTokens: '1200',
265
+ outputTokens: '800',
266
+ requestCount: '10',
267
+ modelTokens: '2000',
268
+ modelCost: '0.03',
269
+ },
270
+ ])
271
+
272
+ const result = await tokenTracker.getUsage(context, '30d')
273
+
274
+ expect(result.totalTokens).toBe(3000)
275
+ expect(result.totalCost).toBeCloseTo(0.08, 2)
276
+ expect(result.requestCount).toBe(15)
277
+ expect(result.byModel['gpt-4o'].tokens).toBe(1000)
278
+ expect(result.byModel['claude-3-5-sonnet'].tokens).toBe(2000)
279
+ })
280
+
281
+ it('should apply correct period filter', async () => {
282
+ mockQuery.mockResolvedValue([])
283
+
284
+ await tokenTracker.getUsage(context, 'today')
285
+
286
+ expect(mockQuery).toHaveBeenCalledWith(
287
+ expect.stringContaining('CURRENT_DATE'),
288
+ expect.any(Array),
289
+ 'user-123'
290
+ )
291
+ })
292
+
293
+ it('should apply 7d period filter', async () => {
294
+ mockQuery.mockResolvedValue([])
295
+
296
+ await tokenTracker.getUsage(context, '7d')
297
+
298
+ expect(mockQuery).toHaveBeenCalledWith(
299
+ expect.stringContaining("7 days"),
300
+ expect.any(Array),
301
+ 'user-123'
302
+ )
303
+ })
304
+
305
+ it('should not apply filter for "all" period', async () => {
306
+ mockQuery.mockResolvedValue([])
307
+
308
+ await tokenTracker.getUsage(context, 'all')
309
+
310
+ // Should not contain any date filter
311
+ const query = mockQuery.mock.calls[0][0] as string
312
+ expect(query).not.toContain('CURRENT_DATE')
313
+ expect(query).not.toContain('interval')
314
+ })
315
+ })
316
+
317
+ describe('getDailyUsage', () => {
318
+ const context = { userId: 'user-123', teamId: 'team-456' }
319
+
320
+ it('should return daily aggregated usage', async () => {
321
+ mockQuery.mockResolvedValue([
322
+ { date: '2024-01-15', tokens: '1000', cost: '0.05', requests: '10' },
323
+ { date: '2024-01-14', tokens: '800', cost: '0.04', requests: '8' },
324
+ { date: '2024-01-13', tokens: '1200', cost: '0.06', requests: '12' },
325
+ ])
326
+
327
+ const result = await tokenTracker.getDailyUsage(context, 7)
328
+
329
+ expect(result).toHaveLength(3)
330
+ expect(result[0].date).toBe('2024-01-15')
331
+ expect(result[0].tokens).toBe(1000)
332
+ expect(result[0].cost).toBeCloseTo(0.05, 2)
333
+ expect(result[0].requests).toBe(10)
334
+ })
335
+
336
+ it('should return empty array when no data', async () => {
337
+ mockQuery.mockResolvedValue([])
338
+
339
+ const result = await tokenTracker.getDailyUsage(context, 30)
340
+
341
+ expect(result).toEqual([])
342
+ })
343
+
344
+ it('should use parameterized query for days', async () => {
345
+ mockQuery.mockResolvedValue([])
346
+
347
+ await tokenTracker.getDailyUsage(context, 14)
348
+
349
+ // Verify the days parameter is passed safely
350
+ expect(mockQuery).toHaveBeenCalledWith(
351
+ expect.stringContaining('$3'),
352
+ expect.arrayContaining(['14']),
353
+ 'user-123'
354
+ )
355
+ })
356
+
357
+ it('should validate days parameter bounds', async () => {
358
+ mockQuery.mockResolvedValue([])
359
+
360
+ // Should clamp negative values to 1
361
+ await tokenTracker.getDailyUsage(context, -5)
362
+ expect(mockQuery).toHaveBeenCalledWith(
363
+ expect.any(String),
364
+ expect.arrayContaining(['1']),
365
+ 'user-123'
366
+ )
367
+ })
368
+
369
+ it('should cap days at 365', async () => {
370
+ mockQuery.mockResolvedValue([])
371
+
372
+ await tokenTracker.getDailyUsage(context, 1000)
373
+ expect(mockQuery).toHaveBeenCalledWith(
374
+ expect.any(String),
375
+ expect.arrayContaining(['365']),
376
+ 'user-123'
377
+ )
378
+ })
379
+ })
380
+
381
+ describe('getTeamUsage', () => {
382
+ it('should aggregate usage by user', async () => {
383
+ mockQuery.mockResolvedValue([
384
+ {
385
+ userId: 'user-1',
386
+ totalTokens: '5000',
387
+ totalCost: '0.25',
388
+ inputTokens: '3000',
389
+ outputTokens: '2000',
390
+ requestCount: '50',
391
+ },
392
+ {
393
+ userId: 'user-2',
394
+ totalTokens: '3000',
395
+ totalCost: '0.15',
396
+ inputTokens: '1800',
397
+ outputTokens: '1200',
398
+ requestCount: '30',
399
+ },
400
+ ])
401
+
402
+ const result = await tokenTracker.getTeamUsage('team-456', '30d')
403
+
404
+ expect(result.totalTokens).toBe(8000)
405
+ expect(result.totalCost).toBeCloseTo(0.4, 2)
406
+ expect(result.requestCount).toBe(80)
407
+ expect(result.byUser['user-1'].tokens).toBe(5000)
408
+ expect(result.byUser['user-2'].tokens).toBe(3000)
409
+ expect(result.byModel).toEqual({}) // Not grouped by model for team view
410
+ })
411
+
412
+ it('should return empty stats when no team data', async () => {
413
+ mockQuery.mockResolvedValue([])
414
+
415
+ const result = await tokenTracker.getTeamUsage('team-456', '30d')
416
+
417
+ expect(result.totalTokens).toBe(0)
418
+ expect(result.byUser).toEqual({})
419
+ })
420
+
421
+ it('should use admin context for RLS', async () => {
422
+ mockQuery.mockResolvedValue([])
423
+
424
+ await tokenTracker.getTeamUsage('team-456', '7d')
425
+
426
+ expect(mockQuery).toHaveBeenCalledWith(
427
+ expect.any(String),
428
+ expect.arrayContaining(['team-456']),
429
+ 'admin' // Uses admin context
430
+ )
431
+ })
432
+ })
433
+
434
+ describe('getPeriodClause', () => {
435
+ it('should return correct clause for today', () => {
436
+ const clause = tokenTracker.getPeriodClause('today')
437
+ expect(clause).toContain('CURRENT_DATE')
438
+ })
439
+
440
+ it('should return correct clause for 7d', () => {
441
+ const clause = tokenTracker.getPeriodClause('7d')
442
+ expect(clause).toContain('7 days')
443
+ })
444
+
445
+ it('should return correct clause for 30d', () => {
446
+ const clause = tokenTracker.getPeriodClause('30d')
447
+ expect(clause).toContain('30 days')
448
+ })
449
+
450
+ it('should return empty string for all', () => {
451
+ const clause = tokenTracker.getPeriodClause('all')
452
+ expect(clause).toBe('')
453
+ })
454
+ })
455
+ })