@alumbwe/anvil 1.0.41 → 1.0.49

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 (241) hide show
  1. package/bin/anvil.js +18 -0
  2. package/package.json +13 -6
  3. package/src/agents/bundled-agents.generated.ts +1 -1
  4. package/src/entry.ts +5 -0
  5. package/src/index.tsx +27 -4
  6. package/src/login/plain-login.ts +60 -0
  7. package/src/login/supabase-auth.ts +346 -0
  8. package/src/pre-init/client-env.ts +17 -0
  9. package/src/utils/anvil-client.ts +4 -0
  10. package/src/utils/auth.ts +16 -4
  11. package/src/utils/client-env-defaults.ts +28 -0
  12. package/src/utils/config-dir.ts +1 -1
  13. package/vendor/@anvil/common/src/constants/deepseek-direct.ts +30 -1
  14. package/vendor/@anvil/common/src/util/project-ignore.ts +4 -3
  15. package/vendor/@anvil/sdk/dist/index.cjs +46 -10
  16. package/vendor/@anvil/sdk/dist/index.mjs +46 -10
  17. package/src/commands/__tests__/anvil-command-aliases.test.ts +0 -56
  18. package/src/commands/__tests__/bash-command.test.ts +0 -445
  19. package/src/commands/__tests__/command-args.test.ts +0 -349
  20. package/src/commands/__tests__/copy-conversation-clipboard.test.ts +0 -108
  21. package/src/commands/__tests__/copy-conversation.test.ts +0 -226
  22. package/src/commands/__tests__/diagnostics-command.test.ts +0 -35
  23. package/src/commands/__tests__/image.test.ts +0 -99
  24. package/src/commands/__tests__/init.test.ts +0 -456
  25. package/src/commands/__tests__/new-command-rotates-chat.test.ts +0 -91
  26. package/src/commands/__tests__/process-diagnostics.test.ts +0 -54
  27. package/src/commands/__tests__/prompt-builders.test.ts +0 -28
  28. package/src/commands/__tests__/router-input.test.ts +0 -298
  29. package/src/components/__tests__/ad-banner.test.tsx +0 -83
  30. package/src/components/__tests__/anvil-landing-heading-row.test.tsx +0 -91
  31. package/src/components/__tests__/anvil-model-selector.test.tsx +0 -519
  32. package/src/components/__tests__/anvil-offer-invariants.test.ts +0 -54
  33. package/src/components/__tests__/anvil-takeover-prompt.test.tsx +0 -212
  34. package/src/components/__tests__/chat-input-bar.test.tsx +0 -130
  35. package/src/components/__tests__/grid-layout.integration.test.tsx +0 -304
  36. package/src/components/__tests__/grid-layout.test.tsx +0 -1051
  37. package/src/components/__tests__/message-block.completion.test.tsx +0 -104
  38. package/src/components/__tests__/message-block.streaming.test.tsx +0 -78
  39. package/src/components/__tests__/message-with-agents.test.tsx +0 -579
  40. package/src/components/__tests__/multiline-input.test.tsx +0 -1132
  41. package/src/components/__tests__/queue-panel.test.tsx +0 -398
  42. package/src/components/__tests__/selectable-list.test.ts +0 -232
  43. package/src/components/__tests__/status-bar.test.tsx +0 -44
  44. package/src/components/__tests__/status-indicator.test.tsx +0 -204
  45. package/src/components/__tests__/terminal-thinking-orb.test.tsx +0 -111
  46. package/src/components/__tests__/user-error-banner.test.tsx +0 -102
  47. package/src/components/ask-user/__tests__/multiple-choice-form.test.ts +0 -432
  48. package/src/components/ask-user/__tests__/validation.test.ts +0 -213
  49. package/src/components/tools/__tests__/apply-patch.test.tsx +0 -84
  50. package/src/components/tools/__tests__/code-search.test.tsx +0 -45
  51. package/src/components/tools/__tests__/gravity-index.test.ts +0 -76
  52. package/src/components/tools/__tests__/render-ui.test.tsx +0 -92
  53. package/src/components/tools/__tests__/run-terminal-command.test.ts +0 -233
  54. package/src/contexts/__tests__/chat-runtime-context.test.tsx +0 -149
  55. package/src/data/slash-commands.test.ts +0 -24
  56. package/src/hooks/__tests__/holds-live-anvil-slot.test.ts +0 -49
  57. package/src/hooks/__tests__/session-fetch-signal.test.ts +0 -63
  58. package/src/hooks/__tests__/use-activity-query.test.ts +0 -1000
  59. package/src/hooks/__tests__/use-ask-user-bridge.test.ts +0 -176
  60. package/src/hooks/__tests__/use-auth-query.test.ts +0 -21
  61. package/src/hooks/__tests__/use-chat-input-remount.test.tsx +0 -66
  62. package/src/hooks/__tests__/use-clipboard.test.tsx +0 -73
  63. package/src/hooks/__tests__/use-connection-status.test.ts +0 -112
  64. package/src/hooks/__tests__/use-directory-browser.test.ts +0 -203
  65. package/src/hooks/__tests__/use-gravity-ad.test.ts +0 -73
  66. package/src/hooks/__tests__/use-grid-layout.test.ts +0 -349
  67. package/src/hooks/__tests__/use-input-history.test.ts +0 -699
  68. package/src/hooks/__tests__/use-message-queue-editing.test.tsx +0 -209
  69. package/src/hooks/__tests__/use-message-queue.test.tsx +0 -71
  70. package/src/hooks/__tests__/use-path-tab-completion.test.ts +0 -315
  71. package/src/hooks/__tests__/use-queue-controls.test.ts +0 -68
  72. package/src/hooks/__tests__/use-queue-ui.test.tsx +0 -83
  73. package/src/hooks/__tests__/use-searchable-list.test.ts +0 -359
  74. package/src/hooks/__tests__/use-send-message-timer.test.ts +0 -146
  75. package/src/hooks/__tests__/use-suggestion-engine-mention.test.ts +0 -361
  76. package/src/hooks/__tests__/use-suggestion-engine.test.ts +0 -485
  77. package/src/hooks/__tests__/use-terminal-focus.test.ts +0 -66
  78. package/src/hooks/__tests__/use-terminal-layout.test.ts +0 -675
  79. package/src/hooks/__tests__/use-timeout.test.ts +0 -330
  80. package/src/hooks/__tests__/use-usage-query.test.ts +0 -502
  81. package/src/hooks/__tests__/use-user-details-query.test.ts +0 -195
  82. package/src/hooks/helpers/__tests__/send-message.test.ts +0 -1918
  83. package/src/init/__tests__/init-direnv.test.ts +0 -526
  84. package/src/state/__tests__/chat-store-focus.test.ts +0 -22
  85. package/src/state/__tests__/feedback-store.test.ts +0 -257
  86. package/src/utils/__tests__/active-run.test.ts +0 -140
  87. package/src/utils/__tests__/activity-tracker.test.ts +0 -440
  88. package/src/utils/__tests__/agent-display.test.ts +0 -139
  89. package/src/utils/__tests__/analytics-client.test.ts +0 -262
  90. package/src/utils/__tests__/anonymous-id.test.ts +0 -74
  91. package/src/utils/__tests__/anvil-api.test.ts +0 -578
  92. package/src/utils/__tests__/anvil-instance-owner.test.ts +0 -69
  93. package/src/utils/__tests__/anvil-model-navigation.test.ts +0 -102
  94. package/src/utils/__tests__/anvil-premium-reset.test.ts +0 -79
  95. package/src/utils/__tests__/anvil-referral-cache.test.ts +0 -116
  96. package/src/utils/__tests__/anvil-session-api.test.ts +0 -215
  97. package/src/utils/__tests__/anvil-session-display.test.ts +0 -23
  98. package/src/utils/__tests__/anvil-streak-line.test.ts +0 -217
  99. package/src/utils/__tests__/arrays.test.ts +0 -264
  100. package/src/utils/__tests__/bash-context-processor.test.ts +0 -38
  101. package/src/utils/__tests__/block-processor.test.ts +0 -810
  102. package/src/utils/__tests__/chat-history.test.ts +0 -203
  103. package/src/utils/__tests__/chat-input-key-intercept.test.ts +0 -40
  104. package/src/utils/__tests__/chat-meta.test.ts +0 -130
  105. package/src/utils/__tests__/clipboard-linux.test.ts +0 -359
  106. package/src/utils/__tests__/clipboard.test.ts +0 -910
  107. package/src/utils/__tests__/code-search-summary.test.ts +0 -84
  108. package/src/utils/__tests__/collapse-helpers.test.ts +0 -1109
  109. package/src/utils/__tests__/error-handling.test.ts +0 -658
  110. package/src/utils/__tests__/exit-cleanly.test.ts +0 -110
  111. package/src/utils/__tests__/feedback-helpers.test.ts +0 -444
  112. package/src/utils/__tests__/feedback-submission.test.ts +0 -26
  113. package/src/utils/__tests__/fetch-usage.test.ts +0 -355
  114. package/src/utils/__tests__/fingerprint.test.ts +0 -144
  115. package/src/utils/__tests__/fixtures/terminal-command-broker-child.ts +0 -7
  116. package/src/utils/__tests__/fixtures/terminal-command-broker-entry.ts +0 -3
  117. package/src/utils/__tests__/fixtures/terminal-command-broker-owner.ts +0 -33
  118. package/src/utils/__tests__/format-elapsed-time.test.ts +0 -103
  119. package/src/utils/__tests__/format-timeout.test.ts +0 -87
  120. package/src/utils/__tests__/image-dimensions.test.ts +0 -249
  121. package/src/utils/__tests__/image-processor.test.ts +0 -209
  122. package/src/utils/__tests__/implementor-helpers.test.ts +0 -1458
  123. package/src/utils/__tests__/keyboard-actions.test.ts +0 -660
  124. package/src/utils/__tests__/layout-helpers.test.ts +0 -36
  125. package/src/utils/__tests__/lazy-response-ads.test.ts +0 -167
  126. package/src/utils/__tests__/log-shipper.test.ts +0 -53
  127. package/src/utils/__tests__/markdown-renderer.test.tsx +0 -421
  128. package/src/utils/__tests__/message-block-helpers.test.ts +0 -1263
  129. package/src/utils/__tests__/message-updater.test.ts +0 -645
  130. package/src/utils/__tests__/osc-timeout-scenarios.test.ts +0 -172
  131. package/src/utils/__tests__/pending-attachments.test.ts +0 -277
  132. package/src/utils/__tests__/polling-backoff.test.ts +0 -51
  133. package/src/utils/__tests__/queue-panel-actions.test.ts +0 -148
  134. package/src/utils/__tests__/run-state-storage.test.ts +0 -919
  135. package/src/utils/__tests__/safe-json.test.ts +0 -77
  136. package/src/utils/__tests__/sdk-event-handlers.test.ts +0 -685
  137. package/src/utils/__tests__/send-message-helpers.test.ts +0 -1898
  138. package/src/utils/__tests__/send-message-timer.test.ts +0 -64
  139. package/src/utils/__tests__/settings.test.ts +0 -99
  140. package/src/utils/__tests__/strings.test.ts +0 -192
  141. package/src/utils/__tests__/terminal-color-detection.test.ts +0 -425
  142. package/src/utils/__tests__/terminal-command-broker.test.ts +0 -528
  143. package/src/utils/__tests__/terminal-enter-detection.test.ts +0 -77
  144. package/src/utils/__tests__/terminal-io.test.ts +0 -137
  145. package/src/utils/__tests__/terminal-protocol-controller.test.ts +0 -119
  146. package/src/utils/__tests__/text-layout.test.ts +0 -82
  147. package/src/utils/__tests__/theme-platform-detection.test.ts +0 -125
  148. package/src/utils/__tests__/think-tag-parser.test.ts +0 -159
  149. package/src/utils/__tests__/trace-writer.test.ts +0 -223
  150. package/src/utils/__tests__/trim-chat-logs.test.ts +0 -78
  151. package/src/utils/__tests__/usage-banner-state.test.ts +0 -298
  152. package/src/utils/__tests__/validation-error-formatting.test.ts +0 -126
  153. package/src/utils/__tests__/windows-terminal-health.test.ts +0 -101
  154. package/src/utils/__tests__/write-file-atomic.test.ts +0 -127
  155. package/vendor/@anvil/common/src/__tests__/agent-validation.test.ts +0 -846
  156. package/vendor/@anvil/common/src/__tests__/anvil-model-availability.test.ts +0 -101
  157. package/vendor/@anvil/common/src/__tests__/anvil-models.test.ts +0 -1408
  158. package/vendor/@anvil/common/src/__tests__/anvil-public-data-use-copy.test.ts +0 -258
  159. package/vendor/@anvil/common/src/__tests__/anvil-referral-tiers.test.ts +0 -48
  160. package/vendor/@anvil/common/src/__tests__/anvil-spend-ceilings.test.ts +0 -282
  161. package/vendor/@anvil/common/src/__tests__/anvil-trust.test.ts +0 -526
  162. package/vendor/@anvil/common/src/__tests__/deepseek-direct.test.ts +0 -68
  163. package/vendor/@anvil/common/src/__tests__/disposable-email.test.ts +0 -132
  164. package/vendor/@anvil/common/src/__tests__/dynamic-agent-template-schema.test.ts +0 -422
  165. package/vendor/@anvil/common/src/__tests__/env-ci.test.ts +0 -167
  166. package/vendor/@anvil/common/src/__tests__/env-process.test.ts +0 -145
  167. package/vendor/@anvil/common/src/__tests__/foreign-client-shipped-agents.test.ts +0 -234
  168. package/vendor/@anvil/common/src/__tests__/foreign-client-signals.test.ts +0 -379
  169. package/vendor/@anvil/common/src/__tests__/free-agents.test.ts +0 -807
  170. package/vendor/@anvil/common/src/__tests__/gravity-capi.test.ts +0 -154
  171. package/vendor/@anvil/common/src/__tests__/handlesteps-parsing.test.ts +0 -246
  172. package/vendor/@anvil/common/src/__tests__/kimi-k3-god-only.test.ts +0 -77
  173. package/vendor/@anvil/common/src/__tests__/model-config.test.ts +0 -89
  174. package/vendor/@anvil/common/src/__tests__/project-file-tree.test.ts +0 -298
  175. package/vendor/@anvil/common/src/__tests__/provisioned-model-tiers.test.ts +0 -116
  176. package/vendor/@anvil/common/src/__tests__/reasoning-effort.test.ts +0 -167
  177. package/vendor/@anvil/common/src/__tests__/reddit-capi.test.ts +0 -132
  178. package/vendor/@anvil/common/src/__tests__/response-ad-positions.test.ts +0 -72
  179. package/vendor/@anvil/common/src/__tests__/user-state.test.ts +0 -30
  180. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding-gate.test.ts +0 -63
  181. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding.test.ts +0 -246
  182. package/vendor/@anvil/common/src/constants/__tests__/cf-worker-signals.test.ts +0 -106
  183. package/vendor/@anvil/common/src/testing/TESTING_PATTERNS.md +0 -351
  184. package/vendor/@anvil/common/src/testing/anvil-offer-invariants.ts +0 -169
  185. package/vendor/@anvil/common/src/testing/errors.ts +0 -33
  186. package/vendor/@anvil/common/src/testing/fixtures/agent-runtime.ts +0 -334
  187. package/vendor/@anvil/common/src/testing/impl/agent-runtime.ts +0 -6
  188. package/vendor/@anvil/common/src/testing/index.ts +0 -84
  189. package/vendor/@anvil/common/src/testing/mock-modules.ts +0 -53
  190. package/vendor/@anvil/common/src/testing/mock-types.ts +0 -123
  191. package/vendor/@anvil/common/src/testing/mocks/analytics.ts +0 -261
  192. package/vendor/@anvil/common/src/testing/mocks/child-process.ts +0 -93
  193. package/vendor/@anvil/common/src/testing/mocks/crypto.ts +0 -218
  194. package/vendor/@anvil/common/src/testing/mocks/database.ts +0 -337
  195. package/vendor/@anvil/common/src/testing/mocks/fetch.ts +0 -219
  196. package/vendor/@anvil/common/src/testing/mocks/filesystem.ts +0 -166
  197. package/vendor/@anvil/common/src/testing/mocks/index.ts +0 -101
  198. package/vendor/@anvil/common/src/testing/mocks/logger.ts +0 -135
  199. package/vendor/@anvil/common/src/testing/mocks/stream.ts +0 -313
  200. package/vendor/@anvil/common/src/testing/mocks/timers.ts +0 -132
  201. package/vendor/@anvil/common/src/testing/mocks/tree-sitter.ts +0 -127
  202. package/vendor/@anvil/common/src/testing/setup.ts +0 -282
  203. package/vendor/@anvil/common/src/tools/__tests__/compile-tool-definitions.test.ts +0 -34
  204. package/vendor/@anvil/common/src/tools/params/__tests__/coerce-to-array.test.ts +0 -213
  205. package/vendor/@anvil/common/src/tools/params/__tests__/read-docs.test.ts +0 -47
  206. package/vendor/@anvil/common/src/tools/params/__tests__/render-ui.test.ts +0 -65
  207. package/vendor/@anvil/common/src/tools/params/tool/__tests__/run-terminal-command-timeout.test.ts +0 -49
  208. package/vendor/@anvil/common/src/types/__tests__/dynamic-agent-template.test.ts +0 -20
  209. package/vendor/@anvil/common/src/util/__tests__/ad-user-agent.test.ts +0 -26
  210. package/vendor/@anvil/common/src/util/__tests__/analytics-dispatcher.test.ts +0 -122
  211. package/vendor/@anvil/common/src/util/__tests__/analytics-log.test.ts +0 -102
  212. package/vendor/@anvil/common/src/util/__tests__/analytics-sampling.test.ts +0 -143
  213. package/vendor/@anvil/common/src/util/__tests__/anvil-streak.test.ts +0 -202
  214. package/vendor/@anvil/common/src/util/__tests__/anvil-usage-summary.test.ts +0 -193
  215. package/vendor/@anvil/common/src/util/__tests__/axiom-only-log.test.ts +0 -140
  216. package/vendor/@anvil/common/src/util/__tests__/client-user-agent.test.ts +0 -148
  217. package/vendor/@anvil/common/src/util/__tests__/engagement-tracker.test.ts +0 -115
  218. package/vendor/@anvil/common/src/util/__tests__/env-file-path.test.ts +0 -57
  219. package/vendor/@anvil/common/src/util/__tests__/error-abort.test.ts +0 -774
  220. package/vendor/@anvil/common/src/util/__tests__/error-api-details.test.ts +0 -206
  221. package/vendor/@anvil/common/src/util/__tests__/file-read-limits.test.ts +0 -173
  222. package/vendor/@anvil/common/src/util/__tests__/format-code-search.test.ts +0 -60
  223. package/vendor/@anvil/common/src/util/__tests__/log-mirror.test.ts +0 -31
  224. package/vendor/@anvil/common/src/util/__tests__/messages.test.ts +0 -1252
  225. package/vendor/@anvil/common/src/util/__tests__/partial-json-delta.test.ts +0 -505
  226. package/vendor/@anvil/common/src/util/__tests__/path.test.ts +0 -22
  227. package/vendor/@anvil/common/src/util/__tests__/project-ignore.test.ts +0 -90
  228. package/vendor/@anvil/common/src/util/__tests__/promise.test.ts +0 -325
  229. package/vendor/@anvil/common/src/util/__tests__/rate-limit.test.ts +0 -28
  230. package/vendor/@anvil/common/src/util/__tests__/reddit-anvil-retention.test.ts +0 -130
  231. package/vendor/@anvil/common/src/util/__tests__/saxy.test.ts +0 -1008
  232. package/vendor/@anvil/common/src/util/__tests__/split-data.test.ts +0 -289
  233. package/vendor/@anvil/common/src/util/__tests__/string.test.ts +0 -239
  234. package/vendor/@anvil/common/src/util/__tests__/thread-title.test.ts +0 -70
  235. package/vendor/@anvil/common/src/util/__tests__/tool-result-media-order.test.ts +0 -108
  236. package/vendor/@anvil/common/src/util/__tests__/ttft-histogram.test.ts +0 -180
  237. package/vendor/@anvil/common/src/util/__tests__/with-timeout.test.ts +0 -24
  238. package/vendor/@anvil/common/src/util/__tests__/zoned-time.test.ts +0 -88
  239. package/vendor/@anvil/common/src/util/skills.test.ts +0 -44
  240. package/vendor/@anvil/sdk/dist/index.cjs.map +0 -440
  241. package/vendor/@anvil/sdk/dist/index.mjs.map +0 -440
