sailfork / t3code-github / packages/ source-control-github/ src/ server/ GitHubQuota.test.ts

import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";

import * as GitHubQuota from "./GitHubQuota.ts";
import { CredentialScope } from "@t3tools/source-control-core/server/SourceControlRateLimit";

const RESET = Date.parse("2099-08-13T14:00:00Z");
const headers = (remaining: number, resource = "graphql", reset = RESET) => ({
  "x-ratelimit-resource": resource,
  "x-ratelimit-limit": "5000",
  "x-ratelimit-remaining": String(remaining),
  "x-ratelimit-reset": String(reset / 1000),
});

describe("GitHubQuota", () => {
  it.effect("refuses a background request below the reserve, per quota and account", () =>
    Effect.gen(function* () {
      const quota = yield* GitHubQuota.GitHubQuota;
      yield* quota.observe("github.com", headers(499));
      const refused = yield* Effect.flip(quota.admit("github.com", "graphql"));
      assert.strictEqual(refused.retryAt, RESET);
      yield* quota.admit("github.com", "core");
      yield* quota.admit("ghe.example", "graphql");
      yield* quota.admit("github.com", "graphql").pipe(Effect.provideService(CredentialScope, "b"));
      yield* quota.admit("github.com", "graphql", { allowReserve: true });
    }).pipe(Effect.provide(GitHubQuota.layer)),
  );

  it.effect("takes the latest balance in a window, and ignores an older window", () =>
    Effect.gen(function* () {
      const quota = yield* GitHubQuota.GitHubQuota;
      yield* quota.observe("github.com", headers(400));
      yield* Effect.flip(quota.admit("github.com", "graphql"));
      // GitHub can report more left later in the same window, from another region.
      yield* quota.observe("github.com", headers(900));
      yield* quota.admit("github.com", "graphql");
      yield* quota.observe("github.com", headers(400));
      yield* Effect.flip(quota.admit("github.com", "graphql"));
      yield* quota.observe("github.com", headers(4000, "graphql", RESET - 3_600_000));
      yield* Effect.flip(quota.admit("github.com", "graphql"));
      yield* quota.observe("github.com", headers(4000, "graphql", RESET + 3_600_000));
      yield* quota.admit("github.com", "graphql");
    }).pipe(Effect.provide(GitHubQuota.layer)),
  );

  it.effect("lets interactive requests spend the reserve but not an empty quota", () =>
    Effect.gen(function* () {
      const quota = yield* GitHubQuota.GitHubQuota;
      yield* quota.observe("github.com", headers(1));
      yield* quota.admit("github.com", "graphql", { allowReserve: true });
      yield* quota.observe("github.com", headers(0));
      const refused = yield* Effect.flip(
        quota.admit("github.com", "graphql", { allowReserve: true }),
      );
      assert.strictEqual(refused.retryAt, RESET);
    }).pipe(Effect.provide(GitHubQuota.layer)),
  );

  it.effect("spends nothing locally, so a free 304 cannot run the balance down", () =>
    Effect.gen(function* () {
      const quota = yield* GitHubQuota.GitHubQuota;
      yield* quota.observe("github.com", headers(501, "core"));
      for (let index = 0; index < 5; index++) {
        yield* quota.admit("github.com", "core");
        yield* quota.observe("github.com", headers(501, "core"));
      }
      yield* quota.admit("github.com", "core");
    }).pipe(Effect.provide(GitHubQuota.layer)),
  );
});