Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 149 lines · 4.8 KiB · at 358de06

import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { createLimiter } = await load();const clock = (start = 0) => {  const c = { t: start };  c.now = () => c.t;  return c;};const make = (config, c) => h.okOf(h.res(() => createLimiter(config, c.now)));const invalid = { ok: false, error: { kind: "invalid_config" } };const some = (value) => ({ some: true, value });const none = { some: false };test("invalid configs fail at creation", () => {  const c = clock();  for (const config of [    { capacity: 0, refillPerSecond: 1 },    { capacity: -1, refillPerSecond: 1 },    { capacity: Number.NaN, refillPerSecond: 1 },    { capacity: Number.POSITIVE_INFINITY, refillPerSecond: 1 },    { capacity: "10", refillPerSecond: 1 },    { capacity: 5, refillPerSecond: -1 },    { capacity: 5, refillPerSecond: Number.NaN },    { capacity: 5, refillPerSecond: Number.POSITIVE_INFINITY },  ]) {    assert.deepEqual(h.res(() => createLimiter(config, c.now)), invalid, JSON.stringify(config));  }  assert.equal(h.res(() => createLimiter({ capacity: 5, refillPerSecond: 0 }, c.now)).ok, true);});test("starts full and drains", () => {  const c = clock(1000);  const l = make({ capacity: 3, refillPerSecond: 1 }, c);  assert.equal(l.available(), 3);  assert.equal(l.tryTake(3), true);  assert.equal(l.tryTake(1), false);  assert.equal(l.available(), 0);});test("a failed take removes nothing", () => {  const c = clock();  const l = make({ capacity: 5, refillPerSecond: 1 }, c);  assert.equal(l.tryTake(3), true);  assert.equal(l.tryTake(3), false);  assert.equal(l.available(), 2);  assert.equal(l.tryTake(2), true);});test("n must be a positive integer", () => {  const c = clock();  const l = make({ capacity: 5, refillPerSecond: 1 }, c);  for (const n of [0, -1, 1.5, Number.NaN]) {    assert.equal(l.tryTake(n), false, String(n));    assert.deepEqual(h.opt(l.retryAfterMs(n)), none, String(n));  }  assert.equal(l.available(), 5);});test("refill is continuous and capped", () => {  const c = clock();  const l = make({ capacity: 10, refillPerSecond: 2 }, c);  assert.equal(l.tryTake(10), true);  c.t = 250;  assert.equal(l.available(), 0);  c.t = 500;  assert.equal(l.available(), 1);  c.t = 1250;  assert.equal(l.available(), 2);  c.t = 100_000;  assert.equal(l.available(), 10);  assert.equal(l.tryTake(11), false);});test("available rounds down", () => {  const c = clock();  const l = make({ capacity: 4, refillPerSecond: 1 }, c);  assert.equal(l.tryTake(4), true);  c.t = 1900;  assert.equal(l.available(), 1);  assert.equal(l.tryTake(2), false);});test("retryAfterMs is 0 when tokens are available now", () => {  const c = clock();  const l = make({ capacity: 5, refillPerSecond: 1 }, c);  assert.deepEqual(h.opt(l.retryAfterMs(1)), some(0));  assert.deepEqual(h.opt(l.retryAfterMs(5)), some(0));  assert.equal(l.available(), 5);});test("retryAfterMs waits for refill and does not consume", () => {  const c = clock();  const l = make({ capacity: 5, refillPerSecond: 2 }, c);  assert.equal(l.tryTake(5), true);  assert.deepEqual(h.opt(l.retryAfterMs(1)), some(500));  assert.deepEqual(h.opt(l.retryAfterMs(5)), some(2500));  c.t = 250;  assert.deepEqual(h.opt(l.retryAfterMs(1)), some(250));  c.t = 500;  assert.deepEqual(h.opt(l.retryAfterMs(1)), some(0));  assert.equal(l.tryTake(1), true);});test("retryAfterMs rounds up to whole milliseconds", () => {  const c = clock();  const l = make({ capacity: 3, refillPerSecond: 3 }, c);  assert.equal(l.tryTake(3), true);  const wait = h.opt(l.retryAfterMs(1));  assert.deepEqual(wait, some(334));  c.t = 334;  assert.equal(l.tryTake(1), true);});test("retryAfterMs is absent when the take can never succeed", () => {  const c = clock();  const l = make({ capacity: 5, refillPerSecond: 1 }, c);  assert.deepEqual(h.opt(l.retryAfterMs(6)), none);  const frozen = make({ capacity: 2, refillPerSecond: 0 }, c);  assert.deepEqual(h.opt(frozen.retryAfterMs(2)), some(0));  assert.equal(frozen.tryTake(2), true);  assert.deepEqual(h.opt(frozen.retryAfterMs(1)), none);  c.t = 1_000_000;  assert.equal(frozen.available(), 0);  assert.deepEqual(h.opt(frozen.retryAfterMs(1)), none);});test("a clock that goes backwards adds nothing and does not rewind", () => {  const c = clock(10_000);  const l = make({ capacity: 10, refillPerSecond: 1 }, c);  assert.equal(l.tryTake(10), true);  c.t = 5_000;  assert.equal(l.available(), 0);  assert.equal(l.tryTake(1), false);  c.t = 11_000;  assert.equal(l.available(), 1);});test("refill is measured from creation time", () => {  const c = clock(5_000);  const l = make({ capacity: 2, refillPerSecond: 1 }, c);  assert.equal(l.tryTake(2), true);  c.t = 5_500;  assert.equal(l.available(), 0);  c.t = 6_000;  assert.equal(l.available(), 1);});