opencode/packages/core/test/lib/clock.ts
Filip 367c8fbec7
Some checks are pending
check / typecheck (push) Waiting to run
containers / build (push) Waiting to run
deploy-files / deploy (push) Waiting to run
deploy-www / deploy (push) Waiting to run
deploy / deploy (push) Waiting to run
nix-eval / nix-eval (push) Waiting to run
nix-hashes / compute-hash (blacksmith-4vcpu-ubuntu-2404, x86_64-linux) (push) Waiting to run
nix-hashes / compute-hash (blacksmith-4vcpu-ubuntu-2404-arm, aarch64-linux) (push) Waiting to run
nix-hashes / compute-hash (macos-latest, aarch64-darwin) (push) Waiting to run
nix-hashes / update-hashes (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--os=darwin --cpu=x64 host:macos-26-intel platform_flag:--mac --x64 target:x86_64-apple-darwin]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-ubuntu-2404 platform_flag:--linux target:x86_64-unknown-linux-gnu]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-ubuntu-2404-arm platform_flag:--linux --arm64 target:aarch64-unknown-linux-gnu]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-windows-2025 platform_flag:--win target:x86_64-pc-windows-msvc]) (push) Blocked by required conditions
publish / publish (push) Blocked by required conditions
publish / version (push) Waiting to run
publish / build-cli (push) Blocked by required conditions
publish / sign-cli-macos (push) Blocked by required conditions
publish / build-node-app-archive (push) Blocked by required conditions
publish / build-node-cli (map[bun_install_flags:--cpu=* host:blacksmith-4vcpu-windows-2025 target:windows-arm64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-ubuntu-2404 target:linux-x64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-ubuntu-2404-arm target:linux-arm64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-windows-2025 target:windows-x64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:macos-26 target:darwin-arm64]) (push) Blocked by required conditions
publish / sign-cli-windows (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--cpu=* host:windows-2025 platform_flag:--win --arm64 target:aarch64-pc-windows-msvc]) (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--os=darwin --cpu=arm64 host:macos-26 platform_flag:--mac --arm64 target:aarch64-apple-darwin]) (push) Blocked by required conditions
publish / notify-discord-v2 (push) Blocked by required conditions
storybook / storybook build (push) Waiting to run
test / e2e (linux) (push) Blocked by required conditions
test / e2e (windows) (push) Blocked by required conditions
test / affected packages (push) Waiting to run
test / unit (linux) (push) Waiting to run
test / unit (windows) (push) Waiting to run
test / build (linux) (push) Waiting to run
test / build (windows) (push) Waiting to run
test / e2e (linux, app-1) (push) Blocked by required conditions
test / e2e (linux, app-2) (push) Blocked by required conditions
test / e2e (linux, app-components) (push) Blocked by required conditions
test / e2e (linux, session-ui-components) (push) Blocked by required conditions
test / e2e (windows, app-1) (push) Blocked by required conditions
test / e2e (windows, app-2) (push) Blocked by required conditions
test / e2e (windows, app-components) (push) Blocked by required conditions
test / e2e (windows, session-ui-components) (push) Blocked by required conditions
test(core): bound advance by real time instead of steps (#57119)
2026-10-19 16:08:14 +00:00

43 lines
1.5 KiB
TypeScript

import { Effect } from "effect"
import { TestClock } from "effect/testing"
// Defers on a real macrotask so pubsub delivery, fiber hops, and filesystem
// work can complete between TestClock advances.
const settle = Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 1)))
// One advance step: let pending real work finish, then fire due TestClock timers.
const tick = Effect.gen(function* () {
yield* settle
yield* TestClock.adjust("500 millis")
})
/**
* Drives every pending TestClock timer to completion: stream debounces register
* their sleeps from separate fiber hops that a single adjust can miss, so the
* loop alternates real-macrotask settles with adjusts until the condition
* holds. Extra adjusts are harmless when nothing is pending.
*
* Bounded by real time, not a step count: the condition can also wait on real
* work such as a plugin import, which needs many more steps on a loaded CI
* runner than locally.
*/
export const advance = Effect.fnUntraced(function* (condition: () => boolean) {
const deadline = performance.now() + 15_000
while (performance.now() < deadline) {
if (condition()) return
yield* tick
}
if (condition()) return
return yield* Effect.die(new Error("condition never became true within 15s"))
})
/**
* Advances far enough that any pending debounced work would have completed,
* so a no-op assertion afterwards is meaningful.
*/
export const drain = Effect.gen(function* () {
for (let round = 0; round < 4; round++) {
yield* tick
}
yield* settle
})