@arkiv-network/sdk 0.7.0-dev.1 → 0.8.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/README.md +29 -16
  2. package/dist/attr/index.cjs +34 -0
  3. package/dist/attr/index.d.cts +2 -0
  4. package/dist/attr/index.d.ts +2 -0
  5. package/dist/attr/index.js +3 -0
  6. package/dist/attr-BDZ-APg5.js +809 -0
  7. package/dist/attr-BDZ-APg5.js.map +1 -0
  8. package/dist/attr-BvxHN99W.cjs +1029 -0
  9. package/dist/chains/index.cjs +2 -3
  10. package/dist/chains/index.d.cts +22 -60
  11. package/dist/chains/index.d.cts.map +1 -1
  12. package/dist/chains/index.d.ts +22 -60
  13. package/dist/chains/index.d.ts.map +1 -1
  14. package/dist/chains/index.js +2 -2
  15. package/dist/chains-BJrZs6sG.js +57 -0
  16. package/dist/chains-BJrZs6sG.js.map +1 -0
  17. package/dist/chains-BYRTTipa.cjs +73 -0
  18. package/dist/changeOwnership-B7bEgla4.d.cts +509 -0
  19. package/dist/changeOwnership-B7bEgla4.d.cts.map +1 -0
  20. package/dist/changeOwnership-DwgtdSFc.d.ts +509 -0
  21. package/dist/changeOwnership-DwgtdSFc.d.ts.map +1 -0
  22. package/dist/entity/index.cjs +19 -0
  23. package/dist/entity/index.d.cts +4 -0
  24. package/dist/entity/index.d.ts +4 -0
  25. package/dist/entity/index.js +6 -0
  26. package/dist/entity-B0t6sN7F.cjs +71 -0
  27. package/dist/entity-BIp_df9u.js +67 -0
  28. package/dist/entity-BIp_df9u.js.map +1 -0
  29. package/dist/entity-CFhbAv0R.js +70 -0
  30. package/dist/entity-CFhbAv0R.js.map +1 -0
  31. package/dist/entity-wfBnZd91.cjs +98 -0
  32. package/dist/errors-D-eJkGVc.cjs +56 -0
  33. package/dist/errors-DGB8AgT2.js +33 -0
  34. package/dist/errors-DGB8AgT2.js.map +1 -0
  35. package/dist/expiry-BUYIXTGb.cjs +135 -0
  36. package/dist/expiry-BxOlvVWC.d.ts +79 -0
  37. package/dist/expiry-BxOlvVWC.d.ts.map +1 -0
  38. package/dist/expiry-DGRzrSan.js +95 -0
  39. package/dist/expiry-DGRzrSan.js.map +1 -0
  40. package/dist/expiry-DRV29F1r.d.cts +79 -0
  41. package/dist/expiry-DRV29F1r.d.cts.map +1 -0
  42. package/dist/flags-0Vl5rrHq.d.cts +41 -0
  43. package/dist/flags-0Vl5rrHq.d.cts.map +1 -0
  44. package/dist/flags-Ct8k0gpB.cjs +43 -0
  45. package/dist/flags-ZtlvOIIk.js +33 -0
  46. package/dist/flags-ZtlvOIIk.js.map +1 -0
  47. package/dist/flags-wmcMoAnN.d.ts +41 -0
  48. package/dist/flags-wmcMoAnN.d.ts.map +1 -0
  49. package/dist/index-Cgw1u5aY.d.cts +278 -0
  50. package/dist/index-Cgw1u5aY.d.cts.map +1 -0
  51. package/dist/index-ClgIuxJ8.d.ts +159 -0
  52. package/dist/index-ClgIuxJ8.d.ts.map +1 -0
  53. package/dist/index-D0CmtiWT.d.cts +159 -0
  54. package/dist/index-D0CmtiWT.d.cts.map +1 -0
  55. package/dist/index-D8LZt-3J.d.cts +417 -0
  56. package/dist/index-D8LZt-3J.d.cts.map +1 -0
  57. package/dist/index-DVYFaXNV.d.ts +417 -0
  58. package/dist/index-DVYFaXNV.d.ts.map +1 -0
  59. package/dist/index-VCqgn29D.d.ts +278 -0
  60. package/dist/index-VCqgn29D.d.ts.map +1 -0
  61. package/dist/index.cjs +676 -375
  62. package/dist/index.d.cts +247 -208
  63. package/dist/index.d.cts.map +1 -1
  64. package/dist/index.d.ts +247 -208
  65. package/dist/index.d.ts.map +1 -1
  66. package/dist/index.js +626 -368
  67. package/dist/index.js.map +1 -1
  68. package/dist/payload-Bnw1hrvb.cjs +83 -0
  69. package/dist/payload-CIxSn_DW.js +67 -0
  70. package/dist/payload-CIxSn_DW.js.map +1 -0
  71. package/dist/payload-Dgx3PQeK.d.ts +94 -0
  72. package/dist/payload-Dgx3PQeK.d.ts.map +1 -0
  73. package/dist/payload-P0nWyjLg.d.cts +94 -0
  74. package/dist/payload-P0nWyjLg.d.cts.map +1 -0
  75. package/dist/query/index.cjs +24 -150
  76. package/dist/query/index.d.cts +5 -3
  77. package/dist/query/index.d.ts +5 -3
  78. package/dist/query/index.js +6 -138
  79. package/dist/queryBuilder-BXDvX1K_.cjs +1123 -0
  80. package/dist/queryBuilder-CbLL4pG5.js +963 -0
  81. package/dist/queryBuilder-CbLL4pG5.js.map +1 -0
  82. package/dist/selection-BvK6yX0g.d.cts +433 -0
  83. package/dist/selection-BvK6yX0g.d.cts.map +1 -0
  84. package/dist/selection-fpmI3ADQ.d.ts +433 -0
  85. package/dist/selection-fpmI3ADQ.d.ts.map +1 -0
  86. package/dist/types/index.cjs +3 -3
  87. package/dist/types/index.d.cts +7 -2
  88. package/dist/types/index.d.ts +7 -2
  89. package/dist/types/index.js +3 -3
  90. package/dist/utils/index.cjs +4 -17
  91. package/dist/utils/index.d.cts +3 -17
  92. package/dist/utils/index.d.ts +3 -17
  93. package/dist/utils/index.js +4 -18
  94. package/package.json +26 -5
  95. package/src/actions/public/getEntity.ts +27 -43
  96. package/src/actions/public/getEntityNonce.ts +30 -0
  97. package/src/actions/public/predictEntityKeys.test.ts +143 -0
  98. package/src/actions/public/predictEntityKeys.ts +138 -0
  99. package/src/actions/public/query.ts +33 -69
  100. package/src/actions/public/watchEntityEvents.test.ts +414 -0
  101. package/src/actions/public/watchEntityEvents.ts +225 -0
  102. package/src/actions/wallet/changeOwnership.ts +2 -2
  103. package/src/actions/wallet/createEntity.ts +99 -24
  104. package/src/actions/wallet/extendEntity.ts +37 -13
  105. package/src/actions/wallet/mutateEntities.test.ts +76 -0
  106. package/src/actions/wallet/mutateEntities.ts +19 -21
  107. package/src/actions/wallet/patchEntity.ts +90 -0
  108. package/src/attr/attributes.test.ts +120 -0
  109. package/src/attr/attributes.ts +190 -0
  110. package/src/attr/codec.test.ts +162 -0
  111. package/src/attr/codec.ts +220 -0
  112. package/src/attr/decimal.ts +86 -0
  113. package/src/attr/errors.ts +145 -0
  114. package/src/attr/index.ts +50 -0
  115. package/src/attr/names.ts +106 -0
  116. package/src/attr/types.ts +150 -0
  117. package/src/attr/values.test.ts +271 -0
  118. package/src/attr/values.ts +502 -0
  119. package/src/chains/cheesecake.ts +22 -0
  120. package/src/chains/index.ts +1 -2
  121. package/src/clients/createPublicClient.test.ts +3 -5
  122. package/src/clients/createPublicClient.ts +5 -4
  123. package/src/clients/createWalletClient.test.ts +2 -2
  124. package/src/clients/createWalletClient.ts +13 -7
  125. package/src/clients/decorators/arkivPublic.ts +160 -139
  126. package/src/clients/decorators/arkivWallet.ts +86 -94
  127. package/src/entity/create.test.ts +184 -0
  128. package/src/entity/errors.ts +38 -0
  129. package/src/entity/events.ts +16 -0
  130. package/src/entity/expiry.test.ts +195 -0
  131. package/src/entity/expiry.ts +181 -0
  132. package/src/entity/flags.ts +65 -0
  133. package/src/entity/index.ts +14 -0
  134. package/src/entity/key.ts +68 -0
  135. package/src/entity/operations.ts +134 -0
  136. package/src/entity/params.ts +15 -0
  137. package/src/errors.ts +26 -49
  138. package/src/index.ts +6 -0
  139. package/src/query/engine.ts +59 -139
  140. package/src/query/errors.ts +151 -0
  141. package/src/query/expression.test.ts +286 -0
  142. package/src/query/expression.ts +493 -0
  143. package/src/query/index.ts +40 -4
  144. package/src/query/injection.test.ts +151 -0
  145. package/src/query/queryBuilder.test.ts +322 -637
  146. package/src/query/queryBuilder.ts +126 -359
  147. package/src/query/queryResult.ts +54 -51
  148. package/src/query/selection.test.ts +48 -98
  149. package/src/query/selection.ts +77 -65
  150. package/src/types/entity.test.ts +44 -155
  151. package/src/types/entity.ts +60 -52
  152. package/src/types/events.ts +59 -16
  153. package/src/types/index.ts +20 -17
  154. package/src/types/rpcSchema.ts +69 -37
  155. package/src/utils/arkivTransactions.test.ts +600 -155
  156. package/src/utils/arkivTransactions.ts +181 -190
  157. package/src/utils/chains.test.ts +21 -24
  158. package/src/utils/compression.ts +1 -1
  159. package/src/utils/entities.test.ts +62 -0
  160. package/src/utils/entities.ts +109 -37
  161. package/src/utils/expirationTime.ts +114 -10
  162. package/src/utils/logger.ts +2 -2
  163. package/src/utils/revert.test.ts +99 -0
  164. package/src/utils/revert.ts +145 -0
  165. package/dist/chains-BifaY04V.cjs +0 -108
  166. package/dist/chains-CrtkVbBh.js +0 -86
  167. package/dist/chains-CrtkVbBh.js.map +0 -1
  168. package/dist/consts-Bz0LZQZY.js +0 -7
  169. package/dist/consts-Bz0LZQZY.js.map +0 -1
  170. package/dist/consts-CxT1-RXU.cjs +0 -18
  171. package/dist/entity-BhSXaB23.cjs +0 -81
  172. package/dist/entity-CN41IOrq.d.cts +0 -401
  173. package/dist/entity-CN41IOrq.d.cts.map +0 -1
  174. package/dist/entity-DGfVyB-a.js +0 -71
  175. package/dist/entity-DGfVyB-a.js.map +0 -1
  176. package/dist/entity-NnEVlePN.d.ts +0 -401
  177. package/dist/entity-NnEVlePN.d.ts.map +0 -1
  178. package/dist/index-ClkpS6s_.d.ts +0 -481
  179. package/dist/index-ClkpS6s_.d.ts.map +0 -1
  180. package/dist/index-w4qop0Pe.d.cts +0 -481
  181. package/dist/index-w4qop0Pe.d.cts.map +0 -1
  182. package/dist/payload-BpNUPOnY.js +0 -13
  183. package/dist/payload-BpNUPOnY.js.map +0 -1
  184. package/dist/payload-C2KHOUoj.d.ts +0 -6
  185. package/dist/payload-C2KHOUoj.d.ts.map +0 -1
  186. package/dist/payload-CJN8fTvX.d.cts +0 -6
  187. package/dist/payload-CJN8fTvX.d.cts.map +0 -1
  188. package/dist/payload-D8YLKcJj.cjs +0 -23
  189. package/dist/query/index.js.map +0 -1
  190. package/dist/queryBuilder-BE_XhLLa.js +0 -735
  191. package/dist/queryBuilder-BE_XhLLa.js.map +0 -1
  192. package/dist/queryBuilder-J37buGCi.cjs +0 -865
  193. package/dist/utils/index.d.cts.map +0 -1
  194. package/dist/utils/index.d.ts.map +0 -1
  195. package/dist/utils/index.js.map +0 -1
  196. package/src/actions/public/buildQuery.ts +0 -10
  197. package/src/actions/public/subscribeEntityEvents.ts +0 -93
  198. package/src/actions/wallet/updateEntity.ts +0 -59
  199. package/src/chains/braga.ts +0 -26
  200. package/src/chains/kaolin.ts +0 -29
  201. package/src/query/engine.test.ts +0 -571
  202. package/src/query/predicate.test.ts +0 -461
  203. package/src/query/predicate.ts +0 -144
  204. package/src/query/queryResult.test.ts +0 -270
  205. package/src/query/selectQueryBuilder.test.ts +0 -213
  206. package/src/types/attributes.ts +0 -28
  207. package/src/utils/validation.test.ts +0 -106
  208. package/src/utils/validation.ts +0 -80
  209. /package/dist/{types-Ci4BRClw.js → types-Bx1EKe0v.js} +0 -0
  210. /package/dist/{types-a8gm_IaQ.cjs → types-CKPddlfS.cjs} +0 -0
