@donotdev/functions 0.0.19 → 0.1.0

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 (292) hide show
  1. package/LICENSE.md +3 -3
  2. package/lib/firebase/auth/getCustomClaims.d.ts +25 -0
  3. package/lib/firebase/auth/getUserAuthStatus.d.ts +29 -0
  4. package/lib/firebase/auth/index.d.ts +12 -0
  5. package/lib/firebase/auth/removeCustomClaims.d.ts +28 -0
  6. package/lib/firebase/auth/setCustomClaims.d.ts +34 -0
  7. package/lib/firebase/baseFunction.d.ts +26 -0
  8. package/lib/firebase/billing/cancelSubscription.d.ts +10 -0
  9. package/lib/firebase/billing/changePlan.d.ts +13 -0
  10. package/lib/firebase/billing/createCheckoutSession.d.ts +21 -0
  11. package/lib/firebase/billing/createCustomerPortal.d.ts +19 -0
  12. package/lib/firebase/billing/healthCheck.d.ts +5 -0
  13. package/lib/firebase/billing/index.d.ts +18 -0
  14. package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +19 -0
  15. package/lib/firebase/billing/webhookHandler.d.ts +6 -0
  16. package/lib/firebase/config/constants.d.ts +89 -0
  17. package/lib/firebase/config/index.d.ts +9 -0
  18. package/lib/firebase/config/secrets.d.ts +15 -0
  19. package/lib/firebase/crud/aggregate.d.ts +98 -0
  20. package/lib/firebase/crud/create.d.ts +16 -0
  21. package/lib/firebase/crud/delete.d.ts +20 -0
  22. package/lib/firebase/crud/get.d.ts +24 -0
  23. package/lib/firebase/crud/index.d.ts +14 -0
  24. package/lib/firebase/crud/list.d.ts +25 -0
  25. package/lib/firebase/crud/update.d.ts +25 -0
  26. package/lib/firebase/helpers/githubAccessHelper.d.ts +30 -0
  27. package/lib/firebase/helpers/githubTeamAccessHelper.d.ts +36 -0
  28. package/lib/firebase/helpers/index.d.ts +10 -0
  29. package/lib/firebase/index.d.ts +17 -0
  30. package/lib/firebase/index.js +25 -9110
  31. package/lib/firebase/index.js.map +4 -4
  32. package/lib/firebase/oauth/disconnect.d.ts +12 -0
  33. package/lib/firebase/oauth/exchangeToken.d.ts +9 -0
  34. package/lib/firebase/oauth/getConnections.d.ts +11 -0
  35. package/lib/firebase/oauth/githubAccess.d.ts +83 -0
  36. package/lib/firebase/oauth/index.d.ts +13 -0
  37. package/lib/firebase/oauth/refreshToken.d.ts +9 -0
  38. package/lib/firebase/registerCrudFunctions.d.ts +32 -0
  39. package/lib/firebase/scheduled/checkExpiredSubscriptions.d.ts +14 -0
  40. package/lib/firebase/scheduled/index.d.ts +9 -0
  41. package/lib/shared/billing/createCheckout.d.ts +50 -0
  42. package/lib/shared/billing/helpers/index.d.ts +10 -0
  43. package/lib/shared/billing/helpers/subscriptionManagement.d.ts +34 -0
  44. package/lib/shared/billing/helpers/updateUserSubscription.d.ts +45 -0
  45. package/lib/shared/billing/idempotency.d.ts +22 -0
  46. package/lib/shared/billing/index.d.ts +11 -0
  47. package/lib/shared/billing/webhookHandler.d.ts +37 -0
  48. package/lib/shared/errorHandling.d.ts +14 -0
  49. package/lib/shared/firebase.d.ts +73 -0
  50. package/lib/shared/github.d.ts +128 -0
  51. package/lib/shared/index.d.ts +16 -0
  52. package/lib/shared/index.js +25 -6927
  53. package/lib/shared/index.js.map +4 -4
  54. package/lib/shared/logger.d.ts +133 -0
  55. package/lib/shared/metadata.d.ts +36 -0
  56. package/lib/shared/oauth/exchangeToken.d.ts +24 -0
  57. package/lib/shared/oauth/grantAccess.d.ts +30 -0
  58. package/lib/shared/oauth/index.d.ts +10 -0
  59. package/lib/shared/schema.d.ts +56 -0
  60. package/lib/shared/utils/detectFirestore.d.ts +31 -0
  61. package/lib/shared/utils/external/date.d.ts +36 -0
  62. package/lib/shared/utils/external/index.d.ts +11 -0
  63. package/lib/shared/utils/external/references.d.ts +12 -0
  64. package/lib/shared/utils/external/subscription.d.ts +41 -0
  65. package/lib/shared/utils/firebaseHelpers.d.ts +23 -0
  66. package/lib/shared/utils/functionWrapper.d.ts +36 -0
  67. package/lib/shared/utils/index.d.ts +14 -0
  68. package/lib/shared/utils/internal/auth.d.ts +57 -0
  69. package/lib/shared/utils/internal/firebase.d.ts +5 -0
  70. package/lib/shared/utils/internal/idempotency.d.ts +21 -0
  71. package/lib/shared/utils/internal/index.d.ts +14 -0
  72. package/lib/shared/utils/internal/monitoring.d.ts +80 -0
  73. package/lib/shared/utils/internal/rateLimiter.d.ts +82 -0
  74. package/lib/shared/utils/internal/validation.d.ts +43 -0
  75. package/lib/shared/utils/schemaValidation.d.ts +60 -0
  76. package/lib/shared/utils/validation.d.ts +98 -0
  77. package/lib/shared/utils/vercelHelpers.d.ts +18 -0
  78. package/lib/shared/utils.d.ts +234 -0
  79. package/lib/shared/validation.d.ts +16 -0
  80. package/lib/supabase/auth/deleteAccount.d.ts +16 -0
  81. package/lib/supabase/auth/getCustomClaims.d.ts +16 -0
  82. package/lib/supabase/auth/getUserAuthStatus.d.ts +18 -0
  83. package/lib/supabase/auth/removeCustomClaims.d.ts +16 -0
  84. package/lib/supabase/auth/setCustomClaims.d.ts +18 -0
  85. package/lib/supabase/baseFunction.d.ts +43 -0
  86. package/lib/supabase/billing/cancelSubscription.d.ts +9 -0
  87. package/lib/supabase/billing/changePlan.d.ts +11 -0
  88. package/lib/supabase/billing/createCheckoutSession.d.ts +11 -0
  89. package/lib/supabase/billing/createCustomerPortal.d.ts +9 -0
  90. package/lib/supabase/billing/refreshSubscriptionStatus.d.ts +9 -0
  91. package/lib/supabase/crud/aggregate.d.ts +27 -0
  92. package/lib/supabase/crud/create.d.ts +23 -0
  93. package/lib/supabase/crud/delete.d.ts +30 -0
  94. package/lib/supabase/crud/get.d.ts +23 -0
  95. package/lib/supabase/crud/index.d.ts +20 -0
  96. package/lib/supabase/crud/list.d.ts +33 -0
  97. package/lib/supabase/crud/update.d.ts +24 -0
  98. package/lib/supabase/helpers/authProvider.d.ts +22 -0
  99. package/lib/supabase/index.d.ts +30 -0
  100. package/lib/supabase/index.js +23 -0
  101. package/lib/supabase/index.js.map +7 -0
  102. package/lib/supabase/oauth/disconnect.d.ts +13 -0
  103. package/lib/supabase/oauth/exchangeToken.d.ts +13 -0
  104. package/lib/supabase/oauth/getConnections.d.ts +13 -0
  105. package/lib/supabase/oauth/index.d.ts +12 -0
  106. package/lib/supabase/oauth/refreshToken.d.ts +13 -0
  107. package/lib/supabase/registerCrudFunctions.d.ts +24 -0
  108. package/lib/supabase/utils/idempotency.d.ts +36 -0
  109. package/lib/supabase/utils/monitoring.d.ts +59 -0
  110. package/lib/supabase/utils/rateLimiter.d.ts +56 -0
  111. package/lib/vercel/api/auth/get-custom-claims.d.ts +2 -0
  112. package/lib/vercel/api/auth/get-user-auth-status.d.ts +2 -0
  113. package/lib/vercel/api/auth/index.d.ts +12 -0
  114. package/lib/vercel/api/auth/remove-custom-claims.d.ts +2 -0
  115. package/lib/vercel/api/auth/set-custom-claims.d.ts +10 -0
  116. package/lib/vercel/api/billing/cancel.d.ts +10 -0
  117. package/lib/vercel/api/billing/change-plan.d.ts +11 -0
  118. package/lib/vercel/api/billing/create-checkout-session.d.ts +13 -0
  119. package/lib/vercel/api/billing/customer-portal.d.ts +3 -0
  120. package/lib/vercel/api/billing/index.d.ts +14 -0
  121. package/lib/vercel/api/billing/refresh-subscription-status.d.ts +31 -0
  122. package/lib/vercel/api/billing/webhook-handler.d.ts +22 -0
  123. package/lib/vercel/api/config/constants.d.ts +103 -0
  124. package/lib/vercel/api/config/firebase-admin.d.ts +19 -0
  125. package/lib/vercel/api/config/index.d.ts +10 -0
  126. package/lib/vercel/api/crud/create.d.ts +2 -0
  127. package/lib/vercel/api/crud/delete.d.ts +2 -0
  128. package/lib/vercel/api/crud/get.d.ts +2 -0
  129. package/lib/vercel/api/crud/index.d.ts +13 -0
  130. package/lib/vercel/api/crud/list.d.ts +2 -0
  131. package/lib/vercel/api/crud/update.d.ts +2 -0
  132. package/lib/vercel/api/index.d.ts +13 -0
  133. package/lib/vercel/api/index.js +22 -4790
  134. package/lib/vercel/api/index.js.map +4 -4
  135. package/lib/vercel/api/oauth/check-github-access.d.ts +12 -0
  136. package/lib/vercel/api/oauth/exchange-token.d.ts +8 -0
  137. package/lib/vercel/api/oauth/grant-github-access.d.ts +12 -0
  138. package/lib/vercel/api/oauth/index.d.ts +12 -0
  139. package/lib/vercel/api/oauth/revoke-github-access.d.ts +11 -0
  140. package/lib/vercel/api/utils/cors.d.ts +49 -0
  141. package/lib/vercel/baseFunction.d.ts +25 -0
  142. package/package.json +4 -4
  143. package/src/firebase/auth/getCustomClaims.ts +3 -3
  144. package/src/firebase/auth/getUserAuthStatus.ts +3 -3
  145. package/src/firebase/auth/index.ts +1 -1
  146. package/src/firebase/auth/removeCustomClaims.ts +3 -3
  147. package/src/firebase/auth/setCustomClaims.ts +130 -84
  148. package/src/firebase/baseFunction.ts +2 -2
  149. package/src/firebase/billing/cancelSubscription.ts +3 -3
  150. package/src/firebase/billing/changePlan.ts +3 -3
  151. package/src/firebase/billing/createCheckoutSession.ts +1 -1
  152. package/src/firebase/billing/createCustomerPortal.ts +1 -1
  153. package/src/firebase/billing/healthCheck.ts +8 -24
  154. package/src/firebase/billing/index.d.ts +11 -11
  155. package/src/firebase/billing/index.ts +1 -1
  156. package/src/firebase/billing/refreshSubscriptionStatus.ts +3 -3
  157. package/src/firebase/billing/webhookHandler.ts +1 -2
  158. package/src/firebase/config/constants.ts +4 -4
  159. package/src/firebase/config/index.ts +1 -1
  160. package/src/firebase/config/secrets.ts +1 -1
  161. package/src/firebase/crud/aggregate.ts +2 -2
  162. package/src/firebase/crud/create.ts +19 -4
  163. package/src/firebase/crud/delete.ts +3 -3
  164. package/src/firebase/crud/get.ts +1 -1
  165. package/src/firebase/crud/index.ts +1 -1
  166. package/src/firebase/crud/list.ts +1 -1
  167. package/src/firebase/crud/update.ts +2 -2
  168. package/src/firebase/helpers/githubAccessHelper.ts +4 -4
  169. package/src/firebase/helpers/githubTeamAccessHelper.ts +3 -3
  170. package/src/firebase/helpers/index.ts +1 -1
  171. package/src/firebase/index.d.ts +9 -9
  172. package/src/firebase/index.ts +1 -1
  173. package/src/firebase/oauth/disconnect.ts +2 -2
  174. package/src/firebase/oauth/exchangeToken.ts +7 -7
  175. package/src/firebase/oauth/getConnections.ts +2 -2
  176. package/src/firebase/oauth/githubAccess.ts +10 -10
  177. package/src/firebase/oauth/index.ts +1 -1
  178. package/src/firebase/oauth/refreshToken.ts +2 -2
  179. package/src/firebase/registerCrudFunctions.ts +1 -1
  180. package/src/firebase/scheduled/checkExpiredSubscriptions.ts +1 -1
  181. package/src/firebase/scheduled/index.ts +1 -1
  182. package/src/shared/__tests__/auth.test.ts +46 -0
  183. package/src/shared/__tests__/errorHandling.test.ts +66 -1
  184. package/src/shared/__tests__/firebase.test.ts +293 -0
  185. package/src/shared/__tests__/internalValidation.test.ts +190 -0
  186. package/src/shared/__tests__/metadata.test.ts +66 -0
  187. package/src/shared/__tests__/schema.test.ts +219 -0
  188. package/src/shared/__tests__/schemaValidation.test.ts +72 -0
  189. package/src/shared/__tests__/subscription.test.ts +45 -0
  190. package/src/shared/__tests__/utils.test.ts +281 -0
  191. package/src/shared/__tests__/validation-env.test.ts +73 -0
  192. package/src/shared/billing/__tests__/createCheckout.test.ts +1 -1
  193. package/src/shared/billing/__tests__/webhookHandler.test.ts +1 -1
  194. package/src/shared/billing/createCheckout.ts +10 -5
  195. package/src/shared/billing/helpers/index.ts +1 -1
  196. package/src/shared/billing/helpers/subscriptionManagement.ts +4 -4
  197. package/src/shared/billing/helpers/updateUserSubscription.ts +4 -4
  198. package/src/shared/billing/idempotency.ts +4 -4
  199. package/src/shared/billing/index.ts +1 -1
  200. package/src/shared/billing/webhookHandler.ts +7 -7
  201. package/src/shared/errorHandling.ts +2 -2
  202. package/src/shared/firebase.ts +7 -7
  203. package/src/shared/github.ts +11 -11
  204. package/src/shared/index.d.ts +9 -9
  205. package/src/shared/index.ts +1 -1
  206. package/src/shared/logger.ts +8 -8
  207. package/src/shared/metadata.ts +3 -3
  208. package/src/shared/oauth/__tests__/exchangeToken.test.ts +1 -1
  209. package/src/shared/oauth/exchangeToken.ts +2 -2
  210. package/src/shared/oauth/grantAccess.ts +2 -2
  211. package/src/shared/oauth/index.ts +1 -1
  212. package/src/shared/schema.ts +4 -4
  213. package/src/shared/utils/__tests__/functionWrapper.test.ts +1 -1
  214. package/src/shared/utils/detectFirestore.ts +3 -3
  215. package/src/shared/utils/external/date.ts +5 -5
  216. package/src/shared/utils/external/index.ts +1 -1
  217. package/src/shared/utils/external/references.ts +2 -2
  218. package/src/shared/utils/external/subscription.ts +5 -5
  219. package/src/shared/utils/firebaseHelpers.ts +3 -3
  220. package/src/shared/utils/functionWrapper.ts +4 -4
  221. package/src/shared/utils/index.ts +1 -1
  222. package/src/shared/utils/internal/auth.ts +7 -7
  223. package/src/shared/utils/internal/firebase.ts +1 -1
  224. package/src/shared/utils/internal/idempotency.ts +3 -3
  225. package/src/shared/utils/internal/index.ts +1 -1
  226. package/src/shared/utils/internal/monitoring.ts +8 -8
  227. package/src/shared/utils/internal/rateLimiter.ts +6 -6
  228. package/src/shared/utils/internal/validation.ts +6 -6
  229. package/src/shared/utils/schemaValidation.ts +6 -6
  230. package/src/shared/utils/validation.ts +10 -10
  231. package/src/shared/utils/vercelHelpers.ts +3 -3
  232. package/src/shared/utils.ts +19 -19
  233. package/src/shared/validation.ts +2 -2
  234. package/src/supabase/auth/deleteAccount.ts +2 -2
  235. package/src/supabase/auth/getCustomClaims.ts +2 -2
  236. package/src/supabase/auth/getUserAuthStatus.ts +2 -2
  237. package/src/supabase/auth/removeCustomClaims.ts +2 -2
  238. package/src/supabase/auth/setCustomClaims.ts +2 -2
  239. package/src/supabase/baseFunction.ts +3 -3
  240. package/src/supabase/billing/cancelSubscription.ts +2 -2
  241. package/src/supabase/billing/changePlan.ts +2 -2
  242. package/src/supabase/billing/createCheckoutSession.ts +2 -2
  243. package/src/supabase/billing/createCustomerPortal.ts +2 -2
  244. package/src/supabase/billing/refreshSubscriptionStatus.ts +2 -2
  245. package/src/supabase/crud/aggregate.ts +11 -8
  246. package/src/supabase/crud/create.ts +2 -2
  247. package/src/supabase/crud/delete.ts +1 -1
  248. package/src/supabase/crud/get.ts +2 -2
  249. package/src/supabase/crud/index.ts +1 -1
  250. package/src/supabase/crud/list.ts +2 -2
  251. package/src/supabase/crud/update.ts +2 -2
  252. package/src/supabase/helpers/authProvider.ts +2 -2
  253. package/src/supabase/index.ts +10 -2
  254. package/src/supabase/oauth/disconnect.ts +104 -0
  255. package/src/supabase/oauth/exchangeToken.ts +152 -0
  256. package/src/supabase/oauth/getConnections.ts +85 -0
  257. package/src/supabase/oauth/index.ts +15 -0
  258. package/src/supabase/oauth/refreshToken.ts +131 -0
  259. package/src/supabase/registerCrudFunctions.ts +1 -1
  260. package/src/supabase/utils/idempotency.ts +1 -1
  261. package/src/supabase/utils/monitoring.ts +1 -1
  262. package/src/supabase/utils/rateLimiter.ts +1 -1
  263. package/src/vercel/api/auth/get-custom-claims.ts +1 -1
  264. package/src/vercel/api/auth/get-user-auth-status.ts +1 -1
  265. package/src/vercel/api/auth/index.ts +1 -1
  266. package/src/vercel/api/auth/remove-custom-claims.ts +1 -1
  267. package/src/vercel/api/auth/set-custom-claims.ts +69 -24
  268. package/src/vercel/api/billing/cancel.ts +3 -3
  269. package/src/vercel/api/billing/change-plan.ts +3 -3
  270. package/src/vercel/api/billing/create-checkout-session.ts +3 -3
  271. package/src/vercel/api/billing/customer-portal.ts +1 -1
  272. package/src/vercel/api/billing/index.ts +1 -1
  273. package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
  274. package/src/vercel/api/billing/webhook-handler.ts +3 -3
  275. package/src/vercel/api/config/constants.ts +9 -9
  276. package/src/vercel/api/config/firebase-admin.ts +3 -3
  277. package/src/vercel/api/config/index.ts +1 -1
  278. package/src/vercel/api/crud/create.ts +1 -1
  279. package/src/vercel/api/crud/delete.ts +1 -1
  280. package/src/vercel/api/crud/get.ts +1 -1
  281. package/src/vercel/api/crud/index.ts +1 -1
  282. package/src/vercel/api/crud/list.ts +1 -1
  283. package/src/vercel/api/crud/update.ts +1 -1
  284. package/src/vercel/api/index.d.ts +1 -1
  285. package/src/vercel/api/index.ts +1 -1
  286. package/src/vercel/api/oauth/check-github-access.ts +3 -3
  287. package/src/vercel/api/oauth/exchange-token.ts +1 -1
  288. package/src/vercel/api/oauth/grant-github-access.ts +3 -3
  289. package/src/vercel/api/oauth/index.ts +1 -1
  290. package/src/vercel/api/oauth/revoke-github-access.ts +1 -1
  291. package/src/vercel/api/utils/cors.ts +4 -4
  292. package/src/vercel/baseFunction.ts +2 -2
