Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 135 lines · 4.8 KiB · at 83c2f03

import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { retryFetch } = await load();// A scripted fetcher plus a recording sleep. Each step is a status number// (optionally [status, body]) or the string "net" for a network error.// `log` records the order of fetch calls and sleep start/end.const setup = (steps) => {  const log = [];  const urls = [];  const sleeps = [];  const fetcher = (url) => {    urls.push(url);    const i = urls.length - 1;    log.push(`fetch${i}`);    if (i >= steps.length) {      return h.resolveOk({ status: 599, body: "script exhausted" });    }    const step = steps[i];    if (step === "net") {      return h.resolveErr({ kind: "network", message: `boom ${i}` });    }    const [status, body] = Array.isArray(step) ? step : [step, `body ${i}`];    return h.resolveOk({ status, body });  };  const sleep = async (ms) => {    sleeps.push(ms);    log.push(`sleep${ms}`);    await Promise.resolve();    await Promise.resolve();    log.push("woke");  };  return { fetcher, sleep, log, urls, sleeps };};// Normalize errors to just the contract fields for each kind.const run = async (steps, retries, baseDelayMs = 100) => {  const s = setup(steps);  const r = await h.resAsync(() => retryFetch(s.fetcher, "https://api.test/x", { retries, baseDelayMs, sleep: s.sleep }));  let out = r;  if (r.ok === false && r.error) {    const e = r.error;    out =      e.kind === "exhausted"        ? { ok: false, error: { kind: e.kind, attempts: e.attempts, lastStatus: h.opt(e.lastStatus) } }        : e.kind === "http"          ? { ok: false, error: { kind: e.kind, status: e.status } }          : r;  }  return { r: out, ...s };};const exhausted = (attempts, lastStatus) => ({  ok: false,  error: { kind: "exhausted", attempts, lastStatus: lastStatus === undefined ? { some: false } : { some: true, value: lastStatus } },});const http = (status) => ({ ok: false, error: { kind: "http", status } });test("first attempt succeeds", async () => {  const { r, sleeps, urls } = await run([[200, "hello"]], 3);  assert.deepEqual(r, { ok: true, value: "hello" });  assert.deepEqual(sleeps, []);  assert.deepEqual(urls, ["https://api.test/x"]);});test("empty body on 204 is a success", async () => {  const { r } = await run([[204, ""]], 2);  assert.deepEqual(r, { ok: true, value: "" });});test("5xx is retried with exponential delays", async () => {  const { r, sleeps, urls } = await run([500, 503, [201, "made"]], 5, 100);  assert.deepEqual(r, { ok: true, value: "made" });  assert.deepEqual(sleeps, [100, 200]);  assert.equal(urls.length, 3);  assert.ok(urls.every((u) => u === "https://api.test/x"));});test("network errors are retried", async () => {  const { r, sleeps } = await run(["net", "net", [200, "ok"]], 2, 50);  assert.deepEqual(r, { ok: true, value: "ok" });  assert.deepEqual(sleeps, [50, 100]);});test("4xx fails immediately without retry", async () => {  const { r, sleeps, urls } = await run([404], 3);  assert.deepEqual(r, http(404));  assert.deepEqual(sleeps, []);  assert.equal(urls.length, 1);  assert.deepEqual((await run([500, 429], 3)).r, http(429));});test("1xx and 3xx are not retried either", async () => {  assert.deepEqual((await run([302], 3)).r, http(302));  assert.deepEqual((await run([199], 3)).r, http(199));  assert.deepEqual((await run([600], 3)).r, http(600));});test("exhausted after 5xx reports attempts and last status", async () => {  const { r, sleeps } = await run([500, 502, 503, 504], 3, 10);  assert.deepEqual(r, exhausted(4, 504));  assert.deepEqual(sleeps, [10, 20, 40]);});test("exhausted after a final network error has no lastStatus", async () => {  const { r } = await run([500, "net"], 1, 10);  assert.deepEqual(r, exhausted(2, undefined));  assert.deepEqual((await run(["net", 503], 1, 10)).r, exhausted(2, 503));});test("retries 0 means exactly one attempt and no sleep", async () => {  const a = await run([500], 0);  assert.deepEqual(a.r, exhausted(1, 500));  assert.deepEqual(a.sleeps, []);  const b = await run(["net"], 0);  assert.deepEqual(b.r, exhausted(1, undefined));  assert.equal(b.urls.length, 1);});test("sleep is still called when the delay is 0", async () => {  const { r, sleeps } = await run([500, 500, [200, "x"]], 3, 0);  assert.deepEqual(r, { ok: true, value: "x" });  assert.deepEqual(sleeps, [0, 0]);});test("each sleep finishes before the next attempt", async () => {  const { log } = await run([500, "net", [200, "ok"]], 2, 5);  assert.deepEqual(log, ["fetch0", "sleep5", "woke", "fetch1", "sleep10", "woke", "fetch2"]);});test("no sleep after the last attempt", async () => {  const { log } = await run([500, 500], 1, 7);  assert.deepEqual(log, ["fetch0", "sleep7", "woke", "fetch1"]);});