@arki/clock

archive

for @arki/[email protected] · Tier 2: standalone libraries

Clock abstraction for testable time-dependent code, with deterministic mock and system implementations.

Version 0.0.1 archive

Install

bun add @arki/[email protected]
Other package managers
npm install @arki/[email protected]
pnpm add @arki/[email protected]
yarn add @arki/[email protected]

Release health for @arki/[email protected]

Doc examples

No example report was recorded for this version.

Open gaps

6 release checks fail

  • checkfile-llms-txt
  • checkfile-agents-md
  • checkfile-agent-guide
  • checkreadme-h2-order
  • checkpkg-engines
  • checkfiles-ship-dist-docs

Used by demos

No demo uses this package yet.

Runtimes

No engines field, so no runtime requirement is declared.

Benchmarks

No benchmarks are published for this version.

README

Clock abstraction for testable time-dependent code, with deterministic mock and system implementations.

Installation

npm install @arki/clock
# or
bun add @arki/clock
# or
pnpm add @arki/clock

Why

Code that calls new Date() or Date.now() directly is hard to test deterministically. Inject a Clock instead, and tests can swap in MockClock to advance time on demand — useful for cooling windows, scheduled jobs, expiration logic, and any time-sensitive flow.

Usage

Production code

import { SystemClock, type Clock } from '@arki/clock';
 
function makeService(clock: Clock = new SystemClock()) {
  return {
    isExpired(at: Date) {
      return clock.now().getTime() > at.getTime();
    },
  };
}

Tests

import { MockClock } from '@arki/clock';
import { describe, expect, it } from 'vitest';
 
describe('isExpired', () => {
  it('returns true after the deadline passes', () => {
    const clock = new MockClock(new Date('2026-01-01T00:00:00.000Z'));
    const service = makeService(clock);
    const deadline = new Date('2026-01-01T01:00:00.000Z');
 
    expect(service.isExpired(deadline)).toBe(false);
 
    clock.advance(60 * 60 * 1000 + 1);
    expect(service.isExpired(deadline)).toBe(true);
  });
});

API

Clock

Interface with a single method:

  • now(): Date — return the current time as a Date.

SystemClock

Production implementation backed by the runtime Date. now() returns new Date().

MockClock

Controllable clock for tests.

  • new MockClock(initialNow?: Date) — defaults to the Unix epoch.
  • now(): Date — returns a fresh Date on every call (callers cannot corrupt internal state by mutating it).
  • advance(ms: number): void — moves time forward. Throws RangeError on negative or non-finite values.
  • setNow(date: Date): void — sets absolute time; allowed to go backward for arranging past scenarios.

Documentation

@arki/clock is framework-agnostic and works on its own. When you compose it with the @arki/dot application framework, see packages/dot/docs/ for plugin authoring, lifecycle, and diagnostics.

License

MIT. See LICENSE.

Public imports

This version has no API index.