50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { DeviceClock } from "../src/index";
|
|
|
|
/** A device clock the test can set, forward or back. */
|
|
function device(start: number) {
|
|
let time = start;
|
|
return { now: () => time, set: (t: number) => (time = t) };
|
|
}
|
|
|
|
describe("device clock (Step 14.1, decision 0.5.88)", () => {
|
|
it("returns device time plus the developer offset", () => {
|
|
const d = device(1_000_000);
|
|
const clock = new DeviceClock(d.now, { offsetMs: 5_000 });
|
|
expect(clock.now()).toBe(1_005_000);
|
|
clock.advance(60_000);
|
|
expect(clock.now()).toBe(1_065_000);
|
|
});
|
|
|
|
it("never runs backward when the device clock is set back", () => {
|
|
const d = device(2_000_000);
|
|
const clock = new DeviceClock(d.now);
|
|
expect(clock.now()).toBe(2_000_000);
|
|
d.set(1_500_000);
|
|
expect(clock.now()).toBe(2_000_000);
|
|
expect(clock.lastTimeSeen).toBe(2_000_000);
|
|
});
|
|
|
|
it("reflects a forward move at once", () => {
|
|
const d = device(2_000_000);
|
|
const clock = new DeviceClock(d.now);
|
|
clock.now();
|
|
d.set(9_000_000);
|
|
expect(clock.now()).toBe(9_000_000);
|
|
});
|
|
|
|
it("starts from a save's last time seen when the device is behind it", () => {
|
|
const d = device(1_000);
|
|
const clock = new DeviceClock(d.now, { lastTimeSeen: 5_000 });
|
|
expect(clock.now()).toBe(5_000);
|
|
clock.restore(4_000);
|
|
expect(clock.now()).toBe(5_000);
|
|
clock.restore(7_000);
|
|
expect(clock.now()).toBe(7_000);
|
|
});
|
|
|
|
it("refuses to move backward through the developer offset", () => {
|
|
expect(() => new DeviceClock(() => 0).advance(-1)).toThrow(RangeError);
|
|
});
|
|
});
|