@@ -1,337 +0,0 @@
1
- /**
2
- * Typed database mock factory for testing.
3
- *
4
- * Provides type-safe mocks for Drizzle database operations used throughout the codebase.
5
- * Replaces the need for `as any` casts when setting up database spies.
6
- *
7
- * @example
8
- * ```typescript
9
- * import { createMockDbOperations, setupDbSpies } from '@anvil/common/testing/mocks/database'
10
- *
11
- * // Option 1: Create mock operations object
12
- * const dbOps = createMockDbOperations()
13
- *
14
- * // Option 2: Setup spies on actual db module
15
- * const spies = setupDbSpies(db)
16
- * await runTest()
17
- * spies.restore()
18
- * ```
19
- */
20
-
21
- import { mock, spyOn } from 'bun:test'
22
-
23
- import type { Mock } from 'bun:test'
24
-
25
- /**
26
- * Type for the chainable insert result.
27
- */
28
- export interface MockInsertResult<T = unknown> {
29
- values: Mock<(data: T | T[]) => Promise<{ id: string }>>
30
- returning: Mock<() => Promise<T[]>>
31
- onConflictDoNothing: Mock<() => MockInsertResult<T>>
32
- onConflictDoUpdate: Mock<
33
- (config: { target: unknown; set: unknown }) => MockInsertResult<T>
34
- >
35
- }
36
-
37
- /**
38
- * Type for the chainable update result.
39
- */
40
- export interface MockUpdateResult<T = unknown> {
41
- set: Mock<(data: Partial<T>) => MockUpdateSetResult>
42
- }
43
-
44
- /**
45
- * Type for the update.set result.
46
- */
47
- export interface MockUpdateSetResult {
48
- where: Mock<(condition: unknown) => Promise<void>>
49
- returning: Mock<() => Promise<unknown[]>>
50
- }
51
-
52
- /**
53
- * Type for the chainable select result.
54
- */
55
- export interface MockSelectResult<T = unknown> {
56
- from: Mock<(table: unknown) => MockSelectFromResult<T>>
57
- }
58
-
59
- /**
60
- * Type for the select.from result.
61
- */
62
- export interface MockSelectFromResult<T = unknown> {
63
- where: Mock<(condition: unknown) => MockSelectWhereResult<T>>
64
- leftJoin: Mock<
65
- (table: unknown, condition: unknown) => MockSelectFromResult<T>
66
- >
67
- innerJoin: Mock<
68
- (table: unknown, condition: unknown) => MockSelectFromResult<T>
69
- >
70
- orderBy: Mock<(...columns: unknown[]) => MockSelectFromResult<T>>
71
- limit: Mock<(n: number) => MockSelectFromResult<T>>
72
- offset: Mock<(n: number) => MockSelectFromResult<T>>
73
- then: Mock<(resolve: (value: T[]) => void) => Promise<T[]>>
74
- }
75
-
76
- /**
77
- * Type for the select.from.where result.
78
- */
79
- export interface MockSelectWhereResult<T = unknown> {
80
- then: Mock<(resolve: (value: T[]) => void) => Promise<T[]>>
81
- leftJoin: Mock<
82
- (table: unknown, condition: unknown) => MockSelectWhereResult<T>
83
- >
84
- innerJoin: Mock<
85
- (table: unknown, condition: unknown) => MockSelectWhereResult<T>
86
- >
87
- orderBy: Mock<(...columns: unknown[]) => MockSelectWhereResult<T>>
88
- limit: Mock<(n: number) => MockSelectWhereResult<T>>
89
- offset: Mock<(n: number) => MockSelectWhereResult<T>>
90
- }
91
-
92
- /**
93
- * Type for the chainable delete result.
94
- */
95
- export interface MockDeleteResult {
96
- where: Mock<(condition: unknown) => Promise<void>>
97
- }
98
-
99
- /**
100
- * Interface for the complete mock database operations.
101
- */
102
- export interface MockDbOperations {
103
- insert: Mock<(table: unknown) => MockInsertResult>
104
- update: Mock<(table: unknown) => MockUpdateResult>
105
- select: Mock<(columns?: unknown) => MockSelectResult>
106
- delete: Mock<(table: unknown) => MockDeleteResult>
107
- transaction: Mock<<T>(fn: (tx: MockDbOperations) => Promise<T>) => Promise<T>>
108
- }
109
-
110
- /**
111
- * Options for creating mock database operations.
112
- */
113
- export interface CreateMockDbOptions {
114
- /**
115
- * Default data to return from select queries.
116
- */
117
- defaultSelectData?: unknown[]
118
-
119
- /**
120
- * Default ID to return from insert operations.
121
- */
122
- defaultInsertId?: string
123
- }
124
-
125
- /**
126
- * Creates type-safe mock database operations for testing.
127
- *
128
- * @param options - Configuration options for the mock
129
- * @returns A mock database operations object
130
- *
131
- * @example
132
- * ```typescript
133
- * const dbOps = createMockDbOperations({
134
- * defaultSelectData: [{ id: '1', name: 'Test' }],
135
- * defaultInsertId: 'new-id',
136
- * })
137
- *
138
- * // The mocks are chainable just like real Drizzle
139
- * await dbOps.insert(users).values({ name: 'Test' })
140
- * await dbOps.select().from(users).where(eq(users.id, '1'))
141
- * ```
142
- */
143
- export function createMockDbOperations(
144
- options: CreateMockDbOptions = {},
145
- ): MockDbOperations {
146
- const { defaultSelectData = [], defaultInsertId = 'mock-id' } = options
147
-
148
- const createMockSelectWhereResult = <T>(
149
- data: T[] = defaultSelectData as T[],
150
- ): MockSelectWhereResult<T> => {
151
- const result: MockSelectWhereResult<T> = {
152
- then: mock((resolve) => {
153
- resolve(data)
154
- return Promise.resolve(data)
155
- }),
156
- leftJoin: mock(() => result),
157
- innerJoin: mock(() => result),
158
- orderBy: mock(() => result),
159
- limit: mock(() => result),
160
- offset: mock(() => result),
161
- }
162
- return result
163
- }
164
-
165
- const createMockSelectFromResult = <T>(
166
- data: T[] = defaultSelectData as T[],
167
- ): MockSelectFromResult<T> => {
168
- const whereResult = createMockSelectWhereResult(data)
169
- const result: MockSelectFromResult<T> = {
170
- where: mock(() => whereResult),
171
- leftJoin: mock(() => result),
172
- innerJoin: mock(() => result),
173
- orderBy: mock(() => result),
174
- limit: mock(() => result),
175
- offset: mock(() => result),
176
- then: mock((resolve) => {
177
- resolve(data)
178
- return Promise.resolve(data)
179
- }),
180
- }
181
- return result
182
- }
183
-
184
- const createMockInsertResult = <T>(): MockInsertResult<T> => {
185
- const result: MockInsertResult<T> = {
186
- values: mock(() => Promise.resolve({ id: defaultInsertId })),
187
- returning: mock(() => Promise.resolve([])),
188
- onConflictDoNothing: mock(() => result),
189
- onConflictDoUpdate: mock(() => result),
190
- }
191
- return result
192
- }
193
-
194
- const createMockUpdateSetResult = (): MockUpdateSetResult => ({
195
- where: mock(() => Promise.resolve()),
196
- returning: mock(() => Promise.resolve([])),
197
- })
198
-
199
- const createMockUpdateResult = <T>(): MockUpdateResult<T> => ({
200
- set: mock(() => createMockUpdateSetResult()),
201
- })
202
-
203
- const createMockDeleteResult = (): MockDeleteResult => ({
204
- where: mock(() => Promise.resolve()),
205
- })
206
-
207
- const dbOps: MockDbOperations = {
208
- insert: mock(() => createMockInsertResult()),
209
- update: mock(() => createMockUpdateResult()),
210
- select: mock(() => ({
211
- from: mock(() => createMockSelectFromResult()),
212
- })),
213
- delete: mock(() => createMockDeleteResult()),
214
- transaction: mock(async (fn) => fn(dbOps)),
215
- }
216
-
217
- return dbOps
218
- }
219
-
220
- /**
221
- * Result of setting up database spies.
222
- */
223
- export interface DbSpies {
224
- /** Spy on insert operations */
225
- insert: ReturnType<typeof spyOn>
226
- /** Spy on update operations */
227
- update: ReturnType<typeof spyOn>
228
- /** Restore all spies */
229
- restore: () => void
230
- /** Clear all spy call history */
231
- clear: () => void
232
- }
233
-
234
- /**
235
- * Sets up spies on a database module for insert and update operations.
236
- * This is the most common pattern used in tests.
237
- *
238
- * @param db - The database module to spy on
239
- * @param options - Configuration options
240
- * @returns Object containing the spies and cleanup utilities
241
- *
242
- * @example
243
- * ```typescript
244
- * const db = createMockDbOperations()
245
- *
246
- * describe('my test', () => {
247
- * let dbSpies: DbSpies
248
- *
249
- * beforeEach(() => {
250
- * dbSpies = setupDbSpies(db)
251
- * })
252
- *
253
- * afterEach(() => {
254
- * dbSpies.restore()
255
- * })
256
- *
257
- * it('inserts data', async () => {
258
- * await createUser({ name: 'Test' })
259
- * expect(dbSpies.insert).toHaveBeenCalled()
260
- * })
261
- * })
262
- * ```
263
- */
264
-
265
- /**
266
- * Sets up spies on a database module for insert and update operations.
267
- * Accepts any object with insert and update methods.
268
- */
269
- export function setupDbSpies(
270
- db: { insert: unknown; update: unknown },
271
- options: CreateMockDbOptions = {},
272
- ): DbSpies {
273
- const { defaultInsertId = 'test-run-id' } = options
274
-
275
- const mockInsertResult = {
276
- values: mock(() => Promise.resolve({ id: defaultInsertId })),
277
- }
278
-
279
- const mockUpdateResult = {
280
- set: mock(() => ({
281
- where: mock(() => Promise.resolve()),
282
- })),
283
- }
284
-
285
- // Cast db to a spyable type - the actual db module has complex types that
286
- // don't play well with spyOn's inference, but the spy still works at runtime
287
- const spyableDb = db as { insert: () => unknown; update: () => unknown }
288
- const insertSpy = spyOn(spyableDb, 'insert').mockReturnValue(mockInsertResult)
289
- const updateSpy = spyOn(spyableDb, 'update').mockReturnValue(mockUpdateResult)
290
-
291
- return {
292
- insert: insertSpy,
293
- update: updateSpy,
294
- restore: () => {
295
- insertSpy.mockRestore()
296
- updateSpy.mockRestore()
297
- },
298
- clear: () => {
299
- insertSpy.mockClear()
300
- updateSpy.mockClear()
301
- },
302
- }
303
- }
304
-
305
- /**
306
- * Creates a mock for a database query builder chain that returns specific data.
307
- *
308
- * @param data - The data to return from the query
309
- * @returns A thenable mock that resolves to the data
310
- *
311
- * @example
312
- * ```typescript
313
- * const mockQuery = createMockQueryResult([
314
- * { id: '1', name: 'User 1' },
315
- * { id: '2', name: 'User 2' },
316
- * ])
317
- *
318
- * spyOn(userService, 'findAll').mockReturnValue(mockQuery)
319
- * ```
320
- */
321
- export function createMockQueryResult<T>(data: T[]): Promise<T[]> & {
322
- where: Mock<() => Promise<T[]>>
323
- orderBy: Mock<() => Promise<T[]>>
324
- limit: Mock<() => Promise<T[]>>
325
- } {
326
- const promise = Promise.resolve(data) as Promise<T[]> & {
327
- where: Mock<() => Promise<T[]>>
328
- orderBy: Mock<() => Promise<T[]>>
329
- limit: Mock<() => Promise<T[]>>
330
- }
331
-
332
- promise.where = mock(() => promise)
333
- promise.orderBy = mock(() => promise)
334
- promise.limit = mock(() => promise)
335
-
336
- return promise
337
- }
@@ -1,219 +0,0 @@
1
- /** Typed fetch mock utilities for testing. */
2
-
3
- import { mock } from 'bun:test'
4
-
5
- import type { Mock } from 'bun:test'
6
-
7
- export interface MockResponseOptions {
8
- status?: number
9
- statusText?: string
10
- headers?: HeadersInit
11
- }
12
-
13
- export type MockFetch = Mock<typeof globalThis.fetch>
14
-
15
- export interface InstallMockFetchResult {
16
- mockFetch: MockFetch
17
- restore: () => void
18
- getCalls: () => MockFetchCall[]
19
- clear: () => void
20
- }
21
-
22
- export interface MockFetchCall {
23
- url: string | URL | Request
24
- init?: RequestInit
25
- jsonBody?: unknown
26
- }
27
-
28
- export interface CreateMockFetchOptions {
29
- defaultImpl?: (
30
- input: RequestInfo | URL,
31
- init?: RequestInit,
32
- ) => Promise<Response>
33
- }
34
-
35
- /** Creates a Response with JSON body. */
36
- export function mockJsonResponse(
37
- data: unknown,
38
- options: MockResponseOptions = {},
39
- ): Response {
40
- const { status = 200, statusText, headers = {} } = options
41
-
42
- return new Response(JSON.stringify(data), {
43
- status,
44
- statusText,
45
- headers: {
46
- 'Content-Type': 'application/json',
47
- ...normalizeHeaders(headers),
48
- },
49
- })
50
- }
51
-
52
- /** Creates a Response with text body. */
53
- export function mockTextResponse(
54
- text: string,
55
- options: MockResponseOptions = {},
56
- ): Response {
57
- const { status = 200, statusText, headers = {} } = options
58
-
59
- return new Response(text, {
60
- status,
61
- statusText,
62
- headers: {
63
- 'Content-Type': 'text/plain',
64
- ...normalizeHeaders(headers),
65
- },
66
- })
67
- }
68
-
69
- /** Creates an error Response with default status text. */
70
- export function mockErrorResponse(
71
- status: number,
72
- body?: string | object,
73
- options: Omit<MockResponseOptions, 'status'> = {},
74
- ): Response {
75
- const { statusText, headers = {} } = options
76
-
77
- let responseBody: string
78
- let contentType: string
79
-
80
- if (body === undefined) {
81
- responseBody = ''
82
- contentType = 'text/plain'
83
- } else if (typeof body === 'string') {
84
- responseBody = body
85
- contentType = 'text/plain'
86
- } else {
87
- responseBody = JSON.stringify(body)
88
- contentType = 'application/json'
89
- }
90
-
91
- return new Response(responseBody, {
92
- status,
93
- statusText: statusText ?? getDefaultStatusText(status),
94
- headers: {
95
- 'Content-Type': contentType,
96
- ...normalizeHeaders(headers),
97
- },
98
- })
99
- }
100
-
101
- /** Creates a mock fetch function. */
102
- export function createMockFetch(
103
- options: CreateMockFetchOptions = {},
104
- ): MockFetch {
105
- const { defaultImpl } = options
106
-
107
- const baseFn =
108
- defaultImpl ??
109
- (async (): Promise<Response> => {
110
- throw new Error('Mock fetch not configured for this call')
111
- })
112
-
113
- const mockFn = Object.assign(mock(baseFn), {
114
- preconnect: mock(async () => {}),
115
- }) as unknown as MockFetch
116
-
117
- return mockFn
118
- }
119
-
120
- /**
121
- * Installs mock fetch globally. Returns mockFetch for configuration -
122
- * the wrapper always captures calls before delegating to mockFetch.
123
- */
124
- export function installMockFetch(
125
- options: CreateMockFetchOptions = {},
126
- ): InstallMockFetchResult {
127
- const originalFetch = globalThis.fetch
128
- const capturedCalls: MockFetchCall[] = []
129
-
130
- const mockFetch = createMockFetch({
131
- defaultImpl:
132
- options.defaultImpl ??
133
- (async (): Promise<Response> => {
134
- throw new Error('Mock fetch not configured for this call')
135
- }),
136
- })
137
-
138
- // Wrap to capture calls
139
- const wrappedMockFn = mock(
140
- async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
141
- const call: MockFetchCall = {
142
- url: input,
143
- init,
144
- }
145
-
146
- // Try to parse JSON body if present
147
- if (init?.body && typeof init.body === 'string') {
148
- try {
149
- call.jsonBody = JSON.parse(init.body)
150
- } catch {
151
- // Not JSON, that's fine
152
- }
153
- }
154
-
155
- capturedCalls.push(call)
156
-
157
- // Call the actual mock implementation
158
- return mockFetch(input, init)
159
- },
160
- )
161
-
162
- const wrappedMock = Object.assign(wrappedMockFn, {
163
- preconnect: mock(async () => {}),
164
- }) as unknown as MockFetch
165
-
166
- ;(globalThis as any).fetch = wrappedMock
167
-
168
- return {
169
- mockFetch,
170
- restore: () => {
171
- globalThis.fetch = originalFetch
172
- },
173
- getCalls: () => [...capturedCalls],
174
- clear: () => {
175
- capturedCalls.length = 0
176
- mockFetch.mockClear()
177
- wrappedMock.mockClear()
178
- },
179
- }
180
- }
181
-
182
- function normalizeHeaders(headers: HeadersInit): Record<string, string> {
183
- if (headers instanceof Headers) {
184
- const result: Record<string, string> = {}
185
- headers.forEach((value, key) => {
186
- result[key] = value
187
- })
188
- return result
189
- }
190
-
191
- if (Array.isArray(headers)) {
192
- return Object.fromEntries(headers)
193
- }
194
-
195
- return headers as Record<string, string>
196
- }
197
-
198
- function getDefaultStatusText(status: number): string {
199
- const statusTexts: Record<number, string> = {
200
- 200: 'OK',
201
- 201: 'Created',
202
- 204: 'No Content',
203
- 400: 'Bad Request',
204
- 401: 'Unauthorized',
205
- 402: 'Payment Required',
206
- 403: 'Forbidden',
207
- 404: 'Not Found',
208
- 405: 'Method Not Allowed',
209
- 409: 'Conflict',
210
- 422: 'Unprocessable Entity',
211
- 429: 'Too Many Requests',
212
- 500: 'Internal Server Error',
213
- 502: 'Bad Gateway',
214
- 503: 'Service Unavailable',
215
- 504: 'Gateway Timeout',
216
- }
217
-
218
- return statusTexts[status] ?? ''
219
- }
@@ -1,166 +0,0 @@
1
- import { mock } from 'bun:test'
2
-
3
- import type { AnvilFileSystem } from '../../types/filesystem'
4
- import type { Mock } from 'bun:test'
5
- import type { PathLike , Stats } from 'node:fs'
6
-
7
- export interface CreateMockFsOptions {
8
- files?: Record<string, string>
9
- directories?: Record<string, string[]>
10
- readFileImpl?: (path: string) => Promise<string>
11
- readdirImpl?: (path: string) => Promise<string[]>
12
- writeFileImpl?: (path: string, content: string) => Promise<void>
13
- mkdirImpl?: (
14
- path: string,
15
- options?: { recursive?: boolean },
16
- ) => Promise<string | undefined>
17
- statImpl?: (path: string) => Promise<Stats>
18
- }
19
-
20
- export interface MockFs extends AnvilFileSystem {}
21
-
22
- export interface MockFsWithMocks {
23
- readFile: Mock<
24
- (path: PathLike, options?: { encoding?: BufferEncoding }) => Promise<string>
25
- >
26
- readdir: Mock<(path: PathLike) => Promise<string[]>>
27
- writeFile: Mock<(path: PathLike, data: string) => Promise<void>>
28
- mkdir: Mock<
29
- (
30
- path: PathLike,
31
- options?: { recursive?: boolean },
32
- ) => Promise<string | undefined>
33
- >
34
- stat: Mock<(path: PathLike) => Promise<Stats>>
35
- }
36
-
37
- /** Creates a mock filesystem compatible with AnvilFileSystem. */
38
- export function createMockFs(options: CreateMockFsOptions = {}): MockFs {
39
- const {
40
- files = {},
41
- directories = {},
42
- readFileImpl,
43
- readdirImpl,
44
- writeFileImpl,
45
- mkdirImpl,
46
- statImpl,
47
- } = options
48
-
49
- const writtenFiles: Record<string, string> = { ...files }
50
- const createdDirs: Set<string> = new Set(Object.keys(directories))
51
-
52
- const defaultReadFile = async (path: PathLike): Promise<string> => {
53
- const pathStr = String(path)
54
- if (pathStr in writtenFiles) {
55
- return writtenFiles[pathStr]
56
- }
57
- throw new Error(`File not found: ${pathStr}`)
58
- }
59
-
60
- const defaultReaddir = async (path: PathLike): Promise<string[]> => {
61
- const pathStr = String(path)
62
- if (pathStr in directories) {
63
- return directories[pathStr]
64
- }
65
- throw new Error(`Directory not found: ${pathStr}`)
66
- }
67
-
68
- const defaultWriteFile = async (
69
- path: PathLike,
70
- data: string,
71
- ): Promise<void> => {
72
- const pathStr = String(path)
73
- writtenFiles[pathStr] = data
74
- }
75
-
76
- const defaultMkdir = async (path: PathLike): Promise<string | undefined> => {
77
- const pathStr = String(path)
78
- createdDirs.add(pathStr)
79
- return undefined
80
- }
81
-
82
- const defaultStat = async (path: PathLike): Promise<Stats> => {
83
- const pathStr = String(path)
84
- const isFile = pathStr in writtenFiles
85
- const isDir = pathStr in directories || createdDirs.has(pathStr)
86
-
87
- if (!isFile && !isDir) {
88
- throw new Error(`Path not found: ${pathStr}`)
89
- }
90
-
91
- return {
92
- isFile: () => isFile,
93
- isDirectory: () => isDir,
94
- isBlockDevice: () => false,
95
- isCharacterDevice: () => false,
96
- isSymbolicLink: () => false,
97
- isFIFO: () => false,
98
- isSocket: () => false,
99
- dev: 0,
100
- ino: 0,
101
- mode: isDir ? 0o755 : 0o644,
102
- nlink: 1,
103
- uid: 0,
104
- gid: 0,
105
- rdev: 0,
106
- size: isFile ? writtenFiles[pathStr].length : 0,
107
- blksize: 4096,
108
- blocks: 0,
109
- atimeMs: Date.now(),
110
- mtimeMs: Date.now(),
111
- ctimeMs: Date.now(),
112
- birthtimeMs: Date.now(),
113
- atime: new Date(),
114
- mtime: new Date(),
115
- ctime: new Date(),
116
- birthtime: new Date(),
117
- } as Stats
118
- }
119
-
120
- const readFileFn = readFileImpl
121
- ? async (path: PathLike) => readFileImpl(String(path))
122
- : defaultReadFile
123
-
124
- const readdirFn = readdirImpl
125
- ? async (path: PathLike) => readdirImpl(String(path))
126
- : defaultReaddir
127
-
128
- const writeFileFn = writeFileImpl
129
- ? async (path: PathLike, data: string) => writeFileImpl(String(path), data)
130
- : defaultWriteFile
131
-
132
- const mkdirFn = mkdirImpl
133
- ? async (path: PathLike, opts?: { recursive?: boolean }) =>
134
- mkdirImpl(String(path), opts)
135
- : defaultMkdir
136
-
137
- const statFn = statImpl
138
- ? async (path: PathLike) => statImpl(String(path))
139
- : defaultStat
140
-
141
- return {
142
- readFile: mock(readFileFn),
143
- readdir: mock(readdirFn),
144
- writeFile: mock(writeFileFn),
145
- mkdir: mock(mkdirFn),
146
- stat: mock(statFn),
147
- } as unknown as MockFs
148
- }
149
-
150
- export function restoreMockFs(mockFs: MockFs): void {
151
- const mocks = mockFs as unknown as MockFsWithMocks
152
- mocks.readFile.mockRestore()
153
- mocks.readdir.mockRestore()
154
- mocks.writeFile.mockRestore()
155
- mocks.mkdir.mockRestore()
156
- mocks.stat.mockRestore()
157
- }
158
-
159
- export function clearMockFs(mockFs: MockFs): void {
160
- const mocks = mockFs as unknown as MockFsWithMocks
161
- mocks.readFile.mockClear()
162
- mocks.readdir.mockClear()
163
- mocks.writeFile.mockClear()
164
- mocks.mkdir.mockClear()
165
- mocks.stat.mockClear()
166
- }