t3-code-android-nightly/.repos/effect-smol/packages/effect/test/Clock.test.ts
Julius Marminge e3c85ead63
chore(refs): sync Effect and Alchemy references to rc.115 and beta.78 (#12327)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-17 23:21:25 -07:00

37 lines
1.5 KiB
TypeScript

import { assert, describe, it, vi } from "@effect/vitest"
import { Clock, Effect } from "effect"
describe("Clock", { concurrent: false }, () => {
it.live("keeps wall time aligned while exposing the monotonic source", () => {
let wallMillis = 1_000_000
let monotonicNanos = 5_000_000_000n
const dateNow = vi.spyOn(Date, "now").mockImplementation(() => wallMillis)
const hrtime = vi.spyOn(process.hrtime, "bigint").mockImplementation(() => monotonicNanos)
return Effect.gen(function*() {
const clock = yield* Clock.Clock
const nanosPerMilli = 1_000_000n
assert.strictEqual(clock.currentTimeNanosUnsafe(), BigInt(wallMillis) * nanosPerMilli)
assert.strictEqual(clock.monotonicTimeNanosUnsafe(), monotonicNanos)
wallMillis += 250
monotonicNanos += 250_000_000n
assert.strictEqual(clock.currentTimeNanosUnsafe(), BigInt(wallMillis) * nanosPerMilli)
wallMillis += 5_000
const beforeSuspend = clock.monotonicTimeNanosUnsafe()
assert.strictEqual(clock.currentTimeNanosUnsafe(), BigInt(wallMillis) * nanosPerMilli)
assert.strictEqual(clock.monotonicTimeNanosUnsafe(), beforeSuspend)
wallMillis -= 3_000
monotonicNanos += 100_000_000n
assert.strictEqual(clock.currentTimeNanosUnsafe(), BigInt(wallMillis) * nanosPerMilli)
assert.isTrue(clock.monotonicTimeNanosUnsafe() > beforeSuspend)
}).pipe(
Effect.ensuring(Effect.sync(() => {
dateNow.mockRestore()
hrtime.mockRestore()
}))
)
})
})