@@ -1,270 +0,0 @@
1
- import { beforeEach, describe, expect, jest, test } from "bun:test"
2
- import { NoCursorOrLimitError, NoMoreResultsError } from "../errors"
3
- import { Entity } from "../types/entity"
4
- import type { QueryBuilder } from "./queryBuilder"
5
- import { QueryResult } from "./queryResult"
6
-
7
- describe("QueryResult", () => {
8
- let mockQueryBuilder: QueryBuilder
9
- let mockEntities: Entity[]
10
-
11
- beforeEach(() => {
12
- // Create mock entities
13
- mockEntities = [
14
- new Entity("0x1" as const, "0xowner1" as const, 1000n, new Uint8Array([1]), []),
15
- new Entity("0x2" as const, "0xowner2" as const, 2000n, new Uint8Array([2]), []),
16
- new Entity("0x3" as const, "0xowner3" as const, 3000n, new Uint8Array([3]), []),
17
- ]
18
-
19
- // Create mock query builder
20
- mockQueryBuilder = {
21
- cursor: jest.fn().mockReturnThis(),
22
- fetch: jest.fn(),
23
- } as unknown as QueryBuilder
24
- })
25
-
26
- describe("constructor", () => {
27
- test("creates QueryResult with entities and query builder", () => {
28
- const result = new QueryResult(
29
- mockEntities,
30
- mockQueryBuilder,
31
- undefined,
32
- undefined,
33
- undefined,
34
- )
35
-
36
- expect(result.entities).toEqual(mockEntities)
37
- expect(result.entities).toHaveLength(3)
38
- })
39
-
40
- test("creates QueryResult with limit and cursor", () => {
41
- const result = new QueryResult(mockEntities, mockQueryBuilder, "0xABC123", 10, undefined)
42
-
43
- expect(result.entities).toEqual(mockEntities)
44
- })
45
-
46
- test("creates QueryResult with undefined limit and cursor", () => {
47
- const result = new QueryResult(
48
- mockEntities,
49
- mockQueryBuilder,
50
- undefined,
51
- undefined,
52
- undefined,
53
- )
54
-
55
- expect(result.entities).toEqual(mockEntities)
56
- })
57
-
58
- test("creates QueryResult with empty entities array", () => {
59
- const result = new QueryResult([], mockQueryBuilder, "0xABC123", 10, undefined)
60
-
61
- expect(result.entities).toEqual([])
62
- expect(result.entities).toHaveLength(0)
63
- })
64
- })
65
-
66
- describe("next()", () => {
67
- test("throws NoCursorOrLimitError when cursor is undefined", async () => {
68
- const result = new QueryResult(mockEntities, mockQueryBuilder, undefined, 10, undefined)
69
-
70
- await expect(result.next()).rejects.toThrow(NoCursorOrLimitError)
71
- await expect(result.next()).rejects.toThrow("Cursor and limit must be defined to fetch next")
72
- })
73
-
74
- test("throws NoCursorOrLimitError when limit is undefined", async () => {
75
- const result = new QueryResult(
76
- mockEntities,
77
- mockQueryBuilder,
78
- "0xABC123",
79
- undefined,
80
- undefined,
81
- )
82
-
83
- await expect(result.next()).rejects.toThrow(NoCursorOrLimitError)
84
- await expect(result.next()).rejects.toThrow("Cursor and limit must be defined to fetch next")
85
- })
86
-
87
- test("throws NoCursorOrLimitError when both cursor and limit are undefined", async () => {
88
- const result = new QueryResult(
89
- mockEntities,
90
- mockQueryBuilder,
91
- undefined,
92
- undefined,
93
- undefined,
94
- )
95
-
96
- await expect(result.next()).rejects.toThrow(NoCursorOrLimitError)
97
- })
98
-
99
- test("throws NoMoreResultsError when limit > entities.length", async () => {
100
- // limit is 4 but we have 3 entities, so limit > entities.length triggers error
101
- const result = new QueryResult(mockEntities, mockQueryBuilder, "0xABC123", 4, 123n)
102
-
103
- await expect(result.next()).rejects.toThrow(NoMoreResultsError)
104
- })
105
-
106
- test("fetches next page when limit equals entities length", async () => {
107
- const newEntities = [
108
- new Entity("0x4" as const, "0xowner4" as const, 4000n, new Uint8Array([4]), []),
109
- new Entity("0x5" as const, "0xowner5" as const, 5000n, new Uint8Array([5]), []),
110
- new Entity("0x6" as const, "0xowner6" as const, 6000n, new Uint8Array([6]), []),
111
- ]
112
-
113
- const mockFetch = jest.fn().mockResolvedValue({
114
- entities: newEntities,
115
- })
116
-
117
- mockQueryBuilder.fetch = mockFetch
118
- mockQueryBuilder.cursor = jest.fn().mockReturnValue(mockQueryBuilder)
119
-
120
- // limit is 3 and we have 3 entities, so there might be more
121
- const result = new QueryResult(mockEntities, mockQueryBuilder, "0xABC123", 3, undefined)
122
-
123
- await result.next()
124
-
125
- expect(mockQueryBuilder.cursor).toHaveBeenCalledWith("0xABC123")
126
- expect(mockFetch).toHaveBeenCalled()
127
- expect(result.entities).toEqual(newEntities)
128
- })
129
-
130
- test("updates offset correctly for multiple next calls", async () => {
131
- const initialEntities = [
132
- new Entity("0x1" as const, "0xowner1" as const, 1000n, new Uint8Array([1]), []),
133
- new Entity("0x2" as const, "0xowner2" as const, 2000n, new Uint8Array([2]), []),
134
- new Entity("0x3" as const, "0xowner3" as const, 3000n, new Uint8Array([3]), []),
135
- ]
136
-
137
- const newEntities1 = [
138
- new Entity("0x4" as const, "0xowner4" as const, 4000n, new Uint8Array([4]), []),
139
- new Entity("0x5" as const, "0xowner5" as const, 5000n, new Uint8Array([5]), []),
140
- new Entity("0x6" as const, "0xowner6" as const, 6000n, new Uint8Array([6]), []),
141
- ]
142
-
143
- const newEntities2 = [
144
- new Entity("0x7" as const, "0xowner7" as const, 7000n, new Uint8Array([7]), []),
145
- new Entity("0x8" as const, "0xowner8" as const, 8000n, new Uint8Array([8]), []),
146
- new Entity("0x9" as const, "0xowner9" as const, 9000n, new Uint8Array([9]), []),
147
- ]
148
-
149
- const mockCursor = jest.fn().mockReturnValue(mockQueryBuilder)
150
- const mockFetch = jest
151
- .fn()
152
- .mockResolvedValueOnce({
153
- entities: newEntities1,
154
- queryBuilder: mockQueryBuilder,
155
- cursor: "0xABC123",
156
- })
157
- .mockResolvedValueOnce({
158
- entities: newEntities2,
159
- queryBuilder: mockQueryBuilder,
160
- cursor: "0xABC123",
161
- })
162
-
163
- mockQueryBuilder.cursor = mockCursor
164
- mockQueryBuilder.fetch = mockFetch
165
-
166
- // limit = 3, entities.length = 3, so 3 < 3 is false (won't throw)
167
- const result = new QueryResult(initialEntities, mockQueryBuilder, "0xABC123", 3, undefined)
168
-
169
- // First next call - offset should be 0 + 3 = 3
170
- await result.next()
171
- expect(mockCursor).toHaveBeenCalledWith("0xABC123")
172
- expect(result.entities).toEqual(newEntities1)
173
-
174
- // Second next call - offset should be 3 + 3 = 6
175
- await result.next()
176
- expect(mockCursor).toHaveBeenCalledWith("0xABC123")
177
- expect(result.entities).toEqual(newEntities2)
178
- })
179
-
180
- test("fetches next page when limit is greater than entities length", async () => {
181
- // limit is 5 but only 3 entities (5 > 3) so there is no more results
182
- // so NoMoreResultsError is thrown
183
- const result = new QueryResult(mockEntities, mockQueryBuilder, "0xABC123", 5, undefined)
184
-
185
- await expect(result.next()).rejects.toThrow(NoMoreResultsError)
186
- })
187
- })
188
-
189
- describe("pagination flow", () => {
190
- test("simulates complete pagination: next -> next -> previous -> previous", async () => {
191
- const page1 = [
192
- new Entity("0x1" as const, "0xowner1" as const, 1000n, new Uint8Array([1]), []),
193
- ]
194
- const page2 = [
195
- new Entity("0x2" as const, "0xowner2" as const, 2000n, new Uint8Array([2]), []),
196
- ]
197
- const page3 = [
198
- new Entity("0x3" as const, "0xowner3" as const, 3000n, new Uint8Array([3]), []),
199
- ]
200
-
201
- const mockCursor = jest.fn().mockReturnValue(mockQueryBuilder)
202
- const mockFetch = jest
203
- .fn()
204
- .mockResolvedValueOnce({
205
- entities: page2,
206
- queryBuilder: mockQueryBuilder,
207
- cursor: "0xABC123",
208
- }) // first next
209
- .mockResolvedValueOnce({
210
- entities: page3,
211
- queryBuilder: mockQueryBuilder,
212
- cursor: "0xABC123",
213
- }) // second next
214
- .mockResolvedValueOnce({
215
- entities: page2,
216
- queryBuilder: mockQueryBuilder,
217
- cursor: "0xABC123",
218
- }) // first previous
219
- .mockResolvedValueOnce({
220
- entities: page1,
221
- queryBuilder: mockQueryBuilder,
222
- cursor: "0xABC123",
223
- }) // second previous
224
-
225
- mockQueryBuilder.cursor = mockCursor
226
- mockQueryBuilder.fetch = mockFetch
227
-
228
- const result = new QueryResult(page1, mockQueryBuilder, "0xABC123", 1, undefined)
229
-
230
- // Start at page 1 (offset 0)
231
- expect(result.entities).toEqual(page1)
232
-
233
- // Next to page 2 (offset 1)
234
- await result.next()
235
- expect(mockCursor).toHaveBeenCalledWith("0xABC123")
236
- expect(result.entities).toEqual(page2)
237
-
238
- // Next to page 3 (offset 2)
239
- await result.next()
240
- expect(mockCursor).toHaveBeenCalledWith("0xABC123")
241
- expect(result.entities).toEqual(page3)
242
- })
243
-
244
- test("There is another page to fetch when limit < entities.length", async () => {
245
- // limit is 2 but 3 entities, so 2 < 3 there is another page to fetch
246
- const mockCursor = jest.fn().mockReturnValue(mockQueryBuilder)
247
- const mockFetch = jest
248
- .fn()
249
- .mockResolvedValueOnce({
250
- entities: mockEntities,
251
- queryBuilder: mockQueryBuilder,
252
- cursor: "0xABC123",
253
- })
254
- .mockResolvedValueOnce({
255
- entities: mockEntities,
256
- queryBuilder: mockQueryBuilder,
257
- cursor: "0xABC123",
258
- })
259
-
260
- mockQueryBuilder.cursor = mockCursor
261
- mockQueryBuilder.fetch = mockFetch
262
-
263
- const result = new QueryResult(mockEntities, mockQueryBuilder, "0xABC123", 2, undefined)
264
-
265
- await result.next()
266
- expect(mockCursor).toHaveBeenCalledWith("0xABC123")
267
- expect(result.entities).toEqual(mockEntities)
268
- })
269
- })
270
- })
@@ -1,213 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, jest, test } from "bun:test"
2
- import type { ArkivClient } from "../clients/baseClient"
3
- import type { Entity } from "../types/entity"
4
- import { RpcAttributeValueType } from "../types/rpcSchema"
5
- import * as entitiesUtils from "../utils/entities"
6
- import * as engine from "./engine"
7
- import { eq, gte } from "./predicate"
8
- import { SelectQueryBuilder } from "./queryBuilder"
9
- import { selectionToIncludeData } from "./selection"
10
-
11
- describe("SelectQueryBuilder", () => {
12
- let mockClient: ArkivClient
13
- let mockProcessQuery: any
14
-
15
- beforeEach(() => {
16
- mockClient = {
17
- request: jest.fn(),
18
- } as unknown as ArkivClient
19
-
20
- mockProcessQuery = jest.spyOn(engine, "processQuery")
21
- jest.spyOn(entitiesUtils, "entityFromRpcResult")
22
- })
23
-
24
- afterEach(() => {
25
- jest.restoreAllMocks()
26
- })
27
-
28
- test("select() with no argument forwards include-data for everything", async () => {
29
- mockProcessQuery.mockResolvedValue({ data: [] })
30
-
31
- await new SelectQueryBuilder(mockClient).where(eq("name", "test")).fetch()
32
-
33
- expect(mockProcessQuery).toHaveBeenCalledWith(
34
- mockClient,
35
- expect.objectContaining({
36
- predicates: [eq("name", "test")],
37
- includeData: selectionToIncludeData(),
38
- }),
39
- )
40
- })
41
-
42
- test('select("*") forwards include-data for everything', async () => {
43
- mockProcessQuery.mockResolvedValue({ data: [] })
44
-
45
- await new SelectQueryBuilder(mockClient, "*").fetch()
46
-
47
- expect(mockProcessQuery).toHaveBeenCalledWith(
48
- mockClient,
49
- expect.objectContaining({
50
- includeData: selectionToIncludeData("*"),
51
- }),
52
- )
53
- })
54
-
55
- test("select({ owner: true, attributes: true }) forwards the resolved include-data", async () => {
56
- mockProcessQuery.mockResolvedValue({ data: [] })
57
-
58
- await new SelectQueryBuilder(mockClient, { owner: true, attributes: true }).fetch()
59
-
60
- expect(mockProcessQuery).toHaveBeenCalledWith(
61
- mockClient,
62
- expect.objectContaining({
63
- includeData: selectionToIncludeData({ owner: true, attributes: true }),
64
- }),
65
- )
66
- })
67
-
68
- test("select({ owner: true }) forwards narrowed include-data", async () => {
69
- mockProcessQuery.mockResolvedValue({ data: [] })
70
-
71
- await new SelectQueryBuilder(mockClient, { owner: true }).fetch()
72
-
73
- const call = mockProcessQuery.mock.calls[0][1]
74
- expect(call.includeData).toEqual(selectionToIncludeData({ owner: true }))
75
- expect(call.includeData.owner).toBe(true)
76
- expect(call.includeData.creator).toBe(false)
77
- // key is opt-in: not selected here, so it is excluded
78
- expect(call.includeData.key).toBe(false)
79
- })
80
-
81
- test("does not forward the with* boolean flags", async () => {
82
- mockProcessQuery.mockResolvedValue({ data: [] })
83
-
84
- await new SelectQueryBuilder(mockClient, { attributes: true }).fetch()
85
-
86
- const call = mockProcessQuery.mock.calls[0][1]
87
- expect(call.withAttributes).toBeUndefined()
88
- expect(call.withMetadata).toBeUndefined()
89
- expect(call.withPayload).toBeUndefined()
90
- })
91
-
92
- test("shares the filtering/ordering/pagination chain methods", async () => {
93
- mockProcessQuery.mockResolvedValue({ data: [] })
94
-
95
- const owner = "0x1234567890123456789012345678901234567890" as const
96
- await new SelectQueryBuilder(mockClient, { attributes: true })
97
- .where(eq("name", "test"))
98
- .where(gte("age", 18))
99
- .ownedBy(owner)
100
- .createdBy(owner)
101
- .orderBy("name", "string", "desc")
102
- .limit(10)
103
- .cursor("0xABC123")
104
- .validAtBlock(123n)
105
- .fetch()
106
-
107
- expect(mockProcessQuery).toHaveBeenCalledWith(mockClient, {
108
- predicates: [eq("name", "test"), gte("age", 18)],
109
- limit: 10,
110
- cursor: "0xABC123",
111
- ownedBy: owner,
112
- createdBy: owner,
113
- orderBy: [{ name: "name", type: "string", desc: true }],
114
- validAtBlock: 123n,
115
- includeData: selectionToIncludeData({ attributes: true }),
116
- })
117
- })
118
-
119
- test("count() ignores the selection and requests no data", async () => {
120
- mockProcessQuery.mockResolvedValue({
121
- data: [
122
- { key: "0xabc", value: "data1" },
123
- { key: "0xdef", value: "data2" },
124
- ],
125
- })
126
-
127
- const count = await new SelectQueryBuilder(mockClient, "*")
128
- .where(eq("status", "active"))
129
- .count()
130
-
131
- expect(count).toBe(2)
132
- expect(mockProcessQuery).toHaveBeenCalledWith(mockClient, {
133
- predicates: [eq("status", "active")],
134
- limit: undefined,
135
- cursor: undefined,
136
- ownedBy: undefined,
137
- createdBy: undefined,
138
- orderBy: undefined,
139
- validAtBlock: undefined,
140
- withAttributes: false,
141
- withMetadata: false,
142
- withPayload: false,
143
- })
144
- })
145
-
146
- describe("result entities", () => {
147
- test("fetch returns flat Entity instances with the selected data and methods", async () => {
148
- mockProcessQuery.mockResolvedValue({
149
- data: [
150
- {
151
- key: "0xabc",
152
- owner: "0xowner",
153
- value: "0x68656c6c6f", // "hello"
154
- attributes: [
155
- { key: "category", value: "docs", valueType: RpcAttributeValueType.String },
156
- { key: "score", value: "0x2a", valueType: RpcAttributeValueType.Uint },
157
- ],
158
- },
159
- ],
160
- cursor: "",
161
- blockNumber: "0x1",
162
- })
163
-
164
- const result = await new SelectQueryBuilder(mockClient, {
165
- key: true,
166
- owner: true,
167
- attributes: true,
168
- payload: true,
169
- }).fetch()
170
-
171
- expect(result.entities).toHaveLength(1)
172
- const entity = result.entities[0] as unknown as Entity
173
- // flat, backwards-compatible field access
174
- expect(entity.key).toBe("0xabc")
175
- expect(entity.owner).toBe("0xowner")
176
- expect(entity.attributes).toEqual([
177
- { key: "category", value: "docs" },
178
- { key: "score", value: 42 },
179
- ])
180
- // entity methods are preserved on the result
181
- expect(typeof entity.toText).toBe("function")
182
- expect(entity.toText()).toBe("hello")
183
- })
184
-
185
- test("toJson decodes a JSON payload", async () => {
186
- mockProcessQuery.mockResolvedValue({
187
- data: [{ key: "0xabc", value: "0x7b2261223a317d" }], // {"a":1}
188
- cursor: "",
189
- blockNumber: "0x1",
190
- })
191
-
192
- const result = await new SelectQueryBuilder(mockClient, { payload: true }).fetch()
193
- const entity = result.entities[0] as unknown as Entity
194
- expect(entity.toJson()).toEqual({ a: 1 })
195
- })
196
-
197
- test("fields the RPC omits (because they were not selected) are undefined at runtime", async () => {
198
- // The RPC honours includeData and returns only the key.
199
- mockProcessQuery.mockResolvedValue({
200
- data: [{ key: "0xabc" }],
201
- cursor: "",
202
- blockNumber: "0x1",
203
- })
204
-
205
- const result = await new SelectQueryBuilder(mockClient, { key: true }).fetch()
206
- const entity = result.entities[0] as unknown as Entity
207
- expect(entity.key).toBe("0xabc")
208
- expect(entity.owner).toBeUndefined()
209
- expect(entity.payload).toBeUndefined()
210
- expect(entity.attributes).toEqual([])
211
- })
212
- })
213
- })
@@ -1,28 +0,0 @@
1
- import type { Hex } from "viem"
2
-
3
- /**
4
- * An entity attribute (key/value pair).
5
- *
6
- * Arkiv supports string, Hex and number values.
7
- *
8
- * Hex values are stored as the on-chain `EntityKey` type **only when they are
9
- * exactly 32 bytes** (66 characters, e.g. an entity key). Hex-looking strings
10
- * of any other length are stored as plain strings: they round-trip unchanged
11
- * and compare lexicographically, not as hex.
12
- *
13
- * Numeric values **must be non-negative integers** (they are stored on-chain
14
- * as unsigned 256-bit) — passing a non-integer (e.g. `1.5`) or negative number
15
- * throws an {@link InvalidAttributeError} at mutation time. To store a
16
- * non-integer value:
17
- * - Scale it to an integer (e.g. `1.5` -> `1500`, dividing by the same factor on
18
- * read) to keep numeric ordering and range queries working.
19
- * - Or pass it as a string (e.g. `"1.5"`), which sorts lexicographically and so
20
- * does not support numeric comparisons.
21
- *
22
- * To store a negative value, offset it into the non-negative range (subtracting
23
- * the offset on read) or store it as a string.
24
- */
25
- export type Attribute = {
26
- key: string
27
- value: number | string | Hex
28
- }
@@ -1,106 +0,0 @@
1
- import { describe, expect, it } from "bun:test"
2
- import { BLOCK_TIME } from "../consts"
3
- import { InvalidAttributeError, InvalidAttributeKeyError, InvalidExpirationError } from "../errors"
4
- import { validateAttribute, validateAttributeKey, validateExpiresIn } from "./validation"
5
-
6
- describe("validateExpiresIn", () => {
7
- it("accepts positive integers that are a multiple of the block time", () => {
8
- expect(() => validateExpiresIn(BLOCK_TIME)).not.toThrow()
9
- expect(() => validateExpiresIn(1000)).not.toThrow()
10
- expect(() => validateExpiresIn(BLOCK_TIME * 12345)).not.toThrow()
11
- })
12
-
13
- it("throws for non-integer values", () => {
14
- expect(() => validateExpiresIn(51.5)).toThrow(InvalidExpirationError)
15
- expect(() => validateExpiresIn(1.5)).toThrow(InvalidExpirationError)
16
- })
17
-
18
- it("throws for values that are not a multiple of the block time", () => {
19
- expect(() => validateExpiresIn(51)).toThrow(InvalidExpirationError)
20
- expect(() => validateExpiresIn(999)).toThrow(InvalidExpirationError)
21
- expect(() => validateExpiresIn(1)).toThrow(InvalidExpirationError)
22
- })
23
-
24
- it("throws for zero and negative values", () => {
25
- expect(() => validateExpiresIn(0)).toThrow(InvalidExpirationError)
26
- expect(() => validateExpiresIn(-1000)).toThrow(InvalidExpirationError)
27
- })
28
-
29
- it("throws for NaN and non-finite values", () => {
30
- expect(() => validateExpiresIn(Number.NaN)).toThrow(InvalidExpirationError)
31
- expect(() => validateExpiresIn(Number.POSITIVE_INFINITY)).toThrow(InvalidExpirationError)
32
- })
33
-
34
- it("includes the offending value in the error message", () => {
35
- expect(() => validateExpiresIn(51)).toThrow(/51/)
36
- })
37
- })
38
-
39
- describe("validateAttribute", () => {
40
- it("accepts string values", () => {
41
- expect(() => validateAttribute({ key: "k", value: "1.5" })).not.toThrow()
42
- expect(() => validateAttribute({ key: "k", value: "anything" })).not.toThrow()
43
- })
44
-
45
- it("accepts non-negative integer numeric values", () => {
46
- expect(() => validateAttribute({ key: "k", value: 0 })).not.toThrow()
47
- expect(() => validateAttribute({ key: "k", value: 123 })).not.toThrow()
48
- })
49
-
50
- it("throws for non-integer numeric values", () => {
51
- expect(() => validateAttribute({ key: "k", value: 1.5 })).toThrow(InvalidAttributeError)
52
- expect(() => validateAttribute({ key: "k", value: Number.NaN })).toThrow(InvalidAttributeError)
53
- })
54
-
55
- it("throws for negative numeric values", () => {
56
- expect(() => validateAttribute({ key: "k", value: -5 })).toThrow(InvalidAttributeError)
57
- expect(() => validateAttribute({ key: "k", value: -0.5 })).toThrow(InvalidAttributeError)
58
- })
59
-
60
- it("includes the attribute key and value in the error message", () => {
61
- expect(() => validateAttribute({ key: "my_key", value: 1.5 })).toThrow(/my_key/)
62
- expect(() => validateAttribute({ key: "my_key", value: 1.5 })).toThrow(/1\.5/)
63
- })
64
- })
65
-
66
- describe("validateAttributeKey", () => {
67
- it("accepts valid Ident32 keys", () => {
68
- expect(() => validateAttributeKey("a")).not.toThrow()
69
- expect(() => validateAttributeKey("tag")).not.toThrow()
70
- expect(() => validateAttributeKey("num_attr")).not.toThrow()
71
- expect(() => validateAttributeKey("a.b-c_d9")).not.toThrow()
72
- expect(() => validateAttributeKey("a".repeat(32))).not.toThrow()
73
- })
74
-
75
- it("throws for an empty key", () => {
76
- expect(() => validateAttributeKey("")).toThrow(InvalidAttributeKeyError)
77
- })
78
-
79
- it("throws when the key does not start with a lowercase letter", () => {
80
- expect(() => validateAttributeKey("Tag")).toThrow(InvalidAttributeKeyError)
81
- expect(() => validateAttributeKey("1tag")).toThrow(InvalidAttributeKeyError)
82
- expect(() => validateAttributeKey("_tag")).toThrow(InvalidAttributeKeyError)
83
- expect(() => validateAttributeKey(".tag")).toThrow(InvalidAttributeKeyError)
84
- })
85
-
86
- it("throws for invalid characters after the first", () => {
87
- expect(() => validateAttributeKey("strAttr")).toThrow(InvalidAttributeKeyError)
88
- expect(() => validateAttributeKey("has space")).toThrow(InvalidAttributeKeyError)
89
- expect(() => validateAttributeKey("has:colon")).toThrow(InvalidAttributeKeyError)
90
- expect(() => validateAttributeKey("héllo")).toThrow(InvalidAttributeKeyError)
91
- })
92
-
93
- it("throws for keys longer than 32 bytes", () => {
94
- expect(() => validateAttributeKey("a".repeat(33))).toThrow(InvalidAttributeKeyError)
95
- })
96
-
97
- it("names the offending character and position", () => {
98
- expect(() => validateAttributeKey("strAttr")).toThrow(/"A" at position 3/)
99
- })
100
-
101
- it("is applied by validateAttribute", () => {
102
- expect(() => validateAttribute({ key: "strAttr", value: "x" })).toThrow(
103
- InvalidAttributeKeyError,
104
- )
105
- })
106
- })