@ac-kit/test-util 0.2.4 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
1
  # @ac-kit/test-util
2
2
 
3
+ ## 0.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [065cf8d]
8
+ - Updated dependencies [065cf8d]
9
+ - Updated dependencies [065cf8d]
10
+ - @ac-kit/core@0.11.0
11
+
12
+ ## 0.3.0
13
+
14
+ ### Minor Changes
15
+
16
+ - 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.
17
+
18
+ ### Patch Changes
19
+
20
+ - Updated dependencies [2a0fe74]
21
+ - Updated dependencies [5152230]
22
+ - Updated dependencies [2a0fe74]
23
+ - Updated dependencies [5152230]
24
+ - Updated dependencies [5152230]
25
+ - Updated dependencies [2a0fe74]
26
+ - Updated dependencies [2a0fe74]
27
+ - Updated dependencies [5152230]
28
+ - Updated dependencies [5152230]
29
+ - Updated dependencies [5152230]
30
+ - Updated dependencies [5152230]
31
+ - Updated dependencies [5152230]
32
+ - Updated dependencies [5152230]
33
+ - Updated dependencies [5152230]
34
+ - @ac-kit/core@0.10.0
35
+
3
36
  ## 0.2.4
4
37
 
5
38
  ### Patch Changes
package/README.md CHANGED
@@ -20,9 +20,27 @@ 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
- For helpers that drive and probe real external services (Docker containers,
24
- TCP/TLS endpoints), see `@ac-kit/integration-test-util` instead — that package
25
- is allowed heavier dependencies precisely because its own consumers are few.
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
+
39
+ For helpers that reach for the machine (Docker containers, TCP/TLS endpoints,
40
+ a temporary file tree, a loopback HTTP server), see
41
+ `@ac-kit/integration-test-util` instead — that package is Node only and allowed
42
+ heavier dependencies, precisely because its own consumers are few, while this
43
+ one stays portable.
26
44
 
27
45
  See the
28
46
  [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/test-util/)
@@ -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
@@ -2,4 +2,5 @@ export * from "./busy-wait.js";
2
2
  export * from "./expect-close-relative.js";
3
3
  export * from "./expect-compare-agrees-with-equals.js";
4
4
  export * from "./fake-clock.js";
5
+ export * from "./fake-wall-clock.js";
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -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
@@ -2,4 +2,5 @@ export * from "./busy-wait.js";
2
2
  export * from "./expect-close-relative.js";
3
3
  export * from "./expect-compare-agrees-with-equals.js";
4
4
  export * from "./fake-clock.js";
5
+ export * from "./fake-wall-clock.js";
5
6
  //# sourceMappingURL=index.js.map
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.2.4",
3
+ "version": "0.3.1",
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.9.0"
40
+ "@ac-kit/core": "^0.11.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@ac-kit/tsconfig": "0.1.0",