@ac-kit/test-util 0.2.4 → 0.3.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/CHANGELOG.md +24 -0
- package/README.md +16 -0
- package/dist/fake-wall-clock.d.ts +51 -0
- package/dist/fake-wall-clock.d.ts.map +1 -0
- package/dist/fake-wall-clock.js +69 -0
- package/dist/fake-wall-clock.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @ac-kit/test-util
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 2a0fe74: `FakeWallClock` is a wall clock for tests: `instant()` reads the time it holds as a `Temporal.Instant` (a function property, so it can be passed as an `InstantSource` without binding), `epochMilliseconds` reads it as a number, `set(ms)` moves it to any time and `advance(ms)` moves it by any amount, negative included. Unlike `FakeClock` it has no timers and can go backwards. It throws `RangeError`, leaving the time unchanged, for a time that is not an integer a `Temporal.Instant` can hold.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [2a0fe74]
|
|
12
|
+
- Updated dependencies [5152230]
|
|
13
|
+
- Updated dependencies [2a0fe74]
|
|
14
|
+
- Updated dependencies [5152230]
|
|
15
|
+
- Updated dependencies [5152230]
|
|
16
|
+
- Updated dependencies [2a0fe74]
|
|
17
|
+
- Updated dependencies [2a0fe74]
|
|
18
|
+
- Updated dependencies [5152230]
|
|
19
|
+
- Updated dependencies [5152230]
|
|
20
|
+
- Updated dependencies [5152230]
|
|
21
|
+
- Updated dependencies [5152230]
|
|
22
|
+
- Updated dependencies [5152230]
|
|
23
|
+
- Updated dependencies [5152230]
|
|
24
|
+
- Updated dependencies [5152230]
|
|
25
|
+
- @ac-kit/core@0.10.0
|
|
26
|
+
|
|
3
27
|
## 0.2.4
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -20,6 +20,22 @@ Also exposes `expectCompareAgreesWithEquals`, for asserting that a type's
|
|
|
20
20
|
comparator and its equality agree — the invariant an ordered container silently
|
|
21
21
|
relies on.
|
|
22
22
|
|
|
23
|
+
`FakeClock` is a monotonic `Clock` with timers that only moves when told to, for
|
|
24
|
+
code that schedules work. `FakeWallClock` is the wall clock: it reads a
|
|
25
|
+
`Temporal.Instant` you set, and unlike a real one (or a `FakeClock`) it can be
|
|
26
|
+
set backwards, to test code that must survive a clock that jumps.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { FakeWallClock } from "@ac-kit/test-util";
|
|
30
|
+
|
|
31
|
+
const clock = new FakeWallClock(1_700_000_000_000);
|
|
32
|
+
const now = clock.instant; // an `InstantSource`, usable as `{ now }`
|
|
33
|
+
|
|
34
|
+
clock.advance(5);
|
|
35
|
+
clock.set(1_000); // back in time
|
|
36
|
+
now().epochMilliseconds; // 1000
|
|
37
|
+
```
|
|
38
|
+
|
|
23
39
|
For helpers that drive and probe real external services (Docker containers,
|
|
24
40
|
TCP/TLS endpoints), see `@ac-kit/integration-test-util` instead — that package
|
|
25
41
|
is allowed heavier dependencies precisely because its own consumers are few.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type InstantSource } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* A wall clock that reads what it is told: the `now` of code that stamps events
|
|
4
|
+
* with a date, without fake global timers. Hand `clock.instant` to an
|
|
5
|
+
* {@link InstantSource} option.
|
|
6
|
+
*
|
|
7
|
+
* Unlike a real wall clock it never moves by itself, and unlike
|
|
8
|
+
* {@link FakeClock} it may go backwards, so it can play the jumps an NTP step, a
|
|
9
|
+
* manual change or a restored snapshot make: {@link FakeWallClock.set} accepts
|
|
10
|
+
* any time and {@link FakeWallClock.advance} a negative amount. It has no
|
|
11
|
+
* timers.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const clock = new FakeWallClock(1_700_000_000_000);
|
|
15
|
+
* const generate = createUuidV7Generator({ now: clock.instant });
|
|
16
|
+
* clock.advance(5);
|
|
17
|
+
* clock.set(1_000); // back in time
|
|
18
|
+
*/
|
|
19
|
+
export declare class FakeWallClock {
|
|
20
|
+
private milliseconds;
|
|
21
|
+
/**
|
|
22
|
+
* The current time as a `Temporal.Instant`. A property holding a function, so
|
|
23
|
+
* it can be passed as is where an {@link InstantSource} is wanted.
|
|
24
|
+
*/
|
|
25
|
+
readonly instant: InstantSource;
|
|
26
|
+
/**
|
|
27
|
+
* @param start - The time to start at, in milliseconds since the Unix epoch,
|
|
28
|
+
* 0 by default.
|
|
29
|
+
* @throws {RangeError} When `start` is not an integer a `Temporal.Instant`
|
|
30
|
+
* can hold (±8.64 × 10¹⁵).
|
|
31
|
+
*/
|
|
32
|
+
constructor(start?: number);
|
|
33
|
+
/** The current time in milliseconds since the Unix epoch. */
|
|
34
|
+
get epochMilliseconds(): number;
|
|
35
|
+
/**
|
|
36
|
+
* Sets the time, to any value: before or after the current one.
|
|
37
|
+
*
|
|
38
|
+
* @throws {RangeError} When `epochMilliseconds` is not an integer a
|
|
39
|
+
* `Temporal.Instant` can hold. The time is then unchanged.
|
|
40
|
+
*/
|
|
41
|
+
set(epochMilliseconds: number): void;
|
|
42
|
+
/**
|
|
43
|
+
* Moves the time by `milliseconds`, which may be negative.
|
|
44
|
+
*
|
|
45
|
+
* @throws {RangeError} When `milliseconds` is not an integer, or the result
|
|
46
|
+
* is outside what a `Temporal.Instant` can hold. The time is then
|
|
47
|
+
* unchanged.
|
|
48
|
+
*/
|
|
49
|
+
advance(milliseconds: number): void;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=fake-wall-clock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-wall-clock.d.ts","sourceRoot":"","sources":["../src/fake-wall-clock.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,aAAa,EAGlB,MAAM,cAAc,CAAC;AAgBtB;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,aAAa;IACzB,OAAO,CAAC,YAAY,CAAS;IAE7B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,aAAa,CAC4B;IAE3D;;;;;OAKG;IACH,YAAY,KAAK,SAAI,EAIpB;IAED,6DAA6D;IAC7D,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED;;;;;OAKG;IACH,GAAG,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAInC;IAED;;;;;;OAMG;IACH,OAAO,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAQlC;CACD"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { isIntegerInRange, MAX_INSTANT_EPOCH_MILLISECONDS, } from "@ac-kit/core";
|
|
2
|
+
function assertEpochMilliseconds_(name, milliseconds) {
|
|
3
|
+
if (!isIntegerInRange(milliseconds, -MAX_INSTANT_EPOCH_MILLISECONDS, MAX_INSTANT_EPOCH_MILLISECONDS + 1)) {
|
|
4
|
+
throw new RangeError(`FakeWallClock: ${name} ${String(milliseconds)} is not an integer number of milliseconds a Temporal.Instant can hold`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A wall clock that reads what it is told: the `now` of code that stamps events
|
|
9
|
+
* with a date, without fake global timers. Hand `clock.instant` to an
|
|
10
|
+
* {@link InstantSource} option.
|
|
11
|
+
*
|
|
12
|
+
* Unlike a real wall clock it never moves by itself, and unlike
|
|
13
|
+
* {@link FakeClock} it may go backwards, so it can play the jumps an NTP step, a
|
|
14
|
+
* manual change or a restored snapshot make: {@link FakeWallClock.set} accepts
|
|
15
|
+
* any time and {@link FakeWallClock.advance} a negative amount. It has no
|
|
16
|
+
* timers.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const clock = new FakeWallClock(1_700_000_000_000);
|
|
20
|
+
* const generate = createUuidV7Generator({ now: clock.instant });
|
|
21
|
+
* clock.advance(5);
|
|
22
|
+
* clock.set(1_000); // back in time
|
|
23
|
+
*/
|
|
24
|
+
export class FakeWallClock {
|
|
25
|
+
milliseconds;
|
|
26
|
+
/**
|
|
27
|
+
* The current time as a `Temporal.Instant`. A property holding a function, so
|
|
28
|
+
* it can be passed as is where an {@link InstantSource} is wanted.
|
|
29
|
+
*/
|
|
30
|
+
instant = () => Temporal.Instant.fromEpochMilliseconds(this.milliseconds);
|
|
31
|
+
/**
|
|
32
|
+
* @param start - The time to start at, in milliseconds since the Unix epoch,
|
|
33
|
+
* 0 by default.
|
|
34
|
+
* @throws {RangeError} When `start` is not an integer a `Temporal.Instant`
|
|
35
|
+
* can hold (±8.64 × 10¹⁵).
|
|
36
|
+
*/
|
|
37
|
+
constructor(start = 0) {
|
|
38
|
+
assertEpochMilliseconds_("start", start);
|
|
39
|
+
this.milliseconds = start;
|
|
40
|
+
}
|
|
41
|
+
/** The current time in milliseconds since the Unix epoch. */
|
|
42
|
+
get epochMilliseconds() {
|
|
43
|
+
return this.milliseconds;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sets the time, to any value: before or after the current one.
|
|
47
|
+
*
|
|
48
|
+
* @throws {RangeError} When `epochMilliseconds` is not an integer a
|
|
49
|
+
* `Temporal.Instant` can hold. The time is then unchanged.
|
|
50
|
+
*/
|
|
51
|
+
set(epochMilliseconds) {
|
|
52
|
+
assertEpochMilliseconds_("time", epochMilliseconds);
|
|
53
|
+
this.milliseconds = epochMilliseconds;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Moves the time by `milliseconds`, which may be negative.
|
|
57
|
+
*
|
|
58
|
+
* @throws {RangeError} When `milliseconds` is not an integer, or the result
|
|
59
|
+
* is outside what a `Temporal.Instant` can hold. The time is then
|
|
60
|
+
* unchanged.
|
|
61
|
+
*/
|
|
62
|
+
advance(milliseconds) {
|
|
63
|
+
assertEpochMilliseconds_("step", milliseconds);
|
|
64
|
+
const target = this.milliseconds + milliseconds;
|
|
65
|
+
assertEpochMilliseconds_("time", target);
|
|
66
|
+
this.milliseconds = target;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=fake-wall-clock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-wall-clock.js","sourceRoot":"","sources":["../src/fake-wall-clock.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,gBAAgB,EAChB,8BAA8B,GAC9B,MAAM,cAAc,CAAC;AAEtB,SAAS,wBAAwB,CAAC,IAAY,EAAE,YAAoB;IACnE,IACC,CAAC,gBAAgB,CAChB,YAAY,EACZ,CAAC,8BAA8B,EAC/B,8BAA8B,GAAG,CAAC,CAClC,EACA,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,kBAAkB,IAAI,IAAI,MAAM,CAAC,YAAY,CAAC,uEAAuE,CACrH,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,aAAa;IACjB,YAAY,CAAS;IAE7B;;;OAGG;IACM,OAAO,GAAkB,GAAG,EAAE,CACtC,QAAQ,CAAC,OAAO,CAAC,qBAAqB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAE3D;;;;;OAKG;IACH,YAAY,KAAK,GAAG,CAAC;QACpB,wBAAwB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAEzC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED,6DAA6D;IAC7D,IAAI,iBAAiB;QACpB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,iBAAyB;QAC5B,wBAAwB,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;QAEpD,IAAI,CAAC,YAAY,GAAG,iBAAiB,CAAC;IACvC,CAAC;IAED;;;;;;OAMG;IACH,OAAO,CAAC,YAAoB;QAC3B,wBAAwB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QAE/C,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QAEhD,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAEzC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;IAC5B,CAAC;CACD"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/test-util",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Dependency-free test-harness primitives, such as a deterministic synchronous delay for timing-sensitive tests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"test-utils",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"prepack": "yarn build"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@ac-kit/core": "^0.
|
|
40
|
+
"@ac-kit/core": "^0.10.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@ac-kit/tsconfig": "0.1.0",
|