@evolu/common 7.4.0 → 8.0.0-next.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.
- package/README.md +4 -61
- package/dist/src/Array.d.ts +343 -102
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +181 -85
- package/dist/src/Assert.d.ts +57 -11
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +47 -11
- package/dist/src/BigInt.d.ts +6 -1
- package/dist/src/BigInt.d.ts.map +1 -1
- package/dist/src/BigInt.js +5 -0
- package/dist/src/Brand.d.ts +40 -12
- package/dist/src/Brand.d.ts.map +1 -1
- package/dist/src/Brand.js +5 -0
- package/dist/src/Buffer.d.ts +6 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +7 -2
- package/dist/src/Cache.d.ts +10 -2
- package/dist/src/Cache.d.ts.map +1 -1
- package/dist/src/Cache.js +6 -0
- package/dist/src/Callbacks.d.ts +10 -7
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Callbacks.js +23 -13
- package/dist/src/Console.d.ts +361 -69
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +217 -117
- package/dist/src/Crypto.d.ts +70 -21
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +72 -27
- package/dist/src/Eq.d.ts +22 -2
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +21 -1
- package/dist/src/Error.d.ts +23 -12
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Error.js +27 -11
- package/dist/src/Function.d.ts +128 -56
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +118 -14
- package/dist/src/Identicon.d.ts +5 -0
- package/dist/src/Identicon.d.ts.map +1 -1
- package/dist/src/Identicon.js +6 -1
- package/dist/src/Lookup.d.ts +160 -0
- package/dist/src/Lookup.d.ts.map +1 -0
- package/dist/src/Lookup.js +192 -0
- package/dist/src/Microtask.d.ts +21 -0
- package/dist/src/Microtask.d.ts.map +1 -0
- package/dist/src/Microtask.js +37 -0
- package/dist/src/Number.d.ts +18 -4
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +23 -1
- package/dist/src/Object.d.ts +110 -5
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +108 -5
- package/dist/src/Option.d.ts +58 -0
- package/dist/src/Option.d.ts.map +1 -0
- package/dist/src/Option.js +43 -0
- package/dist/src/Order.d.ts +5 -0
- package/dist/src/Order.d.ts.map +1 -1
- package/dist/src/Order.js +8 -3
- package/dist/src/Platform.d.ts +36 -2
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +10 -6
- package/dist/src/Polyfills.d.ts +27 -0
- package/dist/src/Polyfills.d.ts.map +1 -0
- package/dist/src/Polyfills.js +299 -0
- package/dist/src/Random.d.ts +18 -7
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +9 -7
- package/dist/src/Redacted.d.ts +7 -4
- package/dist/src/Redacted.d.ts.map +1 -1
- package/dist/src/Redacted.js +5 -0
- package/dist/src/Ref.d.ts +31 -16
- package/dist/src/Ref.d.ts.map +1 -1
- package/dist/src/Ref.js +35 -7
- package/dist/src/RefCount.d.ts +62 -0
- package/dist/src/RefCount.d.ts.map +1 -0
- package/dist/src/RefCount.js +83 -0
- package/dist/src/Relation.d.ts +84 -29
- package/dist/src/Relation.d.ts.map +1 -1
- package/dist/src/Relation.js +83 -105
- package/dist/src/Resource.d.ts +263 -0
- package/dist/src/Resource.d.ts.map +1 -0
- package/dist/src/Resource.js +389 -0
- package/dist/src/Result.d.ts +390 -374
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +142 -70
- package/dist/src/Schedule.d.ts +953 -0
- package/dist/src/Schedule.d.ts.map +1 -0
- package/dist/src/Schedule.js +1199 -0
- package/dist/src/Set.d.ts +181 -0
- package/dist/src/Set.d.ts.map +1 -0
- package/dist/src/Set.js +137 -0
- package/dist/src/Skiplist.d.ts +6 -1
- package/dist/src/Skiplist.d.ts.map +1 -1
- package/dist/src/Skiplist.js +5 -0
- package/dist/src/Sqlite.d.ts +160 -60
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +290 -190
- package/dist/src/Store.d.ts +36 -28
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +55 -15
- package/dist/src/String.d.ts +5 -0
- package/dist/src/String.d.ts.map +1 -1
- package/dist/src/String.js +5 -0
- package/dist/src/Task.d.ts +2263 -423
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +1488 -348
- package/dist/src/Test.d.ts +122 -0
- package/dist/src/Test.d.ts.map +1 -0
- package/dist/src/Test.js +66 -0
- package/dist/src/Time.d.ts +201 -48
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +190 -86
- package/dist/src/Tracer.d.ts +48 -0
- package/dist/src/Tracer.d.ts.map +1 -0
- package/dist/src/Tracer.js +6 -0
- package/dist/src/Type.d.ts +582 -209
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +362 -240
- package/dist/src/Types.d.ts +160 -6
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/Types.js +22 -2
- package/dist/src/WebSocket.d.ts +113 -58
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +210 -118
- package/dist/src/Worker.d.ts +245 -60
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +220 -64
- package/dist/src/index.d.ts +25 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +19 -3
- package/dist/src/local-first/Db.d.ts +24 -213
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +622 -293
- package/dist/src/local-first/Error.d.ts +12 -0
- package/dist/src/local-first/Error.d.ts.map +1 -0
- package/dist/src/local-first/Error.js +6 -0
- package/dist/src/local-first/Evolu.d.ts +330 -285
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +472 -402
- package/dist/src/local-first/LocalAuth.d.ts +10 -10
- package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
- package/dist/src/local-first/LocalAuth.js +6 -1
- package/dist/src/local-first/Owner.d.ts +70 -27
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +27 -14
- package/dist/src/local-first/Protocol.d.ts +58 -50
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +253 -155
- package/dist/src/local-first/Query.d.ts +133 -43
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Query.js +139 -90
- package/dist/src/local-first/Relay.d.ts +18 -38
- package/dist/src/local-first/Relay.d.ts.map +1 -1
- package/dist/src/local-first/Relay.js +59 -171
- package/dist/src/local-first/Schema.d.ts +139 -144
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +78 -163
- package/dist/src/local-first/Shared.d.ts +125 -0
- package/dist/src/local-first/Shared.d.ts.map +1 -0
- package/dist/src/local-first/Shared.js +569 -0
- package/dist/src/local-first/Storage.d.ts +50 -53
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +121 -173
- package/dist/src/local-first/Timestamp.d.ts +28 -35
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +24 -25
- package/dist/src/local-first/index.d.ts +2 -15
- package/dist/src/local-first/index.d.ts.map +1 -1
- package/dist/src/local-first/index.js +2 -15
- package/package.json +36 -26
- package/src/Array.ts +467 -106
- package/src/Assert.ts +87 -12
- package/src/BigInt.ts +7 -1
- package/src/Brand.ts +41 -12
- package/src/Buffer.ts +10 -4
- package/src/Cache.ts +11 -2
- package/src/Callbacks.ts +33 -23
- package/src/Console.ts +567 -178
- package/src/Crypto.ts +111 -62
- package/src/Eq.ts +30 -3
- package/src/Error.ts +50 -34
- package/src/Function.ts +140 -68
- package/src/Identicon.ts +7 -1
- package/src/Lookup.ts +415 -0
- package/src/Microtask.ts +58 -0
- package/src/Number.ts +40 -7
- package/src/Object.ts +155 -16
- package/src/Option.ts +74 -0
- package/src/Order.ts +11 -3
- package/src/Platform.ts +43 -7
- package/src/Polyfills.ts +465 -0
- package/src/Random.ts +25 -11
- package/src/Redacted.ts +8 -4
- package/src/Ref.ts +72 -21
- package/src/RefCount.ts +170 -0
- package/src/Relation.ts +188 -136
- package/src/Resource.ts +864 -0
- package/src/Result.ts +548 -381
- package/src/Schedule.ts +1471 -0
- package/src/Set.ts +247 -0
- package/src/Skiplist.ts +7 -1
- package/src/Sqlite.ts +409 -304
- package/src/Store.ts +94 -48
- package/src/String.ts +6 -0
- package/src/Task.ts +3685 -740
- package/src/Test.ts +162 -0
- package/src/Time.ts +348 -145
- package/src/Tracer.ts +54 -0
- package/src/Type.ts +1064 -630
- package/src/Types.ts +202 -7
- package/src/WebSocket.ts +311 -178
- package/src/Worker.ts +527 -147
- package/src/index.ts +74 -3
- package/src/local-first/Db.ts +871 -597
- package/src/local-first/Error.ts +17 -0
- package/src/local-first/Evolu.ts +884 -823
- package/src/local-first/LocalAuth.ts +10 -10
- package/src/local-first/Owner.ts +94 -42
- package/src/local-first/Protocol.ts +440 -303
- package/src/local-first/Query.ts +243 -194
- package/src/local-first/Relay.ts +91 -252
- package/src/local-first/Schema.ts +284 -394
- package/src/local-first/Shared.ts +932 -0
- package/src/local-first/Storage.ts +209 -282
- package/src/local-first/Timestamp.ts +38 -45
- package/src/local-first/index.ts +2 -16
- package/LICENSE +0 -21
- package/dist/src/Instances.d.ts +0 -34
- package/dist/src/Instances.d.ts.map +0 -1
- package/dist/src/Instances.js +0 -44
- package/dist/src/Resources.d.ts +0 -118
- package/dist/src/Resources.d.ts.map +0 -1
- package/dist/src/Resources.js +0 -197
- package/dist/src/local-first/Platform.d.ts +0 -25
- package/dist/src/local-first/Platform.d.ts.map +0 -1
- package/dist/src/local-first/Platform.js +0 -1
- package/dist/src/local-first/Public.d.ts +0 -22
- package/dist/src/local-first/Public.d.ts.map +0 -1
- package/dist/src/local-first/Public.js +0 -15
- package/dist/src/local-first/PublicKysely.d.ts +0 -148
- package/dist/src/local-first/PublicKysely.d.ts.map +0 -1
- package/dist/src/local-first/PublicKysely.js +0 -184
- package/dist/src/local-first/Sync.d.ts +0 -112
- package/dist/src/local-first/Sync.d.ts.map +0 -1
- package/dist/src/local-first/Sync.js +0 -529
- package/src/Instances.ts +0 -90
- package/src/Resources.ts +0 -367
- package/src/local-first/Platform.ts +0 -27
- package/src/local-first/Public.ts +0 -42
- package/src/local-first/PublicKysely.ts +0 -239
- package/src/local-first/Sync.ts +0 -960
package/src/Test.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test utilities for deterministic testing.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
testCreateConsole,
|
|
9
|
+
type TestConsole,
|
|
10
|
+
type TestConsoleDep,
|
|
11
|
+
} from "./Console.js";
|
|
12
|
+
import { testCreateRandomBytes, type RandomBytes } from "./Crypto.js";
|
|
13
|
+
import {
|
|
14
|
+
type Random,
|
|
15
|
+
testCreateRandom,
|
|
16
|
+
testCreateRandomLib,
|
|
17
|
+
type RandomLibDep,
|
|
18
|
+
} from "./Random.js";
|
|
19
|
+
import { createRun, type Run, type RunDeps } from "./Task.js";
|
|
20
|
+
import {
|
|
21
|
+
minMillis,
|
|
22
|
+
setTimeout,
|
|
23
|
+
testCreateTime,
|
|
24
|
+
type Duration,
|
|
25
|
+
type TestTime,
|
|
26
|
+
type TestTimeDep,
|
|
27
|
+
} from "./Time.js";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Cheap deterministic baseline deps for tests.
|
|
31
|
+
*
|
|
32
|
+
* `TestDeps` includes test-friendly replacements for {@link RunDeps}, such as
|
|
33
|
+
* {@link TestConsole} and {@link TestTime}, plus extra helpers commonly useful in
|
|
34
|
+
* tests and fixtures, such as `randomLib` (only that for now).
|
|
35
|
+
*
|
|
36
|
+
* Use it directly for synchronous test setup, fixtures, and helpers. It is also
|
|
37
|
+
* intentionally the base deps used by {@link testCreateRun}.
|
|
38
|
+
*/
|
|
39
|
+
export type TestDeps = Omit<RunDeps, "console" | "time"> &
|
|
40
|
+
TestConsoleDep &
|
|
41
|
+
TestTimeDep &
|
|
42
|
+
RandomLibDep;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates deterministic {@link TestDeps}.
|
|
46
|
+
*
|
|
47
|
+
* Each call creates fresh instances, so tests don't share state.
|
|
48
|
+
*
|
|
49
|
+
* Includes these deps:
|
|
50
|
+
*
|
|
51
|
+
* - `console`: {@link TestConsole}
|
|
52
|
+
* - `random`: {@link Random}
|
|
53
|
+
* - `randomLib`: seeded `random` package instance
|
|
54
|
+
* - `randomBytes`: {@link RandomBytes}
|
|
55
|
+
* - `time`: {@link TestTime}
|
|
56
|
+
*
|
|
57
|
+
* Use this for synchronous code that accepts deps directly. For tests that run
|
|
58
|
+
* Tasks, use {@link testCreateRun}.
|
|
59
|
+
*
|
|
60
|
+
* ### Example
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* test("Callbacks with no argument", () => {
|
|
64
|
+
* const deps = testCreateDeps();
|
|
65
|
+
* const callbacks = createCallbacks(deps);
|
|
66
|
+
*
|
|
67
|
+
* let called = false;
|
|
68
|
+
* const id = callbacks.register(() => {
|
|
69
|
+
* called = true;
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* callbacks.execute(id);
|
|
73
|
+
* expect(called).toBe(true);
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export const testCreateDeps = (options?: {
|
|
78
|
+
readonly seed?: string;
|
|
79
|
+
}): TestDeps => {
|
|
80
|
+
const seed = options?.seed ?? "evolu";
|
|
81
|
+
const console = testCreateConsole();
|
|
82
|
+
const random = testCreateRandom(seed);
|
|
83
|
+
const randomLib = testCreateRandomLib(seed);
|
|
84
|
+
const randomBytes = testCreateRandomBytes({ randomLib });
|
|
85
|
+
const time = testCreateTime();
|
|
86
|
+
return { console, randomBytes, random, randomLib, time };
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a test {@link Run} with deterministic default deps or optional custom
|
|
91
|
+
* deps.
|
|
92
|
+
*
|
|
93
|
+
* Use this as the default composition root in tests. Each call creates fresh
|
|
94
|
+
* {@link TestDeps} via {@link testCreateDeps}, then merges any provided custom
|
|
95
|
+
* deps.
|
|
96
|
+
*
|
|
97
|
+
* ### Example
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* // Built-in TestTime
|
|
101
|
+
* await using run = testCreateRun();
|
|
102
|
+
* const fiber = run(sleep("1s"));
|
|
103
|
+
* run.deps.time.advance("1s");
|
|
104
|
+
* await fiber;
|
|
105
|
+
*
|
|
106
|
+
* // For a single test, create the dependency using the Run.
|
|
107
|
+
* await using run = testCreateRun();
|
|
108
|
+
* await using foo = await run.orThrow(createFoo());
|
|
109
|
+
* const runWithFoo = run.addDeps({ foo });
|
|
110
|
+
*
|
|
111
|
+
* expect(runWithFoo.deps.foo).toBe(foo);
|
|
112
|
+
*
|
|
113
|
+
* // If multiple tests need the same setup, create a disposable helper.
|
|
114
|
+
* // Then `await using setup` disposes everything the helper owns.
|
|
115
|
+
* const setupFoo = async () => {
|
|
116
|
+
* await using stack = new AsyncDisposableStack();
|
|
117
|
+
* const run = stack.use(testCreateRun());
|
|
118
|
+
* const foo = stack.use(await run.orThrow(createFoo()));
|
|
119
|
+
* const moved = stack.move();
|
|
120
|
+
*
|
|
121
|
+
* // Return whatever the tests need: a Run with the dependency,
|
|
122
|
+
* // the dependency itself, or both.
|
|
123
|
+
* return {
|
|
124
|
+
* run: run.addDeps({ foo }),
|
|
125
|
+
* foo,
|
|
126
|
+
* [Symbol.asyncDispose]: () => moved.disposeAsync(),
|
|
127
|
+
* };
|
|
128
|
+
* };
|
|
129
|
+
*
|
|
130
|
+
* await using setup = await setupFoo();
|
|
131
|
+
* const { run, foo } = setup;
|
|
132
|
+
* expect(run.deps.foo).toBe(foo);
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* Name reusable setup helpers after what they set up:
|
|
136
|
+
*
|
|
137
|
+
* - `setupFoo` for reusable test setup for `Foo`
|
|
138
|
+
* - `testSetupFoo` when a library module exports the helper as part of its public
|
|
139
|
+
* test API, e.g. `testSetupSqlite`.
|
|
140
|
+
*/
|
|
141
|
+
export function testCreateRun(): Run<TestDeps>;
|
|
142
|
+
|
|
143
|
+
/** With custom dependencies merged into {@link TestDeps}. */
|
|
144
|
+
export function testCreateRun<D>(deps: D): Run<TestDeps & D>;
|
|
145
|
+
|
|
146
|
+
export function testCreateRun<D>(deps?: D): Run<TestDeps & D> {
|
|
147
|
+
const defaults = testCreateDeps();
|
|
148
|
+
return createRun<TestDeps & D>({ ...defaults, ...deps } as TestDeps & D);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Returns a Promise that resolves after a macrotask delay.
|
|
153
|
+
*
|
|
154
|
+
* Use this to model a real async boundary in a test double, for example an
|
|
155
|
+
* async disposer that should not complete in the same turn.
|
|
156
|
+
*
|
|
157
|
+
* Avoid using it to coordinate assertions. Waiting for a macrotask tends to
|
|
158
|
+
* make tests indirect and brittle.
|
|
159
|
+
*/
|
|
160
|
+
export const testWaitForMacrotask = (
|
|
161
|
+
duration: Duration = minMillis,
|
|
162
|
+
): Promise<void> => setTimeout(duration);
|