@yolean/evidence-sdk 0.0.0-stage → 4.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 (186) hide show
  1. package/CHANGELOG.md +538 -0
  2. package/LICENSE.md +9 -0
  3. package/README.md +237 -2
  4. package/package.json +161 -3
  5. package/src/build-dev/cli/add/addServerHook.js +30 -0
  6. package/src/build-dev/cli/add/addTypeRef.js +19 -0
  7. package/src/build-dev/cli/add/updateGitIgnore.js +33 -0
  8. package/src/build-dev/cli/add/updatePackageJson.js +26 -0
  9. package/src/build-dev/cli/add/updateSvelteConfig.js +199 -0
  10. package/src/build-dev/cli/add/updateViteConfig.js +152 -0
  11. package/src/build-dev/cli/add.js +95 -0
  12. package/src/build-dev/cli/build.js +30 -0
  13. package/src/build-dev/cli/dev.js +49 -0
  14. package/src/build-dev/cli/index.js +14 -0
  15. package/src/build-dev/cli/upgrade.js +54 -0
  16. package/src/build-dev/svelte/index.js +54 -0
  17. package/src/build-dev/svelte/lib/isTargetFile.js +6 -0
  18. package/src/build-dev/svelte/metadatas.js +39 -0
  19. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js +29 -0
  20. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.spec.js +82 -0
  21. package/src/build-dev/svelte/processors/addScriptTags.js +17 -0
  22. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.js +19 -0
  23. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.spec.js +123 -0
  24. package/src/build-dev/svelte/processors/convertToHtml/processMarkdown.js +59 -0
  25. package/src/build-dev/svelte/processors/extractQueries/extractQueries.js +49 -0
  26. package/src/build-dev/svelte/processors/extractQueries/extractQueries.spec.js +54 -0
  27. package/src/build-dev/svelte/processors/highlight/highlight.js +52 -0
  28. package/src/build-dev/svelte/processors/highlight/highlight.spec.js +31 -0
  29. package/src/build-dev/svelte/processors/initializeUniversalSql.js +15 -0
  30. package/src/build-dev/svelte/processors/injectComponents/injectComponents.js +56 -0
  31. package/src/build-dev/svelte/processors/injectHighlightStyles.js +14 -0
  32. package/src/build-dev/svelte/processors/injectQueries/injectQueries.js +77 -0
  33. package/src/build-dev/svelte/processors/markRootLayoutPrerenderable.js +56 -0
  34. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.js +17 -0
  35. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.spec.js +21 -0
  36. package/src/build-dev/svelte/processors/transformQueries/transformQueries.js +58 -0
  37. package/src/build-dev/vite/config-virtual.js +24 -0
  38. package/src/build-dev/vite/constants.js +7 -0
  39. package/src/build-dev/vite/datasource-plugins/source-query-hmr.js +117 -0
  40. package/src/build-dev/vite/evidence-config.d.ts +6 -0
  41. package/src/build-dev/vite/index.js +214 -0
  42. package/src/build-dev/vite/layout-plugins/copy-to-layout.js +71 -0
  43. package/src/build-dev/vite/plugins.js +4 -0
  44. package/src/build-dev/vite/query-directory-hmr.js +32 -0
  45. package/src/build-dev/vite/sidecar-app.js +79 -0
  46. package/src/build-dev/vite/virtuals/QuerySSR.svelte +13 -0
  47. package/src/build-dev/vite/virtuals/__tests/proxyStack.spec.js +52 -0
  48. package/src/build-dev/vite/virtuals/bootstrapContexts.svelte.js +27 -0
  49. package/src/build-dev/vite/virtuals/browser/static-assets.js +3 -0
  50. package/src/build-dev/vite/virtuals/contextKeys.js +3 -0
  51. package/src/build-dev/vite/virtuals/initUsql.js +23 -0
  52. package/src/build-dev/vite/virtuals/node/static-assets.js +56 -0
  53. package/src/build-dev/vite/virtuals/proxyStack.js +37 -0
  54. package/src/build-dev/vite/virtuals/queries.svelte.js +150 -0
  55. package/src/build-dev/vite/virtuals/ssrHook.svelte.js +60 -0
  56. package/src/build-dev/vite/virtuals/updateQueryContext.svelte.js +31 -0
  57. package/src/build-dev/vite/virtuals-dynamic.js +12 -0
  58. package/src/cli.js +5 -0
  59. package/src/cli.root.js +90 -0
  60. package/src/configuration/cli/create.js +20 -0
  61. package/src/configuration/cli/index.js +12 -0
  62. package/src/configuration/createEvidenceConfig.js +51 -0
  63. package/src/configuration/getEvidenceConfig.js +60 -0
  64. package/src/configuration/getEvidenceConfig.legacy.js +27 -0
  65. package/src/configuration/index.js +1 -0
  66. package/src/configuration/schemas/config.schema.js +11 -0
  67. package/src/configuration/schemas/deployment.schema.js +9 -0
  68. package/src/configuration/schemas/deployment.schema.spec.js +34 -0
  69. package/src/configuration/updateEvidenceConfig.js +25 -0
  70. package/src/legacy-compat.js +8 -0
  71. package/src/lib/EvidenceError.js +35 -0
  72. package/src/lib/b64Deep.js +55 -0
  73. package/src/lib/check-import.js +12 -0
  74. package/src/lib/cleanZodErrors.js +22 -0
  75. package/src/lib/cli/cmdFail.js +33 -0
  76. package/src/lib/cli/notices.js +44 -0
  77. package/src/lib/debug.js +40 -0
  78. package/src/lib/flows.d.ts +21 -0
  79. package/src/lib/forceLink.js +28 -0
  80. package/src/lib/history/index.js +70 -0
  81. package/src/lib/history/types.d.ts +8 -0
  82. package/src/lib/pkg-types.helpers.js +16 -0
  83. package/src/lib/processing-queue.js +96 -0
  84. package/src/lib/processing-queue.spec.js +148 -0
  85. package/src/lib/projectPaths.js +29 -0
  86. package/src/lib/projectRoot.js +12 -0
  87. package/src/lib/sharedPromise.js +77 -0
  88. package/src/lib/sharedPromise.spec.js +23 -0
  89. package/src/lib/strict.js +19 -0
  90. package/src/lib/tests/fs.js +21 -0
  91. package/src/lib/tokens.js +7 -0
  92. package/src/lib/unnest-zod-error.js +14 -0
  93. package/src/logger/EvidenceLogger.js +164 -0
  94. package/src/logger/index.js +4 -0
  95. package/src/meta.js +8 -0
  96. package/src/plugins/cli/index.js +13 -0
  97. package/src/plugins/cli/install.js +26 -0
  98. package/src/plugins/cli/install.prompt.js +121 -0
  99. package/src/plugins/components/getAllComponents.js +82 -0
  100. package/src/plugins/components/getComponentsInPlugin.js +33 -0
  101. package/src/plugins/components/loadComponentPlugins.js +48 -0
  102. package/src/plugins/components/loaders/file-loader.js +125 -0
  103. package/src/plugins/components/loaders/manifest-loader.js +53 -0
  104. package/src/plugins/components/schemas/component-manifest.schema.js +5 -0
  105. package/src/plugins/components/types.d.ts +21 -0
  106. package/src/plugins/components/validateOverrides.js +30 -0
  107. package/src/plugins/components/validateOverrides.spec.js +32 -0
  108. package/src/plugins/datasources/Datasources.js +85 -0
  109. package/src/plugins/datasources/Datasources.spec.js +78 -0
  110. package/src/plugins/datasources/SourceResultCache.js +62 -0
  111. package/src/plugins/datasources/buildSourceDirectoryProxy.js +21 -0
  112. package/src/plugins/datasources/cli/edit/Options.fixtures.js +69 -0
  113. package/src/plugins/datasources/cli/edit/Options.js +253 -0
  114. package/src/plugins/datasources/cli/edit/Options.spec.js +496 -0
  115. package/src/plugins/datasources/cli/edit/editSourceConfig.js +201 -0
  116. package/src/plugins/datasources/cli/edit.js +20 -0
  117. package/src/plugins/datasources/cli/edit.prompt.js +100 -0
  118. package/src/plugins/datasources/cli/index.js +18 -0
  119. package/src/plugins/datasources/cli/sources.js +77 -0
  120. package/src/plugins/datasources/evalSources.js +257 -0
  121. package/src/plugins/datasources/getPublishedDatasources.js +62 -0
  122. package/src/plugins/datasources/index.js +6 -0
  123. package/src/plugins/datasources/loadSourceConfig.js +147 -0
  124. package/src/plugins/datasources/loadSourceConfig.spec.js +127 -0
  125. package/src/plugins/datasources/loadSourcePlugins.js +49 -0
  126. package/src/plugins/datasources/loadSources.js +48 -0
  127. package/src/plugins/datasources/schemas/datasource.schema.js +52 -0
  128. package/src/plugins/datasources/schemas/datasourceCache.schema.js +3 -0
  129. package/src/plugins/datasources/schemas/datasourcePlugin.schema.js +37 -0
  130. package/src/plugins/datasources/schemas/datasourcePluginOptions.schema.js +64 -0
  131. package/src/plugins/datasources/schemas/index.js +2 -0
  132. package/src/plugins/datasources/schemas/manifest.schema.js +8 -0
  133. package/src/plugins/datasources/schemas/results.schema.js +126 -0
  134. package/src/plugins/datasources/sub-source-vars.js +55 -0
  135. package/src/plugins/datasources/sub-source-vars.spec.js +46 -0
  136. package/src/plugins/datasources/types.d.ts +53 -0
  137. package/src/plugins/datasources/updateManifest.js +115 -0
  138. package/src/plugins/datasources/updateManifest.spec.js +200 -0
  139. package/src/plugins/datasources/wrapSimpleConnector.js +95 -0
  140. package/src/plugins/datasources/writeSourceConfig.js +60 -0
  141. package/src/plugins/index.js +2 -0
  142. package/src/plugins/layouts/copyMethods/index.js +10 -0
  143. package/src/plugins/layouts/copyMethods/svelte.js +53 -0
  144. package/src/plugins/layouts/copyMethods/sveltekit.js +75 -0
  145. package/src/plugins/layouts/copyToLayout.js +106 -0
  146. package/src/plugins/layouts/loadLayoutPlugin.js +23 -0
  147. package/src/plugins/loadPluginPackage.js +77 -0
  148. package/src/plugins/schemas/index.js +1 -0
  149. package/src/plugins/schemas/plugin-config.schema.js +41 -0
  150. package/src/plugins/schemas/plugin-config.schema.spec.js +21 -0
  151. package/src/plugins/schemas/plugin-package.schema.js +154 -0
  152. package/src/plugins/schemas/plugin-package.schema.spec.js +30 -0
  153. package/src/query-builder.d.ts +1 -0
  154. package/src/query-builder.js +1 -0
  155. package/src/types/duckdb-wellknown.d.ts +35 -0
  156. package/src/types/html-attributes.ts +13 -0
  157. package/src/types/index.d.ts +18 -0
  158. package/src/types/mosaic-sql.d.ts +27 -0
  159. package/src/types/sveltekit-autoimport.d.ts +13 -0
  160. package/src/types/virtuals.d.ts +113 -0
  161. package/src/types.d.ts +1 -0
  162. package/src/usql/index.d.ts +4 -0
  163. package/src/usql/index.js +3 -0
  164. package/src/usql/query/Query.js +1391 -0
  165. package/src/usql/query/Query.spec.js +827 -0
  166. package/src/usql/query/queryScore.js +92 -0
  167. package/src/usql/query/queryScore.spec.js +26 -0
  168. package/src/usql/query/sterilizeQuery.js +89 -0
  169. package/src/usql/query/sterilizeQuery.spec.js +172 -0
  170. package/src/usql/setTrackProxy/setTrackProxy.js +80 -0
  171. package/src/usql/setTrackProxy/setTrackProxy.spec.js +166 -0
  172. package/src/usql/types.ts +86 -0
  173. package/src/usql/utilities/duckdbSerialize.js +29 -0
  174. package/src/usql/utilities/index.js +2 -0
  175. package/src/usql/utilities/resolveMaybePromise.js +30 -0
  176. package/src/utils/batchUp.js +22 -0
  177. package/src/utils/batchUp.spec.js +69 -0
  178. package/src/utils/browserDebounce.js +16 -0
  179. package/src/utils/index.js +7 -0
  180. package/src/utils/svelte/addBasePath.js +45 -0
  181. package/src/utils/svelte/addBasePath.spec.js +86 -0
  182. package/src/utils/svelte/index.js +5 -0
  183. package/src/utils/svelte/inputs.js +138 -0
  184. package/tsconfig.json +22 -0
  185. package/vite.config.js +3 -0
  186. package/vitest.config.js +15 -0
