@evolu/common 8.0.0-next.5 → 8.0.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/dist/src/Array.d.ts +279 -173
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +105 -101
- package/dist/src/Assert.d.ts +39 -44
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +38 -31
- package/dist/src/BigInt.d.ts +6 -3
- package/dist/src/BigInt.d.ts.map +1 -1
- package/dist/src/BigInt.js +5 -2
- package/dist/src/Brand.d.ts +27 -40
- package/dist/src/Brand.d.ts.map +1 -1
- package/dist/src/Buffer.d.ts +27 -11
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Cache.d.ts +9 -4
- package/dist/src/Cache.d.ts.map +1 -1
- package/dist/src/Cache.js +8 -3
- package/dist/src/Callbacks.d.ts +30 -19
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Console.d.ts +145 -66
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +91 -43
- package/dist/src/Crypto.d.ts +85 -42
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +44 -20
- package/dist/src/Eq.d.ts +87 -21
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +67 -19
- package/dist/src/Error.d.ts +3 -3
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Function.d.ts +119 -58
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +86 -43
- package/dist/src/Http.d.ts +394 -0
- package/dist/src/Http.d.ts.map +1 -0
- package/dist/src/Http.js +102 -0
- package/dist/src/Identicon.d.ts +16 -7
- package/dist/src/Identicon.d.ts.map +1 -1
- package/dist/src/Identicon.js +14 -5
- package/dist/src/LeakDetector.d.ts +88 -0
- package/dist/src/LeakDetector.d.ts.map +1 -0
- package/dist/src/LeakDetector.js +86 -0
- package/dist/src/LockManager.d.ts +18 -9
- package/dist/src/LockManager.d.ts.map +1 -1
- package/dist/src/LockManager.js +7 -8
- package/dist/src/Lookup.d.ts +43 -13
- package/dist/src/Lookup.d.ts.map +1 -1
- package/dist/src/Lookup.js +28 -8
- package/dist/src/Microtask.d.ts +1 -1
- package/dist/src/Microtask.d.ts.map +1 -1
- package/dist/src/Microtask.js +3 -7
- package/dist/src/Number.d.ts +49 -13
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +34 -5
- package/dist/src/Object.d.ts +160 -40
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +92 -46
- package/dist/src/Option.d.ts +10 -7
- package/dist/src/Option.d.ts.map +1 -1
- package/dist/src/Option.js +9 -6
- package/dist/src/Order.d.ts +21 -21
- package/dist/src/Order.d.ts.map +1 -1
- package/dist/src/Order.js +18 -21
- package/dist/src/Platform.d.ts +31 -0
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +98 -0
- package/dist/src/Random.d.ts +32 -17
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +12 -8
- package/dist/src/Redacted.d.ts +43 -29
- package/dist/src/Redacted.d.ts.map +1 -1
- package/dist/src/Redacted.js +14 -3
- package/dist/src/Ref.d.ts +11 -3
- package/dist/src/Ref.d.ts.map +1 -1
- package/dist/src/Ref.js +2 -2
- package/dist/src/RefCount.d.ts +2 -6
- package/dist/src/RefCount.d.ts.map +1 -1
- package/dist/src/RefCount.js +33 -90
- package/dist/src/Relation.d.ts +70 -5
- package/dist/src/Relation.d.ts.map +1 -1
- package/dist/src/Relation.js +77 -2
- package/dist/src/Resource.d.ts +590 -155
- package/dist/src/Resource.d.ts.map +1 -1
- package/dist/src/Resource.js +592 -343
- package/dist/src/Result.d.ts +838 -191
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +163 -102
- package/dist/src/Schedule.d.ts +684 -231
- package/dist/src/Schedule.d.ts.map +1 -1
- package/dist/src/Schedule.js +766 -295
- package/dist/src/Set.d.ts +89 -40
- package/dist/src/Set.d.ts.map +1 -1
- package/dist/src/Set.js +50 -29
- package/dist/src/Sqlite.d.ts +29 -72
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +39 -45
- package/dist/src/StackTrace.d.ts +40 -0
- package/dist/src/StackTrace.d.ts.map +1 -0
- package/dist/src/StackTrace.js +140 -0
- package/dist/src/Store.d.ts +5 -4
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +5 -4
- package/dist/src/String.d.ts +7 -1
- package/dist/src/String.d.ts.map +1 -1
- package/dist/src/String.js +20 -7
- package/dist/src/Task.d.ts +3428 -1629
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +1876 -1279
- package/dist/src/Test.d.ts +17 -124
- package/dist/src/Test.d.ts.map +1 -1
- package/dist/src/Test.js +17 -68
- package/dist/src/Time.d.ts +108 -49
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +172 -73
- package/dist/src/Type.d.ts +3542 -1926
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +3070 -1784
- package/dist/src/Types.d.ts +200 -100
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/Types.js +39 -8
- package/dist/src/WebSocket.d.ts +48 -23
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +15 -6
- package/dist/src/Worker.d.ts +94 -36
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +58 -24
- package/dist/src/index.d.ts +55 -52
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/intl/cs.d.ts +94 -0
- package/dist/src/intl/cs.d.ts.map +1 -0
- package/dist/src/intl/cs.js +220 -0
- package/dist/src/intl/index.d.ts +7 -0
- package/dist/src/intl/index.d.ts.map +1 -0
- package/dist/src/intl/index.js +6 -0
- package/dist/src/local-first/Db.d.ts +12 -8
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +107 -130
- package/dist/src/local-first/Error.d.ts +4 -4
- package/dist/src/local-first/Evolu.d.ts +338 -92
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +47 -55
- package/dist/src/local-first/LocalAuth.d.ts +4 -4
- package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
- package/dist/src/local-first/Owner.d.ts +61 -24
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +45 -8
- package/dist/src/local-first/Protocol.d.ts +44 -31
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +36 -13
- package/dist/src/local-first/Query.d.ts +147 -62
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Query.js +112 -58
- package/dist/src/local-first/Relay.d.ts +44 -14
- package/dist/src/local-first/Relay.d.ts.map +1 -1
- package/dist/src/local-first/Schema.d.ts +92 -41
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +25 -8
- package/dist/src/local-first/Shared.d.ts +23 -21
- package/dist/src/local-first/Shared.d.ts.map +1 -1
- package/dist/src/local-first/Shared.js +85 -42
- package/dist/src/local-first/Storage.d.ts +53 -32
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +194 -234
- package/dist/src/local-first/Timestamp.d.ts +21 -21
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +2 -2
- package/dist/src/local-first/index.d.ts +11 -11
- package/package.json +14 -13
- package/src/Array.ts +306 -205
- package/src/Assert.ts +48 -68
- package/src/BigInt.ts +6 -3
- package/src/Brand.ts +27 -40
- package/src/Buffer.ts +28 -12
- package/src/Cache.ts +9 -4
- package/src/Callbacks.ts +30 -19
- package/src/Console.ts +156 -84
- package/src/Crypto.ts +83 -41
- package/src/Eq.ts +95 -22
- package/src/Error.ts +5 -2
- package/src/Function.ts +167 -59
- package/src/Http.ts +549 -0
- package/src/Identicon.ts +16 -7
- package/src/LeakDetector.ts +182 -0
- package/src/LockManager.ts +43 -39
- package/src/Lookup.ts +62 -28
- package/src/Microtask.ts +19 -23
- package/src/Number.ts +64 -12
- package/src/Object.ts +173 -49
- package/src/Option.ts +10 -7
- package/src/Order.ts +23 -21
- package/src/Platform.ts +136 -0
- package/src/Polyfills.ts +6 -12
- package/src/Random.ts +32 -19
- package/src/Redacted.ts +44 -30
- package/src/Ref.ts +14 -6
- package/src/RefCount.ts +38 -80
- package/src/Relation.ts +202 -10
- package/src/Resource.ts +1199 -591
- package/src/Result.ts +930 -271
- package/src/Schedule.ts +859 -343
- package/src/Set.ts +89 -40
- package/src/Sqlite.ts +142 -137
- package/src/StackTrace.ts +230 -0
- package/src/Store.ts +8 -7
- package/src/String.ts +22 -5
- package/src/Task.ts +5109 -2845
- package/src/Test.ts +20 -167
- package/src/Time.ts +293 -90
- package/src/Type.ts +10034 -4117
- package/src/Types.ts +227 -101
- package/src/WebSocket.ts +64 -36
- package/src/Worker.ts +180 -72
- package/src/index.ts +55 -52
- package/src/intl/cs.ts +388 -0
- package/src/intl/index.ts +7 -0
- package/src/local-first/Db.ts +161 -190
- package/src/local-first/Error.ts +4 -4
- package/src/local-first/Evolu.ts +495 -250
- package/src/local-first/LocalAuth.ts +6 -12
- package/src/local-first/Owner.ts +58 -21
- package/src/local-first/Protocol.ts +49 -31
- package/src/local-first/Query.ts +146 -61
- package/src/local-first/Relay.ts +51 -21
- package/src/local-first/Schema.ts +109 -51
- package/src/local-first/Shared.ts +253 -214
- package/src/local-first/Storage.ts +266 -264
- package/src/local-first/Timestamp.ts +25 -18
- package/src/local-first/index.ts +11 -11
- package/dist/src/Skiplist.d.ts +0 -28
- package/dist/src/Skiplist.d.ts.map +0 -1
- package/dist/src/Skiplist.js +0 -64
- package/dist/src/Tracer.d.ts +0 -48
- package/dist/src/Tracer.d.ts.map +0 -1
- package/dist/src/Tracer.js +0 -6
- package/src/Skiplist.ts +0 -108
- package/src/Tracer.ts +0 -54
package/src/Test.ts
CHANGED
|
@@ -1,171 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Cross-module test utilities for deterministic testing.
|
|
3
|
+
*
|
|
4
|
+
* Test helpers are usually colocated with the code they test. Helpers that
|
|
5
|
+
* cannot be colocated because they would create dependency cycles belong here.
|
|
3
6
|
*
|
|
4
7
|
* @module
|
|
5
8
|
*/
|
|
6
9
|
|
|
7
|
-
import type { Brand } from "./Brand.
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
type TestConsoleDep,
|
|
12
|
-
} from "./Console.js";
|
|
13
|
-
import { testCreateRandomBytes, type RandomBytes } from "./Crypto.js";
|
|
14
|
-
import {
|
|
15
|
-
testCreateRandom,
|
|
16
|
-
testCreateRandomLib,
|
|
17
|
-
type Random,
|
|
18
|
-
type RandomLibDep,
|
|
19
|
-
} from "./Random.js";
|
|
20
|
-
import { createRun, type Run, type RunDefaultDeps } from "./Task.js";
|
|
21
|
-
import {
|
|
22
|
-
minMillis,
|
|
23
|
-
setTimeout,
|
|
24
|
-
testCreateTime,
|
|
25
|
-
type Duration,
|
|
26
|
-
type TestTime,
|
|
27
|
-
type TestTimeDep,
|
|
28
|
-
} from "./Time.js";
|
|
29
|
-
import { createId, type Id } from "./Type.js";
|
|
10
|
+
import type { Brand } from "./Brand.ts";
|
|
11
|
+
import { testCreateRandomBytes } from "./Crypto.ts";
|
|
12
|
+
import { testCreateRandomLib } from "./Random.ts";
|
|
13
|
+
import { createId, type Id } from "./Type.ts";
|
|
30
14
|
|
|
31
15
|
export type TestCreateId = <B extends string = never>() => [B] extends [never]
|
|
32
16
|
? Id
|
|
33
17
|
: Id & Brand<B>;
|
|
34
18
|
|
|
35
|
-
/**
|
|
36
|
-
* Cheap deterministic baseline deps for tests.
|
|
37
|
-
*
|
|
38
|
-
* `TestDeps` includes test-friendly replacements for {@link RunDefaultDeps},
|
|
39
|
-
* such as {@link TestConsole} and {@link TestTime}, plus extra helpers commonly
|
|
40
|
-
* useful in tests and fixtures, such as `randomLib` (only that for now).
|
|
41
|
-
*
|
|
42
|
-
* Use it directly for synchronous test setup, fixtures, and helpers. It is also
|
|
43
|
-
* intentionally the base deps used by {@link testCreateRun}.
|
|
44
|
-
*/
|
|
45
|
-
export type TestDeps = Omit<RunDefaultDeps, "console" | "time"> &
|
|
46
|
-
TestConsoleDep &
|
|
47
|
-
TestTimeDep &
|
|
48
|
-
RandomLibDep;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Creates deterministic {@link TestDeps}.
|
|
52
|
-
*
|
|
53
|
-
* Each call creates fresh instances, so tests don't share state.
|
|
54
|
-
*
|
|
55
|
-
* Includes these deps:
|
|
56
|
-
*
|
|
57
|
-
* - `console`: {@link TestConsole}
|
|
58
|
-
* - `random`: {@link Random}
|
|
59
|
-
* - `randomLib`: seeded `random` package instance
|
|
60
|
-
* - `randomBytes`: {@link RandomBytes}
|
|
61
|
-
* - `time`: {@link TestTime}
|
|
62
|
-
*
|
|
63
|
-
* Use this for synchronous code that accepts deps directly. For tests that run
|
|
64
|
-
* Tasks, use {@link testCreateRun}.
|
|
65
|
-
*
|
|
66
|
-
* ### Example
|
|
67
|
-
*
|
|
68
|
-
* ```ts
|
|
69
|
-
* test("Callbacks with no argument", () => {
|
|
70
|
-
* const deps = testCreateDeps();
|
|
71
|
-
* const callbacks = createCallbacks(deps);
|
|
72
|
-
*
|
|
73
|
-
* let called = false;
|
|
74
|
-
* const id = callbacks.register(() => {
|
|
75
|
-
* called = true;
|
|
76
|
-
* });
|
|
77
|
-
*
|
|
78
|
-
* callbacks.execute(id);
|
|
79
|
-
* expect(called).toBe(true);
|
|
80
|
-
* });
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export const testCreateDeps = (options?: {
|
|
84
|
-
readonly seed?: string;
|
|
85
|
-
}): TestDeps => {
|
|
86
|
-
const seed = options?.seed ?? "evolu";
|
|
87
|
-
const console = testCreateConsole();
|
|
88
|
-
const random = testCreateRandom(seed);
|
|
89
|
-
const randomLib = testCreateRandomLib(seed);
|
|
90
|
-
const randomBytes = testCreateRandomBytes({ randomLib });
|
|
91
|
-
const time = testCreateTime();
|
|
92
|
-
return { console, randomBytes, random, randomLib, time };
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Creates a test {@link Run} with deterministic default deps or optional custom
|
|
97
|
-
* deps.
|
|
98
|
-
*
|
|
99
|
-
* Use this as the default composition root in tests. Each call creates fresh
|
|
100
|
-
* {@link TestDeps} via {@link testCreateDeps}, then merges any provided custom
|
|
101
|
-
* deps.
|
|
102
|
-
*
|
|
103
|
-
* ### Example
|
|
104
|
-
*
|
|
105
|
-
* ```ts
|
|
106
|
-
* // Default TestTime
|
|
107
|
-
* await using run = testCreateRun();
|
|
108
|
-
* const fiber = run(sleep("1s"));
|
|
109
|
-
* run.deps.time.advance("1s");
|
|
110
|
-
* await fiber;
|
|
111
|
-
*
|
|
112
|
-
* // For a single test, create the dependency using the Run.
|
|
113
|
-
* await using run = testCreateRun();
|
|
114
|
-
* await using foo = await run.orThrow(createFoo());
|
|
115
|
-
* await run.orThrow(taskThatNeedsFoo, { foo });
|
|
116
|
-
*
|
|
117
|
-
* // If multiple tests need the same setup, create a disposable helper.
|
|
118
|
-
* // Then `await using setup` disposes everything the helper owns.
|
|
119
|
-
* const setupFoo = async () => {
|
|
120
|
-
* await using disposer = new AsyncDisposableStack();
|
|
121
|
-
* const run = disposer.use(testCreateRun());
|
|
122
|
-
* const foo = disposer.use(await run.orThrow(createFoo()));
|
|
123
|
-
* const runWithFoo = disposer.use(run.create({ ...run.deps, foo }));
|
|
124
|
-
* const disposables = disposer.move();
|
|
125
|
-
*
|
|
126
|
-
* // Return whatever the tests need: a Run with the dependency,
|
|
127
|
-
* // the dependency itself, or both.
|
|
128
|
-
* return {
|
|
129
|
-
* run: runWithFoo,
|
|
130
|
-
* foo,
|
|
131
|
-
* [Symbol.asyncDispose]: () => disposables.disposeAsync(),
|
|
132
|
-
* };
|
|
133
|
-
* };
|
|
134
|
-
*
|
|
135
|
-
* await using setup = await setupFoo();
|
|
136
|
-
* const { run, foo } = setup;
|
|
137
|
-
* expect(run.deps.foo).toBe(foo);
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* Name reusable setup helpers after what they set up:
|
|
141
|
-
*
|
|
142
|
-
* - `setupFoo` for reusable test setup for `Foo`
|
|
143
|
-
* - `testSetupFoo` when a library module exports the helper as part of its public
|
|
144
|
-
* test API, e.g. `testSetupSqlite`.
|
|
145
|
-
*/
|
|
146
|
-
export function testCreateRun(): Run<TestDeps>;
|
|
147
|
-
|
|
148
|
-
/** With custom dependencies merged into {@link TestDeps}. */
|
|
149
|
-
export function testCreateRun<D>(deps: D): Run<TestDeps & D>;
|
|
150
|
-
|
|
151
|
-
export function testCreateRun<D>(deps?: D): Run<TestDeps & D> {
|
|
152
|
-
const defaults = testCreateDeps();
|
|
153
|
-
return createRun<TestDeps & D>({ ...defaults, ...deps } as TestDeps & D);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Returns a Promise that resolves after a macrotask delay.
|
|
158
|
-
*
|
|
159
|
-
* Use this to model a real async boundary in a test double, for example an
|
|
160
|
-
* async disposer that should not complete in the same turn.
|
|
161
|
-
*
|
|
162
|
-
* Avoid using it to coordinate assertions. Waiting for a macrotask tends to
|
|
163
|
-
* make tests indirect and brittle.
|
|
164
|
-
*/
|
|
165
|
-
export const testWaitForMacrotask = (
|
|
166
|
-
duration: Duration = minMillis,
|
|
167
|
-
): Promise<void> => setTimeout(duration);
|
|
168
|
-
|
|
169
19
|
/**
|
|
170
20
|
* Creates a deterministic `createId` helper.
|
|
171
21
|
*
|
|
@@ -181,13 +31,18 @@ export const testWaitForMacrotask = (
|
|
|
181
31
|
* ### Example
|
|
182
32
|
*
|
|
183
33
|
* ```ts
|
|
184
|
-
*
|
|
185
|
-
*
|
|
34
|
+
* import { testCreateId, type Brand, type Id } from "@evolu/common";
|
|
35
|
+
*
|
|
36
|
+
* const createId = testCreateId();
|
|
37
|
+
*
|
|
38
|
+
* const callbackId = createId();
|
|
39
|
+
* const secondCallbackId = createId();
|
|
40
|
+
* const todoId = createId<"Todo">();
|
|
41
|
+
* expect(new Set([callbackId, secondCallbackId, todoId]).size).toBe(3);
|
|
186
42
|
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* });
|
|
43
|
+
* const replayCreateId = testCreateId();
|
|
44
|
+
* expect(replayCreateId()).toBe(callbackId);
|
|
45
|
+
* expectTypeOf(todoId).toEqualTypeOf<Id & Brand<"Todo">>();
|
|
191
46
|
* ```
|
|
192
47
|
*/
|
|
193
48
|
export const testCreateId = (): TestCreateId => {
|
|
@@ -195,7 +50,5 @@ export const testCreateId = (): TestCreateId => {
|
|
|
195
50
|
randomLib: testCreateRandomLib(),
|
|
196
51
|
});
|
|
197
52
|
|
|
198
|
-
return
|
|
199
|
-
? Id
|
|
200
|
-
: Id & Brand<B> => createId<B>({ randomBytes });
|
|
53
|
+
return (() => createId<never>({ randomBytes })) as TestCreateId;
|
|
201
54
|
};
|