@@ -0,0 +1,190 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import * as v from 'valibot';
3
+
4
+ // Mock external deps
5
+ vi.mock('@donotdev/core/server', () => {
6
+ class DoNotDevError extends Error {
7
+ code: string;
8
+ details: any;
9
+ constructor(message: string, code: string = 'internal', opts?: any) {
10
+ super(message);
11
+ this.name = 'DoNotDevError';
12
+ this.code = code;
13
+ this.details = opts?.details;
14
+ }
15
+ }
16
+ return { DoNotDevError };
17
+ });
18
+
19
+ import {
20
+ safeJsonParse,
21
+ validateCollectionName,
22
+ validateDocument,
23
+ validateStripeEnvironment,
24
+ } from '../utils/internal/validation';
25
+
26
+ import { DoNotDevError } from '@donotdev/core/server';
27
+
28
+ // ---------------------------------------------------------------------------
29
+ // safeJsonParse
30
+ // ---------------------------------------------------------------------------
31
+
32
+ describe('safeJsonParse', () => {
33
+ it('parses valid JSON', () => {
34
+ expect(safeJsonParse('{"a":1}')).toEqual({ a: 1 });
35
+ });
36
+
37
+ it('parses JSON array', () => {
38
+ expect(safeJsonParse('[1,2,3]')).toEqual([1, 2, 3]);
39
+ });
40
+
41
+ it('parses JSON string', () => {
42
+ expect(safeJsonParse('"hello"')).toBe('hello');
43
+ });
44
+
45
+ it('parses JSON number', () => {
46
+ expect(safeJsonParse('42')).toBe(42);
47
+ });
48
+
49
+ it('returns null for invalid JSON', () => {
50
+ expect(safeJsonParse('{invalid}')).toBe(null);
51
+ });
52
+
53
+ it('returns null for empty string', () => {
54
+ expect(safeJsonParse('')).toBe(null);
55
+ });
56
+ });
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // validateCollectionName (internal version)
60
+ // ---------------------------------------------------------------------------
61
+
62
+ describe('validateCollectionName (internal)', () => {
63
+ it('accepts valid names', () => {
64
+ expect(() => validateCollectionName('users')).not.toThrow();
65
+ expect(() => validateCollectionName('my-collection')).not.toThrow();
66
+ });
67
+
68
+ it('rejects empty string', () => {
69
+ expect(() => validateCollectionName('')).toThrow('Collection name is required');
70
+ });
71
+
72
+ it('rejects non-string', () => {
73
+ expect(() => validateCollectionName(null as any)).toThrow(
74
+ 'Collection name is required'
75
+ );
76
+ expect(() => validateCollectionName(undefined as any)).toThrow(
77
+ 'Collection name is required'
78
+ );
79
+ });
80
+
81
+ it('rejects path traversal with slash', () => {
82
+ expect(() => validateCollectionName('a/b')).toThrow('Invalid collection name');
83
+ });
84
+
85
+ it('rejects path traversal with double dots', () => {
86
+ expect(() => validateCollectionName('a..b')).toThrow('Invalid collection name');
87
+ });
88
+
89
+ it('rejects internal collections starting with underscore', () => {
90
+ expect(() => validateCollectionName('_system')).toThrow(
91
+ 'Invalid collection name'
92
+ );
93
+ });
94
+ });
95
+
96
+ // ---------------------------------------------------------------------------
97
+ // validateDocument (internal version - throws DoNotDevError)
98
+ // ---------------------------------------------------------------------------
99
+
100
+ describe('validateDocument (internal)', () => {
101
+ it('accepts valid object', () => {
102
+ expect(() => validateDocument({ name: 'test' })).not.toThrow();
103
+ });
104
+
105
+ it('throws DoNotDevError for null', () => {
106
+ try {
107
+ validateDocument(null);
108
+ expect.fail('should have thrown');
109
+ } catch (error) {
110
+ expect(error).toBeInstanceOf(DoNotDevError);
111
+ expect((error as any).code).toBe('invalid-argument');
112
+ }
113
+ });
114
+
115
+ it('throws DoNotDevError for array', () => {
116
+ try {
117
+ validateDocument([1, 2]);
118
+ expect.fail('should have thrown');
119
+ } catch (error) {
120
+ expect(error).toBeInstanceOf(DoNotDevError);
121
+ expect((error as any).message).toBe('Document data cannot be an array');
122
+ }
123
+ });
124
+
125
+ it('validates against valibot schema when provided', () => {
126
+ const schema = v.object({
127
+ name: v.pipe(v.string(), v.minLength(1)),
128
+ });
129
+
130
+ expect(() => validateDocument({ name: 'valid' }, schema)).not.toThrow();
131
+ });
132
+
133
+ it('throws DoNotDevError with validation details for invalid schema data', () => {
134
+ const schema = v.object({
135
+ name: v.pipe(v.string(), v.minLength(1)),
136
+ });
137
+
138
+ try {
139
+ validateDocument({ name: '' }, schema);
140
+ expect.fail('should have thrown');
141
+ } catch (error) {
142
+ expect(error).toBeInstanceOf(DoNotDevError);
143
+ expect((error as any).code).toBe('invalid-argument');
144
+ expect((error as any).message).toContain('Validation failed');
145
+ expect((error as any).details).toBeDefined();
146
+ }
147
+ });
148
+
149
+ it('passes without schema (no validation beyond type check)', () => {
150
+ expect(() => validateDocument({ anything: 'goes' })).not.toThrow();
151
+ });
152
+ });
153
+
154
+ // ---------------------------------------------------------------------------
155
+ // validateStripeEnvironment (internal version)
156
+ // ---------------------------------------------------------------------------
157
+
158
+ describe('validateStripeEnvironment (internal)', () => {
159
+ const originalEnv = { ...process.env };
160
+
161
+ beforeEach(() => {
162
+ delete process.env.STRIPE_SECRET_KEY;
163
+ delete process.env.STRIPE_WEBHOOK_SECRET;
164
+ });
165
+
166
+ afterEach(() => {
167
+ process.env = { ...originalEnv };
168
+ });
169
+
170
+ it('throws when both vars missing', () => {
171
+ expect(() => validateStripeEnvironment()).toThrow(
172
+ 'Missing required environment variables'
173
+ );
174
+ expect(() => validateStripeEnvironment()).toThrow('STRIPE_SECRET_KEY');
175
+ expect(() => validateStripeEnvironment()).toThrow('STRIPE_WEBHOOK_SECRET');
176
+ });
177
+
178
+ it('throws when STRIPE_WEBHOOK_SECRET missing', () => {
179
+ process.env.STRIPE_SECRET_KEY = 'sk_test_x';
180
+
181
+ expect(() => validateStripeEnvironment()).toThrow('STRIPE_WEBHOOK_SECRET');
182
+ });
183
+
184
+ it('passes when all vars set', () => {
185
+ process.env.STRIPE_SECRET_KEY = 'sk_test_x';
186
+ process.env.STRIPE_WEBHOOK_SECRET = 'whsec_x';
187
+
188
+ expect(() => validateStripeEnvironment()).not.toThrow();
189
+ });
190
+ });
@@ -0,0 +1,66 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+
3
+ import { createMetadata, updateMetadata } from '../metadata';
4
+
5
+ describe('createMetadata', () => {
6
+ beforeEach(() => {
7
+ vi.useFakeTimers();
8
+ vi.setSystemTime(new Date('2026-03-13T10:00:00.000Z'));
9
+ });
10
+
11
+ afterEach(() => {
12
+ vi.useRealTimers();
13
+ });
14
+
15
+ it('returns all four metadata fields', () => {
16
+ const result = createMetadata('user-123');
17
+
18
+ expect(result).toEqual({
19
+ _createdAt: '2026-03-13T10:00:00.000Z',
20
+ _updatedAt: '2026-03-13T10:00:00.000Z',
21
+ _createdBy: 'user-123',
22
+ _updatedBy: 'user-123',
23
+ });
24
+ });
25
+
26
+ it('sets createdAt and updatedAt to the same value', () => {
27
+ const result = createMetadata('any');
28
+
29
+ expect(result._createdAt).toBe(result._updatedAt);
30
+ });
31
+
32
+ it('uses ISO 8601 format', () => {
33
+ const result = createMetadata('u');
34
+
35
+ expect(result._createdAt).toMatch(
36
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
37
+ );
38
+ });
39
+ });
40
+
41
+ describe('updateMetadata', () => {
42
+ beforeEach(() => {
43
+ vi.useFakeTimers();
44
+ vi.setSystemTime(new Date('2026-06-15T14:30:00.000Z'));
45
+ });
46
+
47
+ afterEach(() => {
48
+ vi.useRealTimers();
49
+ });
50
+
51
+ it('returns only update fields', () => {
52
+ const result = updateMetadata('user-456');
53
+
54
+ expect(result).toEqual({
55
+ _updatedAt: '2026-06-15T14:30:00.000Z',
56
+ _updatedBy: 'user-456',
57
+ });
58
+ });
59
+
60
+ it('does not include creation fields', () => {
61
+ const result = updateMetadata('u') as any;
62
+
63
+ expect(result._createdAt).toBeUndefined();
64
+ expect(result._createdBy).toBeUndefined();
65
+ });
66
+ });
@@ -0,0 +1,219 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+
3
+ import {
4
+ isFieldVisible,
5
+ getVisibleFields,
6
+ filterVisibleFields,
7
+ } from '../schema';
8
+
9
+ // ---------------------------------------------------------------------------
10
+ // isFieldVisible
11
+ // ---------------------------------------------------------------------------
12
+
13
+ describe('isFieldVisible', () => {
14
+ describe('hidden visibility', () => {
15
+ it('never visible regardless of auth/admin', () => {
16
+ expect(isFieldVisible('secret', 'hidden', true, true)).toBe(false);
17
+ expect(isFieldVisible('secret', 'hidden', false, true)).toBe(false);
18
+ expect(isFieldVisible('secret', 'hidden', true, false)).toBe(false);
19
+ expect(isFieldVisible('secret', 'hidden', false, false)).toBe(false);
20
+ });
21
+ });
22
+
23
+ describe('guest visibility', () => {
24
+ it('always visible regardless of auth/admin', () => {
25
+ expect(isFieldVisible('title', 'guest', true, true)).toBe(true);
26
+ expect(isFieldVisible('title', 'guest', false, true)).toBe(true);
27
+ expect(isFieldVisible('title', 'guest', true, false)).toBe(true);
28
+ expect(isFieldVisible('title', 'guest', false, false)).toBe(true);
29
+ });
30
+ });
31
+
32
+ describe('admin visibility', () => {
33
+ it('visible only to admins', () => {
34
+ expect(isFieldVisible('config', 'admin', true, true)).toBe(true);
35
+ expect(isFieldVisible('config', 'admin', false, true)).toBe(false);
36
+ expect(isFieldVisible('config', 'admin', true, false)).toBe(true);
37
+ expect(isFieldVisible('config', 'admin', false, false)).toBe(false);
38
+ });
39
+ });
40
+
41
+ describe('technical visibility', () => {
42
+ it('visible only to admins', () => {
43
+ expect(isFieldVisible('debug', 'technical', true)).toBe(true);
44
+ expect(isFieldVisible('debug', 'technical', false)).toBe(false);
45
+ });
46
+ });
47
+
48
+ describe('owner visibility', () => {
49
+ it('returns false in aggregate context', () => {
50
+ expect(isFieldVisible('email', 'owner', true, true)).toBe(false);
51
+ expect(isFieldVisible('email', 'owner', false, true)).toBe(false);
52
+ });
53
+ });
54
+
55
+ describe('user visibility', () => {
56
+ it('visible to authenticated users', () => {
57
+ expect(isFieldVisible('profile', 'user', false, true)).toBe(true);
58
+ expect(isFieldVisible('profile', 'user', false, false)).toBe(false);
59
+ });
60
+
61
+ it('visible to admins', () => {
62
+ expect(isFieldVisible('profile', 'user', true, true)).toBe(true);
63
+ });
64
+ });
65
+
66
+ describe('undefined visibility (defaults to user)', () => {
67
+ it('visible to authenticated users', () => {
68
+ expect(isFieldVisible('field', undefined, false, true)).toBe(true);
69
+ expect(isFieldVisible('field', undefined, false, false)).toBe(false);
70
+ });
71
+
72
+ it('defaults isAuthenticated to true', () => {
73
+ expect(isFieldVisible('field', undefined, false)).toBe(true);
74
+ });
75
+ });
76
+ });
77
+
78
+ // ---------------------------------------------------------------------------
79
+ // getVisibleFields
80
+ // ---------------------------------------------------------------------------
81
+
82
+ describe('getVisibleFields', () => {
83
+ it('returns empty array for non-object schema', () => {
84
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
85
+
86
+ expect(getVisibleFields(null, false)).toEqual([]);
87
+ expect(getVisibleFields('string', false)).toEqual([]);
88
+ expect(getVisibleFields({}, false)).toEqual([]);
89
+
90
+ warnSpy.mockRestore();
91
+ });
92
+
93
+ it('returns all fields without visibility for authenticated user', () => {
94
+ const schema = {
95
+ entries: {
96
+ name: {},
97
+ email: {},
98
+ },
99
+ };
100
+
101
+ const result = getVisibleFields(schema, false);
102
+
103
+ expect(result).toContain('name');
104
+ expect(result).toContain('email');
105
+ });
106
+
107
+ it('excludes hidden fields', () => {
108
+ const schema = {
109
+ entries: {
110
+ name: {},
111
+ password: { visibility: 'hidden' },
112
+ },
113
+ };
114
+
115
+ const result = getVisibleFields(schema, true);
116
+
117
+ expect(result).toContain('name');
118
+ expect(result).not.toContain('password');
119
+ });
120
+
121
+ it('includes admin fields only for admins', () => {
122
+ const schema = {
123
+ entries: {
124
+ name: {},
125
+ config: { visibility: 'admin' },
126
+ },
127
+ };
128
+
129
+ expect(getVisibleFields(schema, true)).toContain('config');
130
+ expect(getVisibleFields(schema, false)).not.toContain('config');
131
+ });
132
+
133
+ it('includes guest fields for everyone', () => {
134
+ const schema = {
135
+ entries: {
136
+ title: { visibility: 'guest' },
137
+ },
138
+ };
139
+
140
+ expect(getVisibleFields(schema, false)).toContain('title');
141
+ expect(getVisibleFields(schema, true)).toContain('title');
142
+ });
143
+ });
144
+
145
+ // ---------------------------------------------------------------------------
146
+ // filterVisibleFields
147
+ // ---------------------------------------------------------------------------
148
+
149
+ describe('filterVisibleFields', () => {
150
+ it('returns empty object for null data', () => {
151
+ const schema = { entries: { name: {} } };
152
+
153
+ expect(filterVisibleFields(null, schema, false)).toEqual({});
154
+ });
155
+
156
+ it('returns empty object for undefined data', () => {
157
+ const schema = { entries: { name: {} } };
158
+
159
+ expect(filterVisibleFields(undefined, schema, false)).toEqual({});
160
+ });
161
+
162
+ it('filters data to only visible fields', () => {
163
+ const schema = {
164
+ entries: {
165
+ name: { visibility: 'guest' },
166
+ secret: { visibility: 'hidden' },
167
+ config: { visibility: 'admin' },
168
+ },
169
+ };
170
+
171
+ const data = { name: 'Alice', secret: 'pw123', config: 'x', extra: 'y' };
172
+
173
+ const result = filterVisibleFields(data, schema, false);
174
+
175
+ expect(result).toEqual({ name: 'Alice' });
176
+ });
177
+
178
+ it('includes admin fields when isAdmin is true', () => {
179
+ const schema = {
180
+ entries: {
181
+ name: { visibility: 'guest' },
182
+ config: { visibility: 'admin' },
183
+ },
184
+ };
185
+
186
+ const data = { name: 'Alice', config: 'x' };
187
+ const result = filterVisibleFields(data, schema, true);
188
+
189
+ expect(result).toEqual({ name: 'Alice', config: 'x' });
190
+ });
191
+
192
+ it('ignores data keys not in schema', () => {
193
+ const schema = {
194
+ entries: {
195
+ name: {},
196
+ },
197
+ };
198
+
199
+ const data = { name: 'Alice', extraField: 'ignored' };
200
+ const result = filterVisibleFields(data, schema, false);
201
+
202
+ expect(result).toEqual({ name: 'Alice' });
203
+ });
204
+
205
+ it('handles missing data keys gracefully', () => {
206
+ const schema = {
207
+ entries: {
208
+ name: {},
209
+ missing: {},
210
+ },
211
+ };
212
+
213
+ const data = { name: 'Alice' };
214
+ const result = filterVisibleFields(data, schema, false);
215
+
216
+ expect(result).toEqual({ name: 'Alice' });
217
+ expect(result).not.toHaveProperty('missing');
218
+ });
219
+ });
@@ -0,0 +1,72 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import * as v from 'valibot';
3
+
4
+ import { validateRequestData } from '../utils/schemaValidation';
5
+
6
+ const TestSchema = v.object({
7
+ name: v.pipe(v.string(), v.minLength(1)),
8
+ age: v.optional(v.pipe(v.number(), v.minValue(0))),
9
+ });
10
+
11
+ describe('validateRequestData', () => {
12
+ it('returns success with parsed data for valid input', () => {
13
+ const result = validateRequestData({ name: 'Alice', age: 30 }, TestSchema);
14
+
15
+ expect(result.success).toBe(true);
16
+ if (result.success) {
17
+ expect(result.data).toEqual({ name: 'Alice', age: 30 });
18
+ }
19
+ });
20
+
21
+ it('returns success when optional fields are missing', () => {
22
+ const result = validateRequestData({ name: 'Alice' }, TestSchema);
23
+
24
+ expect(result.success).toBe(true);
25
+ });
26
+
27
+ it('returns error for invalid input', () => {
28
+ const result = validateRequestData({ name: '' }, TestSchema);
29
+
30
+ expect(result.success).toBe(false);
31
+ if (!result.success) {
32
+ expect(result.error).toContain('Validation failed');
33
+ }
34
+ });
35
+
36
+ it('returns error for completely wrong type', () => {
37
+ const result = validateRequestData('not an object', TestSchema);
38
+
39
+ expect(result.success).toBe(false);
40
+ });
41
+
42
+ it('returns error for null input', () => {
43
+ const result = validateRequestData(null, TestSchema);
44
+
45
+ expect(result.success).toBe(false);
46
+ });
47
+
48
+ it('uses custom schema when provided', () => {
49
+ const strictSchema = v.object({
50
+ name: v.pipe(v.string(), v.minLength(5)),
51
+ });
52
+
53
+ // Passes default but fails custom
54
+ const result = validateRequestData(
55
+ { name: 'Al' },
56
+ TestSchema,
57
+ strictSchema as any
58
+ );
59
+
60
+ expect(result.success).toBe(false);
61
+ });
62
+
63
+ it('uses default schema when custom is undefined', () => {
64
+ const result = validateRequestData(
65
+ { name: 'Alice' },
66
+ TestSchema,
67
+ undefined
68
+ );
69
+
70
+ expect(result.success).toBe(true);
71
+ });
72
+ });
@@ -0,0 +1,45 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+
3
+ // Mock Firebase deps (only getTierFromPriceId is pure)
4
+ vi.mock('@donotdev/firebase/server', () => ({
5
+ getFirebaseAdminAuth: vi.fn(),
6
+ getFirebaseAdminFirestore: vi.fn(),
7
+ }));
8
+ vi.mock('@donotdev/core/server', () => ({
9
+ SUBSCRIPTION_STATUS: {
10
+ ACTIVE: 'active',
11
+ CANCELED: 'canceled',
12
+ INCOMPLETE: 'incomplete',
13
+ PAST_DUE: 'past_due',
14
+ TRIALING: 'trialing',
15
+ UNPAID: 'unpaid',
16
+ },
17
+ }));
18
+
19
+ import { getTierFromPriceId } from '../utils/external/subscription';
20
+
21
+ describe('getTierFromPriceId', () => {
22
+ it('maps monthly pro price to "pro"', () => {
23
+ expect(getTierFromPriceId('price_pro_monthly')).toBe('pro');
24
+ });
25
+
26
+ it('maps yearly pro price to "pro"', () => {
27
+ expect(getTierFromPriceId('price_pro_yearly')).toBe('pro');
28
+ });
29
+
30
+ it('maps monthly ai price to "ai"', () => {
31
+ expect(getTierFromPriceId('price_ai_monthly')).toBe('ai');
32
+ });
33
+
34
+ it('maps yearly ai price to "ai"', () => {
35
+ expect(getTierFromPriceId('price_ai_yearly')).toBe('ai');
36
+ });
37
+
38
+ it('returns "free" for unknown price ID', () => {
39
+ expect(getTierFromPriceId('price_unknown_123')).toBe('free');
40
+ });
41
+
42
+ it('returns "free" for empty string', () => {
43
+ expect(getTierFromPriceId('')).toBe('free');
44
+ });
45
+ });