@ac-kit/test-util 0.1.3 → 0.2.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 CHANGED
@@ -1,5 +1,19 @@
1
1
  # @ac-kit/test-util
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 235ec3c: `Clock` names a time source with a one-shot timer, so scheduling code can run
8
+ on a fake clock; `SYSTEM_CLOCK` is the real one, on `performance.now()` and
9
+ `setTimeout`. `@ac-kit/test-util`'s `FakeClock` only moves when told to and
10
+ fires due timers in time order.
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [235ec3c]
15
+ - @ac-kit/core@0.5.0
16
+
3
17
  ## 0.1.3
4
18
 
5
19
  ### Patch Changes
@@ -0,0 +1,28 @@
1
+ import type { Clock } from "@ac-kit/core";
2
+ /**
3
+ * A {@link Clock} that only moves when told to, firing due timers in time order,
4
+ * each with `now()` at its own time: the clock for testing code that schedules
5
+ * work, without fake global timers.
6
+ *
7
+ * Timers set while advancing are honoured in the same advance when due.
8
+ *
9
+ * @example
10
+ * const clock = new FakeClock();
11
+ * clock.setTimer(100, () => console.log(clock.now())); // logs 100
12
+ * clock.advance(250);
13
+ * clock.now(); // 250
14
+ */
15
+ export declare class FakeClock implements Clock {
16
+ private time;
17
+ private readonly timers;
18
+ private nextId;
19
+ /** @param start - The time to start at, 0 by default. */
20
+ constructor(start?: number);
21
+ now(): number;
22
+ setTimer(at: number, callback: () => void): () => void;
23
+ /** Moves time forward by `milliseconds`, firing every timer due on the way. */
24
+ advance(milliseconds: number): void;
25
+ /** Moves time to `end`, firing every timer due on the way; never backwards. */
26
+ advanceTo(end: number): void;
27
+ }
28
+ //# sourceMappingURL=fake-clock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fake-clock.d.ts","sourceRoot":"","sources":["../src/fake-clock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE1C;;;;;;;;;;;;GAYG;AACH,qBAAa,SAAU,YAAW,KAAK;IACtC,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAGnB;IACJ,OAAO,CAAC,MAAM,CAAK;IAEnB,yDAAyD;IACzD,YAAY,KAAK,SAAI,EAEpB;IAED,GAAG,IAAI,MAAM,CAEZ;IAED,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAKrD;IAED,+EAA+E;IAC/E,OAAO,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAElC;IAED,+EAA+E;IAC/E,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAqB3B;CACD"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * A {@link Clock} that only moves when told to, firing due timers in time order,
3
+ * each with `now()` at its own time: the clock for testing code that schedules
4
+ * work, without fake global timers.
5
+ *
6
+ * Timers set while advancing are honoured in the same advance when due.
7
+ *
8
+ * @example
9
+ * const clock = new FakeClock();
10
+ * clock.setTimer(100, () => console.log(clock.now())); // logs 100
11
+ * clock.advance(250);
12
+ * clock.now(); // 250
13
+ */
14
+ export class FakeClock {
15
+ time;
16
+ timers = new Map();
17
+ nextId = 0;
18
+ /** @param start - The time to start at, 0 by default. */
19
+ constructor(start = 0) {
20
+ this.time = start;
21
+ }
22
+ now() {
23
+ return this.time;
24
+ }
25
+ setTimer(at, callback) {
26
+ const id = this.nextId++;
27
+ this.timers.set(id, { at, callback });
28
+ return () => this.timers.delete(id);
29
+ }
30
+ /** Moves time forward by `milliseconds`, firing every timer due on the way. */
31
+ advance(milliseconds) {
32
+ this.advanceTo(this.time + milliseconds);
33
+ }
34
+ /** Moves time to `end`, firing every timer due on the way; never backwards. */
35
+ advanceTo(end) {
36
+ for (;;) {
37
+ let dueId;
38
+ let dueAt = Infinity;
39
+ // Ties fire in the order they were set: ids grow with insertion.
40
+ for (const [id, { at }] of this.timers) {
41
+ if (at <= end && at < dueAt) {
42
+ dueId = id;
43
+ dueAt = at;
44
+ }
45
+ }
46
+ if (dueId === undefined) {
47
+ break;
48
+ }
49
+ const { callback } = this.timers.get(dueId);
50
+ this.timers.delete(dueId);
51
+ this.time = Math.max(this.time, dueAt);
52
+ callback();
53
+ }
54
+ this.time = Math.max(this.time, end);
55
+ }
56
+ }
57
+ //# sourceMappingURL=fake-clock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fake-clock.js","sourceRoot":"","sources":["../src/fake-clock.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,SAAS;IACb,IAAI,CAAS;IACJ,MAAM,GAAG,IAAI,GAAG,EAG9B,CAAC;IACI,MAAM,GAAG,CAAC,CAAC;IAEnB,yDAAyD;IACzD,YAAY,KAAK,GAAG,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IACnB,CAAC;IAED,GAAG;QACF,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IAED,QAAQ,CAAC,EAAU,EAAE,QAAoB;QACxC,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QACzB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC;QAEtC,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;IAED,+EAA+E;IAC/E,OAAO,CAAC,YAAoB;QAC3B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC;IAC1C,CAAC;IAED,+EAA+E;IAC/E,SAAS,CAAC,GAAW;QACpB,SAAS,CAAC;YACT,IAAI,KAAyB,CAAC;YAC9B,IAAI,KAAK,GAAG,QAAQ,CAAC;YACrB,iEAAiE;YACjE,KAAK,MAAM,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;gBACxC,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,GAAG,KAAK,EAAE,CAAC;oBAC7B,KAAK,GAAG,EAAE,CAAC;oBACX,KAAK,GAAG,EAAE,CAAC;gBACZ,CAAC;YACF,CAAC;YACD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACzB,MAAM;YACP,CAAC;YAED,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC;YAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACvC,QAAQ,EAAE,CAAC;QACZ,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACtC,CAAC;CACD"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./busy-wait.js";
2
2
  export * from "./expect-close-relative.js";
3
3
  export * from "./expect-compare-agrees-with-equals.js";
4
+ export * from "./fake-clock.js";
4
5
  //# 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"}
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"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./busy-wait.js";
2
2
  export * from "./expect-close-relative.js";
3
3
  export * from "./expect-compare-agrees-with-equals.js";
4
+ export * from "./fake-clock.js";
4
5
  //# 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"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/test-util",
3
- "version": "0.1.3",
3
+ "version": "0.2.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.4.0"
40
+ "@ac-kit/core": "^0.5.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@ac-kit/tsconfig": "0.1.0",