@@ -0,0 +1,827 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { Query } from './Query';
3
+ import { sharedPromise } from '../../lib/sharedPromise';
4
+
5
+ const tick = (timeout = 0) => new Promise((r) => setTimeout(r, timeout));
6
+
7
+ /** @type {import('../types').MaybePromise<[{rowCount: number}]>} */
8
+ let expectedLength = [{ rowCount: -1 }];
9
+ /** @type {import('../types').MaybePromise<import('../../types/duckdb-wellknown').DescribeResultRow[]>} */
10
+ let expectedColumns = [];
11
+ /** @type {import('../types').MaybePromise<import('../types').QueryResultRow[]>} */
12
+ let expectedData = [];
13
+
14
+ const mockRunner = vi.fn((q) => {
15
+ if (q.startsWith('---- Length')) {
16
+ return expectedLength;
17
+ }
18
+ if (q.startsWith('---- Columns')) {
19
+ return expectedColumns;
20
+ }
21
+ if (q.startsWith('---- Data')) {
22
+ return expectedData;
23
+ }
24
+ });
25
+
26
+ let testQueryIndex = 0;
27
+ let testIdx = 0;
28
+
29
+ /**
30
+ * @param {string} s
31
+ * @param {import('../types').QueryOpts} opts
32
+ * @returns
33
+ */
34
+ const getMockQuery = (s, opts) => {
35
+ return Query.create(s, mockRunner, {
36
+ id: `q-${testQueryIndex++}-${testIdx}`,
37
+ disableCache: true,
38
+ ...opts
39
+ });
40
+ };
41
+
42
+ describe('Query', () => {
43
+ beforeEach(() => {
44
+ expectedColumns = [];
45
+ expectedData = [];
46
+ expectedLength = [{ rowCount: -1 }];
47
+ vi.restoreAllMocks();
48
+ testQueryIndex = 0;
49
+ testIdx++;
50
+ Query.emptyCache();
51
+ });
52
+
53
+ describe('Cache Busting', () => {
54
+ it('should not grow the cache when disableCache is set', () => {
55
+ getMockQuery('SELECT 5', { disableCache: true });
56
+ getMockQuery('SELECT 6', { disableCache: true });
57
+ getMockQuery('SELECT 7', { disableCache: true });
58
+ expect(Query.cacheSize).toBe(0);
59
+ });
60
+ it('should cache normally for a few queries', () => {
61
+ getMockQuery('SELECT 5', { disableCache: false });
62
+ getMockQuery('SELECT 6', { disableCache: false });
63
+ getMockQuery('SELECT 7', { disableCache: false });
64
+ expect(Query.cacheSize).toBe(3);
65
+ });
66
+ it('should not cache when CacheMaxScore is 0', async () => {
67
+ Query.CacheMaxScore = 0;
68
+ expectedColumns = [
69
+ {
70
+ column_name: 'x',
71
+ column_type: 'VARCHAR'
72
+ },
73
+ {
74
+ column_name: 'x',
75
+ column_type: 'VARCHAR'
76
+ },
77
+ {
78
+ column_name: 'x',
79
+ column_type: 'VARCHAR'
80
+ },
81
+ {
82
+ column_name: 'x',
83
+ column_type: 'VARCHAR'
84
+ },
85
+ {
86
+ column_name: 'x',
87
+ column_type: 'VARCHAR'
88
+ }
89
+ ];
90
+
91
+ getMockQuery('SELECT 5', { disableCache: false });
92
+ getMockQuery('SELECT 6', { disableCache: false });
93
+ getMockQuery('SELECT 7', { disableCache: false });
94
+ await tick();
95
+ expect(Query.cacheSize).toBe(0);
96
+ });
97
+ it('should rotate the cache when CacheMaxScore is non-zero', async () => {
98
+ Query.CacheMaxScore = 200;
99
+ expectedColumns = [
100
+ {
101
+ column_name: 'x',
102
+ column_type: 'VARCHAR'
103
+ },
104
+ {
105
+ column_name: 'x',
106
+ column_type: 'VARCHAR'
107
+ },
108
+ {
109
+ column_name: 'x',
110
+ column_type: 'VARCHAR'
111
+ },
112
+ {
113
+ column_name: 'x',
114
+ column_type: 'VARCHAR'
115
+ },
116
+ {
117
+ column_name: 'x',
118
+ column_type: 'VARCHAR'
119
+ }
120
+ ];
121
+
122
+ getMockQuery('SELECT 5', { disableCache: false });
123
+ getMockQuery('SELECT 6', { disableCache: false });
124
+ getMockQuery('SELECT 7', { disableCache: false });
125
+ await tick();
126
+ expect(Query.cacheSize).toBe(1);
127
+ });
128
+ });
129
+
130
+ describe('Query Score', () => {
131
+ it('should not be calculated when data has not been fetched, and autoScore is false', () => {
132
+ const q = getMockQuery('SELECT 5', { autoScore: false });
133
+
134
+ expect(q.score).toBe(-1);
135
+ });
136
+ it('should be calculated when autoScore is true', async () => {
137
+ const q = getMockQuery('SELECT 5');
138
+ await tick();
139
+ expect(q.score).toBe(0);
140
+ });
141
+ it('should dispatch an event when the score is too large', async () => {
142
+ const colProm = sharedPromise();
143
+ expectedColumns = colProm.promise;
144
+ expectedLength = [{ rowCount: 100 * 100 * 100 }];
145
+ const q = getMockQuery('SELECT 5');
146
+ const listen = vi.fn();
147
+ q.addEventListener('highScore', listen);
148
+ await tick();
149
+ expect(q.score).toBe(-1);
150
+ expect(listen).not.toHaveBeenCalled();
151
+ colProm.resolve([
152
+ {
153
+ column_name: 'x',
154
+ column_type: 'VARCHAR'
155
+ },
156
+ {
157
+ column_name: 'x',
158
+ column_type: 'VARCHAR'
159
+ },
160
+ {
161
+ column_name: 'x',
162
+ column_type: 'VARCHAR'
163
+ },
164
+ {
165
+ column_name: 'x',
166
+ column_type: 'VARCHAR'
167
+ },
168
+ {
169
+ column_name: 'x',
170
+ column_type: 'VARCHAR'
171
+ }
172
+ ]);
173
+ await tick();
174
+ expect(q.score).toBeGreaterThan(10 * 1024 * 1024);
175
+ expect(listen).toHaveBeenCalledWith(q.score, 'highScore');
176
+ });
177
+ });
178
+
179
+ describe('Data Volume', () => {
180
+ it('should process result sets of 10k rows in <250ms', async () => {
181
+ const rows = Array(10 * 1000)
182
+ .fill(null)
183
+ .map(() => ({
184
+ str: 'Hi',
185
+ num: Math.random(),
186
+ bool: Math.random() > 0.5
187
+ }));
188
+ /** @type {import('../../types/duckdb-wellknown').DescribeResultRow[]} */
189
+ const columns = [
190
+ { column_name: 'str', column_type: 'VARCHAR' },
191
+ { column_name: 'num', column_type: 'INTEGER' },
192
+ { column_name: 'bool', column_type: 'BOOLEAN' }
193
+ ];
194
+ const length = [{ rowCount: 10 * 1000 }];
195
+
196
+ expectedColumns = columns;
197
+ expectedLength = length;
198
+ expectedData = rows;
199
+
200
+ const query = getMockQuery('SELECT 5');
201
+
202
+ const before = performance.now();
203
+ await query.fetch();
204
+ const after = performance.now();
205
+
206
+ expect(query.length).toBe(10 * 1000);
207
+ expect(after - before).toBeLessThan(250);
208
+ });
209
+ it('should process result sets of 100k rows in <250ms', async () => {
210
+ const rows = Array(1000 * 1000)
211
+ .fill(null)
212
+ .map(() => ({
213
+ str: 'Hi',
214
+ num: Math.random(),
215
+ bool: Math.random() > 0.5
216
+ }));
217
+ /** @type {import('../../types/duckdb-wellknown').DescribeResultRow[]} */
218
+ const columns = [
219
+ { column_name: 'str', column_type: 'VARCHAR' },
220
+ { column_name: 'num', column_type: 'INTEGER' },
221
+ { column_name: 'bool', column_type: 'BOOLEAN' }
222
+ ];
223
+ const length = [{ rowCount: 1000 * 1000 }];
224
+
225
+ expectedColumns = columns;
226
+ expectedLength = length;
227
+ expectedData = rows;
228
+
229
+ const before = performance.now();
230
+ const query = getMockQuery('SELECT 7');
231
+ await query.fetch();
232
+ const after = performance.now();
233
+
234
+ console.log(`Took ${(after - before).toFixed(2)}`);
235
+ expect(query.length).toBe(1000 * 1000);
236
+ expect(after - before).toBeLessThan(250);
237
+ });
238
+ });
239
+
240
+ describe('Legacy Compatibility', () => {
241
+ it('should allow id to be the 3rd parameter', () => {
242
+ expect(Query.create('HI', mockRunner, 'Test ID', {}).id).toBe('Test ID');
243
+ });
244
+ });
245
+
246
+ describe('Factory Pattern', () => {
247
+ it('should warn when directly using new', () => {
248
+ const spy = vi.spyOn(console, 'warn');
249
+ new Query('SELECT 1', mockRunner);
250
+ expect(spy).toHaveBeenCalled();
251
+ });
252
+ it('should throw when query string is not a string or QueryBuilder', () => {
253
+ const errored = getMockQuery(null);
254
+
255
+ expect(errored.error.message.startsWith('Refusing to create Query')).toBe(true);
256
+ });
257
+
258
+ describe('Reactive Variant', () => {
259
+ // describe('reactivity with QueryBuilder API', () => {
260
+ // // TODO: Try to use the factory with derived queries
261
+ // });
262
+ describe('change indexing', () => {
263
+ it('should not provide outdated results', async () => {
264
+ /** @type {import('..').QueryValue} */
265
+ let value;
266
+ const react = Query.createReactive({
267
+ execFn: mockRunner,
268
+ callback: (v) => (value = v),
269
+ loadGracePeriod: 0
270
+ });
271
+
272
+ expectedData = [{ i: 0 }];
273
+ react('SELECT 1'); // initial state
274
+ await tick();
275
+ expect(value.ready).toBe(true);
276
+
277
+ const p = sharedPromise();
278
+ expectedData = p.promise;
279
+ react('SELECT 2'); // takes 5 seconds
280
+ await tick();
281
+ expect(value.ready).toBe(false);
282
+ expect(value[0]).toBe(undefined); // we're looking at the right query
283
+
284
+ react('SELECT 1'); // before previous finishes
285
+ await tick();
286
+ expect(value.ready).toBe(true); // Check that the data is correct
287
+
288
+ p.resolve([{ i: 1 }]); // Resolve alternate query
289
+
290
+ // Check that the data is still correct
291
+ await tick();
292
+ expect(value[0].i).toBe(0);
293
+
294
+ react('SELECT 2'); // Sanity check that this query did actually finish
295
+ await tick();
296
+ expect(value[0].i).toBe(1);
297
+ });
298
+ });
299
+ it('should exist', () => {
300
+ expect(Query.createReactive).toBeDefined();
301
+ });
302
+ it('should return a function', () => {
303
+ const react = Query.createReactive({ execFn: mockRunner });
304
+ expect(typeof react).toBe('function');
305
+ });
306
+ it('should produce distinct queries when called multiple times', async () => {
307
+ let values = new Set();
308
+ const react = Query.createReactive({
309
+ execFn: mockRunner,
310
+ loadGracePeriod: 0,
311
+ callback: (v) => values.add(v)
312
+ });
313
+
314
+ await react('SELECT 5');
315
+ await tick();
316
+ await react('SELECT 3');
317
+ await tick();
318
+ const [first, second] = values.values();
319
+
320
+ expect(Query.isQuery(first)).toBe(true);
321
+ expect(Query.isQuery(second)).toBe(true);
322
+ expect(first === second).toBe(false);
323
+ });
324
+ it('should return a pending query when it takes more than 250ms to execute', async () => {
325
+ vi.useFakeTimers();
326
+ // Test initial state (sync fetch)
327
+ /** @type {import('..').QueryValue} */
328
+ let value;
329
+ const react = Query.createReactive({ execFn: mockRunner, callback: (v) => (value = v) });
330
+
331
+ const p = sharedPromise();
332
+ expectedData = p.promise;
333
+ react(`SELECT 2`);
334
+ await expect(
335
+ vi.advanceTimersByTimeAsync(250).then(() => value.loading),
336
+ 'Query should be loading'
337
+ ).resolves.toBe(true);
338
+ vi.clearAllTimers();
339
+ vi.useRealTimers();
340
+ });
341
+ it('should return a finalized query when it takes less than 250ms to execute', async () => {
342
+ vi.useFakeTimers();
343
+
344
+ // Test initial state (sync fetch)
345
+ /** @type {QueryValue} */
346
+ let innerValue;
347
+ const react = Query.createReactive({
348
+ execFn: mockRunner,
349
+ callback: (v) => {
350
+ innerValue = v;
351
+ }
352
+ });
353
+
354
+ expectedData = [{ x: 1 }];
355
+ react(`SELECT 2`);
356
+ await vi.advanceTimersByTimeAsync(250);
357
+ await expect(innerValue.loading, 'Query should not be loading').toBe(false);
358
+
359
+ vi.clearAllTimers();
360
+ vi.useRealTimers();
361
+ });
362
+ it('should play nicely with reactivity', async () => {
363
+ vi.useFakeTimers();
364
+ const updateTracker = vi.fn();
365
+
366
+ let currentValue;
367
+ const react = Query.createReactive(
368
+ {
369
+ execFn: mockRunner,
370
+ callback: (v) => {
371
+ currentValue = v;
372
+ console.log(v.id, v.dataLoaded);
373
+ updateTracker();
374
+ }
375
+ },
376
+ { disableCache: true }
377
+ );
378
+
379
+ expectedData = [{ state: 'Iniital' }];
380
+ react('SELECT 1');
381
+
382
+ expect(currentValue.loading).toBe(false);
383
+ expect(updateTracker).toHaveBeenCalledTimes(2);
384
+
385
+ const p = sharedPromise();
386
+ expectedData = p.promise;
387
+
388
+ react('SELECT 2');
389
+ expect(updateTracker).toHaveBeenCalledTimes(2);
390
+ await vi.advanceTimersByTime(249); // update shouldn't have run yet
391
+ expect(updateTracker).toHaveBeenCalledTimes(2);
392
+ expect(currentValue.loading).toBe(false);
393
+ await vi.advanceTimersByTimeAsync(2); // let the update run
394
+ expect(updateTracker).toHaveBeenCalledTimes(3);
395
+ expect(currentValue.loading).toBe(true); // value has changed
396
+ p.resolve([]);
397
+ await vi.advanceTimersToNextTimerAsync();
398
+ expect(updateTracker).toHaveBeenCalledTimes(4);
399
+ vi.clearAllTimers();
400
+ vi.useRealTimers();
401
+ });
402
+ });
403
+ });
404
+
405
+ describe('Store Contract', () => {
406
+ it('should be subscriptable', () => {
407
+ const q = getMockQuery('SELECT 1');
408
+ expect(q).toBeDefined();
409
+ expect(q.subscribe).toBeTypeOf('function');
410
+ const testSub = vi.fn();
411
+ expect(q.subscribe(testSub)).toBeTypeOf('function');
412
+ });
413
+ it('should correctly publish to subscribers', () => {
414
+ const q = getMockQuery('SELECT 1');
415
+ const testSubs = [vi.fn(), vi.fn(), vi.fn()];
416
+ const unsubs = testSubs.map(q.subscribe);
417
+ unsubs.forEach((unsub) => expect(unsub).toBeTypeOf('function'));
418
+
419
+ testSubs.forEach((fn) => {
420
+ expect(fn).toHaveBeenCalledTimes(1);
421
+ });
422
+ });
423
+ it('should be unsubscribable', () => {
424
+ const q = getMockQuery('SELECT 1');
425
+ const testSubs = [vi.fn(), vi.fn(), vi.fn()];
426
+ const unsubs = testSubs.map(q.subscribe);
427
+
428
+ unsubs.forEach((unsub) => unsub());
429
+ // @ts-expect-error Accessing a protected function
430
+ q.publish();
431
+
432
+ testSubs.forEach((fn) => expect(fn).toHaveBeenCalledTimes(1));
433
+ });
434
+ it('should publish infrequently', async () => {
435
+ expectedLength = [{ rowCount: 100 }];
436
+ expectedColumns = [{ column_name: 'test', column_type: 'BOOLEAN' }];
437
+ expectedData = Promise.resolve([{ test: 'hi' }]);
438
+
439
+ const q = getMockQuery('SELECT 1');
440
+ const testSubs = [vi.fn(), vi.fn(), vi.fn()];
441
+
442
+ testSubs.map(q.subscribe);
443
+
444
+ q.fetch();
445
+ testSubs.forEach((fn) => expect(fn).toHaveBeenCalledTimes(2));
446
+ expect(q.dataLoaded).toBe(false);
447
+ await tick();
448
+ testSubs.forEach((fn) => expect(fn).toHaveBeenCalledTimes(3));
449
+ expect(q.dataLoaded).toBe(true);
450
+
451
+ expect(q.length).toBe(100);
452
+ expect(q.columns[0].column_name).toBe('test');
453
+ expect(q[0].test).toBe('hi');
454
+ });
455
+ });
456
+
457
+ describe('Type Narrowing', () => {
458
+ it('should accept a Query', () => {
459
+ expect(Query.isQuery(getMockQuery('SELECT 1'))).toBe(true);
460
+ });
461
+ it('should fail a plain object', () => {
462
+ expect(Query.isQuery({})).toBe(false);
463
+ });
464
+ it('should fail a class that is pretending', () => {
465
+ class NotQuery {
466
+ #isQuery = true;
467
+ }
468
+ expect(Query.isQuery(new NotQuery())).toBe(false);
469
+ });
470
+ });
471
+
472
+ describe('Implicit Fetching', () => {
473
+ it('should fetch when a numeric property is accessed', () => {
474
+ const p = sharedPromise();
475
+ expectedData = p.promise;
476
+ const q = getMockQuery('SELECT 1');
477
+ expect(q.dataLoading).toBe(false);
478
+ q[0];
479
+ expect(q.dataLoading).toBe(true);
480
+ p.resolve([]);
481
+ });
482
+ it.each(Query.ProxyFetchTriggers)('should fetch when "%s" property is accessed', (trigger) => {
483
+ const p = sharedPromise();
484
+ expectedData = p.promise;
485
+ const q = getMockQuery('SELECT 1');
486
+ expect(q.dataLoading).toBe(false);
487
+ q.value[trigger];
488
+ expect(q.dataLoading).toBe(true);
489
+ p.resolve([]);
490
+ });
491
+ });
492
+
493
+ describe('Global Loading State', () => {
494
+ beforeEach(() => {
495
+ Query.resetInFlightQueries();
496
+ });
497
+ it('should report that no queries are loading when no queries have been created', () => {
498
+ expect(Query.queriesInFlight).toBe(false);
499
+ });
500
+ it('should report that no queries are loading when queries have been created, but not loaded', () => {
501
+ const q = getMockQuery('SELECT 1');
502
+ const q2 = getMockQuery('SELECT 2');
503
+ expect(Query.queriesInFlight).toBe(false);
504
+ expect(q.loading).toBe(false);
505
+ expect(q2.loading).toBe(false);
506
+ });
507
+ it('should report that some queries are loading when one query is currently fetching', async () => {
508
+ const onQueryStart = vi.fn();
509
+ const onQueryEnd = vi.fn();
510
+ Query.addEventListener('inFlightQueryStart', onQueryStart);
511
+ Query.addEventListener('inFlightQueryEnd', onQueryEnd);
512
+
513
+ const mockDataPromise = sharedPromise();
514
+ expectedData = mockDataPromise.promise;
515
+ const q = getMockQuery('SELECT 5');
516
+ q.fetch();
517
+ expect(Query.queriesInFlight).toBe(true);
518
+ expect(q.dataLoading).toBe(true);
519
+ expect(onQueryStart).toHaveBeenCalledOnce();
520
+ expect(onQueryEnd).not.toHaveBeenCalled();
521
+ mockDataPromise.resolve([]);
522
+ await tick();
523
+ expect(Query.queriesInFlight).toBe(false);
524
+ expect(q.dataLoading).toBe(false);
525
+ expect(onQueryStart).toHaveBeenCalledOnce();
526
+ expect(onQueryEnd).toHaveBeenCalledOnce();
527
+
528
+ Query.removeEventListener('inFlightQueryStart', onQueryStart);
529
+ Query.removeEventListener('inFlightQueryEnd', onQueryEnd);
530
+ });
531
+ it('should report that some queries are loading when multiple queries are currently fetching', async () => {
532
+ const onQueryStart = vi.fn();
533
+ const onQueryEnd = vi.fn();
534
+ Query.addEventListener('inFlightQueryStart', onQueryStart);
535
+ Query.addEventListener('inFlightQueryEnd', onQueryEnd);
536
+
537
+ const mockDataPromise = sharedPromise();
538
+ const mockDataPromise2 = sharedPromise();
539
+ const q = getMockQuery('SELECT 5');
540
+ const q2 = getMockQuery('SELECT 5');
541
+ expectedData = mockDataPromise.promise;
542
+ q.fetch();
543
+ // q1 is in flight
544
+ expect(Query.queriesInFlight).toBe(true);
545
+ expect(q.dataLoading).toBe(true);
546
+ expect(q2.dataLoading).toBe(false);
547
+ expect(onQueryStart).toHaveBeenCalledOnce();
548
+ expect(onQueryEnd).not.toHaveBeenCalled();
549
+
550
+ expectedData = mockDataPromise2.promise;
551
+ q2.fetch();
552
+ // q1 and q2 are in flight
553
+ expect(Query.queriesInFlight).toBe(true);
554
+ expect(q.dataLoading).toBe(true);
555
+ expect(q2.dataLoading).toBe(true);
556
+ expect(onQueryStart).toHaveBeenCalledOnce();
557
+ expect(onQueryEnd).not.toHaveBeenCalled();
558
+
559
+ mockDataPromise.resolve([]);
560
+ await tick();
561
+ // q1 resolved, q2 in flight
562
+ expect(Query.queriesInFlight).toBe(true);
563
+ expect(q.dataLoading).toBe(false);
564
+ expect(q2.dataLoading).toBe(true);
565
+ mockDataPromise2.resolve([]);
566
+ expect(onQueryStart).toHaveBeenCalledOnce();
567
+ expect(onQueryEnd).not.toHaveBeenCalled();
568
+
569
+ await tick();
570
+ // q1 resolved, q2 resolved
571
+ expect(Query.queriesInFlight).toBe(false);
572
+ expect(q.dataLoading).toBe(false);
573
+ expect(q2.dataLoading).toBe(false);
574
+ expect(onQueryStart).toHaveBeenCalledOnce();
575
+ expect(onQueryEnd).toHaveBeenCalledOnce();
576
+ });
577
+ });
578
+
579
+ describe('Query initial state handling', () => {
580
+ it('should not execute any queries if provided with initial state', () => {
581
+ const q = getMockQuery('SELECT 1', {
582
+ initialData: [],
583
+ knownColumns: [{ column_name: 'x', column_type: 'VARCHAR', nullable: true }]
584
+ });
585
+
586
+ expect(mockRunner).not.toHaveBeenCalled();
587
+ expect(q.dataLoaded).toBe(true);
588
+ expect(q.lengthLoaded).toBe(true);
589
+ expect(q.length).toBe(0);
590
+ });
591
+ it('should not execute any queries if initialData provided and noResolve=true', () => {
592
+ const q = getMockQuery('SELECT 1', { initialData: [], noResolve: true });
593
+
594
+ expect(mockRunner).not.toHaveBeenCalled();
595
+ expect(q.dataLoading).toBe(false);
596
+ expect(q.lengthLoading).toBe(false);
597
+ expect(q.columnsLoading).toBe(false);
598
+ });
599
+ });
600
+
601
+ describe('Query/Array Masquarade', () => {
602
+ it('it should pass Array.isArray', () => {
603
+ const q = getMockQuery('SELECT 1');
604
+
605
+ expect(Array.isArray(q)).toBe(true);
606
+ });
607
+ it('it should pass Query.isQuery', () => {
608
+ const q = getMockQuery('SELECT 1');
609
+
610
+ expect(q.value).toBeDefined();
611
+ expect(Query.isQuery(q)).toBe(true);
612
+ });
613
+ it('published value should pass Array.isArray', () => {
614
+ const q = getMockQuery('SELECT 1');
615
+ /** @type {import('./Query').QueryValue} */
616
+ let publishedValue;
617
+ q.subscribe((v) => {
618
+ publishedValue = v;
619
+ })();
620
+
621
+ expect(publishedValue.value).toBeDefined();
622
+ expect(Array.isArray(publishedValue)).toBe(true);
623
+ });
624
+ it('published value should pass Query.isQuery', () => {
625
+ const q = getMockQuery('SELECT 1');
626
+ /** @type {import('./Query').QueryValue} */
627
+ let publishedValue;
628
+ q.subscribe((v) => {
629
+ publishedValue = v;
630
+ })();
631
+
632
+ expect(publishedValue.value).toBeDefined();
633
+ expect(Query.isQuery(publishedValue)).toBe(true);
634
+ });
635
+ it('should pass echarts method', () => {
636
+ const q = getMockQuery('SELECT 1');
637
+ expect(Object.prototype.toString.call(q)).toBe('[object Array]');
638
+ });
639
+ it('should iterate through rows by default', () => {
640
+ const rows = [{ x: 1 }, { x: 2 }];
641
+ const q = getMockQuery('SELECT 5', { initialData: rows });
642
+
643
+ // Variety of iteration methods
644
+ let i = 0;
645
+ for (const v of q) {
646
+ if (i > rows.length) throw new Error('Iteration went out of bounds');
647
+ expect(v).toEqual(rows[i++]);
648
+ }
649
+
650
+ i = 0;
651
+ for (const idx in q) {
652
+ if (i > rows.length) throw new Error('Iteration went out of bounds');
653
+ expect(q[idx]).toEqual(rows[i++]);
654
+ }
655
+
656
+ i = 0;
657
+ q.forEach((v) => {
658
+ if (rows.length > 2) throw new Error('Iteration went out of bounds');
659
+ expect(v).toEqual(rows[i++]);
660
+ });
661
+
662
+ for (let idx = 0; idx < rows.length; idx++) {
663
+ expect(q[idx]).toEqual(rows[idx]);
664
+ }
665
+ });
666
+ describe('basic array usages', () => {
667
+ it('filter', () => {
668
+ const rows = [{ x: 1 }, { x: 2 }];
669
+ const q = getMockQuery('SELECT 1', { initialData: rows });
670
+ expect(q.filter((r) => r.x === 1)).toEqual([rows[0]]);
671
+ });
672
+ it('map', () => {
673
+ const rows = [{ x: 1 }, { x: 2 }];
674
+ const q = getMockQuery('SELECT 1', { initialData: rows });
675
+ expect(q.map((r) => r.x)).toEqual([1, 2]);
676
+ });
677
+ });
678
+ });
679
+
680
+ describe('Metadata', () => {
681
+ describe('Columns', () => {
682
+ it('should be fetched when creating a store', () => {
683
+ getMockQuery('SELECT 5');
684
+ expect(mockRunner).toHaveBeenCalledTimes(2); // once for length, once for meta
685
+ });
686
+ it('should not be fetched when creating a store with knownColumns', () => {
687
+ getMockQuery('SELECT 5', { knownColumns: [] });
688
+ expect(mockRunner).toHaveBeenCalledTimes(1); // once for length
689
+ });
690
+ it('should put the results of the metadata query into the columns fields', () => {
691
+ const columns = [{ column_name: '5', column_type: 'INTEGER', null: 'YES' }];
692
+ expectedColumns = columns;
693
+ const q = getMockQuery('SELECT 5');
694
+ expect(q.columns).toEqual(columns);
695
+ });
696
+ });
697
+ describe('Length', () => {
698
+ it('should be fetched when creating a store', () => {
699
+ expectedLength = [{ rowCount: 5 }];
700
+ const q = getMockQuery('SELECT 5');
701
+ expect(q.length).toBe(5);
702
+ });
703
+ it('should be inferred when given initial data', () => {
704
+ const q = getMockQuery('SELECT 5', { initialData: [{ 5: 5 }] });
705
+ expect(q.length).toBe(1);
706
+ });
707
+ });
708
+ });
709
+
710
+ describe('Subscription Lifecycle', () => {
711
+ it('should publish when fetching metadata', async () => {
712
+ // setup
713
+ const metaSharedPromise = sharedPromise();
714
+ expectedColumns = metaSharedPromise.promise; // use a shared promise to control how long the query takes
715
+ const q = getMockQuery('SELECT 5');
716
+ const subscriber = vi.fn();
717
+ let i = 0;
718
+
719
+ subscriber.mockImplementationOnce((v) => {
720
+ i++;
721
+ expect(v.columnsLoaded).toBe(false);
722
+ expect(v.columnsLoading).toBe(true);
723
+ });
724
+ q.subscribe(subscriber);
725
+ await tick();
726
+
727
+ subscriber.mockImplementationOnce((v) => {
728
+ i++;
729
+ expect(v.columnsLoaded).toBe(true);
730
+ expect(v.columnsLoading).toBe(false);
731
+ });
732
+ metaSharedPromise.resolve([]);
733
+
734
+ await tick();
735
+ await tick();
736
+ expect(subscriber).toHaveBeenCalledTimes(2); // cols loading + loaded + length pub
737
+ expect(i).toBe(2);
738
+ });
739
+
740
+ it('should publish when fetching length', async () => {
741
+ const lengthSharedPromise = sharedPromise();
742
+ expectedLength = lengthSharedPromise.promise;
743
+ const q = getMockQuery('SELECT 5', { id: 'I SEE YOU' });
744
+ const subscriber = vi.fn(console.log);
745
+ let i = 0;
746
+
747
+ subscriber.mockImplementationOnce((v) => {
748
+ i++;
749
+ expect(v.lengthLoaded).toBe(false);
750
+ expect(v.lengthLoading).toBe(true);
751
+ });
752
+ q.subscribe(subscriber); // first call
753
+ await tick();
754
+ expect(subscriber).toHaveBeenCalledOnce();
755
+
756
+ subscriber.mockImplementationOnce((v) => {
757
+ i++;
758
+ expect(v.lengthLoaded).toBe(true);
759
+ expect(v.lengthLoading).toBe(false);
760
+ expect(v.length).toBe(5);
761
+ });
762
+ expect(subscriber).toHaveBeenCalledOnce();
763
+ lengthSharedPromise.resolve([{ rowCount: 5 }]); // second call??
764
+ await tick();
765
+ expect(subscriber).toHaveBeenCalledTimes(2); /* Length load start, length load end */
766
+ expect(i).toBe(2);
767
+ });
768
+ });
769
+
770
+ describe('QueryBuilder interface', () => {
771
+ it("should pass known columns when the derived query doesn't have different selects", () => {
772
+ expectedColumns = [];
773
+ const initial = getMockQuery('SELECT 1');
774
+ const filtered = initial.where('1 = 1');
775
+ expect(filtered.columns).toEqual(expectedColumns);
776
+ expect(filtered.columns).toEqual(initial.columns); // referential equality
777
+ /*
778
+ One columns fetch
779
+ Two length fetches
780
+ */
781
+ expect(mockRunner).toHaveBeenCalledTimes(3);
782
+ });
783
+ it('should inherit noResolve from the parent query', () => {
784
+ let initial = getMockQuery('SELECT 1', { noResolve: true });
785
+ let filtered = initial.where('1 = 1');
786
+ expect(filtered.opts.noResolve).toBe(true);
787
+
788
+ initial = getMockQuery('SELECT 1', { noResolve: false });
789
+ filtered = initial.where('1 = 1');
790
+ expect(filtered.opts.noResolve).toBe(false);
791
+ });
792
+ });
793
+
794
+ describe('EventEmitter interface', () => {
795
+ it('should emit dataReady when done fetching data', async () => {
796
+ const q = getMockQuery('SELECT 5');
797
+ const sub = vi.fn();
798
+ q.on('dataReady', sub);
799
+ await q.fetch();
800
+ expect(sub).toHaveBeenCalledWith(undefined, 'dataReady');
801
+ });
802
+ it('should respect .off', async () => {
803
+ const q = getMockQuery('SELECT 5');
804
+ const sub = vi.fn();
805
+ q.on('dataReady', sub);
806
+ q.off('dataReady', sub);
807
+ await q.fetch();
808
+ expect(sub).not.toHaveBeenCalled();
809
+ });
810
+ it('should emit an error event when a query fails', async () => {
811
+ const innerError = new Error('Hello World');
812
+
813
+ expectedColumns = Promise.reject(innerError);
814
+ let output;
815
+ const sub = vi.fn().mockImplementation((v) => (output = v));
816
+ try {
817
+ const q = getMockQuery('SELECT a broken value');
818
+ q.on('error', sub);
819
+ } catch {
820
+ /* Ignore */
821
+ }
822
+ await tick();
823
+ expect(sub).toHaveBeenCalledOnce();
824
+ expect(output).toEqual(innerError); // Check that the error bubbled properly
825
+ });
826
+ });
827
+ });