Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243import assert from 'node:assert/strict';import { test } from 'node:test';import { ensureAlarm, runAndRearm, type AlarmStorage } from '../src/alarm.ts';
function fakeStorage(initial: number | null): AlarmStorage & { alarm: number | null } { return { alarm: initial, async getAlarm() { return this.alarm; }, async setAlarm(time: number) { this.alarm = time; }, };}
test('ensureAlarm arms an alarm when none is set', async () => { const storage = fakeStorage(null); await ensureAlarm(storage, 1000); assert.equal(storage.alarm, 1000);});
test('ensureAlarm leaves an existing alarm alone', async () => { const storage = fakeStorage(5000); await ensureAlarm(storage, 1000); assert.equal(storage.alarm, 5000);});
test('runAndRearm re-arms 60s out after a successful pass', async () => { const storage = fakeStorage(null); await runAndRearm(storage, async () => {}, () => 1000); assert.equal(storage.alarm, 61_000);});
test('runAndRearm re-arms even when the pass throws', async (t) => { t.mock.method(console, 'error', () => {}); const storage = fakeStorage(null); await runAndRearm(storage, async () => { throw new Error('relay down'); }, () => 1000); assert.equal(storage.alarm, 61_000);});